2015-12-24 41 views
0

這是我的代碼,它設置了一個透視視圖體積。矩形顯示正確。爲什麼茶壺不顯示? OpenGL

我想現在添加一個茶壺到我的場景中,所以我在繪製矩形後添加一條繪製茶壺的線條。但沒有顯示茶壺。

我設置了哪些參數?我的看法和茶壺有什麼問題?

GLint winWidth = 600, winHeight = 600; // Initial display-window size. 

GLfloat x0 = 50.0, y0 = 50.0, z0 = 50.0; // Viewing-coordinate origin. 
GLfloat xref = 50.0, yref = 50.0, zref = 0.0; // Look-at point. 
GLfloat Vx = 0.0, Vy = 1.0, Vz = 0.0;   // View-up vector. 

/* Set coordinate limits for the clipping window: */ 
//GLfloat xwMin = -40.0, ywMin = -60.0, xwMax = 40.0, ywMax = 60.0; 
GLfloat xwMin = -100.0, ywMin = -100.0, xwMax = 100.0, ywMax = 100.0; 

/* Set positions for near and far clipping planes: */ 
GLfloat dnear = 25.0, dfar = 125.0; 

void init (void) 
{ 
    glClearColor (1.0, 1.0, 1.0, 0.0); 

    glMatrixMode (GL_MODELVIEW); 
    glLoadIdentity(); 
    gluLookAt (x0, y0, z0, xref, yref, zref, Vx, Vy, Vz); 
    printf("look at orign:%.0f %.0f %.0f, pref: %.0f %.0f %.0f\n",x0, y0, z0, xref, yref, zref); 

    glMatrixMode (GL_PROJECTION); 
    glLoadIdentity(); 
    glFrustum (xwMin, xwMax, ywMin, ywMax, dnear, dfar); 
} 

void displayFcn (void) 
{ 
    init (); 
    glClear (GL_COLOR_BUFFER_BIT); 

    /* Set parameters for a square fill area. */ 
    glColor3f (0.0, 1.0, 0.0);   // Set fill color to green. 
    //glPolygonMode (GL_FRONT, GL_FILL); 
    glPolygonMode(GL_FRONT, GL_LINE); 
    glPolygonMode (GL_BACK, GL_LINE); // Wire-frame back face. 
    glBegin (GL_QUADS); 
    glVertex3f (0.0, 0.0, 0.0); 
    glVertex3f (100.0, 0.0, 0.0); 
    glVertex3f (100.0, 100.0, 0.0); 
    glVertex3f (0.0, 100.0, 0.0); 
    glEnd (); 
    glutSolidTeapot(50.9); 

    glFlush (); 
} 

void reshapeFcn (GLint newWidth, GLint newHeight) 
{ 
    glViewport (0, 0, newWidth, newHeight); 

    winWidth = newWidth; 
    winHeight = newHeight; 
} 
void keyboard(unsigned char key, int x, int y); 
int main (int argc, char** argv) 
{ 
    glutInit (&argc, argv); 
    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); 
    glutInitWindowPosition (50, 50); 
    glutInitWindowSize (winWidth, winHeight); 
    glutCreateWindow ("Perspective View of A Square"); 
    glutKeyboardFunc(keyboard); 
    glutDisplayFunc (displayFcn); 
    glutReshapeFunc (reshapeFcn); 
    glutMainLoop (); 
} 

回答

1

你的茶壺是完全關閉的看法。

你可以把它視體像這裏面:

glMatrixMode(GL_MODELVIEW); 
glTranslatef(50.f, 50.f, 0.f); 
glutSolidTeapot(50.9); 

還要注意的是視角的領域是瘋狂高對任何正常的觀看條件。考慮使用功能gluPerspective而不是glFrustum來輕鬆指定角度,而不是像glFrustum那樣手動指定按近平面距離縮放的角度的切線。

另請注意,所有這一切都是已棄用 GL。您正在使用的大多數功能都是從現代核心配置文件上下文中刪除的。如果您現在開始學習GL,我的建議是學習使用可編程管道的新(十年前)方法,而不是使用內置矩陣堆棧的舊的(20年)固定功能管道。

+0

感謝您的耐心解答。 「視場角非常高」解釋了爲什麼我的茶壺看起來非常透視...在我目前的視錐體中,theta差不多是110度。我的計算是否正確? – HRLTY

+0

在我的計算中,frstum在距離25的近平面上從-100到+100,使得半角的截面積爲100/25 = 4,所以總的來說,如果視場達到〜152度,從水平到垂直。 – derhass