2 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
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:
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
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.
23 * OpenGL/GLUT common code for perf programs.
35 static GLfloat Xrot
= 0, Yrot
= 0, Zrot
= 0;
38 /** Return time in seconds */
42 return glutGet(GLUT_ELAPSED_TIME
) * 0.001;
53 /** make simple checkerboard texture object */
55 PerfCheckerTexture(GLsizei width
, GLsizei height
)
57 const GLenum filter
= GL_NEAREST
;
58 GLubyte
*img
= (GLubyte
*) malloc(width
* height
* 4);
63 for (i
= 0; i
< height
; i
++) {
64 for (j
= 0; j
< width
; j
++) {
66 if (((i
/ 8) ^ (j
/ 8)) & 1) {
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
);
92 CompileShader(GLenum type
, const char *shader
)
97 sh
= glCreateShader(type
);
98 glShaderSource(sh
, 1, (const GLchar
**) &shader
, NULL
);
102 glGetShaderiv(sh
, GL_COMPILE_STATUS
, &stat
);
106 glGetShaderInfoLog(sh
, 1000, &len
, log
);
107 fprintf(stderr
, "Error: problem compiling shader: %s\n", log
);
115 /** Make shader program from given vert/frag shader text */
117 PerfShaderProgram(const char *vertShader
, const char *fragShader
)
123 const char *version
= (const char *) glGetString(GL_VERSION
);
124 if ((version
[0] != '2' &&
125 version
[0] != '3') || version
[1] != '.') {
126 fprintf(stderr
, "Error: GL version 2.x or better required\n");
131 prog
= glCreateProgram();
134 GLuint vs
= CompileShader(GL_VERTEX_SHADER
, vertShader
);
135 glAttachShader(prog
, vs
);
138 GLuint fs
= CompileShader(GL_FRAGMENT_SHADER
, fragShader
);
139 glAttachShader(prog
, fs
);
143 glGetProgramiv(prog
, GL_LINK_STATUS
, &stat
);
147 glGetProgramInfoLog(prog
, 1000, &len
, log
);
148 fprintf(stderr
, "Shader link error:\n%s\n", log
);
157 PerfReshapeWindow( unsigned w
, unsigned h
)
159 if (glutGet(GLUT_SCREEN_WIDTH
) < w
||
160 glutGet(GLUT_SCREEN_HEIGHT
) < h
)
163 glutReshapeWindow( w
, h
);
170 PerfExtensionSupported(const char *ext
)
172 return glutExtensionSupported(ext
);
192 Reshape(int width
, int height
)
196 glViewport(0, 0, width
, height
);
197 glMatrixMode(GL_PROJECTION
);
199 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
200 glMatrixMode(GL_MODELVIEW
);
202 glTranslatef(0.0, 0.0, -15.0);
207 Key(unsigned char key
, int x
, int y
)
209 const GLfloat step
= 3.0;
220 glutDestroyWindow(Win
);
229 SpecialKey(int key
, int x
, int y
)
231 const GLfloat step
= 3.0;
253 main(int argc
, char *argv
[])
255 glutInit(&argc
, argv
);
256 glutInitWindowSize(WinWidth
, WinHeight
);
257 glutInitDisplayMode(GLUT_RGB
| GLUT_DOUBLE
| GLUT_DEPTH
| GLUT_STENCIL
);
258 Win
= glutCreateWindow(argv
[0]);
260 glutReshapeFunc(Reshape
);
261 glutKeyboardFunc(Key
);
262 glutSpecialFunc(SpecialKey
);
263 glutDisplayFunc(Draw
);