游戏开发论坛

 找回密码
 立即注册
搜索
查看: 4466|回复: 1

GL_COLOR_MATERIAL和GL_TEXTURE_2D的切换

[复制链接]

3

主题

4

帖子

8

积分

新手上路

Rank: 1

积分
8
发表于 2007-11-15 17:39:00 | 显示全部楼层 |阅读模式
我在从纹理贴图模式转回颜色贴图模式,总出错。设置颜色是可以的,但设置完颜色,再切换回纹理贴图模式后,整个场景颜色都变了,似乎铺上了一层刚才设置的颜色贴图。不知道怎么回事,忘高手指教。下面是相关源代码:
开纹理贴图是网上的一个读取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);),郁闷死

3

主题

4

帖子

8

积分

新手上路

Rank: 1

积分
8
 楼主| 发表于 2007-11-20 10:49:00 | 显示全部楼层

Re:GL_COLOR_MATERIAL和GL_TEXTURE_2D的切换

谢谢楼上帮助,我要的不是你的那个意思。我的程序是:先  glDisable(GL_COLOR_MATERIAL);glEnable(GL_TEXTURE_2D);开启纹理贴图,然后我又需要回到颜色模式,就调用:void Exp_1_3:rawForce()
{
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);

}
}
,在调用结束回到纹理模式(       glDisable(GL_COLOR_MATERIAL);
glEnable(GL_TEXTURE_2D);),但回去后,整个场景都铺上了刚才的color。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2025-6-17 23:36

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表