Added support for PNG

This commit is contained in:
Robert 2021-01-21 16:11:41 +01:00
parent 7571b3943d
commit 8174ebaee4
5 changed files with 27 additions and 6 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 38 KiB

View file

@ -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);

View file

@ -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);
}

View file

@ -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.

View file

@ -1,4 +1,5 @@
#include "texture.hpp"
#include <iostream>
#define STB_IMAGE_IMPLEMENTATION
#include <stb/stb_image.h>
@ -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);