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.

osdemo16.c 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /* $Id: osdemo16.c,v 1.1 2001/03/08 15:24:18 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, 0.5 };
  32. GLfloat blue_mat[] = { 0.2, 0.2, 1.0, 1.0 };
  33. GLfloat white_mat[] = { 1.0, 1.0, 1.0, 1.0 };
  34. GLfloat purple_mat[] = { 1.0, 0.2, 1.0, 1.0 };
  35. GLUquadricObj *qobj = gluNewQuadric();
  36. glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
  37. glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
  38. glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
  39. glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  40. glEnable(GL_LIGHTING);
  41. glEnable(GL_LIGHT0);
  42. glEnable(GL_DEPTH_TEST);
  43. glMatrixMode(GL_PROJECTION);
  44. glLoadIdentity();
  45. glOrtho(-2.5, 2.5, -2.5, 2.5, -10.0, 10.0);
  46. glMatrixMode(GL_MODELVIEW);
  47. glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  48. glPushMatrix();
  49. glRotatef(20.0, 1.0, 0.0, 0.0);
  50. #if 0
  51. glPushMatrix();
  52. glTranslatef(-0.75, 0.5, 0.0);
  53. glRotatef(90.0, 1.0, 0.0, 0.0);
  54. glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, red_mat );
  55. glutSolidTorus(0.275, 0.85, 20, 20);
  56. glPopMatrix();
  57. #endif
  58. /* red square */
  59. glPushMatrix();
  60. glTranslatef(0.0, -0.5, 0.0);
  61. glRotatef(90, 1, 0.5, 0);
  62. glScalef(3, 3, 3);
  63. glDisable(GL_LIGHTING);
  64. glColor4f(1, 0, 0, 0.5);
  65. glBegin(GL_POLYGON);
  66. glVertex2f(-1, -1);
  67. glVertex2f( 1, -1);
  68. glVertex2f( 1, 1);
  69. glVertex2f(-1, 1);
  70. glEnd();
  71. glEnable(GL_LIGHTING);
  72. glPopMatrix();
  73. #if 0
  74. /* green square */
  75. glPushMatrix();
  76. glTranslatef(0.0, 0.5, 0.1);
  77. glDisable(GL_LIGHTING);
  78. glColor3f(0, 1, 0);
  79. glBegin(GL_POLYGON);
  80. glVertex2f(-1, -1);
  81. glVertex2f( 1, -1);
  82. glVertex2f( 1, 1);
  83. glVertex2f(-1, 1);
  84. glEnd();
  85. glEnable(GL_LIGHTING);
  86. glPopMatrix();
  87. /* blue square */
  88. glPushMatrix();
  89. glTranslatef(0.75, 0.5, 0.3);
  90. glDisable(GL_LIGHTING);
  91. glColor3f(0, 0, 0.5);
  92. glBegin(GL_POLYGON);
  93. glVertex2f(-1, -1);
  94. glVertex2f( 1, -1);
  95. glVertex2f( 1, 1);
  96. glVertex2f(-1, 1);
  97. glEnd();
  98. glEnable(GL_LIGHTING);
  99. glPopMatrix();
  100. #endif
  101. glPushMatrix();
  102. glTranslatef(-0.75, -0.5, 0.0);
  103. glRotatef(270.0, 1.0, 0.0, 0.0);
  104. glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, green_mat );
  105. glColor4f(0,1,0,0.5);
  106. glEnable(GL_BLEND);
  107. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  108. gluCylinder(qobj, 1.0, 0.0, 2.0, 16, 1);
  109. glDisable(GL_BLEND);
  110. glPopMatrix();
  111. glPushMatrix();
  112. glTranslatef(0.75, 1.0, 1.0);
  113. glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, blue_mat );
  114. gluSphere(qobj, 1.0, 20, 20);
  115. glPopMatrix();
  116. glPopMatrix();
  117. gluDeleteQuadric(qobj);
  118. {
  119. GLint r, g, b, a;
  120. glGetIntegerv(GL_RED_BITS, &r);
  121. glGetIntegerv(GL_GREEN_BITS, &g);
  122. glGetIntegerv(GL_BLUE_BITS, &b);
  123. glGetIntegerv(GL_ALPHA_BITS, &a);
  124. printf("channel sizes: %d %d %d %d\n", r, g, b, a);
  125. }
  126. }
  127. static void
  128. write_targa(const char *filename, const GLushort *buffer, int width, int height)
  129. {
  130. FILE *f = fopen( filename, "w" );
  131. if (f) {
  132. int i, x, y;
  133. const GLushort *ptr = buffer;
  134. printf ("osdemo, writing tga file \n");
  135. fputc (0x00, f); /* ID Length, 0 => No ID */
  136. fputc (0x00, f); /* Color Map Type, 0 => No color map included */
  137. fputc (0x02, f); /* Image Type, 2 => Uncompressed, True-color Image */
  138. fputc (0x00, f); /* Next five bytes are about the color map entries */
  139. fputc (0x00, f); /* 2 bytes Index, 2 bytes length, 1 byte size */
  140. fputc (0x00, f);
  141. fputc (0x00, f);
  142. fputc (0x00, f);
  143. fputc (0x00, f); /* X-origin of Image */
  144. fputc (0x00, f);
  145. fputc (0x00, f); /* Y-origin of Image */
  146. fputc (0x00, f);
  147. fputc (WIDTH & 0xff, f); /* Image Width */
  148. fputc ((WIDTH>>8) & 0xff, f);
  149. fputc (HEIGHT & 0xff, f); /* Image Height */
  150. fputc ((HEIGHT>>8) & 0xff, f);
  151. fputc (0x18, f); /* Pixel Depth, 0x18 => 24 Bits */
  152. fputc (0x20, f); /* Image Descriptor */
  153. fclose(f);
  154. f = fopen( filename, "ab" ); /* reopen in binary append mode */
  155. for (y=height-1; y>=0; y--) {
  156. for (x=0; x<width; x++) {
  157. i = (y*width + x) * 4;
  158. fputc(ptr[i+2] >> 8, f); /* write blue */
  159. fputc(ptr[i+1] >> 8, f); /* write green */
  160. fputc(ptr[i] >> 8, f); /* write red */
  161. }
  162. }
  163. }
  164. }
  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. int main( int argc, char *argv[] )
  210. {
  211. void *buffer;
  212. /* Create an RGBA-mode context */
  213. #if OSMESA_MAJOR_VERSION * 100 + OSMESA_MINOR_VERSION >= 305
  214. /* specify Z, stencil, accum sizes */
  215. OSMesaContext ctx = OSMesaCreateContextExt( GL_RGBA, 16, 0, 0, NULL );
  216. #else
  217. OSMesaContext ctx = OSMesaCreateContext( GL_RGBA, NULL );
  218. #endif
  219. if (!ctx) {
  220. printf("OSMesaCreateContext failed!\n");
  221. return 0;
  222. }
  223. /* Allocate the image buffer */
  224. buffer = malloc( WIDTH * HEIGHT * 4 * sizeof(GLushort));
  225. if (!buffer) {
  226. printf("Alloc image buffer failed!\n");
  227. return 0;
  228. }
  229. /* Bind the buffer to the context and make it current */
  230. if (!OSMesaMakeCurrent( ctx, buffer, GL_UNSIGNED_SHORT, WIDTH, HEIGHT )) {
  231. printf("OSMesaMakeCurrent failed!\n");
  232. return 0;
  233. }
  234. render_image();
  235. if (argc>1) {
  236. #ifdef SAVE_TARGA
  237. write_targa(argv[1], buffer, WIDTH, HEIGHT);
  238. #else
  239. write_ppm(argv[1], buffer, WIDTH, HEIGHT);
  240. #endif
  241. }
  242. else {
  243. printf("Specify a filename if you want to make an image file\n");
  244. }
  245. printf("all done\n");
  246. /* free the image buffer */
  247. free( buffer );
  248. /* destroy the context */
  249. OSMesaDestroyContext( ctx );
  250. return 0;
  251. }