Clone of mesa.
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

osdemo.c 8.5KB

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