Brian Paul | 9abbeda | 2009-09-16 19:33:01 -0600 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2009 VMware, Inc. All Rights Reserved. |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | * copy of this software and associated documentation files (the "Software"), |
| 6 | * to deal in the Software without restriction, including without limitation |
| 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | * and/or sell copies of the Software, and to permit persons to whom the |
| 9 | * Software is furnished to do so, subject to the following conditions: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice shall be included |
| 12 | * in all copies or substantial portions of the Software. |
| 13 | * |
| 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 15 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 17 | * VMWARE BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN |
| 18 | * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 19 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 20 | */ |
| 21 | |
| 22 | /** |
| 23 | * OpenGL/GLUT common code for perf programs. |
| 24 | * Brian Paul |
| 25 | * 15 Sep 2009 |
| 26 | */ |
| 27 | |
| 28 | |
| 29 | #include "glmain.h" |
| 30 | #include <GL/glut.h> |
| 31 | |
| 32 | |
| 33 | static int Win; |
| 34 | static GLfloat Xrot = 0, Yrot = 0, Zrot = 0; |
| 35 | static GLboolean Anim = GL_FALSE; |
| 36 | |
| 37 | |
| 38 | /** Return time in seconds */ |
| 39 | double |
| 40 | PerfGetTime(void) |
| 41 | { |
| 42 | return glutGet(GLUT_ELAPSED_TIME) * 0.001; |
| 43 | } |
| 44 | |
| 45 | |
| 46 | void |
| 47 | PerfSwapBuffers(void) |
| 48 | { |
| 49 | glutSwapBuffers(); |
| 50 | } |
| 51 | |
| 52 | |
| 53 | static void |
| 54 | Idle(void) |
| 55 | { |
| 56 | Xrot += 3.0; |
| 57 | Yrot += 4.0; |
| 58 | Zrot += 2.0; |
| 59 | glutPostRedisplay(); |
| 60 | } |
| 61 | |
| 62 | |
| 63 | static void |
| 64 | Draw(void) |
| 65 | { |
| 66 | PerfDraw(); |
| 67 | glutSwapBuffers(); |
| 68 | } |
| 69 | |
| 70 | |
| 71 | static void |
| 72 | Reshape(int width, int height) |
| 73 | { |
| 74 | WinWidth = width; |
| 75 | WinHeight = height; |
| 76 | glViewport(0, 0, width, height); |
| 77 | glMatrixMode(GL_PROJECTION); |
| 78 | glLoadIdentity(); |
| 79 | glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0); |
| 80 | glMatrixMode(GL_MODELVIEW); |
| 81 | glLoadIdentity(); |
| 82 | glTranslatef(0.0, 0.0, -15.0); |
| 83 | } |
| 84 | |
| 85 | |
| 86 | static void |
| 87 | Key(unsigned char key, int x, int y) |
| 88 | { |
| 89 | const GLfloat step = 3.0; |
| 90 | (void) x; |
| 91 | (void) y; |
| 92 | switch (key) { |
| 93 | case 'a': |
| 94 | Anim = !Anim; |
| 95 | if (Anim) |
| 96 | glutIdleFunc(Idle); |
| 97 | else |
| 98 | glutIdleFunc(NULL); |
| 99 | break; |
| 100 | case 'z': |
| 101 | Zrot -= step; |
| 102 | break; |
| 103 | case 'Z': |
| 104 | Zrot += step; |
| 105 | break; |
| 106 | case 27: |
| 107 | glutDestroyWindow(Win); |
| 108 | exit(0); |
| 109 | break; |
| 110 | } |
| 111 | glutPostRedisplay(); |
| 112 | } |
| 113 | |
| 114 | |
| 115 | static void |
| 116 | SpecialKey(int key, int x, int y) |
| 117 | { |
| 118 | const GLfloat step = 3.0; |
| 119 | (void) x; |
| 120 | (void) y; |
| 121 | switch (key) { |
| 122 | case GLUT_KEY_UP: |
| 123 | Xrot -= step; |
| 124 | break; |
| 125 | case GLUT_KEY_DOWN: |
| 126 | Xrot += step; |
| 127 | break; |
| 128 | case GLUT_KEY_LEFT: |
| 129 | Yrot -= step; |
| 130 | break; |
| 131 | case GLUT_KEY_RIGHT: |
| 132 | Yrot += step; |
| 133 | break; |
| 134 | } |
| 135 | glutPostRedisplay(); |
| 136 | } |
| 137 | |
| 138 | |
| 139 | int |
| 140 | main(int argc, char *argv[]) |
| 141 | { |
| 142 | glutInit(&argc, argv); |
| 143 | glutInitWindowSize(WinWidth, WinHeight); |
| 144 | glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); |
| 145 | Win = glutCreateWindow(argv[0]); |
| 146 | glewInit(); |
| 147 | glutReshapeFunc(Reshape); |
| 148 | glutKeyboardFunc(Key); |
| 149 | glutSpecialFunc(SpecialKey); |
| 150 | glutDisplayFunc(Draw); |
| 151 | if (Anim) |
| 152 | glutIdleFunc(Idle); |
| 153 | PerfInit(); |
| 154 | glutMainLoop(); |
| 155 | return 0; |
| 156 | } |