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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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 [filename]
  17. */
  18. #include <math.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include "GL/osmesa.h"
  23. #include "GL/glu.h"
  24. #define SAVE_TARGA
  25. static int Width = 400;
  26. static int Height = 400;
  27. static void
  28. Sphere(float radius, int slices, int stacks)
  29. {
  30. GLUquadric *q = gluNewQuadric();
  31. gluQuadricNormals(q, GLU_SMOOTH);
  32. gluSphere(q, radius, slices, stacks);
  33. gluDeleteQuadric(q);
  34. }
  35. static void
  36. Cone(float base, float height, int slices, int stacks)
  37. {
  38. GLUquadric *q = gluNewQuadric();
  39. gluQuadricDrawStyle(q, GLU_FILL);
  40. gluQuadricNormals(q, GLU_SMOOTH);
  41. gluCylinder(q, base, 0.0, height, slices, stacks);
  42. gluDeleteQuadric(q);
  43. }
  44. static void
  45. Torus(float innerRadius, float outerRadius, int sides, int rings)
  46. {
  47. /* from GLUT... */
  48. int i, j;
  49. GLfloat theta, phi, theta1;
  50. GLfloat cosTheta, sinTheta;
  51. GLfloat cosTheta1, sinTheta1;
  52. const GLfloat ringDelta = 2.0 * M_PI / rings;
  53. const GLfloat sideDelta = 2.0 * M_PI / sides;
  54. theta = 0.0;
  55. cosTheta = 1.0;
  56. sinTheta = 0.0;
  57. for (i = rings - 1; i >= 0; i--) {
  58. theta1 = theta + ringDelta;
  59. cosTheta1 = cos(theta1);
  60. sinTheta1 = sin(theta1);
  61. glBegin(GL_QUAD_STRIP);
  62. phi = 0.0;
  63. for (j = sides; j >= 0; j--) {
  64. GLfloat cosPhi, sinPhi, dist;
  65. phi += sideDelta;
  66. cosPhi = cos(phi);
  67. sinPhi = sin(phi);
  68. dist = outerRadius + innerRadius * cosPhi;
  69. glNormal3f(cosTheta1 * cosPhi, -sinTheta1 * cosPhi, sinPhi);
  70. glVertex3f(cosTheta1 * dist, -sinTheta1 * dist, innerRadius * sinPhi);
  71. glNormal3f(cosTheta * cosPhi, -sinTheta * cosPhi, sinPhi);
  72. glVertex3f(cosTheta * dist, -sinTheta * dist, innerRadius * sinPhi);
  73. }
  74. glEnd();
  75. theta = theta1;
  76. cosTheta = cosTheta1;
  77. sinTheta = sinTheta1;
  78. }
  79. }
  80. static void
  81. render_image(void)
  82. {
  83. GLfloat light_ambient[] = { 0.0, 0.0, 0.0, 1.0 };
  84. GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
  85. GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
  86. GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
  87. GLfloat red_mat[] = { 1.0, 0.2, 0.2, 1.0 };
  88. GLfloat green_mat[] = { 0.2, 1.0, 0.2, 1.0 };
  89. GLfloat blue_mat[] = { 0.2, 0.2, 1.0, 1.0 };
  90. glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
  91. glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
  92. glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
  93. glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  94. glEnable(GL_LIGHTING);
  95. glEnable(GL_LIGHT0);
  96. glEnable(GL_DEPTH_TEST);
  97. glMatrixMode(GL_PROJECTION);
  98. glLoadIdentity();
  99. glOrtho(-2.5, 2.5, -2.5, 2.5, -10.0, 10.0);
  100. glMatrixMode(GL_MODELVIEW);
  101. glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  102. glPushMatrix();
  103. glRotatef(20.0, 1.0, 0.0, 0.0);
  104. glPushMatrix();
  105. glTranslatef(-0.75, 0.5, 0.0);
  106. glRotatef(90.0, 1.0, 0.0, 0.0);
  107. glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, red_mat );
  108. Torus(0.275, 0.85, 20, 20);
  109. glPopMatrix();
  110. glPushMatrix();
  111. glTranslatef(-0.75, -0.5, 0.0);
  112. glRotatef(270.0, 1.0, 0.0, 0.0);
  113. glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, green_mat );
  114. Cone(1.0, 2.0, 16, 1);
  115. glPopMatrix();
  116. glPushMatrix();
  117. glTranslatef(0.75, 0.0, -1.0);
  118. glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, blue_mat );
  119. Sphere(1.0, 20, 20);
  120. glPopMatrix();
  121. glPopMatrix();
  122. /* This is very important!!!
  123. * Make sure buffered commands are finished!!!
  124. */
  125. glFinish();
  126. }
  127. #ifdef SAVE_TARGA
  128. static void
  129. write_targa(const char *filename, const GLubyte *buffer, int width, int height)
  130. {
  131. FILE *f = fopen( filename, "w" );
  132. if (f) {
  133. int i, x, y;
  134. const GLubyte *ptr = buffer;
  135. printf ("osdemo, writing tga file \n");
  136. fputc (0x00, f); /* ID Length, 0 => No ID */
  137. fputc (0x00, f); /* Color Map Type, 0 => No color map included */
  138. fputc (0x02, f); /* Image Type, 2 => Uncompressed, True-color Image */
  139. fputc (0x00, f); /* Next five bytes are about the color map entries */
  140. fputc (0x00, f); /* 2 bytes Index, 2 bytes length, 1 byte size */
  141. fputc (0x00, f);
  142. fputc (0x00, f);
  143. fputc (0x00, f);
  144. fputc (0x00, f); /* X-origin of Image */
  145. fputc (0x00, f);
  146. fputc (0x00, f); /* Y-origin of Image */
  147. fputc (0x00, f);
  148. fputc (Width & 0xff, f); /* Image Width */
  149. fputc ((Width>>8) & 0xff, f);
  150. fputc (Height & 0xff, f); /* Image Height */
  151. fputc ((Height>>8) & 0xff, f);
  152. fputc (0x18, f); /* Pixel Depth, 0x18 => 24 Bits */
  153. fputc (0x20, f); /* Image Descriptor */
  154. fclose(f);
  155. f = fopen( filename, "ab" ); /* reopen in binary append mode */
  156. for (y=height-1; y>=0; y--) {
  157. for (x=0; x<width; x++) {
  158. i = (y*width + x) * 4;
  159. fputc(ptr[i+2], f); /* write blue */
  160. fputc(ptr[i+1], f); /* write green */
  161. fputc(ptr[i], f); /* write red */
  162. }
  163. }
  164. }
  165. }
  166. #else
  167. static void
  168. write_ppm(const char *filename, const GLubyte *buffer, int width, int height)
  169. {
  170. const int binary = 0;
  171. FILE *f = fopen( filename, "w" );
  172. if (f) {
  173. int i, x, y;
  174. const GLubyte *ptr = buffer;
  175. if (binary) {
  176. fprintf(f,"P6\n");
  177. fprintf(f,"# ppm-file created by osdemo.c\n");
  178. fprintf(f,"%i %i\n", width,height);
  179. fprintf(f,"255\n");
  180. fclose(f);
  181. f = fopen( filename, "ab" ); /* reopen in binary append mode */
  182. for (y=height-1; y>=0; y--) {
  183. for (x=0; x<width; x++) {
  184. i = (y*width + x) * 4;
  185. fputc(ptr[i], f); /* write red */
  186. fputc(ptr[i+1], f); /* write green */
  187. fputc(ptr[i+2], f); /* write blue */
  188. }
  189. }
  190. }
  191. else {
  192. /*ASCII*/
  193. int counter = 0;
  194. fprintf(f,"P3\n");
  195. fprintf(f,"# ascii ppm file created by osdemo.c\n");
  196. fprintf(f,"%i %i\n", width, height);
  197. fprintf(f,"255\n");
  198. for (y=height-1; y>=0; y--) {
  199. for (x=0; x<width; x++) {
  200. i = (y*width + x) * 4;
  201. fprintf(f, " %3d %3d %3d", ptr[i], ptr[i+1], ptr[i+2]);
  202. counter++;
  203. if (counter % 5 == 0)
  204. fprintf(f, "\n");
  205. }
  206. }
  207. }
  208. fclose(f);
  209. }
  210. }
  211. #endif
  212. int
  213. main(int argc, char *argv[])
  214. {
  215. OSMesaContext ctx;
  216. void *buffer;
  217. char *filename = NULL;
  218. if (argc < 2) {
  219. fprintf(stderr, "Usage:\n");
  220. fprintf(stderr, " osdemo filename [width height]\n");
  221. return 0;
  222. }
  223. filename = argv[1];
  224. if (argc == 4) {
  225. Width = atoi(argv[2]);
  226. Height = atoi(argv[3]);
  227. }
  228. /* Create an RGBA-mode context */
  229. #if OSMESA_MAJOR_VERSION * 100 + OSMESA_MINOR_VERSION >= 305
  230. /* specify Z, stencil, accum sizes */
  231. ctx = OSMesaCreateContextExt( OSMESA_RGBA, 16, 0, 0, NULL );
  232. #else
  233. ctx = OSMesaCreateContext( OSMESA_RGBA, NULL );
  234. #endif
  235. if (!ctx) {
  236. printf("OSMesaCreateContext failed!\n");
  237. return 0;
  238. }
  239. /* Allocate the image buffer */
  240. buffer = malloc( Width * Height * 4 * sizeof(GLubyte) );
  241. if (!buffer) {
  242. printf("Alloc image buffer failed!\n");
  243. return 0;
  244. }
  245. /* Bind the buffer to the context and make it current */
  246. if (!OSMesaMakeCurrent( ctx, buffer, GL_UNSIGNED_BYTE, Width, Height )) {
  247. printf("OSMesaMakeCurrent failed!\n");
  248. return 0;
  249. }
  250. {
  251. int z, s, a;
  252. glGetIntegerv(GL_DEPTH_BITS, &z);
  253. glGetIntegerv(GL_STENCIL_BITS, &s);
  254. glGetIntegerv(GL_ACCUM_RED_BITS, &a);
  255. printf("Depth=%d Stencil=%d Accum=%d\n", z, s, a);
  256. }
  257. render_image();
  258. if (filename != NULL) {
  259. #ifdef SAVE_TARGA
  260. write_targa(filename, buffer, Width, Height);
  261. #else
  262. write_ppm(filename, buffer, Width, Height);
  263. #endif
  264. }
  265. else {
  266. printf("Specify a filename if you want to make an image file\n");
  267. }
  268. printf("all done\n");
  269. /* free the image buffer */
  270. free( buffer );
  271. /* destroy the context */
  272. OSMesaDestroyContext( ctx );
  273. return 0;
  274. }