1 /* $Id: osdemo.c,v 1.2 2000/01/15 06:11:33 rjfrank 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.2 2000/01/15 06:11:33 rjfrank
23 * Added test for the occlusion test code.
25 * Revision 1.1.1.1 1999/08/19 00:55:40 jtg
28 * Revision 3.0 1998/02/14 18:42:29 brianp
37 #include "GL/osmesa.h"
47 static void render_image( void )
49 GLfloat light_ambient
[] = { 0.0, 0.0, 0.0, 1.0 };
50 GLfloat light_diffuse
[] = { 1.0, 1.0, 1.0, 1.0 };
51 GLfloat light_specular
[] = { 1.0, 1.0, 1.0, 1.0 };
52 GLfloat light_position
[] = { 1.0, 1.0, 1.0, 0.0 };
53 GLfloat red_mat
[] = { 1.0, 0.2, 0.2, 1.0 };
54 GLfloat green_mat
[] = { 0.2, 1.0, 0.2, 1.0 };
55 GLfloat blue_mat
[] = { 0.2, 0.2, 1.0, 1.0 };
58 glLightfv(GL_LIGHT0
, GL_AMBIENT
, light_ambient
);
59 glLightfv(GL_LIGHT0
, GL_DIFFUSE
, light_diffuse
);
60 glLightfv(GL_LIGHT0
, GL_SPECULAR
, light_specular
);
61 glLightfv(GL_LIGHT0
, GL_POSITION
, light_position
);
63 glEnable(GL_LIGHTING
);
65 glEnable(GL_DEPTH_TEST
);
67 glMatrixMode(GL_PROJECTION
);
69 glOrtho(-2.5, 2.5, -2.5, 2.5, -10.0, 10.0);
70 glMatrixMode(GL_MODELVIEW
);
72 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
75 glRotatef(20.0, 1.0, 0.0, 0.0);
78 glTranslatef(-0.75, 0.5, 0.0);
79 glRotatef(90.0, 1.0, 0.0, 0.0);
80 glMaterialfv( GL_FRONT_AND_BACK
, GL_AMBIENT_AND_DIFFUSE
, red_mat
);
81 glutSolidTorus(0.275, 0.85, 20, 20);
85 glTranslatef(-0.75, -0.5, 0.0);
86 glRotatef(270.0, 1.0, 0.0, 0.0);
87 glMaterialfv( GL_FRONT_AND_BACK
, GL_AMBIENT_AND_DIFFUSE
, green_mat
);
88 glutSolidCone(1.0, 2.0, 16, 1);
91 #ifdef OSMESA_OCCLUSION_TEST_RESULT_HP
94 OSMesaGetBooleanv(OSMESA_OCCLUSION_TEST_RESULT_HP
,&bRet
);
95 glDepthMask(GL_FALSE
);
96 glColorMask(GL_FALSE
,GL_FALSE
,GL_FALSE
,GL_FALSE
);
99 glTranslatef(0.75, 0.0, -1.0);
100 glMaterialfv( GL_FRONT_AND_BACK
, GL_AMBIENT_AND_DIFFUSE
, blue_mat
);
101 glutSolidSphere(1.0, 20, 20);
104 OSMesaGetBooleanv(OSMESA_OCCLUSION_TEST_RESULT_HP
,&bRet
);
105 printf("Occlusion test 1 (result should be 1): %d\n",bRet
);
107 glDepthMask(GL_TRUE
);
108 glColorMask(GL_TRUE
,GL_TRUE
,GL_TRUE
,GL_TRUE
);
113 glTranslatef(0.75, 0.0, -1.0);
114 glMaterialfv( GL_FRONT_AND_BACK
, GL_AMBIENT_AND_DIFFUSE
, blue_mat
);
115 glutSolidSphere(1.0, 20, 20);
118 #ifdef OSMESA_OCCLUSION_TEST_RESULT_HP
122 OSMesaGetBooleanv(OSMESA_OCCLUSION_TEST_RESULT_HP
,&bRet
);
123 glDepthMask(GL_FALSE
);
124 glColorMask(GL_FALSE
,GL_FALSE
,GL_FALSE
,GL_FALSE
);
126 /* draw a sphere inside the previous sphere */
128 glTranslatef(0.75, 0.0, -1.0);
129 glMaterialfv( GL_FRONT_AND_BACK
, GL_AMBIENT_AND_DIFFUSE
, blue_mat
);
130 glutSolidSphere(0.5, 20, 20);
133 OSMesaGetBooleanv(OSMESA_OCCLUSION_TEST_RESULT_HP
,&bRet
);
134 printf("Occlusion test 2 (result should be 0): %d\n",bRet
);
136 glDepthMask(GL_TRUE
);
137 glColorMask(GL_TRUE
,GL_TRUE
,GL_TRUE
,GL_TRUE
);
146 int main( int argc
, char *argv
[] )
151 /* Create an RGBA-mode context */
152 ctx
= OSMesaCreateContext( GL_RGBA
, NULL
);
154 /* Allocate the image buffer */
155 buffer
= malloc( WIDTH
* HEIGHT
* 4 );
157 /* Bind the buffer to the context and make it current */
158 OSMesaMakeCurrent( ctx
, buffer
, GL_UNSIGNED_BYTE
, WIDTH
, HEIGHT
);
164 FILE *f
= fopen( argv
[1], "w" );
167 GLubyte
*ptr
= (GLubyte
*) buffer
;
171 fprintf(f
,"# ppm-file created by %s\n", argv
[0]);
172 fprintf(f
,"%i %i\n", WIDTH
,HEIGHT
);
175 f
= fopen( argv
[1], "ab" ); /* reopen in binary append mode */
176 for (y
=HEIGHT
-1; y
>=0; y
--) {
177 for (x
=0; x
<WIDTH
; x
++) {
178 i
= (y
*WIDTH
+ x
) * 4;
179 fputc(ptr
[i
], f
); /* write red */
180 fputc(ptr
[i
+1], f
); /* write green */
181 fputc(ptr
[i
+2], f
); /* write blue */
187 fprintf(f
,"# ascii ppm file created by %s\n", argv
[0]);
188 fprintf(f
,"%i %i\n", WIDTH
, HEIGHT
);
190 for (y
=HEIGHT
-1; y
>=0; y
--) {
191 for (x
=0; x
<WIDTH
; x
++) {
192 i
= (y
*WIDTH
+ x
) * 4;
193 fprintf(f
, " %3d %3d %3d", ptr
[i
], ptr
[i
+1], ptr
[i
+2]);
195 if (counter
% 5 == 0)
204 printf("Specify a filename if you want to make a ppm file\n");
207 printf("all done\n");
209 /* free the image buffer */
212 /* destroy the context */
213 OSMesaDestroyContext( ctx
);