Jump to content
  • Advertisement
Sign in to follow this  
alvarotriana

OpenGL Problem with materials

This topic is 4078 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi I'm working in a MFC application, using OpenGL to show a 3D scene in the viewport. Right now I'm tryng two show two meshes in the viewport, one mesh uses a tga texture, and the other mesh will have a simple material. The code that draws the meshes is this: //Here I check if this mesh has a texture and the uvw coordinates. If not it will be drawed using a blue material. if ( tri_uvw_mesh && texture ) { glColor3ub(255,255,255); glTexImage2D(GL_TEXTURE_2D, 0, tga_texture.bpp / 8, tga_texture.width, tga_texture.height, 0, tga_texture.type, GL_UNSIGNED_BYTE, tga_texture.imageData); glEnable(GL_TEXTURE_2D); glBegin(GL_TRIANGLES); for ( int i_f=0; i_f<tri_mesh->num_faces; i_f++ ) { //Here goes the code that draw the mesh with the verts and thw uvw // //coordinates } glEnd(); } else { ambient[0] = diffuse[0] = 0.0f; ambient[1] = diffuse[1] = 0.0f; ambient[2] = diffuse[2] = 255.0f; ambient[3] = diffuse[3] = 0.0f; glTexImage2D(GL_TEXTURE_2D,0,3,0,0,0,GL_RGB,GL_UNSIGNED_BYTE,0); glDisable( GL_TEXTURE_2D ); glColor3ub(255,255,255); glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT, ambient ); glMaterialfv( GL_FRONT_AND_BACK, GL_DIFFUSE, diffuse ); glBegin(GL_TRIANGLES); for ( int i_f=0; i_f<tri_mesh->num_faces; i_f++ ) { //Here goes the code that draw the mesh } glEnd(); } However, I always end with this in the viewport: http://img41.imagevenue.com/img.php?image=89970_Dibujo_122_800lo.JPG :(. If I create a green material for the second mesh, both meshes will look green. If I put glEnable(GL_COLOR_MATERIAL) and glDisable(GL_COLOR_MATERIAL), the both meshes are drawed with the same texture :s. I really don't know what I'm doing wrong, any help will be apreciated. Thanks a lot!

Share this post


Link to post
Share on other sites
Advertisement
I've put together some code snippets from yours. The comments attempt to explain why I've made the changes.


// You should only need to call glTexImage2D once per texture, at the time it's loaded. Something like this:
GLuint texID;
glGenTextures(1, &texID);
glBindTexture(GL_TEXTURE_2D, texID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, tga_texture.bpp / 8, tga_texture.width, tga_texture.height, 0, tga_texture.type, GL_UNSIGNED_BYTE, tga_texture.imageData);

// After this, the texID value can be used to select that texture whenever you need it.
// This means your drawing code can be changed to something like:
if ( tri_uvw_mesh && texture )
{
glColor3ub(255,255,255);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texID);
glBegin(GL_TRIANGLES);
for ( int i_f=0; i_f<tri_mesh->num_faces; i_f++ )
{
//Here goes the code that draw the mesh with the verts and thw uvw // //coordinates
}
glEnd();
}
else
{
// I altered this to use a range of 0 to 1, since you are using glMaterialfv which takes floats.
// I also changed the last value to be 1, though this won't matter unless you're using alpha blending or testing.
GLfloat ambdiff[] = {0, 0, 1.f, 1.f};
// Make sure we don't use a texture.
glBindTexture(GL_TEXTURE_2D, 0);
glDisable( GL_TEXTURE_2D );
glColor3ub(255,255,255);
glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT, ambdiff );
glMaterialfv( GL_FRONT_AND_BACK, GL_DIFFUSE, ambdiff );

glBegin(GL_TRIANGLES);
for ( int i_f=0; i_f<tri_mesh->num_faces; i_f++ )
{
//Here goes the code that draw the mesh
}
glEnd();
}




I hope some of that helps.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!