Added test for the occlusion test code.
[mesa.git] / progs / demos / osdemo.c
1 /* $Id: osdemo.c,v 1.2 2000/01/15 06:11:33 rjfrank Exp $ */
2
3 /*
4 * Demo of off-screen Mesa rendering
5 *
6 * See Mesa/include/GL/osmesa.h for documentation of the OSMesa functions.
7 *
8 * If you want to render BIG images you'll probably have to increase
9 * MAX_WIDTH and MAX_HEIGHT in src/config.h.
10 *
11 * This program is in the public domain.
12 *
13 * Brian Paul
14 *
15 * PPM output provided by Joerg Schmalzl.
16 * ASCII PPM output added by Brian Paul.
17 */
18
19
20 /*
21 * $Log: osdemo.c,v $
22 * Revision 1.2 2000/01/15 06:11:33 rjfrank
23 * Added test for the occlusion test code.
24 *
25 * Revision 1.1.1.1 1999/08/19 00:55:40 jtg
26 * Imported sources
27 *
28 * Revision 3.0 1998/02/14 18:42:29 brianp
29 * initial rev
30 *
31 */
32
33
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include "GL/osmesa.h"
38 #include "GL/glut.h"
39
40
41
42 #define WIDTH 400
43 #define HEIGHT 400
44
45
46
47 static void render_image( void )
48 {
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 };
56
57
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);
62
63 glEnable(GL_LIGHTING);
64 glEnable(GL_LIGHT0);
65 glEnable(GL_DEPTH_TEST);
66
67 glMatrixMode(GL_PROJECTION);
68 glLoadIdentity();
69 glOrtho(-2.5, 2.5, -2.5, 2.5, -10.0, 10.0);
70 glMatrixMode(GL_MODELVIEW);
71
72 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
73
74 glPushMatrix();
75 glRotatef(20.0, 1.0, 0.0, 0.0);
76
77 glPushMatrix();
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);
82 glPopMatrix();
83
84 glPushMatrix();
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);
89 glPopMatrix();
90
91 #ifdef OSMESA_OCCLUSION_TEST_RESULT_HP
92 {
93 GLboolean bRet;
94 OSMesaGetBooleanv(OSMESA_OCCLUSION_TEST_RESULT_HP,&bRet);
95 glDepthMask(GL_FALSE);
96 glColorMask(GL_FALSE,GL_FALSE,GL_FALSE,GL_FALSE);
97
98 glPushMatrix();
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);
102 glPopMatrix();
103
104 OSMesaGetBooleanv(OSMESA_OCCLUSION_TEST_RESULT_HP,&bRet);
105 printf("Occlusion test 1 (result should be 1): %d\n",bRet);
106
107 glDepthMask(GL_TRUE);
108 glColorMask(GL_TRUE,GL_TRUE,GL_TRUE,GL_TRUE);
109 }
110 #endif
111
112 glPushMatrix();
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);
116 glPopMatrix();
117
118 #ifdef OSMESA_OCCLUSION_TEST_RESULT_HP
119 {
120 GLboolean bRet;
121
122 OSMesaGetBooleanv(OSMESA_OCCLUSION_TEST_RESULT_HP,&bRet);
123 glDepthMask(GL_FALSE);
124 glColorMask(GL_FALSE,GL_FALSE,GL_FALSE,GL_FALSE);
125
126 /* draw a sphere inside the previous sphere */
127 glPushMatrix();
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);
131 glPopMatrix();
132
133 OSMesaGetBooleanv(OSMESA_OCCLUSION_TEST_RESULT_HP,&bRet);
134 printf("Occlusion test 2 (result should be 0): %d\n",bRet);
135
136 glDepthMask(GL_TRUE);
137 glColorMask(GL_TRUE,GL_TRUE,GL_TRUE,GL_TRUE);
138 }
139 #endif
140
141 glPopMatrix();
142 }
143
144
145
146 int main( int argc, char *argv[] )
147 {
148 OSMesaContext ctx;
149 void *buffer;
150
151 /* Create an RGBA-mode context */
152 ctx = OSMesaCreateContext( GL_RGBA, NULL );
153
154 /* Allocate the image buffer */
155 buffer = malloc( WIDTH * HEIGHT * 4 );
156
157 /* Bind the buffer to the context and make it current */
158 OSMesaMakeCurrent( ctx, buffer, GL_UNSIGNED_BYTE, WIDTH, HEIGHT );
159
160 render_image();
161
162 if (argc>1) {
163 /* write PPM file */
164 FILE *f = fopen( argv[1], "w" );
165 if (f) {
166 int i, x, y;
167 GLubyte *ptr = (GLubyte *) buffer;
168 #define BINARY 0
169 #if BINARY
170 fprintf(f,"P6\n");
171 fprintf(f,"# ppm-file created by %s\n", argv[0]);
172 fprintf(f,"%i %i\n", WIDTH,HEIGHT);
173 fprintf(f,"255\n");
174 fclose(f);
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 */
182 }
183 }
184 #else /*ASCII*/
185 int counter = 0;
186 fprintf(f,"P3\n");
187 fprintf(f,"# ascii ppm file created by %s\n", argv[0]);
188 fprintf(f,"%i %i\n", WIDTH, HEIGHT);
189 fprintf(f,"255\n");
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]);
194 counter++;
195 if (counter % 5 == 0)
196 fprintf(f, "\n");
197 }
198 }
199 #endif
200 fclose(f);
201 }
202 }
203 else {
204 printf("Specify a filename if you want to make a ppm file\n");
205 }
206
207 printf("all done\n");
208
209 /* free the image buffer */
210 free( buffer );
211
212 /* destroy the context */
213 OSMesaDestroyContext( ctx );
214
215 return 0;
216 }