Skip to content

Commit

Permalink
fix: grayscale textures
Browse files Browse the repository at this point in the history
  • Loading branch information
JulioJPinto committed May 23, 2024
1 parent bdf34e8 commit 452796b
Showing 1 changed file with 3 additions and 23 deletions.
26 changes: 3 additions & 23 deletions engine/src/model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ bool Model::loadTexture() {
// Load image data
int width, height, num_channels;
unsigned char* image_data = stbi_load(this->texture_filepath.data(), &width,
&height, &num_channels, 3);
&height, &num_channels, STBI_rgb);

if (!image_data) {
std::cerr << "Failed to load texture: " << this->texture_filepath
Expand All @@ -136,29 +136,9 @@ bool Model::loadTexture() {
GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

// Determine the correct format
GLenum format;
switch (num_channels) {
case 1:
format = GL_RED;
break;
case 3:
format = GL_RGB;
break;
case 4:
format = GL_RGBA;
break;
default: {
// Free image data before returning
stbi_image_free(image_data);
std::cerr << "Unsupported number of channels: " << num_channels
<< std::endl;
return false;
}
}

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
// Upload data to GPU
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format,
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB,
GL_UNSIGNED_BYTE, image_data);
glGenerateMipmap(GL_TEXTURE_2D);

Expand Down

0 comments on commit 452796b

Please sign in to comment.