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

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