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 | |
Brian Paul | 05bce08 | 2009-09-21 11:58:03 -0600 | [diff] [blame] | 29 | #include <stdio.h> |
Brian Paul | 9abbeda | 2009-09-16 19:33:01 -0600 | [diff] [blame] | 30 | #include "glmain.h" |
| 31 | #include <GL/glut.h> |
| 32 | |
| 33 | |
| 34 | static int Win; |
| 35 | static GLfloat Xrot = 0, Yrot = 0, Zrot = 0; |
Brian Paul | 9abbeda | 2009-09-16 19:33:01 -0600 | [diff] [blame] | 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 | |
Brian Paul | 05bce08 | 2009-09-21 11:58:03 -0600 | [diff] [blame] | 53 | /** make simple checkerboard texture object */ |
| 54 | GLuint |
| 55 | PerfCheckerTexture(GLsizei width, GLsizei height) |
| 56 | { |
| 57 | const GLenum filter = GL_NEAREST; |
| 58 | GLubyte *img = (GLubyte *) malloc(width * height * 4); |
| 59 | GLint i, j, k; |
| 60 | GLuint obj; |
| 61 | |
| 62 | k = 0; |
| 63 | for (i = 0; i < height; i++) { |
| 64 | for (j = 0; j < width; j++) { |
| 65 | GLubyte color; |
| 66 | if (((i / 8) ^ (j / 8)) & 1) { |
| 67 | color = 0xff; |
| 68 | } |
| 69 | else { |
| 70 | color = 0x0; |
| 71 | } |
| 72 | img[k++] = color; |
| 73 | img[k++] = color; |
| 74 | img[k++] = color; |
| 75 | img[k++] = color; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | glGenTextures(1, &obj); |
| 80 | glBindTexture(GL_TEXTURE_2D, obj); |
| 81 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter); |
| 82 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter); |
| 83 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, |
| 84 | GL_RGBA, GL_UNSIGNED_BYTE, img); |
| 85 | free(img); |
| 86 | |
| 87 | return obj; |
| 88 | } |
| 89 | |
| 90 | |
| 91 | static GLuint |
| 92 | CompileShader(GLenum type, const char *shader) |
| 93 | { |
| 94 | GLuint sh; |
| 95 | GLint stat; |
| 96 | |
| 97 | sh = glCreateShader(type); |
| 98 | glShaderSource(sh, 1, (const GLchar **) &shader, NULL); |
| 99 | |
| 100 | glCompileShader(sh); |
| 101 | |
| 102 | glGetShaderiv(sh, GL_COMPILE_STATUS, &stat); |
| 103 | if (!stat) { |
| 104 | GLchar log[1000]; |
| 105 | GLsizei len; |
| 106 | glGetShaderInfoLog(sh, 1000, &len, log); |
| 107 | fprintf(stderr, "Error: problem compiling shader: %s\n", log); |
| 108 | exit(1); |
| 109 | } |
| 110 | |
| 111 | return sh; |
| 112 | } |
| 113 | |
| 114 | |
| 115 | /** Make shader program from given vert/frag shader text */ |
| 116 | GLuint |
| 117 | PerfShaderProgram(const char *vertShader, const char *fragShader) |
| 118 | { |
| 119 | GLuint prog; |
| 120 | GLint stat; |
| 121 | |
| 122 | { |
| 123 | const char *version = (const char *) glGetString(GL_VERSION); |
| 124 | if (version[0] != '2' || version[1] != '.') { |
| 125 | fprintf(stderr, "Error: GL version 2.x required\n"); |
| 126 | exit(1); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | prog = glCreateProgram(); |
| 131 | |
| 132 | if (vertShader) { |
| 133 | GLuint vs = CompileShader(GL_VERTEX_SHADER, vertShader); |
| 134 | glAttachShader(prog, vs); |
| 135 | } |
| 136 | if (fragShader) { |
| 137 | GLuint fs = CompileShader(GL_FRAGMENT_SHADER, fragShader); |
| 138 | glAttachShader(prog, fs); |
| 139 | } |
| 140 | |
| 141 | glLinkProgram(prog); |
| 142 | glGetProgramiv(prog, GL_LINK_STATUS, &stat); |
| 143 | if (!stat) { |
| 144 | GLchar log[1000]; |
| 145 | GLsizei len; |
| 146 | glGetProgramInfoLog(prog, 1000, &len, log); |
| 147 | fprintf(stderr, "Shader link error:\n%s\n", log); |
| 148 | exit(1); |
| 149 | } |
| 150 | |
| 151 | return prog; |
| 152 | } |
| 153 | |
| 154 | |
Keith Whitwell | a7b2659 | 2009-09-21 16:55:12 +0100 | [diff] [blame^] | 155 | int |
| 156 | PerfReshapeWindow( unsigned w, unsigned h ) |
| 157 | { |
| 158 | if (glutGet(GLUT_SCREEN_WIDTH) < w || |
| 159 | glutGet(GLUT_SCREEN_HEIGHT) < h) |
| 160 | return 0; |
| 161 | |
| 162 | glutReshapeWindow( w, h ); |
| 163 | glutPostRedisplay(); |
| 164 | return 1; |
| 165 | } |
| 166 | |
| 167 | |
Brian Paul | 9abbeda | 2009-09-16 19:33:01 -0600 | [diff] [blame] | 168 | static void |
| 169 | Idle(void) |
| 170 | { |
Keith Whitwell | a7b2659 | 2009-09-21 16:55:12 +0100 | [diff] [blame^] | 171 | PerfNextRound(); |
Brian Paul | 9abbeda | 2009-09-16 19:33:01 -0600 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | |
| 175 | static void |
| 176 | Draw(void) |
| 177 | { |
| 178 | PerfDraw(); |
| 179 | glutSwapBuffers(); |
| 180 | } |
| 181 | |
| 182 | |
| 183 | static void |
| 184 | Reshape(int width, int height) |
| 185 | { |
| 186 | WinWidth = width; |
| 187 | WinHeight = height; |
| 188 | glViewport(0, 0, width, height); |
| 189 | glMatrixMode(GL_PROJECTION); |
| 190 | glLoadIdentity(); |
| 191 | glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0); |
| 192 | glMatrixMode(GL_MODELVIEW); |
| 193 | glLoadIdentity(); |
| 194 | glTranslatef(0.0, 0.0, -15.0); |
| 195 | } |
| 196 | |
| 197 | |
| 198 | static void |
| 199 | Key(unsigned char key, int x, int y) |
| 200 | { |
| 201 | const GLfloat step = 3.0; |
| 202 | (void) x; |
| 203 | (void) y; |
| 204 | switch (key) { |
Brian Paul | 9abbeda | 2009-09-16 19:33:01 -0600 | [diff] [blame] | 205 | case 'z': |
| 206 | Zrot -= step; |
| 207 | break; |
| 208 | case 'Z': |
| 209 | Zrot += step; |
| 210 | break; |
| 211 | case 27: |
| 212 | glutDestroyWindow(Win); |
| 213 | exit(0); |
| 214 | break; |
| 215 | } |
| 216 | glutPostRedisplay(); |
| 217 | } |
| 218 | |
| 219 | |
| 220 | static void |
| 221 | SpecialKey(int key, int x, int y) |
| 222 | { |
| 223 | const GLfloat step = 3.0; |
| 224 | (void) x; |
| 225 | (void) y; |
| 226 | switch (key) { |
| 227 | case GLUT_KEY_UP: |
| 228 | Xrot -= step; |
| 229 | break; |
| 230 | case GLUT_KEY_DOWN: |
| 231 | Xrot += step; |
| 232 | break; |
| 233 | case GLUT_KEY_LEFT: |
| 234 | Yrot -= step; |
| 235 | break; |
| 236 | case GLUT_KEY_RIGHT: |
| 237 | Yrot += step; |
| 238 | break; |
| 239 | } |
| 240 | glutPostRedisplay(); |
| 241 | } |
| 242 | |
| 243 | |
| 244 | int |
| 245 | main(int argc, char *argv[]) |
| 246 | { |
| 247 | glutInit(&argc, argv); |
| 248 | glutInitWindowSize(WinWidth, WinHeight); |
| 249 | glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); |
| 250 | Win = glutCreateWindow(argv[0]); |
| 251 | glewInit(); |
| 252 | glutReshapeFunc(Reshape); |
| 253 | glutKeyboardFunc(Key); |
| 254 | glutSpecialFunc(SpecialKey); |
| 255 | glutDisplayFunc(Draw); |
Keith Whitwell | a7b2659 | 2009-09-21 16:55:12 +0100 | [diff] [blame^] | 256 | glutIdleFunc(Idle); |
Brian Paul | 9abbeda | 2009-09-16 19:33:01 -0600 | [diff] [blame] | 257 | PerfInit(); |
| 258 | glutMainLoop(); |
| 259 | return 0; |
| 260 | } |