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