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

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