我正在尝试绘制一些简单的三维矩形棱柱,但是glBindTexture似乎无法正常工作。
我有N个“部件”,每个都有一个材料名称。
我正在循环遍历每个部件,根据其材料名称找到其纹理,调用glBindTexture,然后绘制该部件。
然而,如果我有多个具有相同材料名称的部件,则仅第一个带有纹理,其余部分则为白色。
非常感谢您的任何帮助。
以下是代码:
OpenGL初始化:
纹理加载
我有N个“部件”,每个都有一个材料名称。
我正在循环遍历每个部件,根据其材料名称找到其纹理,调用glBindTexture,然后绘制该部件。
然而,如果我有多个具有相同材料名称的部件,则仅第一个带有纹理,其余部分则为白色。
非常感谢您的任何帮助。
以下是代码:
OpenGL初始化:
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);
// 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);
纹理加载
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);
接下来是绘图代码:
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();
}
}