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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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 <string.h>
  10. #include <GL/glut.h>
  11. #include "readtex.h"
  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. glFinish();
  58. else
  59. glutSwapBuffers();
  60. }
  61. static void Benchmark( void )
  62. {
  63. int startTime, endTime;
  64. int draws = 500;
  65. double seconds, pixelsPerSecond;
  66. printf("Benchmarking...\n");
  67. /* GL set-up */
  68. glPixelStorei(GL_UNPACK_SKIP_PIXELS, SkipPixels);
  69. glPixelStorei(GL_UNPACK_SKIP_ROWS, SkipRows);
  70. glPixelZoom( Xzoom, Yzoom );
  71. if (Scissor)
  72. glEnable(GL_SCISSOR_TEST);
  73. if (Fog)
  74. glEnable(GL_FOG);
  75. if (DrawFront)
  76. glDrawBuffer(GL_FRONT);
  77. else
  78. glDrawBuffer(GL_BACK);
  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. glDisable(GL_FOG);
  90. /* Results */
  91. seconds = (double) (endTime - startTime) / 1000.0;
  92. pixelsPerSecond = draws * DrawWidth * DrawHeight / seconds;
  93. printf("Result: %d draws in %f seconds = %f pixels/sec\n",
  94. draws, seconds, pixelsPerSecond);
  95. }
  96. static void Reshape( int width, int height )
  97. {
  98. glViewport( 0, 0, width, height );
  99. glMatrixMode( GL_PROJECTION );
  100. glLoadIdentity();
  101. glOrtho( 0.0, width, 0.0, height, 0.0, 2.0 );
  102. glMatrixMode( GL_MODELVIEW );
  103. glLoadIdentity();
  104. glScissor(width/4, height/4, width/2, height/2);
  105. }
  106. static void Key( unsigned char key, int x, int y )
  107. {
  108. (void) x;
  109. (void) y;
  110. switch (key) {
  111. case ' ':
  112. Reset();
  113. break;
  114. case 'd':
  115. Dither = !Dither;
  116. if (Dither)
  117. glEnable(GL_DITHER);
  118. else
  119. glDisable(GL_DITHER);
  120. break;
  121. case 'w':
  122. if (DrawWidth > 0)
  123. DrawWidth--;
  124. break;
  125. case 'W':
  126. DrawWidth++;
  127. break;
  128. case 'h':
  129. if (DrawHeight > 0)
  130. DrawHeight--;
  131. break;
  132. case 'H':
  133. DrawHeight++;
  134. break;
  135. case 'p':
  136. if (SkipPixels > 0)
  137. SkipPixels--;
  138. break;
  139. case 'P':
  140. SkipPixels++;
  141. break;
  142. case 'r':
  143. if (SkipRows > 0)
  144. SkipRows--;
  145. break;
  146. case 'R':
  147. SkipRows++;
  148. break;
  149. case 's':
  150. Scissor = !Scissor;
  151. break;
  152. case 'x':
  153. Xzoom -= 0.1;
  154. break;
  155. case 'X':
  156. Xzoom += 0.1;
  157. break;
  158. case 'y':
  159. Yzoom -= 0.1;
  160. break;
  161. case 'Y':
  162. Yzoom += 0.1;
  163. break;
  164. case 'z':
  165. Zpos -= 0.1;
  166. printf("RasterPos Z = %g\n", Zpos);
  167. break;
  168. case 'Z':
  169. Zpos += 0.1;
  170. printf("RasterPos Z = %g\n", Zpos);
  171. break;
  172. case 'b':
  173. Benchmark();
  174. break;
  175. case 'F':
  176. Fog = !Fog;
  177. printf("Fog %d\n", Fog);
  178. break;
  179. case 'f':
  180. DrawFront = !DrawFront;
  181. if (DrawFront)
  182. glDrawBuffer(GL_FRONT);
  183. else
  184. glDrawBuffer(GL_BACK);
  185. printf("glDrawBuffer(%s)\n", DrawFront ? "GL_FRONT" : "GL_BACK");
  186. break;
  187. case 27:
  188. exit(0);
  189. break;
  190. }
  191. glutPostRedisplay();
  192. }
  193. static void SpecialKey( int key, int x, int y )
  194. {
  195. (void) x;
  196. (void) y;
  197. switch (key) {
  198. case GLUT_KEY_UP:
  199. Ypos += 1;
  200. break;
  201. case GLUT_KEY_DOWN:
  202. Ypos -= 1;
  203. break;
  204. case GLUT_KEY_LEFT:
  205. Xpos -= 1;
  206. break;
  207. case GLUT_KEY_RIGHT:
  208. Xpos += 1;
  209. break;
  210. }
  211. glutPostRedisplay();
  212. }
  213. static void Init( GLboolean ciMode, const char *filename )
  214. {
  215. static const GLfloat fogColor[4] = {0, 1, 0, 0};
  216. printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
  217. printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
  218. Image = LoadRGBImage( filename, &ImgWidth, &ImgHeight, &ImgFormat );
  219. if (!Image) {
  220. printf("Couldn't read %s\n", filename);
  221. exit(0);
  222. }
  223. if (ciMode) {
  224. /* Convert RGB image to grayscale */
  225. GLubyte *indexImage = (GLubyte *) malloc( ImgWidth * ImgHeight );
  226. GLint i;
  227. for (i=0; i<ImgWidth*ImgHeight; i++) {
  228. int gray = Image[i*3] + Image[i*3+1] + Image[i*3+2];
  229. indexImage[i] = gray / 3;
  230. }
  231. free(Image);
  232. Image = indexImage;
  233. ImgFormat = GL_COLOR_INDEX;
  234. for (i=0;i<255;i++) {
  235. float g = i / 255.0;
  236. glutSetColor(i, g, g, g);
  237. }
  238. }
  239. printf("Loaded %d by %d image\n", ImgWidth, ImgHeight );
  240. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  241. glPixelStorei(GL_UNPACK_ROW_LENGTH, ImgWidth);
  242. glFogi(GL_FOG_MODE, GL_LINEAR);
  243. glFogf(GL_FOG_START, 0);
  244. glFogf(GL_FOG_END, 2);
  245. glFogfv(GL_FOG_COLOR, fogColor);
  246. Reset();
  247. }
  248. static void Usage(void)
  249. {
  250. printf("Keys:\n");
  251. printf(" SPACE Reset Parameters\n");
  252. printf(" Up/Down Move image up/down\n");
  253. printf(" Left/Right Move image left/right\n");
  254. printf(" x Decrease X-axis PixelZoom\n");
  255. printf(" X Increase X-axis PixelZoom\n");
  256. printf(" y Decrease Y-axis PixelZoom\n");
  257. printf(" Y Increase Y-axis PixelZoom\n");
  258. printf(" w Decrease glDrawPixels width*\n");
  259. printf(" W Increase glDrawPixels width*\n");
  260. printf(" h Decrease glDrawPixels height*\n");
  261. printf(" H Increase glDrawPixels height*\n");
  262. printf(" p Decrease GL_UNPACK_SKIP_PIXELS*\n");
  263. printf(" P Increase GL_UNPACK_SKIP_PIXELS*\n");
  264. printf(" r Decrease GL_UNPACK_SKIP_ROWS*\n");
  265. printf(" R Increase GL_UNPACK_SKIP_ROWS*\n");
  266. printf(" s Toggle GL_SCISSOR_TEST\n");
  267. printf(" F Toggle GL_FOG\n");
  268. printf(" z Decrease RasterPos Z\n");
  269. printf(" Z Increase RasterPos Z\n");
  270. printf(" f Toggle front/back buffer drawing\n");
  271. printf(" b Benchmark test\n");
  272. printf(" ESC Exit\n");
  273. printf("* Warning: no limits are imposed on these parameters so it's\n");
  274. printf(" possible to cause a segfault if you go too far.\n");
  275. }
  276. int main( int argc, char *argv[] )
  277. {
  278. GLboolean ciMode = GL_FALSE;
  279. const char *filename = IMAGE_FILE;
  280. int i = 1;
  281. if (argc > i && strcmp(argv[i], "-ci")==0) {
  282. ciMode = GL_TRUE;
  283. i++;
  284. }
  285. if (argc > i) {
  286. filename = argv[i];
  287. }
  288. glutInit( &argc, argv );
  289. glutInitWindowPosition( 0, 0 );
  290. glutInitWindowSize( 500, 400 );
  291. if (ciMode)
  292. glutInitDisplayMode( GLUT_INDEX | GLUT_DOUBLE );
  293. else
  294. glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE);
  295. glutCreateWindow(argv[0]);
  296. Init(ciMode, filename);
  297. Usage();
  298. glutReshapeFunc( Reshape );
  299. glutKeyboardFunc( Key );
  300. glutSpecialFunc( SpecialKey );
  301. glutDisplayFunc( Display );
  302. glutMainLoop();
  303. return 0;
  304. }