diff --git a/examples/debug/assets/opengl.png b/examples/debug/assets/opengl.png new file mode 100644 index 0000000..5be3706 Binary files /dev/null and b/examples/debug/assets/opengl.png differ diff --git a/examples/debug/main.cpp b/examples/debug/main.cpp index d5f6e6d..97f6f6c 100644 --- a/examples/debug/main.cpp +++ b/examples/debug/main.cpp @@ -76,7 +76,8 @@ int main(int argc, char** argv) } // Create a texture - oglu::Texture texture = oglu::MakeTexture("assets/crate.jpg"); + oglu::Texture crate = oglu::MakeTexture("assets/crate.jpg"); + oglu::Texture opengl = oglu::MakeTexture("assets/opengl.png"); // Window loop while (!glfwWindowShouldClose(window)) @@ -85,8 +86,15 @@ int main(int argc, char** argv) oglu::ClearScreen(GL_COLOR_BUFFER_BIT, oglu::Color(0.29f, 0.13f, 0.23f)); + oglu::ActiveTexture(0); + crate->Bind(); + oglu::ActiveTexture(1); + opengl->Bind(); + shader->Use(); - texture->Bind(); + shader->SetUniform("texture1", 0); + shader->SetUniform("texture2", 1); + square->BindAndDraw(); glfwSwapBuffers(window); diff --git a/examples/debug/shaders/fragmentShader.frag b/examples/debug/shaders/fragmentShader.frag index d60e23e..bbc8224 100644 --- a/examples/debug/shaders/fragmentShader.frag +++ b/examples/debug/shaders/fragmentShader.frag @@ -4,9 +4,10 @@ in vec2 oUV; out vec4 FragColor; -uniform sampler2D ourTexture; +uniform sampler2D texture1; +uniform sampler2D texture2; void main() { - FragColor = texture(ourTexture, oUV) * vec4(oCol, 1.0); + FragColor = mix(texture(texture1, oUV), texture(texture2, oUV), 0.2); } \ No newline at end of file diff --git a/include/texture.hpp b/include/texture.hpp index 459f2f9..4806b97 100644 --- a/include/texture.hpp +++ b/include/texture.hpp @@ -19,7 +19,7 @@ namespace oglu * * @param[in] index Index of the texture unit (Note: This index is actually an offset to @p GL_TEXTURE0) */ - void ActiveTexture(GLubyte index); + void OGLU_API ActiveTexture(GLubyte index); /** * @brief An object representing an OpenGL Texture. diff --git a/src/texture.cpp b/src/texture.cpp index 4f7387b..aa550e1 100644 --- a/src/texture.cpp +++ b/src/texture.cpp @@ -1,4 +1,5 @@ #include "texture.hpp" +#include #define STB_IMAGE_IMPLEMENTATION #include @@ -40,7 +41,18 @@ namespace oglu glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data); + GLenum pixelFormat; + switch (nrChannels) + { + case 3: pixelFormat = GL_RGB; break; + case 4: pixelFormat = GL_RGBA; break; + default: + glDeleteTextures(1, &texture); + stbi_image_free(data); + throw std::runtime_error(std::string("Texture has unsupported pixel format: " + std::string(filename))); + } + + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, pixelFormat, GL_UNSIGNED_BYTE, data); glGenerateMipmap(GL_TEXTURE_2D); stbi_image_free(data);