I am trying to draw some simple 3D rectangular prisms, but glBindTexture does not seem to work properly.
I have N number of "pieces", each of which has a material name.
I go through each part, find its texture based on its material name, calling glBindTexture, and then draw a piece. It seems that if I have several parts with the same material name, only the first is drawn with a texture, and the rest are just white.
Any help would be greatly appreciated.
Here is the code:
Initializing OpenGL:
static PIXELFORMATDESCRIPTOR pfd =
{
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA,
32,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
16,
0, 0, 0, 0, 0, 0, 0,
};
GLfloat light_pos1[] = {0.0f, 0.0f, 15.0f, 1.0f};
GLfloat light_ambient[] = {1.0f, 1.0f, 1.0f, 1.0f};
hdc = GetDC()->m_hDC;
m_nPixelFormat = ChoosePixelFormat(hdc, &pfd);
SetPixelFormat(hdc, m_nPixelFormat, &pfd);
hrc = wglCreateContext(hdc);
wglMakeCurrent(hdc, hrc);
glEnable(GL_TEXTURE_2D);
glClearColor(0.5f, 0.5f, 0.5f, 0.0f);
glClearDepth(1.0f);
glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glLightfv(GL_LIGHT0, GL_POSITION, light_pos1);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
OnDraw(NULL);
Loading textures
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, t.texture);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, 256, 256, GL_RGB, GL_UNSIGNED_BYTE, data);
Then drawing code:
glPolygonMode(GL_FRONT_AND_BACK, GL_FLAT);
for(int i = 0; i < mPieces.GetCount(); i++)
{
C3DPiece p = mPieces.GetAt(mPieces.FindIndex(i));
if(p.visible)
{
GLuint tex = GetTextureForMaterial(p.material);
glBindTexture(GL_TEXTURE_2D, tex);
glBegin(GL_QUADS);
glNormal3f(..., ..., ...);
glTextCoord2f(..., ...);
glVertex3F(..., ..., ...);
glTextCoord2f(..., ...);
glVertex3F(..., ..., ...);
glTextCoord2f(..., ...);
glVertex3F(..., ..., ...);
glTextCoord2f(..., ...);
glVertex3F(..., ..., ...);
glEnd();
}
}