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

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