Added loading of a TGA mask image to generate alpha canal

This commit is contained in:
trotFunky 2019-09-25 13:02:24 +02:00
parent 5e6b953822
commit db0bd97580
5 changed files with 48 additions and 7 deletions

BIN
resources/arbre_masque.tga Normal file

Binary file not shown.

View file

@ -74,7 +74,46 @@ bool Texture::load_rgb_tga(const std::string& rgb_filename)
return load_tga(rgb_filename,image_data); return load_tga(rgb_filename,image_data);
} }
bool Texture::load_rgba_tga(const std::string& rgb_filename,const std::string& mask_filename) bool Texture::load_rgba_tga(const std::string& rgb_filename, const std::string& mask_filename)
{ {
return false; uint8_t* rgb_data = nullptr;
uint8_t* mask_data = nullptr;
bool load_successful = true;
// Load rgb and alpha data before merging them
load_successful &= load_tga(rgb_filename, rgb_data);
uint16_t temp_width = width;
uint16_t temp_height = height;
load_successful &= load_tga(mask_filename, mask_data);
if (!load_successful)
{
std::cerr << "Error while loading RGBA image" << std::endl;
return false;
}
if (temp_width != width || temp_height != height)
{
std::cerr << "Error while loading RGBA image : image and mask dimensions do no match : " << std::endl;
std::cerr << temp_width << "x" << temp_height << " versus " << width << "x" << height << std::endl;
return false;
}
image_data = new uint8_t[width*height*4];
// Merge the two images
for(int i = 0;i<height*width;i++)
{
for(int j = 0;j<3;j++)
{
image_data[i*4+j] = rgb_data[i*3+j];
}
image_data[i*4+3] = mask_data[i*3];
}
// Now we have an RGBA image, update color_bits
color_bits = 32;
return true;
} }

View file

@ -26,7 +26,7 @@ public:
/// Load an RGB image from an RGB TGA file /// Load an RGB image from an RGB TGA file
bool load_rgb_tga(const std::string& rgb_filename); bool load_rgb_tga(const std::string& rgb_filename);
/// Load an RGBA image from an rgb and an alpha tga file /// Load an RGBA image from an rgb and an alpha (Greyscale) tga file
bool load_rgba_tga(const std::string& rgb_filename,const std::string& mask_filename); bool load_rgba_tga(const std::string& rgb_filename,const std::string& mask_filename);
private: private:

View file

@ -62,6 +62,8 @@ void display_rotating_pyramid(float x, float y, float z, float c, float h, float
void display_tree(float x, float y, float z, float h, float w, const Texture& tree_texture) void display_tree(float x, float y, float z, float h, float w, const Texture& tree_texture)
{ {
glEnable(GL_ALPHA_TEST);
glPolygonMode(GL_FRONT_AND_BACK,GL_FILL); glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
glBindTexture(GL_TEXTURE_2D,tree_texture.opengl_id[0]); glBindTexture(GL_TEXTURE_2D,tree_texture.opengl_id[0]);
@ -95,4 +97,6 @@ void display_tree(float x, float y, float z, float h, float w, const Texture& tr
glVertex3f(x,y+h,z+w/2); glVertex3f(x,y+h,z+w/2);
glEnd(); glEnd();
glDisable(GL_ALPHA_TEST);
} }

View file

@ -56,7 +56,6 @@ int main(int argc, char** argv)
// Setup OPenGL to use textures // Setup OPenGL to use textures
glEnable(GL_TEXTURE_2D); glEnable(GL_TEXTURE_2D);
glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_GREATER, 0.5); glAlphaFunc(GL_GREATER, 0.5);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_NEAREST);
@ -66,11 +65,10 @@ int main(int argc, char** argv)
glTexEnvi(GL_TEXTURE_2D, GL_TEXTURE_ENV_MODE, GL_MODULATE); glTexEnvi(GL_TEXTURE_2D, GL_TEXTURE_ENV_MODE, GL_MODULATE);
// Load and generate tree texture // Load and generate tree texture
tree_texture.load_rgb_tga("resources/arbre.tga"); tree_texture.load_rgba_tga("resources/arbre.tga","resources/arbre_masque.tga");
glGenTextures(1,tree_texture.opengl_id); glGenTextures(1,tree_texture.opengl_id);
glBindTexture(GL_TEXTURE_2D,tree_texture.opengl_id[0]); glBindTexture(GL_TEXTURE_2D,tree_texture.opengl_id[0]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, tree_texture.width,tree_texture.height,0,GL_RGB,GL_UNSIGNED_BYTE,tree_texture.image_data); gluBuild2DMipmaps(GL_TEXTURE_2D,GL_RGBA8,tree_texture.width,tree_texture.height,GL_RGBA,GL_UNSIGNED_BYTE,tree_texture.image_data);
gluBuild2DMipmaps(GL_TEXTURE_2D,GL_RGB8,tree_texture.width,tree_texture.height,GL_RGB,GL_UNSIGNED_BYTE,tree_texture.image_data);
glViewport(0,0,glutGet(GLUT_WINDOW_WIDTH),glutGet(GLUT_WINDOW_HEIGHT)); glViewport(0,0,glutGet(GLUT_WINDOW_WIDTH),glutGet(GLUT_WINDOW_HEIGHT));