fix texture rendering
This commit is contained in:
		
							
								
								
									
										
											BIN
										
									
								
								assets/blocks/block.bmp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								assets/blocks/block.bmp
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							| After Width: | Height: | Size: 12 KiB | 
| @@ -10,40 +10,101 @@ | ||||
| #include <GL/gl.h> | ||||
|  | ||||
| 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() { | ||||
|   | ||||
| @@ -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; | ||||
| })"; | ||||
| @@ -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( ); | ||||
|     } | ||||
|  | ||||
|   | ||||
| @@ -1,9 +1,10 @@ | ||||
| // | ||||
| // Created by lukas on 05.02.22. | ||||
| // | ||||
|  | ||||
| #define GL_GLEXT_PROTOTYPES | ||||
| #include <cstdio> | ||||
| #include <GL/gl.h> | ||||
| #include <GL/glext.h> | ||||
| #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); | ||||
|  | ||||
|  | ||||
|  | ||||
|   | ||||
| @@ -9,7 +9,7 @@ | ||||
| #include <GL/gl.h> | ||||
|  | ||||
| Texture::Texture() { | ||||
|     mTexturehandle = bmploader().loadBMP("../block.bmp"); | ||||
|     mTexturehandle = bmploader().loadBMP("../assets/blocks/block.bmp"); | ||||
| } | ||||
|  | ||||
| void Texture::Bind() { | ||||
|   | ||||
| @@ -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); | ||||
| } | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user