1 /* $Id: osdemo16.c,v 1.1 2001/03/08 15:24:18 brianp Exp $ */
4 * Demo of off-screen Mesa rendering
6 * See Mesa/include/GL/osmesa.h for documentation of the OSMesa functions.
8 * If you want to render BIG images you'll probably have to increase
9 * MAX_WIDTH and MAX_HEIGHT in src/config.h.
11 * This program is in the public domain.
15 * PPM output provided by Joerg Schmalzl.
16 * ASCII PPM output added by Brian Paul.
22 #include "GL/osmesa.h"
34 static void render_image( void )
36 GLfloat light_ambient
[] = { 0.0, 0.0, 0.0, 1.0 };
37 GLfloat light_diffuse
[] = { 1.0, 1.0, 1.0, 1.0 };
38 GLfloat light_specular
[] = { 1.0, 1.0, 1.0, 1.0 };
39 GLfloat light_position
[] = { 1.0, 1.0, 1.0, 0.0 };
40 GLfloat red_mat
[] = { 1.0, 0.2, 0.2, 1.0 };
41 GLfloat green_mat
[] = { 0.2, 1.0, 0.2, 0.5 };
42 GLfloat blue_mat
[] = { 0.2, 0.2, 1.0, 1.0 };
43 GLfloat white_mat
[] = { 1.0, 1.0, 1.0, 1.0 };
44 GLfloat purple_mat
[] = { 1.0, 0.2, 1.0, 1.0 };
45 GLUquadricObj
*qobj
= gluNewQuadric();
47 glLightfv(GL_LIGHT0
, GL_AMBIENT
, light_ambient
);
48 glLightfv(GL_LIGHT0
, GL_DIFFUSE
, light_diffuse
);
49 glLightfv(GL_LIGHT0
, GL_SPECULAR
, light_specular
);
50 glLightfv(GL_LIGHT0
, GL_POSITION
, light_position
);
52 glEnable(GL_LIGHTING
);
54 glEnable(GL_DEPTH_TEST
);
56 glMatrixMode(GL_PROJECTION
);
58 glOrtho(-2.5, 2.5, -2.5, 2.5, -10.0, 10.0);
59 glMatrixMode(GL_MODELVIEW
);
61 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
64 glRotatef(20.0, 1.0, 0.0, 0.0);
68 glTranslatef(-0.75, 0.5, 0.0);
69 glRotatef(90.0, 1.0, 0.0, 0.0);
70 glMaterialfv( GL_FRONT_AND_BACK
, GL_AMBIENT_AND_DIFFUSE
, red_mat
);
71 glutSolidTorus(0.275, 0.85, 20, 20);
77 glTranslatef(0.0, -0.5, 0.0);
78 glRotatef(90, 1, 0.5, 0);
80 glDisable(GL_LIGHTING
);
81 glColor4f(1, 0, 0, 0.5);
88 glEnable(GL_LIGHTING
);
94 glTranslatef(0.0, 0.5, 0.1);
95 glDisable(GL_LIGHTING
);
103 glEnable(GL_LIGHTING
);
108 glTranslatef(0.75, 0.5, 0.3);
109 glDisable(GL_LIGHTING
);
110 glColor3f(0, 0, 0.5);
117 glEnable(GL_LIGHTING
);
121 glTranslatef(-0.75, -0.5, 0.0);
122 glRotatef(270.0, 1.0, 0.0, 0.0);
123 glMaterialfv( GL_FRONT_AND_BACK
, GL_AMBIENT_AND_DIFFUSE
, green_mat
);
124 glColor4f(0,1,0,0.5);
126 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
127 gluCylinder(qobj
, 1.0, 0.0, 2.0, 16, 1);
132 glTranslatef(0.75, 1.0, 1.0);
133 glMaterialfv( GL_FRONT_AND_BACK
, GL_AMBIENT_AND_DIFFUSE
, blue_mat
);
134 gluSphere(qobj
, 1.0, 20, 20);
139 gluDeleteQuadric(qobj
);
143 glGetIntegerv(GL_RED_BITS
, &r
);
144 glGetIntegerv(GL_GREEN_BITS
, &g
);
145 glGetIntegerv(GL_BLUE_BITS
, &b
);
146 glGetIntegerv(GL_ALPHA_BITS
, &a
);
147 printf("channel sizes: %d %d %d %d\n", r
, g
, b
, a
);
154 write_targa(const char *filename
, const GLushort
*buffer
, int width
, int height
)
156 FILE *f
= fopen( filename
, "w" );
159 const GLushort
*ptr
= buffer
;
160 printf ("osdemo, writing tga file \n");
161 fputc (0x00, f
); /* ID Length, 0 => No ID */
162 fputc (0x00, f
); /* Color Map Type, 0 => No color map included */
163 fputc (0x02, f
); /* Image Type, 2 => Uncompressed, True-color Image */
164 fputc (0x00, f
); /* Next five bytes are about the color map entries */
165 fputc (0x00, f
); /* 2 bytes Index, 2 bytes length, 1 byte size */
169 fputc (0x00, f
); /* X-origin of Image */
171 fputc (0x00, f
); /* Y-origin of Image */
173 fputc (WIDTH
& 0xff, f
); /* Image Width */
174 fputc ((WIDTH
>>8) & 0xff, f
);
175 fputc (HEIGHT
& 0xff, f
); /* Image Height */
176 fputc ((HEIGHT
>>8) & 0xff, f
);
177 fputc (0x18, f
); /* Pixel Depth, 0x18 => 24 Bits */
178 fputc (0x20, f
); /* Image Descriptor */
180 f
= fopen( filename
, "ab" ); /* reopen in binary append mode */
181 for (y
=height
-1; y
>=0; y
--) {
182 for (x
=0; x
<width
; x
++) {
183 i
= (y
*width
+ x
) * 4;
184 fputc(ptr
[i
+2] >> 8, f
); /* write blue */
185 fputc(ptr
[i
+1] >> 8, f
); /* write green */
186 fputc(ptr
[i
] >> 8, f
); /* write red */
194 write_ppm(const char *filename
, const GLubyte
*buffer
, int width
, int height
)
196 const int binary
= 0;
197 FILE *f
= fopen( filename
, "w" );
200 const GLubyte
*ptr
= buffer
;
203 fprintf(f
,"# ppm-file created by osdemo.c\n");
204 fprintf(f
,"%i %i\n", width
,height
);
207 f
= fopen( filename
, "ab" ); /* reopen in binary append mode */
208 for (y
=height
-1; y
>=0; y
--) {
209 for (x
=0; x
<width
; x
++) {
210 i
= (y
*width
+ x
) * 4;
211 fputc(ptr
[i
], f
); /* write red */
212 fputc(ptr
[i
+1], f
); /* write green */
213 fputc(ptr
[i
+2], f
); /* write blue */
221 fprintf(f
,"# ascii ppm file created by osdemo.c\n");
222 fprintf(f
,"%i %i\n", width
, height
);
224 for (y
=height
-1; y
>=0; y
--) {
225 for (x
=0; x
<width
; x
++) {
226 i
= (y
*width
+ x
) * 4;
227 fprintf(f
, " %3d %3d %3d", ptr
[i
], ptr
[i
+1], ptr
[i
+2]);
229 if (counter
% 5 == 0)
240 int main( int argc
, char *argv
[] )
244 /* Create an RGBA-mode context */
245 #if OSMESA_MAJOR_VERSION * 100 + OSMESA_MINOR_VERSION >= 305
246 /* specify Z, stencil, accum sizes */
247 OSMesaContext ctx
= OSMesaCreateContextExt( GL_RGBA
, 16, 0, 0, NULL
);
249 OSMesaContext ctx
= OSMesaCreateContext( GL_RGBA
, NULL
);
252 printf("OSMesaCreateContext failed!\n");
256 /* Allocate the image buffer */
257 buffer
= malloc( WIDTH
* HEIGHT
* 4 * sizeof(GLushort
));
259 printf("Alloc image buffer failed!\n");
263 /* Bind the buffer to the context and make it current */
264 if (!OSMesaMakeCurrent( ctx
, buffer
, GL_UNSIGNED_SHORT
, WIDTH
, HEIGHT
)) {
265 printf("OSMesaMakeCurrent failed!\n");
273 write_targa(argv
[1], buffer
, WIDTH
, HEIGHT
);
275 write_ppm(argv
[1], buffer
, WIDTH
, HEIGHT
);
279 printf("Specify a filename if you want to make an image file\n");
282 printf("all done\n");
284 /* free the image buffer */
287 /* destroy the context */
288 OSMesaDestroyContext( ctx
);