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.

osdemo.c 7.7KB

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