WindML updates (Stephane Raimbault)
[mesa.git] / progs / windml / ugldrawpix.c
1 /*
2 * glDrawPixels demo/test/benchmark
3 *
4 * Brian Paul September 25, 1997 This file is in the public domain.
5 *
6 * Conversion to UGL/Mesa by Stephane Raimbault july, 2001
7 */
8
9 /*
10 * $Log: ugldrawpix.c,v $
11 * Revision 1.2 2001/09/10 19:21:13 brianp
12 * WindML updates (Stephane Raimbault)
13 *
14 * Revision 1.1 2001/08/20 16:07:11 brianp
15 * WindML driver (Stephane Raimbault)
16 *
17 * Revision 1.5 2000/12/24 22:53:54 pesco
18 * * demos/Makefile.am (INCLUDES): Added -I$(top_srcdir)/util.
19 * * demos/Makefile.X11, demos/Makefile.BeOS-R4, demos/Makefile.cygnus:
20 * Essentially the same.
21 * Program files updated to include "readtex.c", not "../util/readtex.c".
22 * * demos/reflect.c: Likewise for "showbuffer.c".
23 *
24 *
25 * * Makefile.am (EXTRA_DIST): Added top-level regular files.
26 *
27 * * include/GL/Makefile.am (INC_X11): Added glxext.h.
28 *
29 *
30 * * src/GGI/include/ggi/mesa/Makefile.am (EXTRA_HEADERS): Include
31 * Mesa GGI headers in dist even if HAVE_GGI is not given.
32 *
33 * * configure.in: Look for GLUT and demo source dirs in $srcdir.
34 *
35 * * src/swrast/Makefile.am (libMesaSwrast_la_SOURCES): Set to *.[ch].
36 * More source list updates in various Makefile.am's.
37 *
38 * * Makefile.am (dist-hook): Remove CVS directory from distribution.
39 * (DIST_SUBDIRS): List all possible subdirs here.
40 * (SUBDIRS): Only list subdirs selected for build again.
41 * The above two applied to all subdir Makefile.am's also.
42 *
43 * Revision 1.4 2000/09/08 21:45:21 brianp
44 * added dither key option
45 *
46 * Revision 1.3 1999/10/28 18:23:29 brianp
47 * minor changes to Usage() function
48 *
49 * Revision 1.2 1999/10/21 22:13:58 brianp
50 * added f key to toggle front/back drawing
51 *
52 * Revision 1.1.1.1 1999/08/19 00:55:40 jtg
53 * Imported sources
54 *
55 * Revision 3.3 1999/03/28 18:18:33 brianp
56 * minor clean-up
57 *
58 * Revision 3.2 1998/11/05 04:34:04 brianp
59 * moved image files to ../images/ directory
60 *
61 * Revision 3.1 1998/02/22 16:43:17 brianp
62 * added a few casts to silence compiler warnings
63 *
64 * Revision 3.0 1998/02/14 18:42:29 brianp
65 * initial rev
66 *
67 */
68
69
70 #include <stdio.h>
71 #include <stdlib.h>
72 #include <math.h>
73 #include <tickLib.h>
74
75 #include <ugl/ugl.h>
76 #include <ugl/uglucode.h>
77 #include <ugl/uglevent.h>
78 #include <ugl/uglinput.h>
79
80 #include <GL/uglmesa.h>
81 #include <GL/glu.h>
82
83 #include "../util/readtex.h"
84
85 #define IMAGE_FILE "Mesa/images/wrs_logo.rgb"
86
87 UGL_LOCAL UGL_EVENT_SERVICE_ID eventServiceId;
88 UGL_LOCAL UGL_EVENT_Q_ID qId;
89 UGL_LOCAL volatile UGL_BOOL stopWex;
90 UGL_LOCAL UGL_MESA_CONTEXT umc;
91
92 UGL_LOCAL int ImgWidth, ImgHeight;
93 UGL_LOCAL GLenum ImgFormat;
94 UGL_LOCAL GLubyte *Image;
95
96 UGL_LOCAL int Xpos, Ypos;
97 UGL_LOCAL int SkipPixels, SkipRows;
98 UGL_LOCAL int DrawWidth, DrawHeight;
99 UGL_LOCAL float Xzoom, Yzoom;
100 UGL_LOCAL GLboolean Scissor;
101 UGL_LOCAL GLboolean DrawFront;
102 UGL_LOCAL GLboolean Dither;
103
104 UGL_LOCAL void cleanUp (void);
105
106 UGL_LOCAL void reset(void)
107 {
108 Xpos = Ypos = 20;
109 DrawWidth = ImgWidth;
110 DrawHeight = ImgHeight;
111 SkipPixels = SkipRows = 0;
112 Scissor = GL_FALSE;
113 Xzoom = Yzoom = 1.0;
114 }
115
116 UGL_LOCAL void initGL(GLboolean ciMode, GLsizei width, GLsizei height)
117 {
118 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
119 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
120 printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
121
122 Image = LoadRGBImage(IMAGE_FILE, &ImgWidth, &ImgHeight, &ImgFormat);
123 if (!Image)
124 {
125 printf("Couldn't read %s\n", IMAGE_FILE);
126 cleanUp();
127 exit(1);
128 }
129
130 glScissor(width/4, height/4, width/2, height/2);
131
132 if (ciMode)
133 {
134 /* Convert RGB image to grayscale */
135 GLubyte *indexImage = malloc( ImgWidth * ImgHeight );
136 GLint i;
137 for (i=0; i<ImgWidth*ImgHeight; i++)
138 {
139 int gray = Image[i*3] + Image[i*3+1] + Image[i*3+2];
140 indexImage[i] = gray / 3;
141 }
142 free(Image);
143 Image = indexImage;
144 ImgFormat = GL_COLOR_INDEX;
145
146 for (i=0;i<255;i++)
147 {
148 float g = i / 255.0;
149 uglMesaSetColor(i, g, g, g);
150 }
151 }
152
153 printf("Loaded %d by %d image\n", ImgWidth, ImgHeight );
154
155 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
156 glPixelStorei(GL_UNPACK_ROW_LENGTH, ImgWidth);
157
158 reset();
159
160 glViewport( 0, 0, width, height );
161 glMatrixMode( GL_PROJECTION );
162 glLoadIdentity();
163 glOrtho( 0.0, width, 0.0, height, -1.0, 1.0 );
164 glMatrixMode( GL_MODELVIEW );
165 glLoadIdentity();
166 }
167
168 UGL_LOCAL void drawGL(void)
169 {
170 glClear(GL_COLOR_BUFFER_BIT);
171
172 /* This allows negative raster positions: */
173 glRasterPos2i(0, 0);
174 glBitmap(0, 0, 0, 0, Xpos, Ypos, NULL);
175
176 glPixelStorei(GL_UNPACK_SKIP_PIXELS, SkipPixels);
177 glPixelStorei(GL_UNPACK_SKIP_ROWS, SkipRows);
178
179 glPixelZoom( Xzoom, Yzoom );
180
181 if (Scissor)
182 glEnable(GL_SCISSOR_TEST);
183
184 glDrawPixels(DrawWidth, DrawHeight, ImgFormat, GL_UNSIGNED_BYTE, Image);
185
186 glDisable(GL_SCISSOR_TEST);
187
188 uglMesaSwapBuffers();
189 }
190
191
192 UGL_LOCAL void benchmark( void )
193 {
194 int startTick, endTick, ticksBySec;
195 int draws;
196 double seconds, pixelsPerSecond;
197
198 printf("Benchmarking (4 sec)...\n");
199
200 /* GL set-up */
201 glPixelStorei(GL_UNPACK_SKIP_PIXELS, SkipPixels);
202 glPixelStorei(GL_UNPACK_SKIP_ROWS, SkipRows);
203 glPixelZoom( Xzoom, Yzoom );
204 if (Scissor)
205 glEnable(GL_SCISSOR_TEST);
206
207 if (DrawFront)
208 glDrawBuffer(GL_FRONT);
209 else
210 glDrawBuffer(GL_BACK);
211
212 /* Run timing test */
213 draws = 0;
214
215 ticksBySec = sysClkRateGet ();
216 startTick = tickGet();
217
218 do {
219 glDrawPixels(DrawWidth, DrawHeight, ImgFormat, GL_UNSIGNED_BYTE, Image);
220 draws++;
221 endTick = tickGet ();
222 } while ((endTick - startTick)/ticksBySec < 4); /* 4 seconds */
223
224 /* GL clean-up */
225 glDisable(GL_SCISSOR_TEST);
226
227 /* Results */
228 seconds = (endTick - startTick)/ticksBySec;
229 pixelsPerSecond = draws * DrawWidth * DrawHeight / seconds;
230 printf("Result: %d draws in %f seconds = %f pixels/sec\n",
231 draws, seconds, pixelsPerSecond);
232 }
233
234 UGL_LOCAL void echoUse(void)
235 {
236 printf("Keys:\n");
237 printf(" SPACE Reset Parameters\n");
238 printf(" Up/Down Move image up/down\n");
239 printf(" Left/Right Move image left/right\n");
240 printf(" x Decrease X-axis PixelZoom\n");
241 printf(" X Increase X-axis PixelZoom\n");
242 printf(" y Decrease Y-axis PixelZoom\n");
243 printf(" Y Increase Y-axis PixelZoom\n");
244 printf(" w Decrease glDrawPixels width*\n");
245 printf(" W Increase glDrawPixels width*\n");
246 printf(" h Decrease glDrawPixels height*\n");
247 printf(" H Increase glDrawPixels height*\n");
248 printf(" p Decrease GL_UNPACK_SKIP_PIXELS*\n");
249 printf(" P Increase GL_UNPACK_SKIP_PIXELS*\n");
250 printf(" r Decrease GL_UNPACK_SKIP_ROWS*\n");
251 printf(" R Increase GL_UNPACK_SKIP_ROWS*\n");
252 printf(" s Toggle GL_SCISSOR_TEST\n");
253 printf(" f Toggle front/back buffer drawing\n");
254 printf(" d Toggle dithering\n");
255 printf(" b Benchmark test\n");
256 printf(" ESC Exit\n");
257 printf("* Warning: no limits are imposed on these parameters so it's\n");
258 printf(" possible to cause a segfault if you go too far.\n");
259 }
260
261
262 UGL_LOCAL void readKey(UGL_WCHAR key)
263 {
264 switch (key)
265 {
266 case UGL_UNI_SPACE:
267 reset();
268 break;
269 case 'd':
270 Dither = !Dither;
271 if (Dither)
272 glEnable(GL_DITHER);
273 else
274 glDisable(GL_DITHER);
275 break;
276 case 'w':
277 if (DrawWidth > 0)
278 DrawWidth--;
279 break;
280 case 'W':
281 DrawWidth++;
282 break;
283 case 'h':
284 if (DrawHeight > 0)
285 DrawHeight--;
286 break;
287 case 'H':
288 DrawHeight++;
289 break;
290 case 'p':
291 if (SkipPixels > 0)
292 SkipPixels--;
293 break;
294 case 'P':
295 SkipPixels++;
296 break;
297 case 'r':
298 if (SkipRows > 0)
299 SkipRows--;
300 break;
301 case 'R':
302 SkipRows++;
303 break;
304 case 's':
305 Scissor = !Scissor;
306 break;
307 case 'x':
308 Xzoom -= 0.1;
309 break;
310 case 'X':
311 Xzoom += 0.1;
312 break;
313 case 'y':
314 Yzoom -= 0.1;
315 break;
316 case 'Y':
317 Yzoom += 0.1;
318 break;
319 case 'b':
320 benchmark();
321 break;
322 case 'f':
323 DrawFront = !DrawFront;
324 if (DrawFront)
325 glDrawBuffer(GL_FRONT);
326 else
327 glDrawBuffer(GL_BACK);
328 printf("glDrawBuffer(%s)\n", DrawFront ? "GL_FRONT" : "GL_BACK");
329 break;
330 case UGL_UNI_UP_ARROW:
331 Ypos += 1;
332 break;
333 case UGL_UNI_DOWN_ARROW:
334 Ypos -= 1;
335 break;
336 case UGL_UNI_LEFT_ARROW:
337 Xpos -= 1;
338 break;
339 case UGL_UNI_RIGHT_ARROW:
340 Xpos += 1;
341 break;
342 case UGL_UNI_ESCAPE:
343 stopWex = UGL_TRUE;
344 break;
345 }
346 }
347
348 UGL_LOCAL void loopEvent(void)
349 {
350 UGL_EVENT event;
351 UGL_INPUT_EVENT * pInputEvent;
352
353 UGL_FOREVER
354 {
355 if (uglEventGet (qId, &event, sizeof (event), UGL_NO_WAIT)
356 != UGL_STATUS_Q_EMPTY)
357 {
358 pInputEvent = (UGL_INPUT_EVENT *)&event;
359
360 if (pInputEvent->header.type == UGL_EVENT_TYPE_KEYBOARD &&
361 pInputEvent->modifiers & UGL_KEYBOARD_KEYDOWN)
362 readKey(pInputEvent->type.keyboard.key);
363 }
364
365 drawGL();
366 if (stopWex)
367 break;
368 }
369 }
370
371 UGL_LOCAL void cleanUp (void)
372 {
373 uglEventQDestroy (eventServiceId, qId);
374
375 uglMesaDestroyContext();
376 uglDeinitialize ();
377 }
378
379 void windMLDrawPix (UGL_BOOL windMLMode);
380
381 void ugldrawpix (void)
382 {
383 taskSpawn ("tDrawPix", 210, VX_FP_TASK, 100000, (FUNCPTR)windMLDrawPix,
384 UGL_FALSE,1,2,3,4,5,6,7,8,9);
385 }
386
387 void windMLDrawPix (UGL_BOOL windMLMode)
388 {
389 UGL_INPUT_DEVICE_ID keyboardDevId;
390 GLuint ciMode;
391 GLsizei width, height;
392
393 Image = NULL;
394 Scissor = GL_FALSE;
395 DrawFront = GL_FALSE;
396 Dither = GL_TRUE;
397
398 uglInitialize ();
399
400 uglDriverFind (UGL_KEYBOARD_TYPE, 0,
401 (UGL_UINT32 *)&keyboardDevId);
402
403 uglDriverFind (UGL_EVENT_SERVICE_TYPE, 0, (UGL_UINT32 *)&eventServiceId);
404
405 qId = uglEventQCreate (eventServiceId, 100);
406
407 /* Double buffering */
408 if (windMLMode)
409 umc = uglMesaCreateNewContext(UGL_MESA_DOUBLE
410 | UGL_MESA_WINDML_EXCLUSIVE, NULL);
411 else
412 umc = uglMesaCreateNewContext(UGL_MESA_DOUBLE, NULL);
413
414 if (umc == NULL)
415 {
416 uglDeinitialize ();
417 return;
418 }
419
420 uglMesaMakeCurrentContext(umc, 0, 0, UGL_MESA_FULLSCREEN_WIDTH,
421 UGL_MESA_FULLSCREEN_HEIGHT);
422
423 uglMesaGetIntegerv(UGL_MESA_COLOR_INDEXED, &ciMode);
424 uglMesaGetIntegerv(UGL_MESA_WIDTH, &width);
425 uglMesaGetIntegerv(UGL_MESA_HEIGHT, &height);
426
427 initGL(ciMode, width, height);
428
429 echoUse();
430
431 stopWex = UGL_FALSE;
432 loopEvent();
433
434 cleanUp();
435 free(Image);
436
437 return;
438 }