diff --git a/headers/textures.hpp b/headers/textures.hpp index 513af29..17c0033 100644 --- a/headers/textures.hpp +++ b/headers/textures.hpp @@ -1,8 +1,17 @@ #ifndef TEXTURES_HPP #define TEXTURES_HPP -#define STB_IMAGE_IMPLEMENTATION -#include "../extern/stb/stb_image.h" - +namespace Textures { + class Texture2D { + public: + unsigned int id; + Texture2D(const char* t_src); + void load(); + void use(); + private: + const char* texture_src; + int width, height, nrChannels; + }; +} #endif diff --git a/shaders/fragment.glsl b/shaders/fragment.glsl index 388c596..e96ad42 100644 --- a/shaders/fragment.glsl +++ b/shaders/fragment.glsl @@ -1,11 +1,16 @@ #version 330 core + out vec4 FragColor; -in vec4 vertexColor; + +in vec4 VertexColor; +in vec2 TexCoord; + +uniform sampler2D inpTexture; uniform float r; uniform float g; uniform float b; void main() { - FragColor = vertexColor + vec4(r, g, b, 1.0f); + FragColor = texture(inpTexture, TexCoord) * (VertexColor + vec4(r, g, b, 1.0f)); } diff --git a/shaders/vertex.glsl b/shaders/vertex.glsl index ad2e13e..ad44138 100644 --- a/shaders/vertex.glsl +++ b/shaders/vertex.glsl @@ -1,10 +1,13 @@ #version 330 core layout (location = 0) in vec3 aPos; layout (location = 1) in vec3 aColor; +layout (location = 2) in vec2 aTexCoord; -out vec4 vertexColor; +out vec4 VertexColor; +out vec2 TexCoord; void main() { gl_Position = vec4(aPos, 1.0); - vertexColor = vec4(aColor, 1.0); + VertexColor = vec4(aColor, 1.0); + TexCoord = aTexCoord; } diff --git a/src/main.cpp b/src/main.cpp index 2a45833..ac080e2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,6 +5,7 @@ #include #include "../headers/shaders.hpp" +#include "../headers/textures.hpp" #define WINDOW_WIDTH 640 #define WINDOW_HEIGHT 480 @@ -28,11 +29,10 @@ void processInput(GLFWwindow *win) { struct RenderObj { unsigned int EBO; Shaders::Shader shader; + Textures::Texture2D texture; }; RenderObj preRenderCallback(unsigned int indices[], unsigned int indices_count, float verts[], unsigned int verts_count) { - Shaders::Shader shader(VERT_SHADER_SRC_FILE, FRAG_SHADER_SRC_FILE); - // Vertex buffer object unsigned int VBO; glGenBuffers(1, &VBO); @@ -47,36 +47,49 @@ RenderObj preRenderCallback(unsigned int indices[], unsigned int indices_count, glBufferData(GL_ARRAY_BUFFER, verts_count, verts, GL_DYNAMIC_DRAW); // for moving stuff //glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW); + unsigned int EBO; + glGenBuffers(1, &EBO); + + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices_count, indices, GL_STATIC_DRAW); + // Set attrib pointers + #define VERTEX_ATTRIB_PTR_SIZE 8 * sizeof(float) + // Pos - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, VERTEX_ATTRIB_PTR_SIZE, (void*)0); glEnableVertexAttribArray(0); // Color - glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3*sizeof(float))); + glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, VERTEX_ATTRIB_PTR_SIZE, (void*)(3*sizeof(float))); glEnableVertexAttribArray(1); - unsigned int EBO; - glGenBuffers(1, &EBO); + // Texture + glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, VERTEX_ATTRIB_PTR_SIZE, (void*)(6*sizeof(float))); + glEnableVertexAttribArray(2); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices_count, indices, GL_STATIC_DRAW); - return RenderObj {EBO, shader}; + Shaders::Shader shader(VERT_SHADER_SRC_FILE, FRAG_SHADER_SRC_FILE); + Textures::Texture2D texture(RUSTY_METAL_TEXTURE); + + return RenderObj {EBO, shader, texture}; } void renderCallback(RenderObj ro) { - glClearColor(0.0f, 0.0f, 0.0f, 1.0f); + glClearColor(0.0f, 1.0f, 0.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); float time = glfwGetTime(); float gVal = (sin(time) / 1.5f) + 0.5f; + ro.shader.use(); ro.shader.setFloat("r", gVal); ro.shader.setFloat("g", gVal); ro.shader.setFloat("b", gVal); + ro.texture.use(); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ro.EBO); glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); } @@ -102,7 +115,7 @@ int main() { } float verts[] = { - 0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 1.0f, + 0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 1.0f, 0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, -0.5f, -0.5f, 0.0f, 1.0f, 1.0f, 0.0f, -0.5f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f @@ -123,14 +136,6 @@ int main() { glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT); glfwSetFramebufferSizeCallback(win, framebuffer_size_callback); - // Textures - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT); - - // "blending" - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - float borderColor[] = {1.0f, 1.0f, 1.0f, 1.0f}; glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor); diff --git a/src/shaders.cpp b/src/shaders.cpp index 8978a88..870006c 100644 --- a/src/shaders.cpp +++ b/src/shaders.cpp @@ -38,7 +38,7 @@ namespace Shaders { glGetShaderiv(shader, GL_COMPILE_STATUS, &success); if (!success) { glGetShaderInfoLog(shader, 512, NULL, infolog); - printf("Shader Compilation Error:\n%s\n", infolog); + printf("[%s] Shader Compilation Error:\n%s\n", shaderSource, infolog); } return shader; diff --git a/src/textures.cpp b/src/textures.cpp index e69de29..2e7ff0b 100644 --- a/src/textures.cpp +++ b/src/textures.cpp @@ -0,0 +1,46 @@ +#include +#include "../headers/textures.hpp" +#include + +#define STB_IMAGE_IMPLEMENTATION +#include "../extern/stb/stb_image.h" + +namespace Textures { + Texture2D::Texture2D(const char* t_src) { + texture_src = t_src; + + // Bind this texture + glGenTextures(1, &id); + glBindTexture(GL_TEXTURE_2D, id); + + // Repeat/bounded etc + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT); + + // Mipmap "blending" + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + + // Load texture source etc + load(); + } + + void Texture2D::load() { + printf("[%i] \"%s\" - ", id, texture_src); + unsigned char* data = stbi_load(texture_src, &width, &height, &nrChannels, 0); + + if (data) { + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data); + glGenerateMipmap(GL_TEXTURE_2D); + printf("Loaded!\n"); + } else { + printf("[ERROR] Failed to load texture \"%s\"\n", texture_src); + } + + stbi_image_free(data); + } + + void Texture2D::use() { + glBindTexture(GL_TEXTURE_2D, id); + } +}