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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * Demo of off-screen Mesa rendering with 16-bit color channels.
  3. * This requires the libOSMesa16.so library.
  4. *
  5. * Compile with something like this:
  6. *
  7. * gcc osdemo16.c -I../../include -L../../lib -lglut -lGLU -lOSMesa16 -lm -o osdemo16
  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 GLushort *buffer, int width, int height)
  125. {
  126. FILE *f = fopen( filename, "w" );
  127. if (f) {
  128. int i, x, y;
  129. const GLushort *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. i = (y*width + x) * 4;
  154. /* just write 8 high bits */
  155. fputc(ptr[i+2] >> 8, f); /* write blue */
  156. fputc(ptr[i+1] >> 8, f); /* write green */
  157. fputc(ptr[i] >> 8, f); /* write red */
  158. }
  159. }
  160. }
  161. }
  162. static void
  163. write_ppm(const char *filename, const GLushort *buffer, int width, int height)
  164. {
  165. const int binary = 0;
  166. FILE *f = fopen( filename, "w" );
  167. if (f) {
  168. int i, x, y;
  169. const GLushort *ptr = buffer;
  170. if (binary) {
  171. fprintf(f,"P6\n");
  172. fprintf(f,"# ppm-file created by osdemo.c\n");
  173. fprintf(f,"%i %i\n", width,height);
  174. fprintf(f,"255\n");
  175. fclose(f);
  176. f = fopen( filename, "ab" ); /* reopen in binary append mode */
  177. for (y=height-1; y>=0; y--) {
  178. for (x=0; x<width; x++) {
  179. i = (y*width + x) * 4;
  180. /* just write 8 high bits */
  181. fputc(ptr[i] >> 8, f); /* write red */
  182. fputc(ptr[i+1] >> 8, f); /* write green */
  183. fputc(ptr[i+2] >> 8, f); /* write blue */
  184. }
  185. }
  186. }
  187. else {
  188. /*ASCII*/
  189. int counter = 0;
  190. fprintf(f,"P3\n");
  191. fprintf(f,"# ascii ppm file created by osdemo.c\n");
  192. fprintf(f,"%i %i\n", width, height);
  193. fprintf(f,"255\n");
  194. for (y=height-1; y>=0; y--) {
  195. for (x=0; x<width; x++) {
  196. i = (y*width + x) * 4;
  197. /* just write 8 high bits */
  198. fprintf(f, " %3d %3d %3d", ptr[i] >> 8, ptr[i+1] >> 8, ptr[i+2] >> 8);
  199. counter++;
  200. if (counter % 5 == 0)
  201. fprintf(f, "\n");
  202. }
  203. }
  204. }
  205. fclose(f);
  206. }
  207. }
  208. int main( int argc, char *argv[] )
  209. {
  210. GLushort *buffer;
  211. /* Create an RGBA-mode context */
  212. #if OSMESA_MAJOR_VERSION * 100 + OSMESA_MINOR_VERSION >= 305
  213. /* specify Z, stencil, accum sizes */
  214. OSMesaContext ctx = OSMesaCreateContextExt( GL_RGBA, 16, 0, 0, NULL );
  215. #else
  216. OSMesaContext ctx = OSMesaCreateContext( GL_RGBA, NULL );
  217. #endif
  218. if (!ctx) {
  219. printf("OSMesaCreateContext failed!\n");
  220. return 0;
  221. }
  222. /* Allocate the image buffer */
  223. buffer = (GLushort *) malloc( WIDTH * HEIGHT * 4 * sizeof(GLushort));
  224. if (!buffer) {
  225. printf("Alloc image buffer failed!\n");
  226. return 0;
  227. }
  228. /* Bind the buffer to the context and make it current */
  229. if (!OSMesaMakeCurrent( ctx, buffer, GL_UNSIGNED_SHORT, WIDTH, HEIGHT )) {
  230. printf("OSMesaMakeCurrent failed!\n");
  231. return 0;
  232. }
  233. render_image();
  234. if (argc>1) {
  235. #ifdef SAVE_TARGA
  236. write_targa(argv[1], buffer, WIDTH, HEIGHT);
  237. #else
  238. write_ppm(argv[1], buffer, WIDTH, HEIGHT);
  239. #endif
  240. }
  241. else {
  242. printf("Specify a filename if you want to make an image file\n");
  243. }
  244. printf("all done\n");
  245. /* free the image buffer */
  246. free( buffer );
  247. /* destroy the context */
  248. OSMesaDestroyContext( ctx );
  249. return 0;
  250. }