Merge branch 'master' into i915-unification
[mesa.git] / progs / demos / drawpix.c
1
2 /*
3 * glDrawPixels demo/test/benchmark
4 *
5 * Brian Paul September 25, 1997 This file is in the public domain.
6 */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <math.h>
11 #include <string.h>
12 #include <GL/glut.h>
13
14 #include "readtex.h"
15
16 #define IMAGE_FILE "../images/girl.rgb"
17
18 static int ImgWidth, ImgHeight;
19 static GLenum ImgFormat;
20 static GLubyte *Image = NULL;
21
22 static int Xpos, Ypos;
23 static int SkipPixels, SkipRows;
24 static int DrawWidth, DrawHeight;
25 static int Scissor = 0;
26 static int Fog = 0;
27 static GLfloat Zpos = -1.0;
28 static float Xzoom, Yzoom;
29 static GLboolean DrawFront = GL_FALSE;
30 static GLboolean Dither = GL_TRUE;
31
32
33 static void Reset( void )
34 {
35 Xpos = Ypos = 20;
36 DrawWidth = ImgWidth;
37 DrawHeight = ImgHeight;
38 SkipPixels = SkipRows = 0;
39 Scissor = 0;
40 Fog = 0;
41 Zpos = -1.0;
42 Xzoom = Yzoom = 1.0;
43 }
44
45
46 static void Display( void )
47 {
48 glClear( GL_COLOR_BUFFER_BIT );
49
50 #if 0
51 glRasterPos2i(Xpos, Ypos);
52 #else
53 /* This allows negative raster positions: */
54 glRasterPos3f(0, 0, Zpos);
55 glBitmap(0, 0, 0, 0, Xpos, Ypos, NULL);
56 #endif
57
58 glPixelStorei(GL_UNPACK_SKIP_PIXELS, SkipPixels);
59 glPixelStorei(GL_UNPACK_SKIP_ROWS, SkipRows);
60
61 glPixelZoom( Xzoom, Yzoom );
62
63 if (Scissor)
64 glEnable(GL_SCISSOR_TEST);
65
66 if (Fog)
67 glEnable(GL_FOG);
68
69 glDrawPixels(DrawWidth, DrawHeight, ImgFormat, GL_UNSIGNED_BYTE, Image);
70
71 glDisable(GL_SCISSOR_TEST);
72 glDisable(GL_FOG);
73
74 if (DrawFront)
75 glFinish();
76 else
77 glutSwapBuffers();
78 }
79
80
81 static void Benchmark( void )
82 {
83 int startTime, endTime;
84 int draws = 500;
85 double seconds, pixelsPerSecond;
86
87 printf("Benchmarking...\n");
88 /* GL set-up */
89 glPixelStorei(GL_UNPACK_SKIP_PIXELS, SkipPixels);
90 glPixelStorei(GL_UNPACK_SKIP_ROWS, SkipRows);
91 glPixelZoom( Xzoom, Yzoom );
92 if (Scissor)
93 glEnable(GL_SCISSOR_TEST);
94 if (Fog)
95 glEnable(GL_FOG);
96
97 if (DrawFront)
98 glDrawBuffer(GL_FRONT);
99 else
100 glDrawBuffer(GL_BACK);
101
102 /* Run timing test */
103 draws = 0;
104 startTime = glutGet(GLUT_ELAPSED_TIME);
105 do {
106 glDrawPixels(DrawWidth, DrawHeight, ImgFormat, GL_UNSIGNED_BYTE, Image);
107 draws++;
108 endTime = glutGet(GLUT_ELAPSED_TIME);
109 } while (endTime - startTime < 4000); /* 4 seconds */
110
111 /* GL clean-up */
112 glDisable(GL_SCISSOR_TEST);
113 glDisable(GL_FOG);
114
115 /* Results */
116 seconds = (double) (endTime - startTime) / 1000.0;
117 pixelsPerSecond = draws * DrawWidth * DrawHeight / seconds;
118 printf("Result: %d draws in %f seconds = %f pixels/sec\n",
119 draws, seconds, pixelsPerSecond);
120 }
121
122
123 static void Reshape( int width, int height )
124 {
125 glViewport( 0, 0, width, height );
126 glMatrixMode( GL_PROJECTION );
127 glLoadIdentity();
128 glOrtho( 0.0, width, 0.0, height, 0.0, 2.0 );
129 glMatrixMode( GL_MODELVIEW );
130 glLoadIdentity();
131
132 glScissor(width/4, height/4, width/2, height/2);
133 }
134
135
136 static void Key( unsigned char key, int x, int y )
137 {
138 (void) x;
139 (void) y;
140 switch (key) {
141 case ' ':
142 Reset();
143 break;
144 case 'd':
145 Dither = !Dither;
146 if (Dither)
147 glEnable(GL_DITHER);
148 else
149 glDisable(GL_DITHER);
150 break;
151 case 'w':
152 if (DrawWidth > 0)
153 DrawWidth--;
154 break;
155 case 'W':
156 DrawWidth++;
157 break;
158 case 'h':
159 if (DrawHeight > 0)
160 DrawHeight--;
161 break;
162 case 'H':
163 DrawHeight++;
164 break;
165 case 'p':
166 if (SkipPixels > 0)
167 SkipPixels--;
168 break;
169 case 'P':
170 SkipPixels++;
171 break;
172 case 'r':
173 if (SkipRows > 0)
174 SkipRows--;
175 break;
176 case 'R':
177 SkipRows++;
178 break;
179 case 's':
180 Scissor = !Scissor;
181 break;
182 case 'x':
183 Xzoom -= 0.1;
184 break;
185 case 'X':
186 Xzoom += 0.1;
187 break;
188 case 'y':
189 Yzoom -= 0.1;
190 break;
191 case 'Y':
192 Yzoom += 0.1;
193 break;
194 case 'z':
195 Zpos -= 0.1;
196 printf("RasterPos Z = %g\n", Zpos);
197 break;
198 case 'Z':
199 Zpos += 0.1;
200 printf("RasterPos Z = %g\n", Zpos);
201 break;
202 case 'b':
203 Benchmark();
204 break;
205 case 'F':
206 Fog = !Fog;
207 printf("Fog %d\n", Fog);
208 break;
209 case 'f':
210 DrawFront = !DrawFront;
211 if (DrawFront)
212 glDrawBuffer(GL_FRONT);
213 else
214 glDrawBuffer(GL_BACK);
215 printf("glDrawBuffer(%s)\n", DrawFront ? "GL_FRONT" : "GL_BACK");
216 break;
217 case 27:
218 exit(0);
219 break;
220 }
221 glutPostRedisplay();
222 }
223
224
225 static void SpecialKey( int key, int x, int y )
226 {
227 (void) x;
228 (void) y;
229 switch (key) {
230 case GLUT_KEY_UP:
231 Ypos += 1;
232 break;
233 case GLUT_KEY_DOWN:
234 Ypos -= 1;
235 break;
236 case GLUT_KEY_LEFT:
237 Xpos -= 1;
238 break;
239 case GLUT_KEY_RIGHT:
240 Xpos += 1;
241 break;
242 }
243 glutPostRedisplay();
244 }
245
246
247 static void Init( GLboolean ciMode, const char *filename )
248 {
249 static const GLfloat fogColor[4] = {0, 1, 0, 0};
250
251 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
252 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
253
254 Image = LoadRGBImage( filename, &ImgWidth, &ImgHeight, &ImgFormat );
255 if (!Image) {
256 printf("Couldn't read %s\n", filename);
257 exit(0);
258 }
259
260 if (ciMode) {
261 /* Convert RGB image to grayscale */
262 GLubyte *indexImage = (GLubyte *) malloc( ImgWidth * ImgHeight );
263 GLint i;
264 for (i=0; i<ImgWidth*ImgHeight; i++) {
265 int gray = Image[i*3] + Image[i*3+1] + Image[i*3+2];
266 indexImage[i] = gray / 3;
267 }
268 free(Image);
269 Image = indexImage;
270 ImgFormat = GL_COLOR_INDEX;
271
272 for (i=0;i<255;i++) {
273 float g = i / 255.0;
274 glutSetColor(i, g, g, g);
275 }
276 }
277
278 printf("Loaded %d by %d image\n", ImgWidth, ImgHeight );
279
280 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
281 glPixelStorei(GL_UNPACK_ROW_LENGTH, ImgWidth);
282
283 glFogi(GL_FOG_MODE, GL_LINEAR);
284 glFogf(GL_FOG_START, 0);
285 glFogf(GL_FOG_END, 2);
286 glFogfv(GL_FOG_COLOR, fogColor);
287
288 Reset();
289 }
290
291
292 static void Usage(void)
293 {
294 printf("Keys:\n");
295 printf(" SPACE Reset Parameters\n");
296 printf(" Up/Down Move image up/down\n");
297 printf(" Left/Right Move image left/right\n");
298 printf(" x Decrease X-axis PixelZoom\n");
299 printf(" X Increase X-axis PixelZoom\n");
300 printf(" y Decrease Y-axis PixelZoom\n");
301 printf(" Y Increase Y-axis PixelZoom\n");
302 printf(" w Decrease glDrawPixels width*\n");
303 printf(" W Increase glDrawPixels width*\n");
304 printf(" h Decrease glDrawPixels height*\n");
305 printf(" H Increase glDrawPixels height*\n");
306 printf(" p Decrease GL_UNPACK_SKIP_PIXELS*\n");
307 printf(" P Increase GL_UNPACK_SKIP_PIXELS*\n");
308 printf(" r Decrease GL_UNPACK_SKIP_ROWS*\n");
309 printf(" R Increase GL_UNPACK_SKIP_ROWS*\n");
310 printf(" s Toggle GL_SCISSOR_TEST\n");
311 printf(" F Toggle GL_FOG\n");
312 printf(" z Decrease RasterPos Z\n");
313 printf(" Z Increase RasterPos Z\n");
314
315 printf(" f Toggle front/back buffer drawing\n");
316 printf(" b Benchmark test\n");
317 printf(" ESC Exit\n");
318 printf("* Warning: no limits are imposed on these parameters so it's\n");
319 printf(" possible to cause a segfault if you go too far.\n");
320 }
321
322
323 int main( int argc, char *argv[] )
324 {
325 GLboolean ciMode = GL_FALSE;
326 const char *filename = IMAGE_FILE;
327 int i = 1;
328
329 if (argc > i && strcmp(argv[i], "-ci")==0) {
330 ciMode = GL_TRUE;
331 i++;
332 }
333 if (argc > i) {
334 filename = argv[i];
335 }
336
337 glutInit( &argc, argv );
338 glutInitWindowPosition( 0, 0 );
339 glutInitWindowSize( 500, 400 );
340
341 if (ciMode)
342 glutInitDisplayMode( GLUT_INDEX | GLUT_DOUBLE );
343 else
344 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE);
345
346 glutCreateWindow(argv[0]);
347
348 Init(ciMode, filename);
349 Usage();
350
351 glutReshapeFunc( Reshape );
352 glutKeyboardFunc( Key );
353 glutSpecialFunc( SpecialKey );
354 glutDisplayFunc( Display );
355
356 glutMainLoop();
357 return 0;
358 }