I have this OpenGL project I'm working on that involves creating a 2D flat mesh to mess around with. The code is as such:
#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 is array that determines how big the mesh is and the coordinates of each point. glMap2f must be changed to accommodate the array, which I believe I have used correctly. Right now it's is 9x9. However, nothing is showing up when I try running the program. What's really strange however is that if I make ctrlpoints smaller, such as 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}}
};
and modify glMap2f as such
glMap2f(GL_MAP2_VERTEX_3, 0, 1, 3, 8, 0, 1, 24, 8, &ctrlpoints[0][0][0]);
The mesh suddenly appears on the screen! It worked if I went lower as well, like 7x7.
What exactly is going on here? How come if I try making the mesh bigger then 8x8, it doesn't appear?
Make sure GL_MAX_EVAL_ORDER
is greater than 8. Evaluators are old and janky so I wouldn't be surprised if your OpenGL implementation only provided the minimum required (8).
You can retrieve the value of GL_MAX_EVAL_ORDER
via glGet()
.
You can also check glGetError()
for GL_INVALID_VALUE
after your glMap2f()
call.