#include #include #define GL_GLEXT_PROTOTYPES #include #include #include #include #include #include #include #include GLfloat points [] = { 0.5, -1, 0.5, 0, 0, 1, 0.5, 0, 0.5, 0, 0, 1, -0.5, 0, 0.5, 0, 0, 1, -0.5, -1, 0.5, 0, 0, 1, 0.5, -0.5, 0.5, 0, 0, 1, 0, 0, 0.5, 0, 0, 1, -0.5, -0.5, 0.5, 0, 0, 1, 0, -1, 0.5, 0, 0, 1, 0.5, 1, 0.5, 0, 0, 1, -0.5, 1, 0.5, 0, 0, 1, 0, 1, 0.5, 0, 0, 1, -0.5, 0.5, 0.5, 0, 0, 1, 0.5, 0.5, 0.5, 0, 0, 1 }; GLushort conn [] = { 4, 1, 5, 2, 6, 3, 7, 0, 10, 9, 11, 2, 5, 1, 12, 8 }; const GLvoid* offsets [] = { (GLvoid*)0x0, (GLvoid*)0x10 }; const GLsizei counts [] = { 8, 8 }; GLuint stride = 24; GLuint normal_offset = 12; GLuint vert_buffer, conn_buffer; static int screen_width, screen_height; static void draw(void) { glBindBuffer(GL_ARRAY_BUFFER, vert_buffer); glEnableClientState(GL_VERTEX_ARRAY); glVertexPointer(3, GL_FLOAT, stride, 0); glEnableClientState(GL_NORMAL_ARRAY); glNormalPointer(GL_FLOAT, stride, reinterpret_cast(normal_offset)); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, conn_buffer); glMultiDrawElements(GL_POLYGON, counts, GL_UNSIGNED_SHORT, offsets, 2); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); } static void drawfunc(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glPushMatrix(); // Render some triangles directly in red glColor3f(.5, 0, 0); draw(); glPopMatrix(); glutSwapBuffers(); } static void idle_visible() { glutPostRedisplay(); } /* new window size or exposure */ static void reshape(int width, int height) { float h = (float) height / (float) width; float s = 1.0; screen_width = width; screen_height = height; glViewport(0, 0, (GLint) width, (GLint) height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-2, 2, -2, 2, -2, 2); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } static void init() { float ambient[3] = { 1, 1, 1 }; glClearColor(.5, .5, .9, 1.0); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glLightfv(GL_LIGHT0, GL_DIFFUSE, ambient); glGenBuffers(1, &vert_buffer); glBindBuffer(GL_ARRAY_BUFFER, vert_buffer); glBufferData(GL_ARRAY_BUFFER, sizeof(points), points, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); glGenBuffers(1, &conn_buffer); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, conn_buffer); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(conn), conn, GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); draw(); glEnable(GL_COLOR_MATERIAL); glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE); } void visible(int vis) { glutIdleFunc(idle_visible); } int main(int argc, char *argv[]) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB|GLUT_DEPTH|GLUT_DOUBLE); glutInitWindowPosition(0, 0); glutInitWindowSize(200, 200); glutCreateWindow("Test VBO glMultiDrawElements"); glutDisplayFunc(drawfunc); glutReshapeFunc(reshape); glutVisibilityFunc(visible); printf("Vendor: %s\n", glGetString(GL_VENDOR)); printf("Renderer: %s\n", glGetString(GL_RENDERER)); printf("Version: %s\n", glGetString(GL_VERSION)); printf("Extensions: %s\n", glGetString(GL_EXTENSIONS)); printf("GLU Version: %s\n", gluGetString(GLU_VERSION)); printf("GLU Extensions: %s\n", gluGetString(GLU_EXTENSIONS)); fflush(stdout); init(); glutMainLoop(); return 0; }