尝试绘制多边形并用特定颜色填充时,我遇到了一个奇怪的问题:
如果我将多边形模式设置为:
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
多边形渲染得很好:
但是,只要用以下内容替换该行:
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
多边形没有向右填充,但似乎大多数线都朝向其第一个顶点投射,或沿着这些线投射:
我显然做错了什么。我想要做的是保持多边形内的颜色,但它似乎忽略了几个顶点。什么可能是错的?
以下是我的代码中可能感兴趣的部分选定部分。我正在跳过一些数据结构加载和其他可能不太相关的东西:
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
glutInitWindowSize(640, 480);
glutInitWindowPosition(150, 100);
glutCreateWindow("CR-View GL");
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
void display(void) {
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT);
/* Set drawing color */
glColor3f(1, 0, 1);
drawPolys(currentDrawingMode);
/* Clear screen and draw */
glutSwapBuffers();
}
// Draws the polygons
void drawPolys (int id) {
int poly, vertex;
// set wireframe mode (if an empty polygon is required)
if (id == 0) {
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
}
//Sets color fill mode
if (id == 1) {
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
}
// Draw each polygon...
for (poly = 0; poly < polyCount; poly++) {
glBegin(GL_POLYGON);
// Draw each vertex...
for (vertex = 0; vertex < Polygons[poly].vertexCount; vertex++) {
glVertex2f((float)Polygons[poly].vertices[vertex].x, (float)Polygons[poly].vertices[vertex].y);
}
glEnd();
}
}