简体   繁体   中英

Opengl texture not showing

I'm trying to display a simple texture (from an array) in opengl, but I just get a blank white screen.

My code to generate the texture:

GLbyte textureData[] = { 0, 0, 0, 255, 0, 0, 0, 255, 0, 0, 0, 255 };
GLsizei width = 2;
GLsizei height = 2;


glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glPixelStorei(GL_PACK_ALIGNMENT, 1);
glGenTextures(1, &textureName);   
glBindTexture(GL_TEXTURE_2D, textureName); 

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, width, height, 0, GL_UNSIGNED_BYTE, GL_RGB8, (GLvoid*)textureData);

And the display code:

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textureName);
glBegin(GL_TRIANGLE_STRIP);
glTexCoord2f(0.0, 0.0);
glVertex2f(-1.0, -1.0);

glTexCoord2f(1.0, 0.0);
glVertex2f(1.0, -1.0);

glTexCoord2f(0.0, 1.0);
glVertex2f(-1.0, 1.0);

glTexCoord2f(1.0, 1.0);
glVertex2f(1.0, 1.0);
glEnd();

glutSwapBuffers();

I know the problem is with the textures as I can draw solid shapes to the screen.

Default minification filtering setting is to use mipmaps. Since you do not provide all mipmap levels down to 1×1 and didn't switch to a filter method that does not require mipmapping, texture sampling will be skipped.

You have to add setting the filter method to something non-mipmapped, like this:

glTexImage2D(…);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

Or provide a full stack of image levels.

update due to comment

Also there's a problem with the glTexImage2D call: GL_RGB8 is not a valid token for the data format. Explicitly sized format tokens are permitted only for the internal format. However for starters you should probably use the unsized tokens (just GL_RGB without the 8 ) anyway (explicit size should be used only, when it really matters, eg for certain precision requirements or to avoid conversion costs for streaming texture updates).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM