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.1KB

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