opengl Color&立方体无法正常工作

时间:2017-06-18 10:28:32

标签: opengl glut

    #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);
  对于上面的代码,颜色不起作用;我得到默认的彩色圆锥
  但是,如果我将这两行更改为这三行,那么颜色就可以完美地工作:
  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;
    }

1 个答案:

答案 0 :(得分:1)

  

我这里没有任何立方体。

获取一个立方体。它只是轴相交的微小斑点。当你绘制2个单位大,约160个单位,70度视野的东西时,你还期望看到什么呢?

  

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

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

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

glutMainLoop();     //The main loop of OpenGL

不。只是没有!!! 。 OpenGL没有&#34;主循环&#34;。 GLUT不是OpenGL。老实说,这一切都很可怕。