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 6.7KB

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