blob: 8b41b8ee829ed5002386e6887d96ba70e8995aaf [file] [log] [blame]
Brian Paul9abbeda2009-09-16 19:33:01 -06001/*
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 Paul05bce082009-09-21 11:58:03 -060029#include <stdio.h>
Brian Paul9abbeda2009-09-16 19:33:01 -060030#include "glmain.h"
31#include <GL/glut.h>
32
33
34static int Win;
35static GLfloat Xrot = 0, Yrot = 0, Zrot = 0;
Brian Paul9abbeda2009-09-16 19:33:01 -060036
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
Brian Paul05bce082009-09-21 11:58:03 -060053/** 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 {
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 Whitwella7b26592009-09-21 16:55:12 +0100155int
156PerfReshapeWindow( 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 Paul9abbeda2009-09-16 19:33:01 -0600168static void
169Idle(void)
170{
Keith Whitwella7b26592009-09-21 16:55:12 +0100171 PerfNextRound();
Brian Paul9abbeda2009-09-16 19:33:01 -0600172}
173
174
175static void
176Draw(void)
177{
178 PerfDraw();
179 glutSwapBuffers();
180}
181
182
183static void
184Reshape(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
198static void
199Key(unsigned char key, int x, int y)
200{
201 const GLfloat step = 3.0;
202 (void) x;
203 (void) y;
204 switch (key) {
Brian Paul9abbeda2009-09-16 19:33:01 -0600205 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
220static void
221SpecialKey(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
244int
245main(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 Whitwella7b26592009-09-21 16:55:12 +0100256 glutIdleFunc(Idle);
Brian Paul9abbeda2009-09-16 19:33:01 -0600257 PerfInit();
258 glutMainLoop();
259 return 0;
260}