在我的OpenGL代碼中存在一個奇怪的行爲。我想在地上畫一塊地毯。同一平面內的另一個多邊形內的多邊形
在下面的代碼中,如果GROUND_SIZE大於2071且CARPET_HEIGHT小於0.0003,則不會繪製較小的多邊形。繪圖順序不會更改生成的圖像。
#include <GL/glut.h>
const int GROUND_SIZE = 3000;
const bool GROUND_FIRST = true;
const float CARPET_HEIGHT = 0.0003;
void carpet(){
glColor3f(1.0,0.0,0.0);
glBegin(GL_QUADS);
glNormal3f(0,1,0);
glVertex3f(-1.0, -1.0, CARPET_HEIGHT);
glVertex3f(1.0, -1.0, CARPET_HEIGHT);
glVertex3f(1.0, 1.0, CARPET_HEIGHT);
glVertex3f(-1.0, 1.0, CARPET_HEIGHT);
glEnd();
}
void ground(){
glColor3f(0.0,0.7,0.0);
glBegin(GL_QUADS);
glNormal3f(0,1,0);
glVertex3f(-GROUND_SIZE, -GROUND_SIZE, 0);
glVertex3f(GROUND_SIZE, -GROUND_SIZE, 0);
glVertex3f(GROUND_SIZE, GROUND_SIZE, 0);
glVertex3f(-GROUND_SIZE, GROUND_SIZE, 0);
glEnd();
}
void draw(){
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
if (GROUND_FIRST) {
ground();
carpet();
}
else {
carpet();
ground();
}
glutSwapBuffers();
}
int main(){
int argc = 1; char* argv[] = { (char*)"" };
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowPosition(100, 50);
glutInitWindowSize(640, 640);
glutCreateWindow("Window");
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_DEPTH_TEST);
glClearColor(0.0f, 1.0f, 1.0f, 1.0f);
glutDisplayFunc(draw);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45, 1, 0.1, 1000.0);
gluLookAt(0.0, -5.0, 1.0, 0.0f, 0.0f, 1.0f, 0,0,1);
glutMainLoop();
return 0;
}
我想了解OpenGL用來決定是否會在同一平面內的另一個多邊形內繪製多邊形的規則。
謝謝
我通常建議設置在任何矩陣操作之前的適當矩陣模式,而不是依賴於事物被正確地切換回來。 – datenwolf 2011-06-14 09:43:37