#define GL_GLEXT_PROTOTYPES 1 #include #include #include #include #include typedef float vector3[3]; typedef struct { vector3 position; vector3 colour; } vertex; static GLuint vertices_name; static GLuint triangles[2]; static void init(void) { vertex *data = malloc(4 * sizeof(vertex)); if (data == NULL) abort(); data[0].position[0] = 0.0; data[0].position[1] = 0.0; data[0].position[2] = 0.0; data[0].colour[0] = 1.0; data[0].colour[1] = 0.0; data[0].colour[2] = 0.0; data[1].position[0] = 100.0; data[1].position[1] = 0.0; data[1].position[2] = 0.0; data[1].colour[0] = 0.0; data[1].colour[1] = 1.0; data[1].colour[2] = 0.0; data[2].position[0] = 100.0; data[2].position[1] = 100.0; data[2].position[2] = 0.0; data[2].colour[0] = 0.0; data[2].colour[1] = 0.0; data[2].colour[2] = 1.0; data[3].position[0] = 0.0; data[3].position[1] = 100.0; data[3].position[2] = 0.0; data[3].colour[0] = 1.0; data[3].colour[1] = 1.0; data[3].colour[2] = 0.0; glGenBuffers(1, &vertices_name); glBindBuffer(GL_ARRAY_BUFFER, vertices_name); glBufferData(GL_ARRAY_BUFFER, 4 * sizeof(vertex), data, GL_STATIC_DRAW); free(data); glGenBuffers(2, triangles); { unsigned char indices[] = { 0, 1, 2 }; glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, triangles[0]); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); } { unsigned char indices[] = { 1, 2, 3 }; glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, triangles[1]); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); } assert(glGetError() == GL_NO_ERROR); } 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); glBindBuffer(GL_ARRAY_BUFFER, vertices_name); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, triangles[0]); glVertexPointer(3, GL_FLOAT, sizeof(vertex), (void *) 0); glColorPointer(3, GL_FLOAT, sizeof(vertex), (void *) offsetof(vertex, colour)); glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_BYTE, (void *) 0); glTranslated(120.0, 120.0, 0.0); glBindBuffer(GL_ARRAY_BUFFER, vertices_name); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, triangles[1]); glVertexPointer(3, GL_FLOAT, sizeof(vertex), (void *) 0); glColorPointer(3, GL_FLOAT, sizeof(vertex), (void *) offsetof(vertex, colour)); glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_BYTE, (void *) 0); 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"); init(); glutReshapeFunc(reshape); glutDisplayFunc(display); glutIdleFunc(glutPostRedisplay); glutMainLoop(); return 0; }