glmain.c revision 32001f49
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 <stdio.h> 30#include "glmain.h" 31#include "glut_wrap.h" 32 33 34static int Win; 35static GLfloat Xrot = 0, Yrot = 0, Zrot = 0; 36 37 38/** Return time in seconds */ 39double 40PerfGetTime(void) 41{ 42 return glutGet(GLUT_ELAPSED_TIME) * 0.001; 43} 44 45 46void 47PerfSwapBuffers(void) 48{ 49 glutSwapBuffers(); 50} 51 52 53/** make simple checkerboard texture object */ 54GLuint 55PerfCheckerTexture(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 91static GLuint 92CompileShader(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 */ 116GLuint 117PerfShaderProgram(const char *vertShader, const char *fragShader) 118{ 119 GLuint prog; 120 GLint stat; 121 122 if (!GLEW_VERSION_2_0) { 123 fprintf(stderr, "Error: GL version 2.x or better required\n"); 124 exit(1); 125 } 126 127 prog = glCreateProgram(); 128 129 if (vertShader) { 130 GLuint vs = CompileShader(GL_VERTEX_SHADER, vertShader); 131 glAttachShader(prog, vs); 132 } 133 if (fragShader) { 134 GLuint fs = CompileShader(GL_FRAGMENT_SHADER, fragShader); 135 glAttachShader(prog, fs); 136 } 137 138 glLinkProgram(prog); 139 glGetProgramiv(prog, GL_LINK_STATUS, &stat); 140 if (!stat) { 141 GLchar log[1000]; 142 GLsizei len; 143 glGetProgramInfoLog(prog, 1000, &len, log); 144 fprintf(stderr, "Shader link error:\n%s\n", log); 145 exit(1); 146 } 147 148 return prog; 149} 150 151 152int 153PerfReshapeWindow( unsigned w, unsigned h ) 154{ 155 if (glutGet(GLUT_SCREEN_WIDTH) < w || 156 glutGet(GLUT_SCREEN_HEIGHT) < h) 157 return 0; 158 159 glutReshapeWindow( w, h ); 160 glutPostRedisplay(); 161 return 1; 162} 163 164 165GLboolean 166PerfExtensionSupported(const char *ext) 167{ 168 return glutExtensionSupported(ext); 169} 170 171 172static void 173Idle(void) 174{ 175 PerfNextRound(); 176} 177 178 179static void 180Draw(void) 181{ 182 PerfDraw(); 183 glutSwapBuffers(); 184} 185 186 187static void 188Reshape(int width, int height) 189{ 190 WinWidth = width; 191 WinHeight = height; 192 glViewport(0, 0, width, height); 193 glMatrixMode(GL_PROJECTION); 194 glLoadIdentity(); 195 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0); 196 glMatrixMode(GL_MODELVIEW); 197 glLoadIdentity(); 198 glTranslatef(0.0, 0.0, -15.0); 199} 200 201 202static void 203Key(unsigned char key, int x, int y) 204{ 205 const GLfloat step = 3.0; 206 (void) x; 207 (void) y; 208 switch (key) { 209 case 'z': 210 Zrot -= step; 211 break; 212 case 'Z': 213 Zrot += step; 214 break; 215 case 27: 216 glutDestroyWindow(Win); 217 exit(0); 218 break; 219 } 220 glutPostRedisplay(); 221} 222 223 224static void 225SpecialKey(int key, int x, int y) 226{ 227 const GLfloat step = 3.0; 228 (void) x; 229 (void) y; 230 switch (key) { 231 case GLUT_KEY_UP: 232 Xrot -= step; 233 break; 234 case GLUT_KEY_DOWN: 235 Xrot += step; 236 break; 237 case GLUT_KEY_LEFT: 238 Yrot -= step; 239 break; 240 case GLUT_KEY_RIGHT: 241 Yrot += step; 242 break; 243 } 244 glutPostRedisplay(); 245} 246 247 248int 249main(int argc, char *argv[]) 250{ 251 glutInit(&argc, argv); 252 glutInitWindowSize(WinWidth, WinHeight); 253 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH | GLUT_STENCIL); 254 Win = glutCreateWindow(argv[0]); 255 glewInit(); 256 glutReshapeFunc(Reshape); 257 glutKeyboardFunc(Key); 258 glutSpecialFunc(SpecialKey); 259 glutDisplayFunc(Draw); 260 glutIdleFunc(Idle); 261 PerfInit(); 262 glutMainLoop(); 263 return 0; 264} 265