我要发帖 回复

正式会员

3

主题

25

积分

0

专家分

OpenGL迷

:

私信
发表时间 : 2009-3-4 22:12:29 | 浏览 : 1419    评论 : 5
在下面这个程序中,我在画不同的图时采用了不同的颜色,可是编译运行后却只有一种颜色:品红。
请问这是为什么,不胜感谢
#include <windows.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glaux.h>

void CALLBACK reshape(int width,int height);
void CALLBACK draw(void);

void CALLBACK reshape(int width,int height){
        glViewport(0,0,(GLint)width,(GLint)height);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(-8.0,8.0,-8.0,8.0,-8.0,8.0);
        glMatrixMode(GL_MODELVIEW);
}

void CALLBACK draw(void){
        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

        glLoadIdentity();
        glTranslatef(-2.5,0.0,0.0);
        glRotatef(-30,1.0,0.0,0.0);
        glRotatef(30,0.0,1.0,0.0);
        glRotatef(30,0.0,0.0,1.0);
       
        glColor3f(1.0,0.0,1.0);
        //Draw the sides of the three-sided pyramid
        glBegin(GL_TRIANGLE_FAN);
             glVertex3d(0,4,0);
                 glVertex3d(0,-4,-4);
                 glVertex3d(-4,-4,4);
                 glVertex3d(4,-4,4);
                 glVertex3d(0,-4,-4);
        glEnd();

        glClearColor(0.0,0.0,0.0,0.0);//tian
        //glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);//
        glColor3f(0.0,1.0,1.0);
        //draw the abse of the pyramid
        glBegin(GL_TRIANGLES);
             glVertex3d(0,-4,-4);
                 glVertex3d(-4,-4,4);
                 glVertex3d(4,-4,4);
        glEnd();

        glLoadIdentity();
        glTranslatef(2.5,0.0,0.0);
        glRotatef(45,1.0,0.0,0.0);
        glRotatef(45,0.0,1.0,0.0);
        glRotatef(45,0.0,0.0,1.0);

        glClearColor(0.0,0.0,0.0,0.0);//tian
        //glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);//
        glColor3f(0.0,1.0,0.0);
        //draw the sides of the cube
        glBegin(GL_QUAD_STRIP);
                 glVertex3d(3,3,-3);
                 glVertex3d(3,-3,-3);
                 glVertex3d(-3,3,-3);
                 glVertex3d(-3,-3,-3);
                 glVertex3d(-3,3,3);
                 glVertex3d(-3,-3,3);
                 glVertex3d(3,3,3);
                 glVertex3d(3,-3,3);
                 glVertex3d(3,3,-3);
                 glVertex3d(3,-3,-3);
        glEnd();

        glClearColor(0.0,0.0,0.0,0.0);//tian
        //glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
        glColor3f(0.0,0.0,1.0);
        //Draw the top and bottom of the cube
        glBegin(GL_QUADS);
             glVertex3d(-3,-3,-3);
                 glVertex3d(3,-3,-3);
                 glVertex3d(3,-3,3);
                 glVertex3d(-3,-3,3);

                 glVertex3d(-3,3,-3);
                 glVertex3d(3,3,-3);
                 glVertex3d(3,3,3);
                 glVertex3d(-3,3,3);
        glEnd();

        glFlush();
}

void main(int argc,char **argv){
        auxInitPosition(0,0,500,500);
        auxInitWindow(argv[0]);

        auxInitDisplayMode(AUX_RGB|AUX_DEPTH);

        glClearColor(0.0,0.0,0.0,0.0);

        glShadeModel(GL_FLAT);

        glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
        glEnable(GL_DEPTH_TEST);
        auxReshapeFunc(reshape);
        auxMainLoop(draw);
}

最近VR访客

tan_dunming 评论于2009-3-5 09:03:46
应该是你的AUX库使用的不正确吧? 我没有研究过AUX库,一般都用GLUT,我将你的代码用GLUT重新写了一下,编译运行没有问题。
GLUT.jpg

附上GLUT的代码供你测试。
main.rar (919 Bytes, 下载次数: 23)
追求卓越
maildaty 评论于2009-3-5 17:01:14
谢谢了啊
飞飞羽 评论于2009-4-1 11:03:18

修改如下即可

这样修改就可以了     
   //Draw the sides of the three-sided pyramid
        glBegin(GL_TRIANGLE_FAN);
             glVertex3d(0,4,0);
                 glVertex3d(0,-4,-4);
                 glVertex3d(-4,-4,4);
                 glColor3f(1.0,0.0,0.0);
                 glVertex3d(4,-4,4);
                 glColor3f(1.0,1.0,0.0);
                 glVertex3d(0,-4,-4);
        glEnd();
dongzh311 评论于2009-4-2 13:28:21
试了一下,没问题
dongzh311 评论于2009-4-2 13:42:19
奇怪

手机版|VR开发网 ( 津ICP备18009691号 ) 统计 网安备12019202000257

GMT+8, 2020-10-27 02:47 PM

返回顶部