#include #include #include #include typedef float vector3[3]; typedef struct { vector3 position; vector3 colour; } vertex; static vertex data[] = { { { 0.0, 0.0, 0.0 }, { 1.0, 0.0, 0.0 } }, { { 100.0, 0.0, 0.0 }, { 0.0, 1.0, 0.0 } }, { { 100.0, 100.0, 0.0 }, { 0.0, 0.0, 1.0 } }, { { 0.0, 100.0, 0.0 }, { 1.0, 1.0, 0.0 } }, }; static void reshape(int width, int height) { glViewport(0, 0, width, height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0, width, 0, height, -1.0, 100.0); assert(glGetError() == GL_NO_ERROR); } static void display(void) { glClearColor(0.3f, 0.3f, 0.3f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glPushMatrix(); glTranslated(20.0, 20.0, 0.0); glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_COLOR_ARRAY); { const unsigned char *data_v = (unsigned char *) &data; const unsigned char *data_c = ((unsigned char *) &data) + offsetof(vertex, colour); { const unsigned char indices[] = { 0, 1, 2 }; glVertexPointer(3, GL_FLOAT, sizeof(vertex), data_v); glColorPointer(3, GL_FLOAT, sizeof(vertex), data_c); glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_BYTE, indices); } glTranslated(120.0, 120.0, 0.0); { const unsigned char indices[] = { 1, 2, 3 }; glVertexPointer(3, GL_FLOAT, sizeof(vertex), data_v); glColorPointer(3, GL_FLOAT, sizeof(vertex), data_c); glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_BYTE, indices); } } glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_COLOR_ARRAY); glPopMatrix(); assert(glGetError() == GL_NO_ERROR); glutSwapBuffers(); } int main(int argc, char **argv) { glutInit(&argc, argv); glutCreateWindow("Vertex array triangle"); glutReshapeFunc(reshape); glutDisplayFunc(display); glutIdleFunc(glutPostRedisplay); glutMainLoop(); return 0; }