blob: f86b1386b2a2b3cc371e25e8d6a24b2ee5b388ea [file] [log] [blame]
Brian Paul9d3e5db2000-09-08 21:45:21 +00001/* $Id: drawpix.c,v 1.4 2000/09/08 21:45:21 brianp Exp $ */
jtgafb833d1999-08-19 00:55:39 +00002
3/*
4 * glDrawPixels demo/test/benchmark
5 *
6 * Brian Paul September 25, 1997 This file is in the public domain.
7 */
8
9/*
10 * $Log: drawpix.c,v $
Brian Paul9d3e5db2000-09-08 21:45:21 +000011 * Revision 1.4 2000/09/08 21:45:21 brianp
12 * added dither key option
13 *
Brian Paul8de4a381999-10-28 18:23:29 +000014 * Revision 1.3 1999/10/28 18:23:29 brianp
15 * minor changes to Usage() function
16 *
Brian Pauld13c0a91999-10-21 22:13:58 +000017 * Revision 1.2 1999/10/21 22:13:58 brianp
18 * added f key to toggle front/back drawing
19 *
20 * Revision 1.1.1.1 1999/08/19 00:55:40 jtg
21 * Imported sources
jtgafb833d1999-08-19 00:55:39 +000022 *
23 * Revision 3.3 1999/03/28 18:18:33 brianp
24 * minor clean-up
25 *
26 * Revision 3.2 1998/11/05 04:34:04 brianp
27 * moved image files to ../images/ directory
28 *
29 * Revision 3.1 1998/02/22 16:43:17 brianp
30 * added a few casts to silence compiler warnings
31 *
32 * Revision 3.0 1998/02/14 18:42:29 brianp
33 * initial rev
34 *
35 */
36
37
38#include <stdio.h>
39#include <stdlib.h>
40#include <math.h>
41#include <GL/glut.h>
42
43#include "../util/readtex.c" /* a hack, I know */
44
45#define IMAGE_FILE "../images/girl.rgb"
46
47static int ImgWidth, ImgHeight;
48static GLenum ImgFormat;
49static GLubyte *Image = NULL;
50
51static int Xpos, Ypos;
52static int SkipPixels, SkipRows;
53static int DrawWidth, DrawHeight;
54static int Scissor = 0;
55static float Xzoom, Yzoom;
Brian Pauld13c0a91999-10-21 22:13:58 +000056static GLboolean DrawFront = GL_FALSE;
Brian Paul9d3e5db2000-09-08 21:45:21 +000057static GLboolean Dither = GL_TRUE;
jtgafb833d1999-08-19 00:55:39 +000058
59
60static void Reset( void )
61{
62 Xpos = Ypos = 20;
63 DrawWidth = ImgWidth;
64 DrawHeight = ImgHeight;
65 SkipPixels = SkipRows = 0;
66 Scissor = 0;
67 Xzoom = Yzoom = 1.0;
68}
69
70
71static void Display( void )
72{
73 glClear( GL_COLOR_BUFFER_BIT );
74
75#if 0
76 glRasterPos2i(Xpos, Ypos);
77#else
78 /* This allows negative raster positions: */
79 glRasterPos2i(0, 0);
80 glBitmap(0, 0, 0, 0, Xpos, Ypos, NULL);
81#endif
82
83 glPixelStorei(GL_UNPACK_SKIP_PIXELS, SkipPixels);
84 glPixelStorei(GL_UNPACK_SKIP_ROWS, SkipRows);
85
86 glPixelZoom( Xzoom, Yzoom );
87
88 if (Scissor)
89 glEnable(GL_SCISSOR_TEST);
90
91 glDrawPixels(DrawWidth, DrawHeight, ImgFormat, GL_UNSIGNED_BYTE, Image);
92
93 glDisable(GL_SCISSOR_TEST);
94
Brian Pauld13c0a91999-10-21 22:13:58 +000095 if (!DrawFront)
96 glutSwapBuffers();
jtgafb833d1999-08-19 00:55:39 +000097}
98
99
100static void Benchmark( void )
101{
102 int startTime, endTime;
103 int draws = 500;
104 double seconds, pixelsPerSecond;
105
106 printf("Benchmarking...\n");
107 /* GL set-up */
108 glPixelStorei(GL_UNPACK_SKIP_PIXELS, SkipPixels);
109 glPixelStorei(GL_UNPACK_SKIP_ROWS, SkipRows);
110 glPixelZoom( Xzoom, Yzoom );
111 if (Scissor)
112 glEnable(GL_SCISSOR_TEST);
113
Brian Pauld13c0a91999-10-21 22:13:58 +0000114 if (DrawFront)
115 glDrawBuffer(GL_FRONT);
116 else
117 glDrawBuffer(GL_BACK);
118
jtgafb833d1999-08-19 00:55:39 +0000119 /* Run timing test */
120 draws = 0;
121 startTime = glutGet(GLUT_ELAPSED_TIME);
122 do {
123 glDrawPixels(DrawWidth, DrawHeight, ImgFormat, GL_UNSIGNED_BYTE, Image);
124 draws++;
125 endTime = glutGet(GLUT_ELAPSED_TIME);
126 } while (endTime - startTime < 4000); /* 4 seconds */
127
128 /* GL clean-up */
129 glDisable(GL_SCISSOR_TEST);
130
131 /* Results */
132 seconds = (double) (endTime - startTime) / 1000.0;
133 pixelsPerSecond = draws * DrawWidth * DrawHeight / seconds;
134 printf("Result: %d draws in %f seconds = %f pixels/sec\n",
135 draws, seconds, pixelsPerSecond);
136}
137
138
139static void Reshape( int width, int height )
140{
141 glViewport( 0, 0, width, height );
142 glMatrixMode( GL_PROJECTION );
143 glLoadIdentity();
144 glOrtho( 0.0, width, 0.0, height, -1.0, 1.0 );
145 glMatrixMode( GL_MODELVIEW );
146 glLoadIdentity();
147
148 glScissor(width/4, height/4, width/2, height/2);
149}
150
151
152static void Key( unsigned char key, int x, int y )
153{
154 (void) x;
155 (void) y;
156 switch (key) {
157 case ' ':
158 Reset();
159 break;
Brian Paul9d3e5db2000-09-08 21:45:21 +0000160 case 'd':
161 Dither = !Dither;
162 if (Dither)
163 glEnable(GL_DITHER);
164 else
165 glDisable(GL_DITHER);
166 break;
jtgafb833d1999-08-19 00:55:39 +0000167 case 'w':
168 if (DrawWidth > 0)
169 DrawWidth--;
170 break;
171 case 'W':
172 DrawWidth++;
173 break;
174 case 'h':
175 if (DrawHeight > 0)
176 DrawHeight--;
177 break;
178 case 'H':
179 DrawHeight++;
180 break;
181 case 'p':
182 if (SkipPixels > 0)
183 SkipPixels--;
184 break;
185 case 'P':
186 SkipPixels++;
187 break;
188 case 'r':
189 if (SkipRows > 0)
190 SkipRows--;
191 break;
192 case 'R':
193 SkipRows++;
194 break;
195 case 's':
196 Scissor = !Scissor;
197 break;
198 case 'x':
199 Xzoom -= 0.1;
200 break;
201 case 'X':
202 Xzoom += 0.1;
203 break;
204 case 'y':
205 Yzoom -= 0.1;
206 break;
207 case 'Y':
208 Yzoom += 0.1;
209 break;
210 case 'b':
211 Benchmark();
212 break;
Brian Pauld13c0a91999-10-21 22:13:58 +0000213 case 'f':
214 DrawFront = !DrawFront;
215 if (DrawFront)
216 glDrawBuffer(GL_FRONT);
217 else
218 glDrawBuffer(GL_BACK);
219 printf("glDrawBuffer(%s)\n", DrawFront ? "GL_FRONT" : "GL_BACK");
220 break;
jtgafb833d1999-08-19 00:55:39 +0000221 case 27:
222 exit(0);
223 break;
224 }
225 glutPostRedisplay();
226}
227
228
229static void SpecialKey( int key, int x, int y )
230{
231 (void) x;
232 (void) y;
233 switch (key) {
234 case GLUT_KEY_UP:
235 Ypos += 1;
236 break;
237 case GLUT_KEY_DOWN:
238 Ypos -= 1;
239 break;
240 case GLUT_KEY_LEFT:
241 Xpos -= 1;
242 break;
243 case GLUT_KEY_RIGHT:
244 Xpos += 1;
245 break;
246 }
247 glutPostRedisplay();
248}
249
250
251static void Init( GLboolean ciMode )
252{
253 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
254 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
255
256 Image = LoadRGBImage( IMAGE_FILE, &ImgWidth, &ImgHeight, &ImgFormat );
257 if (!Image) {
258 printf("Couldn't read %s\n", IMAGE_FILE);
259 exit(0);
260 }
261
262 if (ciMode) {
263 /* Convert RGB image to grayscale */
264 GLubyte *indexImage = malloc( ImgWidth * ImgHeight );
265 GLint i;
266 for (i=0; i<ImgWidth*ImgHeight; i++) {
267 int gray = Image[i*3] + Image[i*3+1] + Image[i*3+2];
268 indexImage[i] = gray / 3;
269 }
270 free(Image);
271 Image = indexImage;
272 ImgFormat = GL_COLOR_INDEX;
273
274 for (i=0;i<255;i++) {
275 float g = i / 255.0;
276 glutSetColor(i, g, g, g);
277 }
278 }
279
280 printf("Loaded %d by %d image\n", ImgWidth, ImgHeight );
281
282 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
283 glPixelStorei(GL_UNPACK_ROW_LENGTH, ImgWidth);
284
285 Reset();
286}
287
288
289static void Usage(void)
290{
291 printf("Keys:\n");
Brian Paul8de4a381999-10-28 18:23:29 +0000292 printf(" SPACE Reset Parameters\n");
jtgafb833d1999-08-19 00:55:39 +0000293 printf(" Up/Down Move image up/down\n");
294 printf(" Left/Right Move image left/right\n");
Brian Paul8de4a381999-10-28 18:23:29 +0000295 printf(" x Decrease X-axis PixelZoom\n");
296 printf(" X Increase X-axis PixelZoom\n");
297 printf(" y Decrease Y-axis PixelZoom\n");
298 printf(" Y Increase Y-axis PixelZoom\n");
299 printf(" w Decrease glDrawPixels width*\n");
300 printf(" W Increase glDrawPixels width*\n");
301 printf(" h Decrease glDrawPixels height*\n");
302 printf(" H Increase glDrawPixels height*\n");
303 printf(" p Decrease GL_UNPACK_SKIP_PIXELS*\n");
304 printf(" P Increase GL_UNPACK_SKIP_PIXELS*\n");
305 printf(" r Decrease GL_UNPACK_SKIP_ROWS*\n");
306 printf(" R Increase GL_UNPACK_SKIP_ROWS*\n");
jtgafb833d1999-08-19 00:55:39 +0000307 printf(" s Toggle GL_SCISSOR_TEST\n");
Brian Pauld13c0a91999-10-21 22:13:58 +0000308 printf(" f Toggle front/back buffer drawing\n");
jtgafb833d1999-08-19 00:55:39 +0000309 printf(" b Benchmark test\n");
310 printf(" ESC Exit\n");
Brian Paul8de4a381999-10-28 18:23:29 +0000311 printf("* Warning: no limits are imposed on these parameters so it's\n");
312 printf(" possible to cause a segfault if you go too far.\n");
jtgafb833d1999-08-19 00:55:39 +0000313}
314
315
316int main( int argc, char *argv[] )
317{
318 GLboolean ciMode = GL_FALSE;
319
320 if (argc > 1 && strcmp(argv[1], "-ci")==0) {
321 ciMode = GL_TRUE;
322 }
323
324 glutInit( &argc, argv );
325 glutInitWindowPosition( 0, 0 );
326 glutInitWindowSize( 500, 400 );
327
328 if (ciMode)
329 glutInitDisplayMode( GLUT_INDEX | GLUT_DOUBLE );
330 else
Brian Paul9d3e5db2000-09-08 21:45:21 +0000331 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE);
jtgafb833d1999-08-19 00:55:39 +0000332
333 glutCreateWindow(argv[0]);
334
335 Init(ciMode);
336 Usage();
337
338 glutReshapeFunc( Reshape );
339 glutKeyboardFunc( Key );
340 glutSpecialFunc( SpecialKey );
341 glutDisplayFunc( Display );
342
343 glutMainLoop();
344 return 0;
345}