1 /* $Id: osdemo32.c,v 1.1 2003/03/08 19:05:45 brianp Exp $ */
4 * Test OSMesa with GLfloat color channels.
10 #include "GL/osmesa.h"
22 static void render_image( void )
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();
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
);
40 glEnable(GL_LIGHTING
);
42 glEnable(GL_DEPTH_TEST
);
44 glMatrixMode(GL_PROJECTION
);
46 glOrtho(-2.5, 2.5, -2.5, 2.5, -10.0, 10.0);
47 glMatrixMode(GL_MODELVIEW
);
49 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
52 glRotatef(20.0, 1.0, 0.0, 0.0);
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);
65 glTranslatef(0.0, -0.5, 0.0);
66 glRotatef(90, 1, 0.5, 0);
68 glDisable(GL_LIGHTING
);
69 glColor4f(1, 0, 0, 0.5);
76 glEnable(GL_LIGHTING
);
82 glTranslatef(0.0, 0.5, 0.1);
83 glDisable(GL_LIGHTING
);
91 glEnable(GL_LIGHTING
);
96 glTranslatef(0.75, 0.5, 0.3);
97 glDisable(GL_LIGHTING
);
105 glEnable(GL_LIGHTING
);
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);
114 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
115 gluCylinder(qobj
, 1.0, 0.0, 2.0, 16, 1);
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);
127 gluDeleteQuadric(qobj
);
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
);
142 write_targa(const char *filename
, const GLfloat
*buffer
, int width
, int height
)
144 FILE *f
= fopen( filename
, "w" );
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 */
157 fputc (0x00, f
); /* X-origin of Image */
159 fputc (0x00, f
); /* Y-origin of Image */
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 */
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
++) {
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 */
189 write_ppm(const char *filename
, const GLubyte
*buffer
, int width
, int height
)
191 const int binary
= 0;
192 FILE *f
= fopen( filename
, "w" );
195 const GLubyte
*ptr
= buffer
;
198 fprintf(f
,"# ppm-file created by osdemo.c\n");
199 fprintf(f
,"%i %i\n", width
,height
);
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 */
216 fprintf(f
,"# ascii ppm file created by osdemo.c\n");
217 fprintf(f
,"%i %i\n", width
, height
);
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]);
224 if (counter
% 5 == 0)
235 int main( int argc
, char *argv
[] )
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
);
244 OSMesaContext ctx
= OSMesaCreateContext( GL_RGBA
, NULL
);
247 printf("OSMesaCreateContext failed!\n");
251 /* Allocate the image buffer */
252 buffer
= malloc( WIDTH
* HEIGHT
* 4 * sizeof(GLfloat
));
254 printf("Alloc image buffer failed!\n");
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");
268 write_targa(argv
[1], buffer
, WIDTH
, HEIGHT
);
270 write_ppm(argv
[1], buffer
, WIDTH
, HEIGHT
);
274 printf("Specify a filename if you want to make an image file\n");
277 printf("all done\n");
279 /* free the image buffer */
282 /* destroy the context */
283 OSMesaDestroyContext( ctx
);