feat: fix images with gl_rgba not rendering

This commit is contained in:
Krzysztof Rudnicki 2023-03-11 20:06:42 +01:00
parent 6d82225bae
commit 8b1b1bd28c
7 changed files with 28 additions and 18 deletions

View File

@ -96,6 +96,7 @@
},
"C_Cpp.errorSquiggles": "Disabled",
"cSpell.words": [
"awesomeface",
"kuchy",
"multiplatform",
"VERTICE"

Binary file not shown.

After

Width:  |  Height:  |  Size: 43 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 367 KiB

View File

@ -179,11 +179,20 @@ drawFigureReturn drawDebilMode(const char* vertexPath, const char* fragmentPath,
configureVertexAttribute(true, true);
loadAndCreateTexture();
unsigned int texture1 = loadAndCreateTexture("assets/Preview.png", GL_RGBA, true);
// unsigned int texture2 = loadAndCreateTexture("assets/awesomeface.png", GL_RGBA, false);
activateAndBindTextures(texture1, GL_TEXTURE0);
// activateAndBindTextures(texture2, GL_TEXTURE1);
ourShader.use(); // don't forget to activate/use the shader before setting uniforms!
// set it via the texture class
ourShader.setInt("texture1", 1);
// either set it manually like so:
glUniform1i(glGetUniformLocation(ourShader.ID, "texture1"), 0);
// or set it via the texture class
ourShader.setInt("texture2", 1);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
drawFigureReturn newReturn;
@ -227,8 +236,6 @@ void doDrawArrays(const unsigned int shaderProgram, const unsigned int vertexArr
// use shader program to render an object
glUseProgram(shaderProgram);
updateUniformColor(shaderProgram, "ourColor");
const unsigned int texture = generateTexture("./assets/Preview.png");
glBindTexture(GL_TEXTURE_2D, texture);
glBindVertexArray(vertexArrayObject);
// From left:
// primitive type we want to draw

Binary file not shown.

View File

@ -91,10 +91,12 @@ unsigned int generateAndBindTexture(const GLenum textureTarget, const GLsizei nu
return texture;
}
unsigned int loadAndBindTextureFile(unsigned int texture, const char* texturePath) {
unsigned int loadAndBindTextureFile(unsigned int texture, const char* texturePath, GLenum rgbFormat, bool flipImage) {
int textureWidth, textureHeight, colorChannels;
unsigned char *data = stbi_load("assets/container.jpg", &textureWidth, &textureHeight, &colorChannels, 0);
stbi_set_flip_vertically_on_load(flipImage);
unsigned char *data = stbi_load(texturePath, &textureWidth, &textureHeight, &colorChannels, 0);
if(data) {
std::cout << "textureWidth " << textureWidth << " textureHeight " << textureHeight << " colorChannels " << colorChannels << std::endl;
// generate texture
/*
first argument is texture target, setting it to GL_TEXTURE_2D will only affect 2D targets and not 1D or 3D
@ -105,7 +107,7 @@ unsigned int loadAndBindTextureFile(unsigned int texture, const char* texturePat
7th and 8th are the format and datatype of source image, we store the image data as chars (bytes) so we pass that
9th last argument is actual data of the texture
*/
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, textureWidth, textureHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
glTexImage2D(GL_TEXTURE_2D, 0, rgbFormat, textureWidth, textureHeight, 0, rgbFormat, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
// after generating texture, lets free the memory from image
stbi_image_free(data);
@ -118,13 +120,8 @@ unsigned int loadAndBindTextureFile(unsigned int texture, const char* texturePat
}
}
unsigned int generateTexture(const char* texturePath) {
unsigned int texture = generateAndBindTexture(GL_TEXTURE_2D, 1);
texture = loadAndBindTextureFile(texture, texturePath);
}
// const char* texturePath, int textureWidth, int textureHeight, int colorChannels, const GLenum textureTarget, const GLint sCoordinateOption, const GLint tCoordinateOption, const GLint rCoordinateOption, const float* borderColor
unsigned int loadAndCreateTexture() {
unsigned int loadAndCreateTexture(const char* texturePath, GLenum RGBFormat, bool flipImage) {
// load and create a texture
// -------------------------
unsigned int texture = generateAndBindTexture(GL_TEXTURE_2D, 1);
@ -134,9 +131,13 @@ unsigned int loadAndCreateTexture() {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// load image, create texture and generate mipmaps
texture = loadAndBindTextureFile(texture, "assets/Preview.png");
texture = loadAndBindTextureFile(texture, texturePath, RGBFormat, flipImage);
return texture;
}
glActiveTexture(GL_TEXTURE0);
void activateAndBindTextures(unsigned int texture, GLenum textureNumber)
{
glActiveTexture(textureNumber);
glBindTexture(GL_TEXTURE_2D, texture);
}

View File

@ -10,10 +10,11 @@ void setTextureTCoordinate(const GLenum textureTarget = GL_TEXTURE_2D, const GLi
void setTextureRCoordinate(const GLenum textureTarget = GL_TEXTURE_3D, const GLint rCoordinateOption = GL_REPEAT, const float* borderColor = NULL);
void setTextureFilteringAndMipMap(const GLenum textureTarget = GL_TEXTURE_2D, const GLenum filterType = GL_TEXTURE_MAG_FILTER, const GLint textureFilteringMethod = GL_NEAREST, const GLint mipMapFilteringMethod = GL_NEAREST_MIPMAP_NEAREST);
unsigned int generateAndBindTexture(const GLenum textureTarget);
unsigned int generateTexture(const char* texturePath);
unsigned char* loadTexture(const char* texturePath, int textureWidth, int textureHeight, int colorChannels);
// const char* texturePath, int textureWidth, int textureHeight, int colorChannels, const GLenum textureTarget, const GLint sCoordinateOption, const GLint tCoordinateOption, const GLint rCoordinateOption, const float* borderColor
unsigned int loadAndCreateTexture();
unsigned int loadAndCreateTexture(const char* texturePath, GLenum RGBFormat, bool flipImage);
void activateAndBindTextures(unsigned int texture, GLenum textureNumber);
unsigned int loadAndBindTextureFile(unsigned int texture, const char* texturePath, GLenum rgbFormat, bool flipImage);
#endif // TEXTURES_HPP