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