Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

drawpix.c 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /* $Id: drawpix.c,v 1.5 2000/12/24 22:53:54 pesco Exp $ */
  2. /*
  3. * glDrawPixels demo/test/benchmark
  4. *
  5. * Brian Paul September 25, 1997 This file is in the public domain.
  6. */
  7. /*
  8. * $Log: drawpix.c,v $
  9. * Revision 1.5 2000/12/24 22:53:54 pesco
  10. * * demos/Makefile.am (INCLUDES): Added -I$(top_srcdir)/util.
  11. * * demos/Makefile.X11, demos/Makefile.BeOS-R4, demos/Makefile.cygnus:
  12. * Essentially the same.
  13. * Program files updated to include "readtex.c", not "../util/readtex.c".
  14. * * demos/reflect.c: Likewise for "showbuffer.c".
  15. *
  16. *
  17. * * Makefile.am (EXTRA_DIST): Added top-level regular files.
  18. *
  19. * * include/GL/Makefile.am (INC_X11): Added glxext.h.
  20. *
  21. *
  22. * * src/GGI/include/ggi/mesa/Makefile.am (EXTRA_HEADERS): Include
  23. * Mesa GGI headers in dist even if HAVE_GGI is not given.
  24. *
  25. * * configure.in: Look for GLUT and demo source dirs in $srcdir.
  26. *
  27. * * src/swrast/Makefile.am (libMesaSwrast_la_SOURCES): Set to *.[ch].
  28. * More source list updates in various Makefile.am's.
  29. *
  30. * * Makefile.am (dist-hook): Remove CVS directory from distribution.
  31. * (DIST_SUBDIRS): List all possible subdirs here.
  32. * (SUBDIRS): Only list subdirs selected for build again.
  33. * The above two applied to all subdir Makefile.am's also.
  34. *
  35. * Revision 1.4 2000/09/08 21:45:21 brianp
  36. * added dither key option
  37. *
  38. * Revision 1.3 1999/10/28 18:23:29 brianp
  39. * minor changes to Usage() function
  40. *
  41. * Revision 1.2 1999/10/21 22:13:58 brianp
  42. * added f key to toggle front/back drawing
  43. *
  44. * Revision 1.1.1.1 1999/08/19 00:55:40 jtg
  45. * Imported sources
  46. *
  47. * Revision 3.3 1999/03/28 18:18:33 brianp
  48. * minor clean-up
  49. *
  50. * Revision 3.2 1998/11/05 04:34:04 brianp
  51. * moved image files to ../images/ directory
  52. *
  53. * Revision 3.1 1998/02/22 16:43:17 brianp
  54. * added a few casts to silence compiler warnings
  55. *
  56. * Revision 3.0 1998/02/14 18:42:29 brianp
  57. * initial rev
  58. *
  59. */
  60. #include <stdio.h>
  61. #include <stdlib.h>
  62. #include <math.h>
  63. #include <GL/glut.h>
  64. #include "readtex.c"
  65. #define IMAGE_FILE "../images/girl.rgb"
  66. static int ImgWidth, ImgHeight;
  67. static GLenum ImgFormat;
  68. static GLubyte *Image = NULL;
  69. static int Xpos, Ypos;
  70. static int SkipPixels, SkipRows;
  71. static int DrawWidth, DrawHeight;
  72. static int Scissor = 0;
  73. static float Xzoom, Yzoom;
  74. static GLboolean DrawFront = GL_FALSE;
  75. static GLboolean Dither = GL_TRUE;
  76. static void Reset( void )
  77. {
  78. Xpos = Ypos = 20;
  79. DrawWidth = ImgWidth;
  80. DrawHeight = ImgHeight;
  81. SkipPixels = SkipRows = 0;
  82. Scissor = 0;
  83. Xzoom = Yzoom = 1.0;
  84. }
  85. static void Display( void )
  86. {
  87. glClear( GL_COLOR_BUFFER_BIT );
  88. #if 0
  89. glRasterPos2i(Xpos, Ypos);
  90. #else
  91. /* This allows negative raster positions: */
  92. glRasterPos2i(0, 0);
  93. glBitmap(0, 0, 0, 0, Xpos, Ypos, NULL);
  94. #endif
  95. glPixelStorei(GL_UNPACK_SKIP_PIXELS, SkipPixels);
  96. glPixelStorei(GL_UNPACK_SKIP_ROWS, SkipRows);
  97. glPixelZoom( Xzoom, Yzoom );
  98. if (Scissor)
  99. glEnable(GL_SCISSOR_TEST);
  100. glDrawPixels(DrawWidth, DrawHeight, ImgFormat, GL_UNSIGNED_BYTE, Image);
  101. glDisable(GL_SCISSOR_TEST);
  102. if (!DrawFront)
  103. glutSwapBuffers();
  104. }
  105. static void Benchmark( void )
  106. {
  107. int startTime, endTime;
  108. int draws = 500;
  109. double seconds, pixelsPerSecond;
  110. printf("Benchmarking...\n");
  111. /* GL set-up */
  112. glPixelStorei(GL_UNPACK_SKIP_PIXELS, SkipPixels);
  113. glPixelStorei(GL_UNPACK_SKIP_ROWS, SkipRows);
  114. glPixelZoom( Xzoom, Yzoom );
  115. if (Scissor)
  116. glEnable(GL_SCISSOR_TEST);
  117. if (DrawFront)
  118. glDrawBuffer(GL_FRONT);
  119. else
  120. glDrawBuffer(GL_BACK);
  121. /* Run timing test */
  122. draws = 0;
  123. startTime = glutGet(GLUT_ELAPSED_TIME);
  124. do {
  125. glDrawPixels(DrawWidth, DrawHeight, ImgFormat, GL_UNSIGNED_BYTE, Image);
  126. draws++;
  127. endTime = glutGet(GLUT_ELAPSED_TIME);
  128. } while (endTime - startTime < 4000); /* 4 seconds */
  129. /* GL clean-up */
  130. glDisable(GL_SCISSOR_TEST);
  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. static void Reshape( int width, int height )
  138. {
  139. glViewport( 0, 0, width, height );
  140. glMatrixMode( GL_PROJECTION );
  141. glLoadIdentity();
  142. glOrtho( 0.0, width, 0.0, height, -1.0, 1.0 );
  143. glMatrixMode( GL_MODELVIEW );
  144. glLoadIdentity();
  145. glScissor(width/4, height/4, width/2, height/2);
  146. }
  147. static void Key( unsigned char key, int x, int y )
  148. {
  149. (void) x;
  150. (void) y;
  151. switch (key) {
  152. case ' ':
  153. Reset();
  154. break;
  155. case 'd':
  156. Dither = !Dither;
  157. if (Dither)
  158. glEnable(GL_DITHER);
  159. else
  160. glDisable(GL_DITHER);
  161. break;
  162. case 'w':
  163. if (DrawWidth > 0)
  164. DrawWidth--;
  165. break;
  166. case 'W':
  167. DrawWidth++;
  168. break;
  169. case 'h':
  170. if (DrawHeight > 0)
  171. DrawHeight--;
  172. break;
  173. case 'H':
  174. DrawHeight++;
  175. break;
  176. case 'p':
  177. if (SkipPixels > 0)
  178. SkipPixels--;
  179. break;
  180. case 'P':
  181. SkipPixels++;
  182. break;
  183. case 'r':
  184. if (SkipRows > 0)
  185. SkipRows--;
  186. break;
  187. case 'R':
  188. SkipRows++;
  189. break;
  190. case 's':
  191. Scissor = !Scissor;
  192. break;
  193. case 'x':
  194. Xzoom -= 0.1;
  195. break;
  196. case 'X':
  197. Xzoom += 0.1;
  198. break;
  199. case 'y':
  200. Yzoom -= 0.1;
  201. break;
  202. case 'Y':
  203. Yzoom += 0.1;
  204. break;
  205. case 'b':
  206. Benchmark();
  207. break;
  208. case 'f':
  209. DrawFront = !DrawFront;
  210. if (DrawFront)
  211. glDrawBuffer(GL_FRONT);
  212. else
  213. glDrawBuffer(GL_BACK);
  214. printf("glDrawBuffer(%s)\n", DrawFront ? "GL_FRONT" : "GL_BACK");
  215. break;
  216. case 27:
  217. exit(0);
  218. break;
  219. }
  220. glutPostRedisplay();
  221. }
  222. static void SpecialKey( int key, int x, int y )
  223. {
  224. (void) x;
  225. (void) y;
  226. switch (key) {
  227. case GLUT_KEY_UP:
  228. Ypos += 1;
  229. break;
  230. case GLUT_KEY_DOWN:
  231. Ypos -= 1;
  232. break;
  233. case GLUT_KEY_LEFT:
  234. Xpos -= 1;
  235. break;
  236. case GLUT_KEY_RIGHT:
  237. Xpos += 1;
  238. break;
  239. }
  240. glutPostRedisplay();
  241. }
  242. static void Init( GLboolean ciMode )
  243. {
  244. printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
  245. printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
  246. Image = LoadRGBImage( IMAGE_FILE, &ImgWidth, &ImgHeight, &ImgFormat );
  247. if (!Image) {
  248. printf("Couldn't read %s\n", IMAGE_FILE);
  249. exit(0);
  250. }
  251. if (ciMode) {
  252. /* Convert RGB image to grayscale */
  253. GLubyte *indexImage = malloc( ImgWidth * ImgHeight );
  254. GLint i;
  255. for (i=0; i<ImgWidth*ImgHeight; i++) {
  256. int gray = Image[i*3] + Image[i*3+1] + Image[i*3+2];
  257. indexImage[i] = gray / 3;
  258. }
  259. free(Image);
  260. Image = indexImage;
  261. ImgFormat = GL_COLOR_INDEX;
  262. for (i=0;i<255;i++) {
  263. float g = i / 255.0;
  264. glutSetColor(i, g, g, g);
  265. }
  266. }
  267. printf("Loaded %d by %d image\n", ImgWidth, ImgHeight );
  268. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  269. glPixelStorei(GL_UNPACK_ROW_LENGTH, ImgWidth);
  270. Reset();
  271. }
  272. static void Usage(void)
  273. {
  274. printf("Keys:\n");
  275. printf(" SPACE Reset Parameters\n");
  276. printf(" Up/Down Move image up/down\n");
  277. printf(" Left/Right Move image left/right\n");
  278. printf(" x Decrease X-axis PixelZoom\n");
  279. printf(" X Increase X-axis PixelZoom\n");
  280. printf(" y Decrease Y-axis PixelZoom\n");
  281. printf(" Y Increase Y-axis PixelZoom\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");
  291. printf(" f Toggle front/back buffer drawing\n");
  292. printf(" b Benchmark test\n");
  293. printf(" ESC Exit\n");
  294. printf("* Warning: no limits are imposed on these parameters so it's\n");
  295. printf(" possible to cause a segfault if you go too far.\n");
  296. }
  297. int main( int argc, char *argv[] )
  298. {
  299. GLboolean ciMode = GL_FALSE;
  300. if (argc > 1 && strcmp(argv[1], "-ci")==0) {
  301. ciMode = GL_TRUE;
  302. }
  303. glutInit( &argc, argv );
  304. glutInitWindowPosition( 0, 0 );
  305. glutInitWindowSize( 500, 400 );
  306. if (ciMode)
  307. glutInitDisplayMode( GLUT_INDEX | GLUT_DOUBLE );
  308. else
  309. glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE);
  310. glutCreateWindow(argv[0]);
  311. Init(ciMode);
  312. Usage();
  313. glutReshapeFunc( Reshape );
  314. glutKeyboardFunc( Key );
  315. glutSpecialFunc( SpecialKey );
  316. glutDisplayFunc( Display );
  317. glutMainLoop();
  318. return 0;
  319. }