颜色干扰的问题解决了
昨天被纹理贴图的颜色干扰问题弄的很是郁闷,自己也知道OpenGL是个状态机,状态设定好后就一直是有效的,但就是想不明白后面设置的状态怎么会影响到前面呢?
在csdn上请教了高手后,终于知道问题改怎么解决了,只需要在绑定纹理前开启纹理,并且在使用完纹理后就关闭纹理,否则的话纹理就一直处于开启状态,所以会影响到不使用纹理贴图的那个物体的绘制,修改后代码如下:
int COpenGLDemoView::DrawGLScene()
{// Here's Where We Do All The Drawing
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
glPushAttrib(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_CURRENT_BIT);
glPushMatrix();
glShadeModel(GL_SMOOTH);
glLoadIdentity(); // Reset The Current Modelview Matrix
glTranslatef(-1.5f,0.0f,-6.0f);//物体左移1.5,向内移6,相当于移动镜头一样,让物体进入镜头中
glRotatef(rTri,0.0f,1.0f,0.0f);
glBegin(GL_TRIANGLES); // 绘制三角形
glColor3f(1.0f,0.0f,0.0f); // 红色
glVertex3f( 0.0f, 1.0f, 0.0f); // 三角形的上顶点 (前侧面)
glColor3f(0.0f,1.0f,0.0f); // 绿色
glVertex3f(-1.0f,-1.0f, 1.0f); // 三角形的左下顶点 (前侧面)
glColor3f(0.0f,0.0f,1.0f); // 蓝色
glVertex3f( 1.0f,-1.0f, 1.0f); // 三角形的右下顶点 (前侧面)
glColor3f(1.0f,0.0f,0.0f); // 红色
glVertex3f( 0.0f, 1.0f, 0.0f); // 三角形的上顶点 (右侧面)
glColor3f(0.0f,0.0f,1.0f); // 蓝色
glVertex3f( 1.0f,-1.0f, 1.0f); // 三角形的左下顶点 (右侧面)
glColor3f(0.0f,1.0f,0.0f); // 绿色
glVertex3f( 1.0f,-1.0f, -1.0f); // 三角形的右下顶点 (右侧面)
glColor3f(1.0f,0.0f,0.0f); // 红色
glVertex3f( 0.0f, 1.0f, 0.0f); // 三角形的上顶点 (后侧面)
glColor3f(0.0f,1.0f,0.0f); // 绿色
glVertex3f( 1.0f,-1.0f, -1.0f); // 三角形的左下顶点 (后侧面)
glColor3f(0.0f,0.0f,1.0f); // 蓝色
glVertex3f(-1.0f,-1.0f, -1.0f); // 三角形的右下顶点 (后侧面)
glColor3f(1.0f,0.0f,0.0f); // 红色
glVertex3f( 0.0f, 1.0f, 0.0f); // 三角形的上顶点 (左侧面)
glColor3f(0.0f,0.0f,1.0f); // 蓝色
glVertex3f(-1.0f,-1.0f,-1.0f); // 三角形的左下顶点 (左侧面)
glColor3f(0.0f,1.0f,0.0f); // 绿色
glVertex3f(-1.0f,-1.0f, 1.0f); // 三角形的右下顶点 (左侧面
glEnd(); // 三角形绘制结束
glPopMatrix();
glPopAttrib();
glPushAttrib(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_CURRENT_BIT);
glPushMatrix();
glLoadIdentity();
glTranslatef(1.5f,0.0f,-6.0f);
glRotatef(xrot,1.0f,0.0f,0.0f);
glRotatef(yrot,0.0f,1.0f,0.0f);
glRotatef(zrot,0.0f,0.0f,1.0f);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glBegin(GL_QUADS); // 绘制正方形
// Front Face
glNormal3f( 0.0f, 0.0f, 1.0f); // 法线指向观察者
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
// Back Face
glNormal3f( 0.0f, 0.0f,-1.0f); // 法线背向观察者
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
// Top Face
glNormal3f( 0.0f, 1.0f, 0.0f); // 法线向上
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
// Bottom Face
glNormal3f( 0.0f,-1.0f, 0.0f); // 法线朝下
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
// Right face
glNormal3f( 1.0f, 0.0f, 0.0f); // 法线朝右
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
// Left Face
glNormal3f(-1.0f, 0.0f, 0.0f); // 法线朝左
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
glEnd(); // 正方形绘制结束
glDisable(GL_TEXTURE_2D);
glPopMatrix();
glPopAttrib();
glFlush();
return TRUE; // Everything Went OK
}
最终效果如图所示:
作者:洞庭散人
出处:http://phinecos.cnblogs.com/
本博客遵从Creative Commons Attribution 3.0 License,若用于非商业目的,您可以自由转载,但请保留原作者信息和文章链接URL。
posted on 2007-08-10 12:42 Phinecos(洞庭散人) 阅读(1501) 评论(1) 编辑 收藏 举报