diff --git a/assets/blocks/block.bmp b/assets/blocks/block.bmp new file mode 100644 index 0000000..fc8b73d Binary files /dev/null and b/assets/blocks/block.bmp differ diff --git a/block.bmp b/block.bmp deleted file mode 100644 index e3d6366..0000000 Binary files a/block.bmp and /dev/null differ diff --git a/blocks/BlockRenderer.cpp b/blocks/BlockRenderer.cpp index ef71562..cd5e646 100644 --- a/blocks/BlockRenderer.cpp +++ b/blocks/BlockRenderer.cpp @@ -10,40 +10,101 @@ #include VertexArray *BlockRenderer::setVertexArray() { + // float cube_vertices[] = { + // // front + // -1.0, -1.0, 1.0, 0.0f, 0.0f, + // 1.0, -1.0, 1.0, 1.0f, 0.0f, + // 1.0, 1.0, 1.0, 0.0f, 1.0f, + // -1.0, 1.0, 1.0, 1.0f, 1.0f, + // // back + // -1.0, -1.0, -1.0, 0.0f, 0.0f, + // 1.0, -1.0, -1.0, 1.0f, 0.0f, + // 1.0, 1.0, -1.0, 0.0f, 1.0f, + // -1.0, 1.0, -1.0, 1.0f, 1.0f, + // }; + float cube_vertices[] = { // front - -1.0, -1.0, 1.0, 0.0f, 0.0f, - 1.0, -1.0, 1.0, 1.0f, 0.0f, - 1.0, 1.0, 1.0, 0.0f, 1.0f, - -1.0, 1.0, 1.0, 1.0f, 1.0f, + -1, -1, 1, 0, 0, + 1, -1, 1, 1, 0, + 1, 1, 1, 0, 1, + -1, 1, 1, 1, 1, // back - -1.0, -1.0, -1.0, 0.0f, 0.0f, - 1.0, -1.0, -1.0, 1.0f, 0.0f, - 1.0, 1.0, -1.0, 0.0f, 1.0f, - -1.0, 1.0, -1.0, 1.0f, 1.0f, + -1, -1, -1, 0, 0, + 1, -1, -1, 1, 0, + 1, 1, -1, 0, 1, + -1, 1, -1, 1, 1, }; - GLfloat cube_colors[] = { - // front colors - 1.0, 0.0, 0.0, - 0.0, 1.0, 0.0, - 0.0, 0.0, 1.0, - 1.0, 1.0, 1.0, - // back colors - 1.0, 0.0, 0.0, - 0.0, 1.0, 0.0, - 0.0, 0.0, 1.0, - 1.0, 1.0, 1.0 - }; + float cubee[] = { + // face #1 + 1, 1, 1, 1, 0, + -1, 1, 1, 1, 1, + -1, -1, 1, 0, 1, + 1, -1, 1, 0, 0, + + // face #2 + 1, 1, 1, 1, 0, + 1, -1, 1, 1, 1, + 1, -1, -1, 0, 1, + 1, 1, -1, 0, 0, + + // face #3 + 1, 1, 1, 1, 0, + 1, 1, -1, 1, 1, + -1, 1, -1, 0, 1, + -1, 1, 1, 0, 0, + + // face #4 + -1, -1, -1, 1, 0, + -1, 1, -1, 1, 1, + 1, 1, -1, 0, 1, + 1, -1, -1, 0, 0, + + // face #5 + -1, -1, -1, 1, 0, + -1, -1, 1, 1, 1, + -1, 1, 1, 0, 1, + -1, 1, -1, 0, 0, + + // face #6 + -1, -1, -1, 1, 0, + 1, -1, -1, 1, 1, + 1, -1, 1, 0, 1, + -1, -1, 1, 0, 0,}; // Generate a vertex buffer - auto *vb = new VertexBuffer(cube_vertices, sizeof(cube_vertices)); - - // + auto *vb = new VertexBuffer(cubee, sizeof(cubee)); return new VertexArray(*vb); } IndexBuffer *BlockRenderer::setIndexBuffer() { + unsigned indexx[] = { + 0, 1, 2, + 2, 3, 0, + + 4, 5, 6, + 6, 7, 4, + + 8, 9, 10, + 10, 11, 8, + + 12, 13, 14, + 14, 15, 12, + + 16, 17, 18, + 18, 19, 16, + + 20, 21, 22, + 22, 23, 20, + + 24, 25, 26, + 26, 27, 24, + + 28, 29, 30, + 30, 31, 28 + }; + unsigned cube_elements[] = { // front 0, 1, 2, @@ -65,7 +126,7 @@ IndexBuffer *BlockRenderer::setIndexBuffer() { 6, 7, 3 }; - return new IndexBuffer(cube_elements, 36);; + return new IndexBuffer(indexx, 48); } Shader BlockRenderer::setShader() { diff --git a/blocks/fragment.shader b/blocks/fragment.shader index 8a74ec6..e52c309 100644 --- a/blocks/fragment.shader +++ b/blocks/fragment.shader @@ -5,7 +5,7 @@ uniform sampler2D u_texture; in vec3 normal; in vec4 pos; -in vec2 v_texcoords; +in vec2 texcoords; void main() { @@ -17,9 +17,9 @@ void main() // set light color vec3 diffuse = diff * vec3(1.0,1.0,1.0); - vec4 c = texture(u_texture, v_texcoords); + vec4 c = texture(u_texture, texcoords); vec3 result = (diffuse) * c.xyz; -// color = vec4(result,0.0); - color = c; + color = vec4(result,0.0); +// color = c; })"; \ No newline at end of file diff --git a/blocks/geometry.shader b/blocks/geometry.shader index d663c76..afcfba4 100644 --- a/blocks/geometry.shader +++ b/blocks/geometry.shader @@ -6,6 +6,9 @@ layout(triangle_strip, max_vertices=3) out; out vec3 normal; out vec4 pos; +in vec2 v_texcoords[]; +out vec2 texcoords; + void main( void ) { vec3 a = ( gl_in[1].gl_Position - gl_in[0].gl_Position ).xyz; @@ -17,6 +20,7 @@ void main( void ) gl_Position = gl_in[i].gl_Position; normal = N; pos = gl_in[i].gl_Position; + texcoords = v_texcoords[i]; EmitVertex( ); } diff --git a/bmploader.cpp b/bmploader.cpp index 42d223c..ff89f16 100644 --- a/bmploader.cpp +++ b/bmploader.cpp @@ -1,9 +1,10 @@ // // Created by lukas on 05.02.22. // - +#define GL_GLEXT_PROTOTYPES #include #include +#include #include "bmploader.h" unsigned bmploader::loadBMP(const char *imagepath) { @@ -54,18 +55,20 @@ unsigned bmploader::loadBMP(const char *imagepath) { // Create one OpenGL texture unsigned textureID; glGenTextures(1, &textureID); - + glActiveTexture(GL_TEXTURE0); // "Bind" the newly created texture : all future texture functions will modify this texture glBindTexture(GL_TEXTURE_2D, textureID); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); -// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); -// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); // Give the image to OpenGL glTexImage2D(GL_TEXTURE_2D, 0,GL_RGB, width, height, 0, GL_BGR, GL_UNSIGNED_BYTE, data); - + // Generate mipmaps, by the way. + glGenerateMipmap(GL_TEXTURE_2D); diff --git a/gl/Texture.cpp b/gl/Texture.cpp index 33fc84b..86c15c9 100644 --- a/gl/Texture.cpp +++ b/gl/Texture.cpp @@ -9,7 +9,7 @@ #include Texture::Texture() { - mTexturehandle = bmploader().loadBMP("../block.bmp"); + mTexturehandle = bmploader().loadBMP("../assets/blocks/block.bmp"); } void Texture::Bind() { diff --git a/gl/VertexArray.cpp b/gl/VertexArray.cpp index 5bd8ae3..e3b3c2a 100644 --- a/gl/VertexArray.cpp +++ b/gl/VertexArray.cpp @@ -13,13 +13,15 @@ VertexArray::VertexArray(const VertexBuffer& buff) { buff.Bind(); // generate new vertex array object - glGenVertexArrays(2, &handle); + glGenVertexArrays(1, &handle); + Bind(); // specify syntax of my data glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void *) nullptr); - glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void *) nullptr); glEnableVertexAttribArray(0); + + glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void *) 12); glEnableVertexAttribArray(1); } diff --git a/main.cpp b/main.cpp index cfeda65..ee43dd0 100644 --- a/main.cpp +++ b/main.cpp @@ -94,6 +94,8 @@ int main() { #endif glEnable(GL_DEPTH_TEST); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + GrasBlock gb(0, 0, 0); BaseBlock bb(0.0f, (float) rand() / RAND_MAX, 1.0f, 1, 0, 0);