diff --git a/CMakeLists.txt b/CMakeLists.txt index ffb030d..d0a5966 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -27,7 +27,9 @@ SET(srcs main.cpp gl/Shader.cpp gl/Shader.h blocks/GrasBlock.cpp blocks/GrasBlock.h blocks/BlockRenderer.cpp blocks/BlockRenderer.h blocks/RenderBase.cpp blocks/RenderBase.h - gl/Camera.cpp gl/Camera.h) + gl/Camera.cpp gl/Camera.h + bmploader.cpp bmploader.h + gl/Texture.cpp gl/Texture.h) add_executable(opengltest ${srcs}) diff --git a/block.bmp b/block.bmp new file mode 100644 index 0000000..e3d6366 Binary files /dev/null and b/block.bmp differ diff --git a/blocks/BlockRenderer.cpp b/blocks/BlockRenderer.cpp index 576a808..ef71562 100644 --- a/blocks/BlockRenderer.cpp +++ b/blocks/BlockRenderer.cpp @@ -3,6 +3,7 @@ // #include "BlockRenderer.h" +#include "../gl/Texture.h" #define GL_GLEXT_PROTOTYPES @@ -11,15 +12,15 @@ VertexArray *BlockRenderer::setVertexArray() { float cube_vertices[] = { // front - -1.0, -1.0, 1.0, - 1.0, -1.0, 1.0, - 1.0, 1.0, 1.0, - -1.0, 1.0, 1.0, + -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, - 1.0, -1.0, -1.0, - 1.0, 1.0, -1.0, - -1.0, 1.0, -1.0 + -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, }; GLfloat cube_colors[] = { @@ -83,6 +84,12 @@ Shader BlockRenderer::setShader() { Shader s; s.loadShader(vertsrc, geosrc, fragsrc); s.Bind(); + + Texture t; + t.Bind(); + glUniform1i(s.getUniformHandle("u_texture"), 0); + + return s; } diff --git a/blocks/fragment.shader b/blocks/fragment.shader index 273ce43..8a74ec6 100644 --- a/blocks/fragment.shader +++ b/blocks/fragment.shader @@ -1,9 +1,11 @@ R"(#version 330 core out vec4 color; uniform vec3 u_color; +uniform sampler2D u_texture; in vec3 normal; in vec4 pos; +in vec2 v_texcoords; void main() { @@ -15,6 +17,9 @@ void main() // set light color vec3 diffuse = diff * vec3(1.0,1.0,1.0); - vec3 result = (diffuse) * u_color; - color = vec4(result,0.0); + vec4 c = texture(u_texture, v_texcoords); + + vec3 result = (diffuse) * c.xyz; +// color = vec4(result,0.0); + color = c; })"; \ No newline at end of file diff --git a/blocks/geometry.shader b/blocks/geometry.shader index e3eab01..d663c76 100644 --- a/blocks/geometry.shader +++ b/blocks/geometry.shader @@ -4,7 +4,6 @@ layout(triangles) in; layout(triangle_strip, max_vertices=3) out; out vec3 normal; - out vec4 pos; void main( void ) diff --git a/blocks/vertex.shader b/blocks/vertex.shader index 63cd45b..f4c2d43 100644 --- a/blocks/vertex.shader +++ b/blocks/vertex.shader @@ -1,16 +1,17 @@ R"(#version 330 core // Input vertex data, different for all executions of this shader. layout(location = 0) in vec3 vertexPosition_modelspace; +layout(location = 1) in vec2 texCoords; //// Values that stay constant for the whole mesh. uniform mat4 MVP; uniform mat4 translation; -out vec3 FragPos; +out vec2 v_texcoords; void main(){ // Output position of the vertex, in clip space : MVP * position vec4 pos = MVP * translation * vec4(vertexPosition_modelspace,1); gl_Position = pos; - FragPos = pos.xyz; + v_texcoords = texCoords; })"; \ No newline at end of file diff --git a/bmploader.cpp b/bmploader.cpp new file mode 100644 index 0000000..42d223c --- /dev/null +++ b/bmploader.cpp @@ -0,0 +1,73 @@ +// +// Created by lukas on 05.02.22. +// + +#include +#include +#include "bmploader.h" + +unsigned bmploader::loadBMP(const char *imagepath) { + // Data read from the header of the BMP file + unsigned char header[54]; // Each BMP file begins by a 54-bytes header + unsigned int dataPos; // Position in the file where the actual data begins + int width, height; + unsigned int imageSize; // = width*height*3 + // Actual RGB data + unsigned char * data; + + + // Open the file + FILE * file = fopen(imagepath,"rb"); + if (!file){printf("Image could not be opened\n"); return 0;} + + if ( fread(header, 1, 54, file)!=54 ){ // If not 54 bytes read : problem + printf("Not a correct BMP file\n"); + return false; + } + + if ( header[0]!='B' || header[1]!='M' ){ + printf("Not a correct BMP file\n"); + return 0; + } + + // Read ints from the byte array + dataPos = *(int*)&(header[0x0A]); + imageSize = *(int*)&(header[0x22]); + width = *(int*)&(header[0x12]); + height = *(int*)&(header[0x16]); + + // Some BMP files are misformatted, guess missing information + if (imageSize==0) imageSize=width*height*3; // 3 : one byte for each Red, Green and Blue component + if (dataPos==0) dataPos=54; // The BMP header is done that way + + // Create a buffer + data = new unsigned char [imageSize]; + + // Read the actual data from the file into the buffer + fread(data,1,imageSize,file); + + //Everything is in memory now, the file can be closed + fclose(file); + + glEnable(GL_TEXTURE_2D); + + // Create one OpenGL texture + unsigned textureID; + glGenTextures(1, &textureID); + + // "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); + + // Give the image to OpenGL + glTexImage2D(GL_TEXTURE_2D, 0,GL_RGB, width, height, 0, GL_BGR, GL_UNSIGNED_BYTE, data); + + + + + return textureID; +} diff --git a/bmploader.h b/bmploader.h new file mode 100644 index 0000000..0976301 --- /dev/null +++ b/bmploader.h @@ -0,0 +1,15 @@ +// +// Created by lukas on 05.02.22. +// + +#ifndef OPENGLTEST_BMPLOADER_H +#define OPENGLTEST_BMPLOADER_H + + +class bmploader { +public: + unsigned loadBMP(const char * imagepath); +}; + + +#endif //OPENGLTEST_BMPLOADER_H diff --git a/gl/Texture.cpp b/gl/Texture.cpp new file mode 100644 index 0000000..33fc84b --- /dev/null +++ b/gl/Texture.cpp @@ -0,0 +1,22 @@ +// +// Created by lukas on 06.02.22. +// + +#include "Texture.h" +#include "../bmploader.h" +#define GL_GLEXT_PROTOTYPES + +#include + +Texture::Texture() { + mTexturehandle = bmploader().loadBMP("../block.bmp"); +} + +void Texture::Bind() { + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, mTexturehandle); +} + +void Texture::UnBind() { + glBindTexture(GL_TEXTURE_2D, 0); +} diff --git a/gl/Texture.h b/gl/Texture.h new file mode 100644 index 0000000..edab5ff --- /dev/null +++ b/gl/Texture.h @@ -0,0 +1,19 @@ +// +// Created by lukas on 06.02.22. +// + +#ifndef OPENGLTEST_TEXTURE_H +#define OPENGLTEST_TEXTURE_H + + +class Texture { +private: + unsigned mTexturehandle; +public: + Texture(); + void Bind(); + void UnBind(); +}; + + +#endif //OPENGLTEST_TEXTURE_H diff --git a/gl/VertexArray.cpp b/gl/VertexArray.cpp index 48df8e4..5bd8ae3 100644 --- a/gl/VertexArray.cpp +++ b/gl/VertexArray.cpp @@ -13,12 +13,14 @@ VertexArray::VertexArray(const VertexBuffer& buff) { buff.Bind(); // generate new vertex array object - glGenVertexArrays(1, &handle); + glGenVertexArrays(2, &handle); Bind(); // specify syntax of my data - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * 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(1); } unsigned VertexArray::getHandle() const {