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