2017-06-18 69 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