#include <GL/glut.h>
int setupMethod = POINTER;
int derefMethod = ARRAYELEMENT;
void setupPointers(void)
{
static GLint vertices[] = {25, 25,
100, 325,
175, 25,
175, 325,
250, 25,
325, 325};
static GLfloat colors[] = {1.0, 0.2, 0.2,
0.2, 0.2, 1.0,
0.8, 1.0, 0.2,
0.75, 0.75, 0.75,
0.35, 0.35, 0.35,
0.5, 0.5, 0.5};
glEnableClientState (GL_VERTEX_ARRAY);
glEnableClientState (GL_COLOR_ARRAY);
glVertexPointer (2, GL_INT, 0, vertices);
glColorPointer (3, GL_FLOAT, 0, colors);
}
void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_SMOOTH);
setupPointers ();
}
void display(void)
{
glClear (GL_COLOR_BUFFER_BIT);
if (derefMethod == DRAWARRAY)
glDrawArrays (GL_TRIANGLES, 0, 6);
else if (derefMethod == ARRAYELEMENT) {
glBegin (GL_TRIANGLES);
glArrayElement (2);
glArrayElement (3);
glArrayElement (5);
glEnd ();
}
else if (derefMethod == DRAWELEMENTS) {
GLuint indices[4] = {0, 1, 3, 4};
// glDrawElements (GL_POLYGON, 4, GL_UNSIGNED_INT, indices);
}
glFlush ();
}
void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluOrtho2D (0.0, (GLdouble) w, 0.0, (GLdouble) h);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (350, 350);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
启用:glEnableClientState;
初始化:glVertexPointer
渲染:glDrawArrays与glArrayElement