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