Clone of mesa.
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

drawpix.c 7.9KB

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