2017-06-18 71 views
0
#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);
然后颜色工程;问题是什么?请帮助opengl颜色与立方体不能正常工作

 //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 - inits显示模式 GLUT_DOUBLE - 允许双缓冲窗口上显示 GLUT_RGBA - 显示颜色(红,绿,蓝)和阿尔法 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; 
    } 
+0

我还没有看过你的问题,但请使用现代的OpenGL。那里有很多老的教程。 https://learnopengl.com是我见过的最好的网站之一。 –

回答

1

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

得到一个立方体。这只是轴线相交处的小斑点。当你绘制2个大单位,约160个单位,70度视场时,你还会期望看到什么?

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

我不知道你甚至指的是什么。 “默认颜色”将是GL内置颜色属性的初始值 - 即(1, 1, 1, 1) - 白色。随着您设置的代码,您将获得您之前设置的颜色。所以我唯一可以猜测的是你没有把GL的状态机考虑在内,让你感到困惑。

但是,除了这一切,你不应该使用这些代码在所有 - 这是使用固定功能管线和直接模式绘图 - 这是过时因为十年了功能,并没有被现代支持可言核心配置文件的OpenGL。试图在2017年学习这些东西是浪费时间。和BTW:

glutMainLoop();  //The main loop of OpenGL 

都能跟得上。只是不!。 OpenGL没有“主循环”。 GLUT不是OpenGL。老实说,这一切都很糟糕。

+0

感谢您的回答。这是我的大学计算机图形学课程提供的一个模板。他在课程中教授的平台和编码只是在我的头上!痛苦的问题是我没有任何教学与我的教师的方法完全匹配。你也在说这太可怕了:'(我真的不知道该怎么办,因为没有办法放弃课程。“ – misbah

+0

嗯,我不知道你的问题到底是什么,我解释了你为什么没有如果我不知道锥体的颜色有什么问题 - 我确实试过了你问题中的实际代码,而且所有的东西都应该显示出来,而且就大学而言图形课程是这样的:是的,有很多这些教会20年前的OpenGL。是的,通常没有太多的事情可以对付它,但是这并不能改变它太糟糕的事实。 – derhass