|
我在从纹理贴图模式转回颜色贴图模式,总出错。设置颜色是可以的,但设置完颜色,再切换回纹理贴图模式后,整个场景颜色都变了,似乎铺上了一层刚才设置的颜色贴图。不知道怎么回事,忘高手指教。下面是相关源代码:
开纹理贴图是网上的一个读取3DS文件的源代码:
/////////////////////////////////////////////////////////////////////////////////////
//函数名:Draw
//功能描述:绘制模型
//输入参数:glname:模型在名称堆栈的编号,(为一宏定义)
//输出参数:bool:绘制成功返回TRUE
//创建日期:
//////////////////////////////////////////////////////////////////////////////////////
BOOL C3DSModel: raw(GLint glname)
{
CLoad3DS::tMatREF *pmatref;
CLoad3DS::t3DObject *pObject;
// glPushMatrix();
if(glname != -1)
glPushName(glname);
// Since we know how many objects our model has, go through each of them.
for(int i = 0; i < m_3DModel.numOfObjects; i++)
{
// Get the current object that we are displaying
pObject = &m_3DModel.vctObjects;
//对所有的子材料
for(int imat=0;imat<pObject->numOfMaterials;imat++)
{
pmatref=&pObject->pMaterialREFS[imat];;
if(pmatref->bHasTexture)
{
glEnable(GL_TEXTURE_2D);
glColor3ub(255, 255, 255);
glBindTexture(GL_TEXTURE_2D, m_3DModel.vctMaterials[pmatref->nMaterialID].texureId);//g_Texture[pObject->materialID]);
}
else
{
glDisable(GL_TEXTURE_2D);
glColor3ub(255, 255, 255);
}
//开始绘制一个子物体
glBegin(GL_TRIANGLES);// Begin drawing with our selected mode (triangles or lines)
// Go through all of the faces (polygons) of the object and draw them
for(int nfindex = 0,j=0; nfindex < pmatref->nFaceNum; nfindex++)
{
j=int(pmatref->pFaceIndexs[nfindex]);
// Go through each corner of the triangle and draw it.
for(int whichVertex = 0; whichVertex < 3; whichVertex++)
{
// Get the index for each point of the face
int index = pObject->pFaces[j].vertIndex[whichVertex];
// Give OpenGL the normal for this vertex.
glNormal3f(pObject->pNormals[ index ].x, pObject->pNormals[ index ].y, pObject->pNormals[ index ].z);
// If the object has a texture associated with it, give it a texture coordinate.
if(pmatref->bHasTexture)// pObject->bHasTexture) {
{
// Make sure there was a UVW map applied to the object or else it won't have tex coords.
if(pObject->pTexVerts) {
glTexCoord2f(pObject->pTexVerts[ index ].x, pObject->pTexVerts[ index ].y);
}
}
else
{
if(m_3DModel.numOfMaterials>0 && pmatref->nMaterialID>=0)//pObject->materialID >= 0)
{
// Get and set the color that the object is, since it must not have a texture
BYTE *pColor = m_3DModel.vctMaterials[pmatref->nMaterialID].color;
// Assign the current color to this model
glColor3ub(pColor[0], pColor[1], pColor[2]);
}
}
glVertex3f(pObject->pVerts[ index ].x, pObject->pVerts[ index ].y, pObject->pVerts[ index ].z);
}
}
glEnd();
}
}
if(glname != -1)
glPopName();
// glPopMatrix();
return TRUE;
}
切换函数如下:
void Exp_1_3::DrawForce()
{
if(m_bIsCAROn)
{
glDisable(GL_TEXTURE_2D);
glEnable(GL_COLOR_MATERIAL);
glColor3f(0.0f, 1.0f, 0.0f);
glLineWidth(2.0f);
glBegin(GL_LINE_STRIP);
glVertex3f(3.8f,-1.0f,-8.1f);
glVertex3f(3.5f,-1.0f,-8.1f);
glVertex3f(3.5f,-2.0f,-8.1f);
glVertex3f(3.8f,-2.0f,-8.1f);
glVertex3f(3.8f,-1.0f,-8.1f);
glEnd();
glDisable(GL_COLOR_MATERIAL);
glEnable(GL_TEXTURE_2D);
}
}
但一调用上面这个函数,就整个场景读铺上了一次绿色(glColor3f(0.0f, 1.0f, 0.0f);),郁闷死
|
|