【问题标题】:opengl Color & Cube is not working properlyopengl 颜色和立方体无法正常工作
【发布时间】:2017-11-20 16:40:22
【问题描述】:
    #include<stdio.h>
    #include<stdlib.h>
    #include<math.h>

    #include<GL/glut.h>

    double cameraAngle;

    void grid_and_axes() {

        // draw the three major AXES

        glBegin(GL_LINES);
        //X axis
        glColor3f(0, 1, 0); //100% Green
        glVertex3f(-150, 0, 0);
        glVertex3f(150, 0, 0);

        //Y axis
        glColor3f(0, 0, 1); //100% Blue
        glVertex3f(0, -150, 0); // intentionally extended to -150 to 150, no big deal
        glVertex3f(0, 150, 0);

        //Z axis
        glColor3f(1, 1, 1); //100% White
        glVertex3f(0, 0, -150);
        glVertex3f(0, 0, 150);
        glEnd();

        //some gridlines along the field
        int i;

        glColor3f(0.5, 0.5, 0.5);   //grey
        glBegin(GL_LINES);
        for (i = -10; i <= 10; i++) {

            if (i == 0)
                continue;   //SKIP the MAIN axes

                            //lines parallel to Y-axis
            glVertex3f(i * 10, -100, 0);
            glVertex3f(i * 10, 100, 0);

            //lines parallel to X-axis
            glVertex3f(-100, i * 10, 0);
            glVertex3f(100, i * 10, 0);
        }
        glEnd();

    }

    void display() {
        //codes for Models, Camera

        //clear the display
        //glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glClearColor(0, 0, 0, 0);   //color black
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);     //clear buffers to preset values

                                                                /***************************
                                                                / set-up camera (view) here
                                                                ****************************/
                                                                //load the correct matrix -- MODEL-VIEW matrix
        glMatrixMode(GL_MODELVIEW);     //specify which matrix is the current matrix

                                        //initialize the matrix
        glLoadIdentity();               //replace the current matrix with the identity matrix [Diagonals have 1, others have 0]

                                        //now give three info
                                        //1. where is the camera (viewer)?
                                        //2. where is the camera looking?
                                        //3. Which direction is the camera's UP direction?

                                        //gluLookAt(0,-150,20,  0,0,0,  0,0,1);
        gluLookAt(150 * sin(cameraAngle), -150 * cos(cameraAngle), 50, 0, 0, 0, 0, 0, 1);


        /*************************
        / Grid and axes Lines
        **************************/
        grid_and_axes();


        /****************************
        / Add your objects from here
        ****************************/

        /*glColor3f(1, 0, 0);
        glutSolidCone(20, 20, 20, 20);

        glColor3f(0, 0, 1);
        GLUquadricObj *cyl = gluNewQuadric();
        gluCylinder(cyl, 10, 10, 50, 20, 20);

        glTranslatef(0, 0, 50);
        glColor3f(1, 0, 0);
        glutSolidCone(10, 20, 20, 20);
    */
        glColor3f(1, 0, 0);

        glutSolidCube(1);

