fix texture rendering

This commit is contained in:
lukas 2022-02-06 18:28:12 +01:00
parent 29fc4f67d4
commit 9df12e0fe1
9 changed files with 108 additions and 36 deletions

BIN
assets/blocks/block.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

BIN
block.bmp

Binary file not shown.

Before

Width:  |  Height:  |  Size: 12 KiB

View File

@ -10,40 +10,101 @@
#include <GL/gl.h> #include <GL/gl.h>
VertexArray *BlockRenderer::setVertexArray() { 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[] = { float cube_vertices[] = {
// front // front
-1.0, -1.0, 1.0, 0.0f, 0.0f, -1, -1, 1, 0, 0,
1.0, -1.0, 1.0, 1.0f, 0.0f, 1, -1, 1, 1, 0,
1.0, 1.0, 1.0, 0.0f, 1.0f, 1, 1, 1, 0, 1,
-1.0, 1.0, 1.0, 1.0f, 1.0f, -1, 1, 1, 1, 1,
// back // back
-1.0, -1.0, -1.0, 0.0f, 0.0f, -1, -1, -1, 0, 0,
1.0, -1.0, -1.0, 1.0f, 0.0f, 1, -1, -1, 1, 0,
1.0, 1.0, -1.0, 0.0f, 1.0f, 1, 1, -1, 0, 1,
-1.0, 1.0, -1.0, 1.0f, 1.0f, -1, 1, -1, 1, 1,
}; };
GLfloat cube_colors[] = { float cubee[] = {
// front colors // face #1
1.0, 0.0, 0.0, 1, 1, 1, 1, 0,
0.0, 1.0, 0.0, -1, 1, 1, 1, 1,
0.0, 0.0, 1.0, -1, -1, 1, 0, 1,
1.0, 1.0, 1.0, 1, -1, 1, 0, 0,
// back colors
1.0, 0.0, 0.0, // face #2
0.0, 1.0, 0.0, 1, 1, 1, 1, 0,
0.0, 0.0, 1.0, 1, -1, 1, 1, 1,
1.0, 1.0, 1.0 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 // Generate a vertex buffer
auto *vb = new VertexBuffer(cube_vertices, sizeof(cube_vertices)); auto *vb = new VertexBuffer(cubee, sizeof(cubee));
//
return new VertexArray(*vb); return new VertexArray(*vb);
} }
IndexBuffer *BlockRenderer::setIndexBuffer() { 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[] = { unsigned cube_elements[] = {
// front // front
0, 1, 2, 0, 1, 2,
@ -65,7 +126,7 @@ IndexBuffer *BlockRenderer::setIndexBuffer() {
6, 7, 3 6, 7, 3
}; };
return new IndexBuffer(cube_elements, 36);; return new IndexBuffer(indexx, 48);
} }
Shader BlockRenderer::setShader() { Shader BlockRenderer::setShader() {

View File

@ -5,7 +5,7 @@ uniform sampler2D u_texture;
in vec3 normal; in vec3 normal;
in vec4 pos; in vec4 pos;
in vec2 v_texcoords; in vec2 texcoords;
void main() void main()
{ {
@ -17,9 +17,9 @@ void main()
// set light color // set light color
vec3 diffuse = diff * vec3(1.0,1.0,1.0); 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; vec3 result = (diffuse) * c.xyz;
// color = vec4(result,0.0); color = vec4(result,0.0);
color = c; // color = c;
})"; })";

View File

@ -6,6 +6,9 @@ layout(triangle_strip, max_vertices=3) out;
out vec3 normal; out vec3 normal;
out vec4 pos; out vec4 pos;
in vec2 v_texcoords[];
out vec2 texcoords;
void main( void ) void main( void )
{ {
vec3 a = ( gl_in[1].gl_Position - gl_in[0].gl_Position ).xyz; 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; gl_Position = gl_in[i].gl_Position;
normal = N; normal = N;
pos = gl_in[i].gl_Position; pos = gl_in[i].gl_Position;
texcoords = v_texcoords[i];
EmitVertex( ); EmitVertex( );
} }

View File

@ -1,9 +1,10 @@
// //
// Created by lukas on 05.02.22. // Created by lukas on 05.02.22.
// //
#define GL_GLEXT_PROTOTYPES
#include <cstdio> #include <cstdio>
#include <GL/gl.h> #include <GL/gl.h>
#include <GL/glext.h>
#include "bmploader.h" #include "bmploader.h"
unsigned bmploader::loadBMP(const char *imagepath) { unsigned bmploader::loadBMP(const char *imagepath) {
@ -54,18 +55,20 @@ unsigned bmploader::loadBMP(const char *imagepath) {
// Create one OpenGL texture // Create one OpenGL texture
unsigned textureID; unsigned textureID;
glGenTextures(1, &textureID); glGenTextures(1, &textureID);
glActiveTexture(GL_TEXTURE0);
// "Bind" the newly created texture : all future texture functions will modify this texture // "Bind" the newly created texture : all future texture functions will modify this texture
glBindTexture(GL_TEXTURE_2D, textureID); glBindTexture(GL_TEXTURE_2D, textureID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 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_MIN_FILTER, GL_NEAREST);
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, 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); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
// Give the image to OpenGL // Give the image to OpenGL
glTexImage2D(GL_TEXTURE_2D, 0,GL_RGB, width, height, 0, GL_BGR, GL_UNSIGNED_BYTE, data); 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);

View File

@ -9,7 +9,7 @@
#include <GL/gl.h> #include <GL/gl.h>
Texture::Texture() { Texture::Texture() {
mTexturehandle = bmploader().loadBMP("../block.bmp"); mTexturehandle = bmploader().loadBMP("../assets/blocks/block.bmp");
} }
void Texture::Bind() { void Texture::Bind() {

View File

@ -13,13 +13,15 @@ VertexArray::VertexArray(const VertexBuffer& buff) {
buff.Bind(); buff.Bind();
// generate new vertex array object // generate new vertex array object
glGenVertexArrays(2, &handle); glGenVertexArrays(1, &handle);
Bind(); Bind();
// specify syntax of my data // specify syntax of my data
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void *) nullptr); 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); glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void *) 12);
glEnableVertexAttribArray(1); glEnableVertexAttribArray(1);
} }

View File

@ -94,6 +94,8 @@ int main() {
#endif #endif
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
GrasBlock gb(0, 0, 0); GrasBlock gb(0, 0, 0);
BaseBlock bb(0.0f, (float) rand() / RAND_MAX, 1.0f, 1, 0, 0); BaseBlock bb(0.0f, (float) rand() / RAND_MAX, 1.0f, 1, 0, 0);