PDA

View Full Version : no texture displaying with png image



wwuster
11-20-2009, 08:32 AM
I've been using bmp files for texture images, but now need to have a transparent background on a texture, so I'm trying to get that working. I've stepped through the code and can't find anything wrong. The problem is that nothing displays. Here is a summary of the code. I streamlined the code and removed error checking to make it easier to read.

The loadPNGTexture creates an unsigned char array that is width * height * 4 (rgba) and creates the texture from that data. Can anyone tell me why it is not displaying anything?

thanks,
William



// initialization:

glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxtex);
glFlush();

//----------------------------------------------------------

texid = loadPNGTexture ("texture.png");

glEnable( GL_BLEND );
glEnable( GL_TEXTURE_2D );
glBindTexture( GL_TEXTURE_2D, texid );
glBegin(GL_QUADS);
glTexCoord2f (0.0, 0.0);
glVertex3f (lr.left, lr.bottom, z);
glTexCoord2f (1.0, 0.0);
glVertex3f (lr.right, lr.bottom, z);
glTexCoord2f (1.0, 1.0);
glVertex3f (lr.right, lr.top, z);
glTexCoord2f (0.0, 1.0);
glVertex3f (lr.left, lr.top, z);
glEnd();
glDisable( GL_TEXTURE_2D );
glDisable( GL_BLEND );

overlay
11-20-2009, 11:10 AM
First of all, change your background color to something else than black or white, pick something flashy.

Next, you will see if the quad is actually rendered or not (it can be black because of lighting (OK, your code does not show any lighting initialization, but hey, just to be sure)). If can be not rendered for multiple reasons: frustum culling (aka your transform between your geometry and your camera is wrong), backface culling, ...

Next, switch the texture env from GL_MODULATE (initial value) to GL_REPLACE.

Tell us about what you see then.

wwuster
11-20-2009, 11:56 AM
Other parts of the scene display. It's just this one rectangle. I'm using an orthogonal projection so I'm drawing everything at z=0.

This rectangle is being drawn on top of another one after the first one is drawn, so I don't think that the fill color matters. The bmp image version works. I did try adding a line to set GL_REPLACE, but that made no difference.

Here's a snippet from the png texture setup:

textureImage.format = GL_RGBA;
textureImage.internalFormat = 4;

if (png.rgba) {
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); <<<<< ADDED THIS
glGenTextures (1, &amp;textureImage.id);
glBindTexture (GL_TEXTURE_2D, textureImage.id);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

glTexImage2D(
GL_TEXTURE_2D,
0,
textureImage.internalFormat,
textureImage.width,
textureImage.height,
0,
textureImage.format,
GL_UNSIGNED_BYTE,
png.rgba
);

}

wwuster
11-21-2009, 07:01 AM
I added color to the quad. This same code works with my bmp file loader (no alpha). The only difference is that I'm loading a .png file with an alpha channel. I've checked
for errors and there aren't any. The texid IS valid. The clear color is set to (1,1,1,1). This is the white behind the large rounded blue rectangle. The .png file is NOT a power of two, but the .bmp image wasn't either and that works.

I put the image here as screen.jpg:
http://sites.google.com/site/bobslf/home/temp/screen.jpg

This is a summary of the code that produced the part of the image that I'm asking about. The problem is that the texture does not appear on the rectangle with the gray gradient.

glEnable( GL_TEXTURE_2D );
texid = ogl->loadPNGTexture ("test.png");

glBindTexture( GL_TEXTURE_2D, texid );
glBegin(GL_QUADS);
glTexCoord2f (0.0, 0.0);
glColor4f(0.0f, 0.0f, 0.0f, .5f);
glVertex3f (left, bottom, z);

glTexCoord2f (1.0, 0.0);
glColor4f(0.0f, 0.0f, 0.0f, .5f);
glVertex3f (right, bottom, z);

glTexCoord2f (1.0, 1.0);
glColor4f(1.0f, 1.0f, 1.0f, .5f);
glVertex3f (right, top, z);

glTexCoord2f (0.0, 1.0);
glColor4f(1.0f, 1.0f, 1.0f, .5f);
glVertex3f (left, top, z);
glEnd();
glDisable( GL_TEXTURE_2D );
glDisable( GL_BLEND );

Here are the texture generation lines:

glGenTextures (1, &amp;textureImage.id);
glBindTexture (GL_TEXTURE_2D, textureImage.id);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
//glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
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, 4, textureImage.width, textureImage.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, png.rgba );


I've tried other settings in place GL_DECAL, but nothing seems to make a difference. If I only change the line with test.png to use test.bmp with a different loader function it works.

thanks