glBindTexture not working
I'm trying to draw a few simple 3D rectangular prisms, but glBindTexture does not seem to be working properly.
I have N number of "pieces", each with a material name.
I am looping through each piece, finding its texture based on its material name, calling glBindTexture, then drawing the piece. It seems though, if i have multiple pieces 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:
OpenGL initialisation:
static PIXELFORMATDESCRIPTOR pfd =
{
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA,
32, // bit depth
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
16, // z-buffer depth
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};
// Get device context only once.
hdc = GetDC()->m_hDC;
// Pixel format.
m_nPixelFormat = ChoosePixelFormat(hdc, &pfd);
SetPixelFormat(hdc, m_nPixelFormat, &pfd);
开发者_JAVA百科
// Create the OpenGL Rendering Context.
hrc = wglCreateContext(hdc);
wglMakeCurrent(hdc, hrc);
//texture
glEnable(GL_TEXTURE_2D);
//Setup:
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);
//lighting
glLightfv(GL_LIGHT0, GL_POSITION, light_pos1);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
// Send draw request
OnDraw(NULL);
texture loading
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 the 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)
{
//GetTextureForMaterial finds the texture id based on the "piece"'s material name
//have checked this and it is always returning a valid id (>0)
GLuint tex = GetTextureForMaterial(p.material);
glBindTexture(GL_TEXTURE_2D, tex);
glBegin(GL_QUADS);
glNormal3f(..., ..., ...);
glTextCoord2f(..., ...);
glVertex3F(..., ..., ...);
glTextCoord2f(..., ...);
glVertex3F(..., ..., ...);
glTextCoord2f(..., ...);
glVertex3F(..., ..., ...);
glTextCoord2f(..., ...);
glVertex3F(..., ..., ...);
glEnd();
}
}
There's no such thing like OpenGL initialization (apart from setting up a context). All what you do to in the "initialization" code either belongs in the render function or texture loading code:
All the following semantically belongs into the render path. OpenGL is a state machine, and like all state machines you put it into a proper start condition before you're doing some job.
//texture
glEnable(GL_TEXTURE_2D);
//Setup:
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);
//lighting
glLightfv(GL_LIGHT0, GL_POSITION, light_pos1);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
In your texture loader you have this:
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, t.texture);
Either this is a typo, or the generated texture name doesn't make it into 't.' texture.
I think i have fixed it.
Seems there was an actual problem with the texture file (.RAW images) and it was displaying as white instead of the colour/pattern it should have.
I tested again with a smaller amount of materials (3 instead of 3663) and it seems to work OK.
精彩评论