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

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