/* $Id: rail.c,v 1.98 2008/04/06 22:59:38 pb Exp $ */ #include #include #define GL_GLEXT_PROTOTYPES #include #include #include #include #include #include #include #include static int screen_width, screen_height; GLint display_list; float v[] = { 20,0,-100, 0,20,-100, -20,0,-100, 0,-20,-100 }; unsigned short i[] = { 0, 1, 2, 1, 2, 3 }; GLsizei tsci[] = { 3, 3 }; unsigned short *tsfi[] = { i, i+3 }; unsigned int tsin = sizeof(tsci)/sizeof(*tsci); static void draw(void) { glEnableClientState(GL_VERTEX_ARRAY); glInterleavedArrays(GL_V3F, 0, v); glMultiDrawElements(GL_TRIANGLES, tsci, GL_UNSIGNED_SHORT, (void *)tsfi, tsin); } 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(); glTranslatef(0, -50, 0); // Render the same triangles from a display list, in green glColor3f(0, .5, 0); glCallList(display_list); 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(); glFrustum(-s, s, -h*s*1.5, h*s*.5, 1.5, 500); 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); display_list = glGenLists(1); glNewList(display_list, GL_COMPILE); draw(); glEndList(); 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 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; }