我正在研究这个涉及创建2D平面网格的OpenGL项目。代码是这样的:
#include <stdlib.h>
#include <glut.h>
GLfloat ctrlpoints[9][9][3] = {
{{0, 0, 0}, {1, 0, 0}, {2, 0, 0}, {3, 0, 0}, {4, 0, 0}, {5, 0, 0}, {6, 0, 0}, {7, 0, 0}, {8, 0, 0}},
{{0, 1, 0}, {1, 1, 0}, {2, 1, 0}, {3, 1, 0}, {4, 1, 0}, {5, 1, 0}, {6, 1, 0}, {7, 1, 0}, {8, 1, 0}},
{{0, 2, 0}, {1, 2, 0}, {2, 2, 0}, {3, 2, 0}, {4, 2, 0}, {5, 2, 0}, {6, 2, 0}, {7, 2, 0}, {8, 2, 0}},
{{0, 3, 0}, {1, 3, 0}, {2, 3, 0}, {3, 3, 0}, {4, 3, 0}, {5, 3, 0}, {6, 3, 0}, {7, 3, 0}, {8, 3, 0}},
{{0, 4, 0}, {1, 4, 0}, {2, 4, 0}, {3, 4, 0}, {4, 4, 0}, {5, 4, 0}, {6, 4, 0}, {7, 4, 0}, {8, 4, 0}},
{{0, 5, 0}, {1, 5, 0}, {2, 5, 0}, {3, 5, 0}, {4, 5, 0}, {5, 5, 0}, {6, 5, 0}, {7, 5, 0}, {8, 5, 0}},
{{0, 6, 0}, {1, 6, 0}, {2, 6, 0}, {3, 6, 0}, {4, 6, 0}, {5, 6, 0}, {6, 6, 0}, {7, 6, 0}, {8, 6, 0}},
{{0, 7, 0}, {1, 7, 0}, {2, 7, 0}, {3, 7, 0}, {4, 7, 0}, {5, 7, 0}, {6, 7, 0}, {7, 7, 0}, {8, 7, 0}},
{{0, 8, 0}, {1, 8, 0}, {2, 8, 0}, {3, 8, 0}, {4, 8, 0}, {5, 8, 0}, {6, 8, 0}, {7, 8, 0}, {8, 8, 0}}
};
void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_FLAT);
glMap2f(GL_MAP2_VERTEX_3, 0, 1, 3, 9, 0, 1, 27, 9, &ctrlpoints[0][0][0]);
glEnable(GL_MAP2_VERTEX_3);
glEnable(GL_DEPTH_TEST);
}
void display(void)
{
int i, j;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
glColor3f(1.0, 1.0, 1.0);
glPushMatrix ();
gluLookAt(10,10,10, 0,0,0, 0,0,1);
/**** We have the option to use this method rather than dealilng with nasty loops *****/
glMapGrid2f( 50, 0.0, 1.0, 50, 0.0, 1.0);
glEnable(GL_AUTO_NORMAL); //this line calculates all normal vectors for the mesh. It has to be commented in when lighting is enabled.
//glEvalMesh2(GL_LINE, 0, 50, 0, 50); //This defines a mesh that 5 by 6 vetrices defined by our evaluator defined above
glEvalMesh2(GL_FILL, 0, 50, 0, 50); //This one fills the the mesh
/******************************************************/
glPopMatrix ();
glutSwapBuffers();
glutPostRedisplay();
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-10, 10, -10, 10, 0.1, 30);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void initlights(void)
{
GLfloat ambient[] = {0.0, 0.0, 1.0, 1.0};
GLfloat position[] = {1.0, 1.0, 2.0, 1.0};
GLfloat mat_diffuse[] = {1.0, 0.0, 0.0, 1.0};
GLfloat mat_specular[] = {0.0, 1.0, 0.0, 1.0};
GLfloat mat_shininess[] = {50.0};
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
glLightfv(GL_LIGHT0, GL_POSITION, position);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
init ();
initlights();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
GLfloat ctrlpoints 是一个数组,用于确定网格的大小和每个点的坐标。必须更改 glMap2f 以容纳数组,我相信我已经正确使用了该数组。现在它是9x9。但是,当我尝试运行程序时,没有任何内容出现。然而,真正奇怪的是,如果我使ctrlpoints更小,例如8x8:
GLfloat ctrlpoints[8][8][3] = {
{{0, 0, 0}, {1, 0, 0}, {2, 0, 0}, {3, 0, 0}, {4, 0, 0}, {5, 0, 0}, {6, 0, 0}, {7, 0, 0}},
{{0, 1, 0}, {1, 1, 0}, {2, 1, 0}, {3, 1, 0}, {4, 1, 0}, {5, 1, 0}, {6, 1, 0}, {7, 1, 0}},
{{0, 2, 0}, {1, 2, 0}, {2, 2, 0}, {3, 2, 0}, {4, 2, 0}, {5, 2, 0}, {6, 2, 0}, {7, 2, 0}},
{{0, 3, 0}, {1, 3, 0}, {2, 3, 0}, {3, 3, 0}, {4, 3, 0}, {5, 3, 0}, {6, 3, 0}, {7, 3, 0}},
{{0, 4, 0}, {1, 4, 0}, {2, 4, 0}, {3, 4, 0}, {4, 4, 0}, {5, 4, 0}, {6, 4, 0}, {7, 4, 0}},
{{0, 5, 0}, {1, 5, 0}, {2, 5, 0}, {3, 5, 0}, {4, 5, 0}, {5, 5, 0}, {6, 5, 0}, {7, 5, 0}},
{{0, 6, 0}, {1, 6, 0}, {2, 6, 0}, {3, 6, 0}, {4, 6, 0}, {5, 6, 0}, {6, 6, 0}, {7, 6, 0}},
{{0, 7, 0}, {1, 7, 0}, {2, 7, 0}, {3, 7, 0}, {4, 7, 0}, {5, 7, 0}, {6, 7, 0}, {7, 7, 0}}
};
并修改glMap2f
glMap2f(GL_MAP2_VERTEX_3, 0, 1, 3, 8, 0, 1, 24, 8, &ctrlpoints[0][0][0]);
屏幕突然出现在屏幕上!如果我也降低了它就行了,比如7x7。
这到底发生了什么?如果我尝试使网格大于8x8,怎么会出现呢?
答案 0 :(得分:0)
确保GL_MAX_EVAL_ORDER
大于8.评估者陈旧且笨拙,所以如果您的OpenGL实现只提供了最低要求(8),我不会感到惊讶。
您可以通过glGet()
检索GL_MAX_EVAL_ORDER
的值。
您还可以在glGetError()
来电后检查GL_INVALID_VALUE
glMap2f()
。