我在这里没有得到任何立方体。
但是,如果我使用任何转换属性(例如缩放或旋转),那么我会得到所需的立方体,例如
glColor3f(1, 0, 0);
glScalef(50,5,60);
glutSolidCube(1);
问题是什么? 如果我不使用上面提到的转换属性,我面临的另一个问题是颜色不起作用。如果我写:
glColor3f(1, 0, 0);
glutSolidCone(20, 20, 20, 20);
对于上述代码,颜色不起作用;我得到了默认的彩色圆锥
但是,如果我将这两行更改为这 3 行,那么颜色就完美了:
glColor3f(1,0,0);
glTranslatef(0, 0, 50);
glutSolidCone(10,20,20,20);
然后颜色起作用;问题是什么?请帮忙

        //ADD this line in the end --- if you use double buffer (i.e. GL_DOUBLE)
        glutSwapBuffers();
    }

    void animate() {
        //codes for any changes in Models, Camera

        cameraAngle += 0.001;   // camera will rotate at 0.002 radians per frame.

                                //codes for any changes in Models

                                //MISSING SOMETHING? -- YES: add the following
        glutPostRedisplay();    //this will call the display AGAIN

    }

    void init() {
        //codes for initialization

        cameraAngle = 0;    //angle in radian
                            //clear the screen
        glClearColor(0, 0, 0, 0);

        /************************
        / set-up projection here
        ************************/
        //load the PROJECTION matrix
        glMatrixMode(GL_PROJECTION);

        //initialize the matrix
        glLoadIdentity();

        /*
        gluPerspective() — set up a perspective projection matrix

        fovy -         Specifies the field of view angle, in degrees, in the y direction.
        aspect ratio - Specifies the aspect ratio that determines the field of view in the x direction. The aspect ratio is the ratio of x (width) to y (height).
        zNear -        Specifies the distance from the viewer to the near clipping plane (always positive).
        zFar  -        Specifies the distance from the viewer to the far clipping plane (always positive).
        */

        gluPerspective(70, 1, 0.1, 10000.0);

    }

    int main(int argc, char **argv) {

        glutInit(&argc, argv);                          //initialize the GLUT library

        glutInitWindowSize(500, 500);
        glutInitWindowPosition(100, 100);

/* glutInitDisplayMode - 初始化显示模式 GLUT_DOUBLE - 允许在双缓冲窗口上显示 GLUT_RGBA - 显示颜色(红、绿、蓝)和 alpha GLUT_DEPTH - 允许深度缓冲 */ glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGB);

        glutCreateWindow("Some Title");

        init();                     //codes for initialization

        glEnable(GL_DEPTH_TEST);    //enable Depth Testing

        glutDisplayFunc(display);   //display callback function
        glutIdleFunc(animate);      //what you want to do in the idle time (when no drawing is occuring)

        glutMainLoop();     //The main loop of OpenGL

        return 0;
    }

【问题讨论】:

  • 我还没有阅读您的问题,但请使用现代 OpenGL。那里有很多旧教程。 learnopengl.com 是我见过的最好的网站之一。

标签: opengl glut


【解决方案1】:

我在这里没有得到任何立方体。

确实得到了一个立方体。它只是轴相交的那个小斑点。当您绘制 2 个单位大、约 160 个单位、具有 70 度视野的东西时,您还期望看到什么?

如果我不使用上面提到的转换属性,我面临的另一个问题是颜色不起作用。 [...] 我得到了默认的彩色圆锥。

我什至不知道你的意思。 “默认颜色”将是 GL 内置颜色属性的初始值 - 即 (1, 1, 1, 1) - 白色。使用您设置的代码,您将获得之前设置的颜色。所以我在这里唯一能做的猜测是你没有正确考虑到 GL 的状态机,这让你自己感到困惑。

但除此之外,您根本不应该使用该代码 - 这是使用固定函数管道和立即模式绘图 - 十年以来不推荐使用的功能,不是OpenGL 的现代核心配置文件完全支持。试图在 2017 年学习这些东西是浪费时间。顺便说一句:

glutMainLoop();     //The main loop of OpenGL

不。只是不!!!。 OpenGL 没有“主循环”。 GLUT 不是 OpenGL。老实说,这一切都太可怕了。

【讨论】:

  • 感谢您的回答。这是我大学计算机图形学课程提供的模板。他在课程中教授的平台和编码让我头疼!痛苦的问题是我没有任何与我的教师方法完全匹配的教程。你还说这很可怕:'(我真的不知道该怎么办,因为没有办法退出课程。
  • 好吧,我不知道你的问题到底是什么。我确实解释了为什么你没有看到没有缩放的立方体。如果不知道锥体颜色的问题是什么 - 我确实从您的问题中尝试了实际代码,并且一切都按原样出现。就大学图形课程而言:是的,其中有很多教授 20 岁的 OpenGL。是的,您通常无能为力,但这并不能改变它非常可怕的事实。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-04-14
  • 1970-01-01
  • 1970-01-01
  • 2022-08-18
  • 2016-09-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多