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 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /* $Id: osdemo.c,v 1.3 2000/03/06 23:56:21 brianp 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 OSMESA_OCCLUSION_TEST_RESULT_HP
  60. {
  61. GLboolean bRet;
  62. OSMesaGetBooleanv(OSMESA_OCCLUSION_TEST_RESULT_HP,&bRet);
  63. glDepthMask(GL_FALSE);
  64. glColorMask(GL_FALSE,GL_FALSE,GL_FALSE,GL_FALSE);
  65. glPushMatrix();
  66. glTranslatef(0.75, 0.0, -1.0);
  67. glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, blue_mat );
  68. glutSolidSphere(1.0, 20, 20);
  69. glPopMatrix();
  70. OSMesaGetBooleanv(OSMESA_OCCLUSION_TEST_RESULT_HP,&bRet);
  71. printf("Occlusion test 1 (result should be 1): %d\n",bRet);
  72. glDepthMask(GL_TRUE);
  73. glColorMask(GL_TRUE,GL_TRUE,GL_TRUE,GL_TRUE);
  74. }
  75. #endif
  76. glPushMatrix();
  77. glTranslatef(0.75, 0.0, -1.0);
  78. glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, blue_mat );
  79. glutSolidSphere(1.0, 20, 20);
  80. glPopMatrix();
  81. #ifdef OSMESA_OCCLUSION_TEST_RESULT_HP
  82. {
  83. GLboolean bRet;
  84. OSMesaGetBooleanv(OSMESA_OCCLUSION_TEST_RESULT_HP,&bRet);
  85. glDepthMask(GL_FALSE);
  86. glColorMask(GL_FALSE,GL_FALSE,GL_FALSE,GL_FALSE);
  87. /* draw a sphere inside the previous sphere */
  88. glPushMatrix();
  89. glTranslatef(0.75, 0.0, -1.0);
  90. glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, blue_mat );
  91. glutSolidSphere(0.5, 20, 20);
  92. glPopMatrix();
  93. OSMesaGetBooleanv(OSMESA_OCCLUSION_TEST_RESULT_HP,&bRet);
  94. printf("Occlusion test 2 (result should be 0): %d\n",bRet);
  95. glDepthMask(GL_TRUE);
  96. glColorMask(GL_TRUE,GL_TRUE,GL_TRUE,GL_TRUE);
  97. }
  98. #endif
  99. glPopMatrix();
  100. }
  101. static void
  102. write_targa(const char *filename, const GLubyte *buffer, int width, int height)
  103. {
  104. FILE *f = fopen( filename, "w" );
  105. if (f) {
  106. int i, x, y;
  107. const GLubyte *ptr = buffer;
  108. printf ("osdemo, writing tga file \n");
  109. fputc (0x00, f); /* ID Length, 0 => No ID */
  110. fputc (0x00, f); /* Color Map Type, 0 => No color map included */
  111. fputc (0x02, f); /* Image Type, 2 => Uncompressed, True-color Image */
  112. fputc (0x00, f); /* Next five bytes are about the color map entries */
  113. fputc (0x00, f); /* 2 bytes Index, 2 bytes length, 1 byte size */
  114. fputc (0x00, f);
  115. fputc (0x00, f);
  116. fputc (0x00, f);
  117. fputc (0x00, f); /* X-origin of Image */
  118. fputc (0x00, f);
  119. fputc (0x00, f); /* Y-origin of Image */
  120. fputc (0x00, f);
  121. fputc (WIDTH & 0xff, f); /* Image Width */
  122. fputc ((WIDTH>>8) & 0xff, f);
  123. fputc (HEIGHT & 0xff, f); /* Image Height */
  124. fputc ((HEIGHT>>8) & 0xff, f);
  125. fputc (0x18, f); /* Pixel Depth, 0x18 => 24 Bits */
  126. fputc (0x20, f); /* Image Descriptor */
  127. fclose(f);
  128. f = fopen( filename, "ab" ); /* reopen in binary append mode */
  129. for (y=height-1; y>=0; y--) {
  130. for (x=0; x<width; x++) {
  131. i = (y*width + x) * 4;
  132. fputc(ptr[i+2], f); /* write blue */
  133. fputc(ptr[i+1], f); /* write green */
  134. fputc(ptr[i], f); /* write red */
  135. }
  136. }
  137. }
  138. }
  139. static void
  140. write_ppm(const char *filename, const GLubyte *buffer, int width, int height)
  141. {
  142. const int binary = 0;
  143. FILE *f = fopen( filename, "w" );
  144. if (f) {
  145. int i, x, y;
  146. const GLubyte *ptr = buffer;
  147. if (binary) {
  148. fprintf(f,"P6\n");
  149. fprintf(f,"# ppm-file created by osdemo.c\n");
  150. fprintf(f,"%i %i\n", width,height);
  151. fprintf(f,"255\n");
  152. fclose(f);
  153. f = fopen( filename, "ab" ); /* reopen in binary append mode */
  154. for (y=height-1; y>=0; y--) {
  155. for (x=0; x<width; x++) {
  156. i = (y*width + x) * 4;
  157. fputc(ptr[i], f); /* write red */
  158. fputc(ptr[i+1], f); /* write green */
  159. fputc(ptr[i+2], f); /* write blue */
  160. }
  161. }
  162. }
  163. else {
  164. /*ASCII*/
  165. int counter = 0;
  166. fprintf(f,"P3\n");
  167. fprintf(f,"# ascii ppm file created by osdemo.c\n");
  168. fprintf(f,"%i %i\n", width, height);
  169. fprintf(f,"255\n");
  170. for (y=height-1; y>=0; y--) {
  171. for (x=0; x<width; x++) {
  172. i = (y*width + x) * 4;
  173. fprintf(f, " %3d %3d %3d", ptr[i], ptr[i+1], ptr[i+2]);
  174. counter++;
  175. if (counter % 5 == 0)
  176. fprintf(f, "\n");
  177. }
  178. }
  179. }
  180. fclose(f);
  181. }
  182. }
  183. int main( int argc, char *argv[] )
  184. {
  185. /* Create an RGBA-mode context */
  186. OSMesaContext ctx = OSMesaCreateContext( GL_RGBA, NULL );
  187. /* Allocate the image buffer */
  188. void *buffer = malloc( WIDTH * HEIGHT * 4 );
  189. /* Bind the buffer to the context and make it current */
  190. OSMesaMakeCurrent( ctx, buffer, GL_UNSIGNED_BYTE, WIDTH, HEIGHT );
  191. render_image();
  192. if (argc>1) {
  193. #ifdef SAVE_TARGA
  194. write_targa(argv[1], buffer, WIDTH, HEIGHT);
  195. #else
  196. write_ppm(argv[1], buffer, WIDTH, HEIGHT);
  197. #endif
  198. }
  199. else {
  200. printf("Specify a filename if you want to make an image file\n");
  201. }
  202. printf("all done\n");
  203. /* free the image buffer */
  204. free( buffer );
  205. /* destroy the context */
  206. OSMesaDestroyContext( ctx );
  207. return 0;
  208. }