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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. * Demo of off-screen Mesa rendering
  3. *
  4. * See Mesa/include/GL/osmesa.h for documentation of the OSMesa functions.
  5. *
  6. * If you want to render BIG images you'll probably have to increase
  7. * MAX_WIDTH and MAX_HEIGHT in src/config.h.
  8. *
  9. * This program is in the public domain.
  10. *
  11. * Brian Paul
  12. *
  13. * PPM output provided by Joerg Schmalzl.
  14. * ASCII PPM output added by Brian Paul.
  15. *
  16. * Usage: osdemo [-perf] [filename]
  17. *
  18. * -perf: Redraws the image 1000 times, displaying the FPS every 5 secs.
  19. * filename: file to store the TGA or PPM output
  20. */
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include "GL/osmesa.h"
  25. #include "GL/glut.h"
  26. #define SAVE_TARGA
  27. #define WIDTH 400
  28. #define HEIGHT 400
  29. static GLint T0 = 0;
  30. static GLint Frames = 0;
  31. static int perf = 0;
  32. static void render_image( void )
  33. {
  34. GLfloat light_ambient[] = { 0.0, 0.0, 0.0, 1.0 };
  35. GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
  36. GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
  37. GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
  38. GLfloat red_mat[] = { 1.0, 0.2, 0.2, 1.0 };
  39. GLfloat green_mat[] = { 0.2, 1.0, 0.2, 1.0 };
  40. GLfloat blue_mat[] = { 0.2, 0.2, 1.0, 1.0 };
  41. glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
  42. glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
  43. glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
  44. glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  45. glEnable(GL_LIGHTING);
  46. glEnable(GL_LIGHT0);
  47. glEnable(GL_DEPTH_TEST);
  48. glMatrixMode(GL_PROJECTION);
  49. glLoadIdentity();
  50. glOrtho(-2.5, 2.5, -2.5, 2.5, -10.0, 10.0);
  51. glMatrixMode(GL_MODELVIEW);
  52. glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  53. glPushMatrix();
  54. glRotatef(20.0, 1.0, 0.0, 0.0);
  55. glPushMatrix();
  56. glTranslatef(-0.75, 0.5, 0.0);
  57. glRotatef(90.0, 1.0, 0.0, 0.0);
  58. glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, red_mat );
  59. glutSolidTorus(0.275, 0.85, 20, 20);
  60. glPopMatrix();
  61. glPushMatrix();
  62. glTranslatef(-0.75, -0.5, 0.0);
  63. glRotatef(270.0, 1.0, 0.0, 0.0);
  64. glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, green_mat );
  65. glutSolidCone(1.0, 2.0, 16, 1);
  66. glPopMatrix();
  67. #ifdef GL_HP_occlusion_test
  68. if (perf == 0) {
  69. GLboolean bRet;
  70. glDepthMask(GL_FALSE);
  71. glColorMask(GL_FALSE,GL_FALSE,GL_FALSE,GL_FALSE);
  72. glEnable(GL_OCCLUSION_TEST_HP);
  73. glGetBooleanv(GL_OCCLUSION_TEST_RESULT_HP,&bRet);
  74. glPushMatrix();
  75. glTranslatef(0.75, 0.0, -1.0);
  76. glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, blue_mat );
  77. glutSolidSphere(1.0, 20, 20);
  78. glPopMatrix();
  79. glGetBooleanv(GL_OCCLUSION_TEST_RESULT_HP,&bRet);
  80. printf("Occlusion test 1 (result should be 1): %d\n",bRet);
  81. glDepthMask(GL_TRUE);
  82. glColorMask(GL_TRUE,GL_TRUE,GL_TRUE,GL_TRUE);
  83. glDisable(GL_OCCLUSION_TEST_HP);
  84. }
  85. #endif
  86. glPushMatrix();
  87. glTranslatef(0.75, 0.0, -1.0);
  88. glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, blue_mat );
  89. glutSolidSphere(1.0, 20, 20);
  90. glPopMatrix();
  91. #ifdef GL_HP_occlusion_test
  92. if (perf == 0){
  93. GLboolean bRet;
  94. glDepthMask(GL_FALSE);
  95. glColorMask(GL_FALSE,GL_FALSE,GL_FALSE,GL_FALSE);
  96. glEnable(GL_OCCLUSION_TEST_HP);
  97. glGetBooleanv(GL_OCCLUSION_TEST_RESULT_HP,&bRet);
  98. /* draw a sphere inside the previous sphere */
  99. glPushMatrix();
  100. glTranslatef(0.75, 0.0, -1.0);
  101. glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, blue_mat );
  102. glutSolidSphere(0.5, 20, 20);
  103. glPopMatrix();
  104. glGetBooleanv(GL_OCCLUSION_TEST_RESULT_HP,&bRet);
  105. printf("Occlusion test 2 (result should be 0): %d\n",bRet);
  106. glDepthMask(GL_TRUE);
  107. glColorMask(GL_TRUE,GL_TRUE,GL_TRUE,GL_TRUE);
  108. glDisable(GL_OCCLUSION_TEST_HP);
  109. }
  110. #endif
  111. glPopMatrix();
  112. /* This is very important!!!
  113. * Make sure buffered commands are finished!!!
  114. */
  115. glFinish();
  116. Frames++;
  117. if (perf) {
  118. GLint t = glutGet(GLUT_ELAPSED_TIME);
  119. if (t - T0 >= 5000) {
  120. GLfloat seconds = (t - T0) / 1000.0;
  121. GLfloat fps = Frames / seconds;
  122. printf("%d frames in %6.3f seconds = %6.3f FPS\n", Frames, seconds, fps);
  123. T0 = t;
  124. Frames = 0;
  125. }
  126. }
  127. }
  128. #ifdef SAVE_TARGA
  129. static void
  130. write_targa(const char *filename, const GLubyte *buffer, int width, int height)
  131. {
  132. FILE *f = fopen( filename, "w" );
  133. if (f) {
  134. int i, x, y;
  135. const GLubyte *ptr = buffer;
  136. printf ("osdemo, writing tga file \n");
  137. fputc (0x00, f); /* ID Length, 0 => No ID */
  138. fputc (0x00, f); /* Color Map Type, 0 => No color map included */
  139. fputc (0x02, f); /* Image Type, 2 => Uncompressed, True-color Image */
  140. fputc (0x00, f); /* Next five bytes are about the color map entries */
  141. fputc (0x00, f); /* 2 bytes Index, 2 bytes length, 1 byte size */
  142. fputc (0x00, f);
  143. fputc (0x00, f);
  144. fputc (0x00, f);
  145. fputc (0x00, f); /* X-origin of Image */
  146. fputc (0x00, f);
  147. fputc (0x00, f); /* Y-origin of Image */
  148. fputc (0x00, f);
  149. fputc (WIDTH & 0xff, f); /* Image Width */
  150. fputc ((WIDTH>>8) & 0xff, f);
  151. fputc (HEIGHT & 0xff, f); /* Image Height */
  152. fputc ((HEIGHT>>8) & 0xff, f);
  153. fputc (0x18, f); /* Pixel Depth, 0x18 => 24 Bits */
  154. fputc (0x20, f); /* Image Descriptor */
  155. fclose(f);
  156. f = fopen( filename, "ab" ); /* reopen in binary append mode */
  157. for (y=height-1; y>=0; y--) {
  158. for (x=0; x<width; x++) {
  159. i = (y*width + x) * 4;
  160. fputc(ptr[i+2], f); /* write blue */
  161. fputc(ptr[i+1], f); /* write green */
  162. fputc(ptr[i], f); /* write red */
  163. }
  164. }
  165. }
  166. }
  167. #else
  168. static void
  169. write_ppm(const char *filename, const GLubyte *buffer, int width, int height)
  170. {
  171. const int binary = 0;
  172. FILE *f = fopen( filename, "w" );
  173. if (f) {
  174. int i, x, y;
  175. const GLubyte *ptr = buffer;
  176. if (binary) {
  177. fprintf(f,"P6\n");
  178. fprintf(f,"# ppm-file created by osdemo.c\n");
  179. fprintf(f,"%i %i\n", width,height);
  180. fprintf(f,"255\n");
  181. fclose(f);
  182. f = fopen( filename, "ab" ); /* reopen in binary append mode */
  183. for (y=height-1; y>=0; y--) {
  184. for (x=0; x<width; x++) {
  185. i = (y*width + x) * 4;
  186. fputc(ptr[i], f); /* write red */
  187. fputc(ptr[i+1], f); /* write green */
  188. fputc(ptr[i+2], f); /* write blue */
  189. }
  190. }
  191. }
  192. else {
  193. /*ASCII*/
  194. int counter = 0;
  195. fprintf(f,"P3\n");
  196. fprintf(f,"# ascii ppm file created by osdemo.c\n");
  197. fprintf(f,"%i %i\n", width, height);
  198. fprintf(f,"255\n");
  199. for (y=height-1; y>=0; y--) {
  200. for (x=0; x<width; x++) {
  201. i = (y*width + x) * 4;
  202. fprintf(f, " %3d %3d %3d", ptr[i], ptr[i+1], ptr[i+2]);
  203. counter++;
  204. if (counter % 5 == 0)
  205. fprintf(f, "\n");
  206. }
  207. }
  208. }
  209. fclose(f);
  210. }
  211. }
  212. #endif
  213. int main( int argc, char *argv[] )
  214. {
  215. void *buffer;
  216. int i;
  217. char *filename = NULL;
  218. /* Create an RGBA-mode context */
  219. #if OSMESA_MAJOR_VERSION * 100 + OSMESA_MINOR_VERSION >= 305
  220. /* specify Z, stencil, accum sizes */
  221. OSMesaContext ctx = OSMesaCreateContextExt( OSMESA_RGBA, 16, 0, 0, NULL );
  222. #else
  223. OSMesaContext ctx = OSMesaCreateContext( OSMESA_RGBA, NULL );
  224. #endif
  225. if (!ctx) {
  226. printf("OSMesaCreateContext failed!\n");
  227. return 0;
  228. }
  229. for ( i=1; i<argc; i++ ) {
  230. if (argv[i][0] != '-') filename = argv[i];
  231. if (strcmp(argv[i], "-perf")==0) perf = 1;
  232. }
  233. /* Allocate the image buffer */
  234. buffer = malloc( WIDTH * HEIGHT * 4 * sizeof(GLubyte) );
  235. if (!buffer) {
  236. printf("Alloc image buffer failed!\n");
  237. return 0;
  238. }
  239. /* Bind the buffer to the context and make it current */
  240. if (!OSMesaMakeCurrent( ctx, buffer, GL_UNSIGNED_BYTE, WIDTH, HEIGHT )) {
  241. printf("OSMesaMakeCurrent failed!\n");
  242. return 0;
  243. }
  244. {
  245. int z, s, a;
  246. glGetIntegerv(GL_DEPTH_BITS, &z);
  247. glGetIntegerv(GL_STENCIL_BITS, &s);
  248. glGetIntegerv(GL_ACCUM_RED_BITS, &a);
  249. printf("Depth=%d Stencil=%d Accum=%d\n", z, s, a);
  250. }
  251. render_image();
  252. if (perf)
  253. for(i=0; i< 1000; i++)
  254. render_image();
  255. if (filename != NULL) {
  256. #ifdef SAVE_TARGA
  257. write_targa(filename, buffer, WIDTH, HEIGHT);
  258. #else
  259. write_ppm(filename, buffer, WIDTH, HEIGHT);
  260. #endif
  261. }
  262. else {
  263. printf("Specify a filename if you want to make an image file\n");
  264. }
  265. printf("all done\n");
  266. /* free the image buffer */
  267. free( buffer );
  268. /* destroy the context */
  269. OSMesaDestroyContext( ctx );
  270. return 0;
  271. }