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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /* $Id: osdemo.c,v 1.2 2000/01/15 06:11:33 rjfrank 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. /*
  18. * $Log: osdemo.c,v $
  19. * Revision 1.2 2000/01/15 06:11:33 rjfrank
  20. * Added test for the occlusion test code.
  21. *
  22. * Revision 1.1.1.1 1999/08/19 00:55:40 jtg
  23. * Imported sources
  24. *
  25. * Revision 3.0 1998/02/14 18:42:29 brianp
  26. * initial rev
  27. *
  28. */
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include "GL/osmesa.h"
  32. #include "GL/glut.h"
  33. #define WIDTH 400
  34. #define HEIGHT 400
  35. static void render_image( void )
  36. {
  37. GLfloat light_ambient[] = { 0.0, 0.0, 0.0, 1.0 };
  38. GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
  39. GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
  40. GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
  41. GLfloat red_mat[] = { 1.0, 0.2, 0.2, 1.0 };
  42. GLfloat green_mat[] = { 0.2, 1.0, 0.2, 1.0 };
  43. GLfloat blue_mat[] = { 0.2, 0.2, 1.0, 1.0 };
  44. glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
  45. glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
  46. glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
  47. glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  48. glEnable(GL_LIGHTING);
  49. glEnable(GL_LIGHT0);
  50. glEnable(GL_DEPTH_TEST);
  51. glMatrixMode(GL_PROJECTION);
  52. glLoadIdentity();
  53. glOrtho(-2.5, 2.5, -2.5, 2.5, -10.0, 10.0);
  54. glMatrixMode(GL_MODELVIEW);
  55. glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  56. glPushMatrix();
  57. glRotatef(20.0, 1.0, 0.0, 0.0);
  58. glPushMatrix();
  59. glTranslatef(-0.75, 0.5, 0.0);
  60. glRotatef(90.0, 1.0, 0.0, 0.0);
  61. glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, red_mat );
  62. glutSolidTorus(0.275, 0.85, 20, 20);
  63. glPopMatrix();
  64. glPushMatrix();
  65. glTranslatef(-0.75, -0.5, 0.0);
  66. glRotatef(270.0, 1.0, 0.0, 0.0);
  67. glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, green_mat );
  68. glutSolidCone(1.0, 2.0, 16, 1);
  69. glPopMatrix();
  70. #ifdef OSMESA_OCCLUSION_TEST_RESULT_HP
  71. {
  72. GLboolean bRet;
  73. OSMesaGetBooleanv(OSMESA_OCCLUSION_TEST_RESULT_HP,&bRet);
  74. glDepthMask(GL_FALSE);
  75. glColorMask(GL_FALSE,GL_FALSE,GL_FALSE,GL_FALSE);
  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. OSMesaGetBooleanv(OSMESA_OCCLUSION_TEST_RESULT_HP,&bRet);
  82. printf("Occlusion test 1 (result should be 1): %d\n",bRet);
  83. glDepthMask(GL_TRUE);
  84. glColorMask(GL_TRUE,GL_TRUE,GL_TRUE,GL_TRUE);
  85. }
  86. #endif
  87. glPushMatrix();
  88. glTranslatef(0.75, 0.0, -1.0);
  89. glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, blue_mat );
  90. glutSolidSphere(1.0, 20, 20);
  91. glPopMatrix();
  92. #ifdef OSMESA_OCCLUSION_TEST_RESULT_HP
  93. {
  94. GLboolean bRet;
  95. OSMesaGetBooleanv(OSMESA_OCCLUSION_TEST_RESULT_HP,&bRet);
  96. glDepthMask(GL_FALSE);
  97. glColorMask(GL_FALSE,GL_FALSE,GL_FALSE,GL_FALSE);
  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. OSMesaGetBooleanv(OSMESA_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. }
  109. #endif
  110. glPopMatrix();
  111. }
  112. int main( int argc, char *argv[] )
  113. {
  114. OSMesaContext ctx;
  115. void *buffer;
  116. /* Create an RGBA-mode context */
  117. ctx = OSMesaCreateContext( GL_RGBA, NULL );
  118. /* Allocate the image buffer */
  119. buffer = malloc( WIDTH * HEIGHT * 4 );
  120. /* Bind the buffer to the context and make it current */
  121. OSMesaMakeCurrent( ctx, buffer, GL_UNSIGNED_BYTE, WIDTH, HEIGHT );
  122. render_image();
  123. if (argc>1) {
  124. /* write PPM file */
  125. FILE *f = fopen( argv[1], "w" );
  126. if (f) {
  127. int i, x, y;
  128. GLubyte *ptr = (GLubyte *) buffer;
  129. #define BINARY 0
  130. #if BINARY
  131. fprintf(f,"P6\n");
  132. fprintf(f,"# ppm-file created by %s\n", argv[0]);
  133. fprintf(f,"%i %i\n", WIDTH,HEIGHT);
  134. fprintf(f,"255\n");
  135. fclose(f);
  136. f = fopen( argv[1], "ab" ); /* reopen in binary append mode */
  137. for (y=HEIGHT-1; y>=0; y--) {
  138. for (x=0; x<WIDTH; x++) {
  139. i = (y*WIDTH + x) * 4;
  140. fputc(ptr[i], f); /* write red */
  141. fputc(ptr[i+1], f); /* write green */
  142. fputc(ptr[i+2], f); /* write blue */
  143. }
  144. }
  145. #else /*ASCII*/
  146. int counter = 0;
  147. fprintf(f,"P3\n");
  148. fprintf(f,"# ascii ppm file created by %s\n", argv[0]);
  149. fprintf(f,"%i %i\n", WIDTH, HEIGHT);
  150. fprintf(f,"255\n");
  151. for (y=HEIGHT-1; y>=0; y--) {
  152. for (x=0; x<WIDTH; x++) {
  153. i = (y*WIDTH + x) * 4;
  154. fprintf(f, " %3d %3d %3d", ptr[i], ptr[i+1], ptr[i+2]);
  155. counter++;
  156. if (counter % 5 == 0)
  157. fprintf(f, "\n");
  158. }
  159. }
  160. #endif
  161. fclose(f);
  162. }
  163. }
  164. else {
  165. printf("Specify a filename if you want to make a ppm file\n");
  166. }
  167. printf("all done\n");
  168. /* free the image buffer */
  169. free( buffer );
  170. /* destroy the context */
  171. OSMesaDestroyContext( ctx );
  172. return 0;
  173. }