2012-07-11 133 views
0

我正試圖在屏幕上繪製一個四邊形(方形)。我把它插入一個預先存在的程序中,畫出一個圓柱體。這是在一個正交模型視圖矩陣中,我幾乎肯定剪輯音量是正確的(從原點的任何方向都是200)。在我使用的顯示函數中,我推送了一個矩陣,向前翻譯(0.0,0.0,-20.0),稱爲quadriliteral,然後彈出矩陣。任何常見的openGL設置的人使用,我不知道這可能會使這不可見?有沒有辦法獲得當前裁剪卷並在控制檯中打印出來?OpenGL四邊形不被繪製

void quadrilateral() 
{ 
    glLoadIdentity(); 
    glColor3f(0.5f,0.0f,0.8f); 
    glBegin(GL_QUADS); 

    glVertex3f(-10.0f,-10.0f,0.0f); 
    glVertex3f(-10.0f,10.0f,0.0f); 
    glVertex3f(10.0f,10.0f,0.0f); 
    glVertex3f(10.0f,-10.0f,0.0f); 

    glEnd(); 
} 


//This is called from a main function elsewhere 
void draw(void) 
{ 
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); 
    glutInitWindowSize(500, 500); 
    glutInitWindowPosition(100, 100); 
    glutCreateWindow("Cylinder"); 
    glutReshapeFunc(CChangeSize); 
    glutSpecialFunc(CSpecialKeys); 
    glutKeyboardFunc(Ckeyboard); 
    glutDisplayFunc(CRenderScene); 
    CSetupRC(); 
    glPopMatrix(); 
} 


void CSetupRC() 
{ 
// Light values and coordinates 
GLfloat ambientLight[] = {0.4f, 0.4f, 0.4f, 1.0f }; 
GLfloat diffuseLight[] = {0.7f, 0.7f, 0.7f, 1.0f }; 
GLfloat specular[] = { 0.9f, 0.9f, 0.9f, 1.0f}; 
GLfloat lightPos[] = { -50.0f, 200.0f, 200.0f, 1.0f }; 
GLfloat specref[] = { 0.6f, 0.6f, 0.6f, 1.0f }; 


glEnable(GL_DEPTH_TEST); // Hidden surface removal 
glEnable(GL_CULL_FACE);  // Do not calculate inside of solid object 
glFrontFace(GL_CCW); 

// Enable lighting 
glEnable(GL_LIGHTING); 

// Setup light 0 
glLightModelfv(GL_LIGHT_MODEL_AMBIENT,ambientLight); 
glLightfv(GL_LIGHT0,GL_AMBIENT,ambientLight); 
glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuseLight); 
glLightfv(GL_LIGHT0,GL_SPECULAR,specular); 

// Position and turn on the light 
glLightfv(GL_LIGHT0,GL_POSITION,lightPos); 
glEnable(GL_LIGHT0); 

// Enable color tracking 
glEnable(GL_COLOR_MATERIAL); 

// Set Material properties to follow glColor values 
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE); 

// All materials hereafter have full specular reflectivity 
// with a moderate shine 
glMaterialfv(GL_FRONT, GL_SPECULAR,specref); 
glMateriali(GL_FRONT,GL_SHININESS,64); 

// Black background 
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 


glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 

glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); 
glEnable(GL_TEXTURE_2D); 

} 


// Called to draw scene 
void CRenderScene(void) 
{ 
// Clear the window with current clearing color 
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 

// Save the matrix state 
glMatrixMode(GL_MODELVIEW); 
glPushMatrix(); 

// Rotate about x and y axes 
glRotatef(CxRot, 1.0f, 0.0f, 0.0f); 
glRotatef(CyRot, 0.0f, 1.0f, 0.0f); 

// Draw the cylinder 
cylinder(); 

glPopMatrix(); 

// CylinderTwo 
glMatrixMode(GL_MODELVIEW); 
glPushMatrix(); 

// Rotate about x and y axes 
glRotatef(CxRot, 1.0f, 0.0f, 0.0f); 
glRotatef(CyRot, 0.0f, 1.0f, 0.0f); 

// Draw the cylinder 
glRotatef(120.0f, 1.0f, 1.0f, 1.0f); 
cylinderTwo(); 

glPopMatrix(); 

// Random square 
glMatrixMode(GL_MODELVIEW); 
glPushMatrix(); 
glTranslatef(-30.0f,20.0f,10.0f); 
quadrilateral(); 
glPopMatrix(); 

// Swap buffers 
glutSwapBuffers(); 

}

+0

你可以繼續發佈所有相關的OpenGL初始化代碼嗎? (如果您不熟悉網站,請在您的帖子中查找編輯按鈕)。大約有100種不同的方式可以讓你搞砸,所以如果我們不需要猜測的話,這可能是最好的。歡迎來到SO。 – Tim 2012-07-11 16:07:25

+0

當然,它很長,但我會盡量保持它的短暫 – user1515657 2012-07-11 16:08:51

+0

不要讓它超過它所需的時間,但如果你不確定是否有什麼重要的話,就會在臃腫的一邊犯錯。 – Tim 2012-07-11 16:14:08

回答

1

它看起來像你的纏繞順序不正確:

glEnable(GL_CULL_FACE); 

此行意味着將剔除背面。

glFrontFace(GL_CCW); 

這使得正面逆時針方向。

glVertex3f(-10.0f,-10.0f,0.0f); 
    glVertex3f(-10.0f,10.0f,0.0f); 
    glVertex3f(10.0f,10.0f,0.0f); 
    glVertex3f(10.0f,-10.0f,0.0f); 

在這裏,您有順時針順序排列的頂點,這意味着您正在使用背面進行相機繪製。或者禁用撲殺:

glDisable(GL_CULL_FACE); 

使前臉順時針:

glFrontFace(GL_CW); 

或更改頂點的順序爲逆時針順序。

+0

我嘗試了以上所有,但唉,它仍然不會出現:(肯定還有其他東西是錯誤的 – user1515657 2012-07-11 17:15:26

+0

等我得到它的工作,非常感謝! – user1515657 2012-07-11 17:18:34

0

看來,我認爲你的四向後纏繞。你已經啓用了GL_CULL_FACE,它會拋出任何順時針纏繞的多邊形,並且你的四邊形有順時針纏繞。

禁用背面剔除或將頂點的順序逆時針切換。

+0

還在工作,其他東西一定是錯的 – user1515657 2012-07-11 17:17:48

+0

只是在開玩笑,我現在開始工作了。謝謝你的幫助! – user1515657 2012-07-11 17:18:52