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