1 /* $Id: osdemo.c,v 1.1 1999/08/19 00:55:40 jtg 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 * Revision 1.1 1999/08/19 00:55:40 jtg
25 * Revision 3.0 1998/02/14 18:42:29 brianp
34 #include "GL/osmesa.h"
44 static void render_image( void )
46 GLfloat light_ambient
[] = { 0.0, 0.0, 0.0, 1.0 };
47 GLfloat light_diffuse
[] = { 1.0, 1.0, 1.0, 1.0 };
48 GLfloat light_specular
[] = { 1.0, 1.0, 1.0, 1.0 };
49 GLfloat light_position
[] = { 1.0, 1.0, 1.0, 0.0 };
50 GLfloat red_mat
[] = { 1.0, 0.2, 0.2, 1.0 };
51 GLfloat green_mat
[] = { 0.2, 1.0, 0.2, 1.0 };
52 GLfloat blue_mat
[] = { 0.2, 0.2, 1.0, 1.0 };
55 glLightfv(GL_LIGHT0
, GL_AMBIENT
, light_ambient
);
56 glLightfv(GL_LIGHT0
, GL_DIFFUSE
, light_diffuse
);
57 glLightfv(GL_LIGHT0
, GL_SPECULAR
, light_specular
);
58 glLightfv(GL_LIGHT0
, GL_POSITION
, light_position
);
60 glEnable(GL_LIGHTING
);
62 glEnable(GL_DEPTH_TEST
);
64 glMatrixMode(GL_PROJECTION
);
66 glOrtho(-2.5, 2.5, -2.5, 2.5, -10.0, 10.0);
67 glMatrixMode(GL_MODELVIEW
);
69 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
72 glRotatef(20.0, 1.0, 0.0, 0.0);
75 glTranslatef(-0.75, 0.5, 0.0);
76 glRotatef(90.0, 1.0, 0.0, 0.0);
77 glMaterialfv( GL_FRONT_AND_BACK
, GL_AMBIENT_AND_DIFFUSE
, red_mat
);
78 glutSolidTorus(0.275, 0.85, 20, 20);
82 glTranslatef(-0.75, -0.5, 0.0);
83 glRotatef(270.0, 1.0, 0.0, 0.0);
84 glMaterialfv( GL_FRONT_AND_BACK
, GL_AMBIENT_AND_DIFFUSE
, green_mat
);
85 glutSolidCone(1.0, 2.0, 16, 1);
89 glTranslatef(0.75, 0.0, -1.0);
90 glMaterialfv( GL_FRONT_AND_BACK
, GL_AMBIENT_AND_DIFFUSE
, blue_mat
);
91 glutSolidSphere(1.0, 20, 20);
99 int main( int argc
, char *argv
[] )
104 /* Create an RGBA-mode context */
105 ctx
= OSMesaCreateContext( GL_RGBA
, NULL
);
107 /* Allocate the image buffer */
108 buffer
= malloc( WIDTH
* HEIGHT
* 4 );
110 /* Bind the buffer to the context and make it current */
111 OSMesaMakeCurrent( ctx
, buffer
, GL_UNSIGNED_BYTE
, WIDTH
, HEIGHT
);
117 FILE *f
= fopen( argv
[1], "w" );
120 GLubyte
*ptr
= (GLubyte
*) buffer
;
124 fprintf(f
,"# ppm-file created by %s\n", argv
[0]);
125 fprintf(f
,"%i %i\n", WIDTH
,HEIGHT
);
128 f
= fopen( argv
[1], "ab" ); /* reopen in binary append mode */
129 for (y
=HEIGHT
-1; y
>=0; y
--) {
130 for (x
=0; x
<WIDTH
; x
++) {
131 i
= (y
*WIDTH
+ x
) * 4;
132 fputc(ptr
[i
], f
); /* write red */
133 fputc(ptr
[i
+1], f
); /* write green */
134 fputc(ptr
[i
+2], f
); /* write blue */
140 fprintf(f
,"# ascii ppm file created by %s\n", argv
[0]);
141 fprintf(f
,"%i %i\n", WIDTH
, HEIGHT
);
143 for (y
=HEIGHT
-1; y
>=0; y
--) {
144 for (x
=0; x
<WIDTH
; x
++) {
145 i
= (y
*WIDTH
+ x
) * 4;
146 fprintf(f
, " %3d %3d %3d", ptr
[i
], ptr
[i
+1], ptr
[i
+2]);
148 if (counter
% 5 == 0)
157 printf("Specify a filename if you want to make a ppm file\n");
160 printf("all done\n");
162 /* free the image buffer */
165 /* destroy the context */
166 OSMesaDestroyContext( ctx
);