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.

osdemo32.c 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. * Demo of off-screen Mesa rendering with 32-bit float color channels.
  3. * This requires the libOSMesa32.so library.
  4. *
  5. * Compile with something like this:
  6. *
  7. * gcc osdemo32.c -I../../include -L../../lib -lglut -lGLU -lOSMesa32 -lm -o osdemo32
  8. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include "GL/osmesa.h"
  12. #include "GL/glut.h"
  13. #define SAVE_TARGA
  14. #define WIDTH 400
  15. #define HEIGHT 400
  16. static void render_image( void )
  17. {
  18. GLfloat light_ambient[] = { 0.0, 0.0, 0.0, 1.0 };
  19. GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
  20. GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
  21. GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
  22. GLfloat red_mat[] = { 1.0, 0.2, 0.2, 1.0 };
  23. GLfloat green_mat[] = { 0.2, 1.0, 0.2, 0.5 };
  24. GLfloat blue_mat[] = { 0.2, 0.2, 1.0, 1.0 };
  25. GLfloat white_mat[] = { 1.0, 1.0, 1.0, 1.0 };
  26. GLfloat purple_mat[] = { 1.0, 0.2, 1.0, 1.0 };
  27. GLUquadricObj *qobj = gluNewQuadric();
  28. glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
  29. glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
  30. glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
  31. glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  32. glEnable(GL_LIGHTING);
  33. glEnable(GL_LIGHT0);
  34. glEnable(GL_DEPTH_TEST);
  35. glMatrixMode(GL_PROJECTION);
  36. glLoadIdentity();
  37. glOrtho(-2.5, 2.5, -2.5, 2.5, -10.0, 10.0);
  38. glMatrixMode(GL_MODELVIEW);
  39. glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  40. glPushMatrix();
  41. glRotatef(20.0, 1.0, 0.0, 0.0);
  42. #if 0
  43. glPushMatrix();
  44. glTranslatef(-0.75, 0.5, 0.0);
  45. glRotatef(90.0, 1.0, 0.0, 0.0);
  46. glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, red_mat );
  47. glutSolidTorus(0.275, 0.85, 20, 20);
  48. glPopMatrix();
  49. #endif
  50. /* red square */
  51. glPushMatrix();
  52. glTranslatef(0.0, -0.5, 0.0);
  53. glRotatef(90, 1, 0.5, 0);
  54. glScalef(3, 3, 3);
  55. glDisable(GL_LIGHTING);
  56. glColor4f(1, 0, 0, 0.5);
  57. glBegin(GL_POLYGON);
  58. glVertex2f(-1, -1);
  59. glVertex2f( 1, -1);
  60. glVertex2f( 1, 1);
  61. glVertex2f(-1, 1);
  62. glEnd();
  63. glEnable(GL_LIGHTING);
  64. glPopMatrix();
  65. #if 0
  66. /* green square */
  67. glPushMatrix();
  68. glTranslatef(0.0, 0.5, 0.1);
  69. glDisable(GL_LIGHTING);
  70. glColor3f(0, 1, 0);
  71. glBegin(GL_POLYGON);
  72. glVertex2f(-1, -1);
  73. glVertex2f( 1, -1);
  74. glVertex2f( 1, 1);
  75. glVertex2f(-1, 1);
  76. glEnd();
  77. glEnable(GL_LIGHTING);
  78. glPopMatrix();
  79. /* blue square */
  80. glPushMatrix();
  81. glTranslatef(0.75, 0.5, 0.3);
  82. glDisable(GL_LIGHTING);
  83. glColor3f(0, 0, 0.5);
  84. glBegin(GL_POLYGON);
  85. glVertex2f(-1, -1);
  86. glVertex2f( 1, -1);
  87. glVertex2f( 1, 1);
  88. glVertex2f(-1, 1);
  89. glEnd();
  90. glEnable(GL_LIGHTING);
  91. glPopMatrix();
  92. #endif
  93. glPushMatrix();
  94. glTranslatef(-0.75, -0.5, 0.0);
  95. glRotatef(270.0, 1.0, 0.0, 0.0);
  96. glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, green_mat );
  97. glColor4f(0,1,0,0.5);
  98. glEnable(GL_BLEND);
  99. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  100. gluCylinder(qobj, 1.0, 0.0, 2.0, 16, 1);
  101. glDisable(GL_BLEND);
  102. glPopMatrix();
  103. glPushMatrix();
  104. glTranslatef(0.75, 1.0, 1.0);
  105. glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, blue_mat );
  106. gluSphere(qobj, 1.0, 20, 20);
  107. glPopMatrix();
  108. glPopMatrix();
  109. /* This is very important!!!
  110. * Make sure buffered commands are finished!!!
  111. */
  112. glFinish();
  113. gluDeleteQuadric(qobj);
  114. {
  115. GLint r, g, b, a;
  116. glGetIntegerv(GL_RED_BITS, &r);
  117. glGetIntegerv(GL_GREEN_BITS, &g);
  118. glGetIntegerv(GL_BLUE_BITS, &b);
  119. glGetIntegerv(GL_ALPHA_BITS, &a);
  120. printf("channel sizes: %d %d %d %d\n", r, g, b, a);
  121. }
  122. }
  123. static void
  124. write_targa(const char *filename, const GLfloat *buffer, int width, int height)
  125. {
  126. FILE *f = fopen( filename, "w" );
  127. if (f) {
  128. int i, x, y;
  129. const GLfloat *ptr = buffer;
  130. printf ("osdemo, writing tga file \n");
  131. fputc (0x00, f); /* ID Length, 0 => No ID */
  132. fputc (0x00, f); /* Color Map Type, 0 => No color map included */
  133. fputc (0x02, f); /* Image Type, 2 => Uncompressed, True-color Image */
  134. fputc (0x00, f); /* Next five bytes are about the color map entries */
  135. fputc (0x00, f); /* 2 bytes Index, 2 bytes length, 1 byte size */
  136. fputc (0x00, f);
  137. fputc (0x00, f);
  138. fputc (0x00, f);
  139. fputc (0x00, f); /* X-origin of Image */
  140. fputc (0x00, f);
  141. fputc (0x00, f); /* Y-origin of Image */
  142. fputc (0x00, f);
  143. fputc (WIDTH & 0xff, f); /* Image Width */
  144. fputc ((WIDTH>>8) & 0xff, f);
  145. fputc (HEIGHT & 0xff, f); /* Image Height */
  146. fputc ((HEIGHT>>8) & 0xff, f);
  147. fputc (0x18, f); /* Pixel Depth, 0x18 => 24 Bits */
  148. fputc (0x20, f); /* Image Descriptor */
  149. fclose(f);
  150. f = fopen( filename, "ab" ); /* reopen in binary append mode */
  151. for (y=height-1; y>=0; y--) {
  152. for (x=0; x<width; x++) {
  153. int r, g, b;
  154. i = (y*width + x) * 4;
  155. r = (int) (ptr[i+0] * 255.0);
  156. g = (int) (ptr[i+1] * 255.0);
  157. b = (int) (ptr[i+2] * 255.0);
  158. if (r > 255) r = 255;
  159. if (g > 255) g = 255;
  160. if (b > 255) b = 255;
  161. fputc(b, f); /* write blue */
  162. fputc(g, f); /* write green */
  163. fputc(r, f); /* write red */
  164. }
  165. }
  166. }
  167. }
  168. static void
  169. write_ppm(const char *filename, const GLfloat *buffer, int width, int height)
  170. {
  171. const int binary = 0;
  172. FILE *f = fopen( filename, "w" );
  173. if (f) {
  174. int i, x, y;
  175. const GLfloat *ptr = buffer;
  176. if (binary) {
  177. fprintf(f,"P6\n");
  178. fprintf(f,"# ppm-file created by osdemo.c\n");
  179. fprintf(f,"%i %i\n", width,height);
  180. fprintf(f,"255\n");
  181. fclose(f);
  182. f = fopen( filename, "ab" ); /* reopen in binary append mode */
  183. for (y=height-1; y>=0; y--) {
  184. for (x=0; x<width; x++) {
  185. int r, g, b;
  186. i = (y*width + x) * 4;
  187. r = (int) (ptr[i+0] * 255.0);
  188. g = (int) (ptr[i+1] * 255.0);
  189. b = (int) (ptr[i+2] * 255.0);
  190. if (r > 255) r = 255;
  191. if (g > 255) g = 255;
  192. if (b > 255) b = 255;
  193. fputc(r, f); /* write red */
  194. fputc(g, f); /* write green */
  195. fputc(b, f); /* write blue */
  196. }
  197. }
  198. }
  199. else {
  200. /*ASCII*/
  201. int counter = 0;
  202. fprintf(f,"P3\n");
  203. fprintf(f,"# ascii ppm file created by osdemo.c\n");
  204. fprintf(f,"%i %i\n", width, height);
  205. fprintf(f,"255\n");
  206. for (y=height-1; y>=0; y--) {
  207. for (x=0; x<width; x++) {
  208. int r, g, b;
  209. i = (y*width + x) * 4;
  210. r = (int) (ptr[i+0] * 255.0);
  211. g = (int) (ptr[i+1] * 255.0);
  212. b = (int) (ptr[i+2] * 255.0);
  213. if (r > 255) r = 255;
  214. if (g > 255) g = 255;
  215. if (b > 255) b = 255;
  216. fprintf(f, " %3d %3d %3d", r, g, b);
  217. counter++;
  218. if (counter % 5 == 0)
  219. fprintf(f, "\n");
  220. }
  221. }
  222. }
  223. fclose(f);
  224. }
  225. }
  226. int main( int argc, char *argv[] )
  227. {
  228. GLfloat *buffer;
  229. /* Create an RGBA-mode context */
  230. #if OSMESA_MAJOR_VERSION * 100 + OSMESA_MINOR_VERSION >= 305
  231. /* specify Z, stencil, accum sizes */
  232. OSMesaContext ctx = OSMesaCreateContextExt( GL_RGBA, 16, 0, 0, NULL );
  233. #else
  234. OSMesaContext ctx = OSMesaCreateContext( GL_RGBA, NULL );
  235. #endif
  236. if (!ctx) {
  237. printf("OSMesaCreateContext failed!\n");
  238. return 0;
  239. }
  240. /* Allocate the image buffer */
  241. buffer = (GLfloat *) malloc( WIDTH * HEIGHT * 4 * sizeof(GLfloat));
  242. if (!buffer) {
  243. printf("Alloc image buffer failed!\n");
  244. return 0;
  245. }
  246. /* Bind the buffer to the context and make it current */
  247. if (!OSMesaMakeCurrent( ctx, buffer, GL_FLOAT, WIDTH, HEIGHT )) {
  248. printf("OSMesaMakeCurrent failed!\n");
  249. return 0;
  250. }
  251. render_image();
  252. if (argc>1) {
  253. #ifdef SAVE_TARGA
  254. write_targa(argv[1], buffer, WIDTH, HEIGHT);
  255. #else
  256. write_ppm(argv[1], 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. }