2015-12-06 137 views
0

我需要幫助。我發現了幾個在OpenGl中繪製立方體的解決方案,但是我遇到了問題。如何在OpenGL中繪製立方體?

我的代碼是:

wglMakeCurrent(pDC->m_hDC, m_hrc); 
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
glMatrixMode(GL_MODELVIEW); 
glLoadIdentity(); 

glTranslatef(1.5f, 0.0f, -7.0f); // Move right and into the screen 

glBegin(GL_QUADS);    // Begin drawing the color cube with 6 quads 
            // Top face (y = 1.0f) 
            // Define vertices in counter-clockwise (CCW) order with normal pointing out 
glColor3f(0.0f, 1.0f, 0.0f);  // Green 
glVertex3f(1.0f, 1.0f, -1.0f); 
glVertex3f(-1.0f, 1.0f, -1.0f); 
glVertex3f(-1.0f, 1.0f, 1.0f); 
glVertex3f(1.0f, 1.0f, 1.0f); 

// Bottom face (y = -1.0f) 
glColor3f(1.0f, 0.5f, 0.0f);  // Orange 
glVertex3f(1.0f, -1.0f, 1.0f); 
glVertex3f(-1.0f, -1.0f, 1.0f); 
glVertex3f(-1.0f, -1.0f, -1.0f); 
glVertex3f(1.0f, -1.0f, -1.0f); 

// Front face (z = 1.0f) 
glColor3f(1.0f, 0.0f, 0.0f);  // Red 
glVertex3f(1.0f, 1.0f, 1.0f); 
glVertex3f(-1.0f, 1.0f, 1.0f); 
glVertex3f(-1.0f, -1.0f, 1.0f); 
glVertex3f(1.0f, -1.0f, 1.0f); 

// Back face (z = -1.0f) 
glColor3f(1.0f, 1.0f, 0.0f);  // Yellow 
glVertex3f(1.0f, -1.0f, -1.0f); 
glVertex3f(-1.0f, -1.0f, -1.0f); 
glVertex3f(-1.0f, 1.0f, -1.0f); 
glVertex3f(1.0f, 1.0f, -1.0f); 

// Left face (x = -1.0f) 
glColor3f(0.0f, 0.0f, 1.0f);  // Blue 
glVertex3f(-1.0f, 1.0f, 1.0f); 
glVertex3f(-1.0f, 1.0f, -1.0f); 
glVertex3f(-1.0f, -1.0f, -1.0f); 
glVertex3f(-1.0f, -1.0f, 1.0f); 

// Right face (x = 1.0f) 
glColor3f(1.0f, 0.0f, 1.0f);  // Magenta 
glVertex3f(1.0f, 1.0f, -1.0f); 
glVertex3f(1.0f, 1.0f, 1.0f); 
glVertex3f(1.0f, -1.0f, 1.0f); 
glVertex3f(1.0f, -1.0f, -1.0f); 
glEnd(); // End of drawing color-cube 

glFlush();     

SwapBuffers(pDC->m_hDC); 
wglMakeCurrent(NULL, NULL); 

貝婁是我想要得到的形象和我得到的圖像。 expected image and the image I get

如何解決這個問題呢? 在此先感謝。

+0

順時針繪製您的QUADS。 – Rabbid76

+0

我想我必須這樣做,但我不知道如何做到這一點 – VeDe88

+0

您是否啓用了深度測試? – BDL

回答

1

您必須啓用深度測試。否則三角形按代碼給出的順序繪製,即使三角形位於另一個三角形的後面,也會導致透支。要啓用深度測試撥打電話

glEnable(GL_DEPTH_TEST); 

發出第一次撥打電話之前。