I am trying to learn how to use textures. Unfortunately, all my attempts result in a white square. From what I understand, this is very common, but I can’t find any solutions online to solve my problem.
I set up two different sets of code: one loads a bitmap from file and displays it using glDrawPixels. This works, so I know the image is being loaded correctly. The second set of code generates, binds, and then uses glTexImage2D, but it only makes a white square.
Here are the two sample codes. Is there anything obvious that is wrong about this code?
// THIS WORKS
// Object constructor
p_mybitmap = new MyBitmap;
p_mybitmap->load_24_bit_bitmap("C:\\CppPrograms\\New Interface\\Res\\OptionsButtonUp.bmp");
// In Render Function
glRasterPos2i(0,0);
glDrawPixels(p_mybitmap->width,p_mybitmap->height, GL_RGBA, GL_UNSIGNED_BYTE, p_mybitmap->p_data);
// THIS PRODUCES A WHITE SQUARE
// object constructor
glEnable(GL_TEXTURE_2D);
p_background_image = new MyBitmap();
if(p_background_image->load_24_bit_bitmap("C:\\CppPrograms\\New Interface\\Res\\OptionsButtonUp.bmp"))
{
glGenTextures(1, &background_image);
glBindTexture(GL_TEXTURE_2D, background_image);
glTexImage2D(GL_TEXTURE_2D, 0, 4, p_background_image->width, p_background_image->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, p_background_image->p_data);
// break points make it here, so I know the above 3 lines of code are called.
}
// In Render function
glEnable(GL_TEXTURE_2D);
glBegin(GL_QUADS);
glBindTexture(GL_TEXTURE_2D, background_image);
glTexCoord2f(0.0f,1.0f); glVertex3f(0, m_height, 0.0);
glTexCoord2f(0.0f,0.0f); glVertex3f(0, 0.0, 0.0);
glTexCoord2f(1.0f,0.0f); glVertex3f(m_width, 0.0, 0.0);
glTexCoord2f(1.0f,1.0f); glVertex3f(m_width, m_height, 0.0);
glEnd();
// the polygon itself is appearing normally. . . I have another variant of the program that just fills the polygon with various colors, so I know it's being rendered appropriately.