remove the GL_HP_occlusion_test code
[mesa.git] / progs / osdemos / osdemo.c
1
2 /*
3 * Demo of off-screen Mesa rendering
4 *
5 * See Mesa/include/GL/osmesa.h for documentation of the OSMesa functions.
6 *
7 * If you want to render BIG images you'll probably have to increase
8 * MAX_WIDTH and MAX_HEIGHT in src/config.h.
9 *
10 * This program is in the public domain.
11 *
12 * Brian Paul
13 *
14 * PPM output provided by Joerg Schmalzl.
15 * ASCII PPM output added by Brian Paul.
16 *
17 * Usage: osdemo [-perf] [filename]
18 *
19 * -perf: Redraws the image 1000 times, displaying the FPS every 5 secs.
20 * filename: file to store the TGA or PPM output
21 */
22
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include "GL/osmesa.h"
28 #include "GL/glut.h"
29
30
31 #define SAVE_TARGA
32
33
34 #define WIDTH 400
35 #define HEIGHT 400
36
37 static GLint T0 = 0;
38 static GLint Frames = 0;
39 static int perf = 0;
40
41 static void render_image( void )
42 {
43 GLfloat light_ambient[] = { 0.0, 0.0, 0.0, 1.0 };
44 GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
45 GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
46 GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
47 GLfloat red_mat[] = { 1.0, 0.2, 0.2, 1.0 };
48 GLfloat green_mat[] = { 0.2, 1.0, 0.2, 1.0 };
49 GLfloat blue_mat[] = { 0.2, 0.2, 1.0, 1.0 };
50
51
52 glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
53 glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
54 glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
55 glLightfv(GL_LIGHT0, GL_POSITION, light_position);
56
57 glEnable(GL_LIGHTING);
58 glEnable(GL_LIGHT0);
59 glEnable(GL_DEPTH_TEST);
60
61 glMatrixMode(GL_PROJECTION);
62 glLoadIdentity();
63 glOrtho(-2.5, 2.5, -2.5, 2.5, -10.0, 10.0);
64 glMatrixMode(GL_MODELVIEW);
65
66 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
67
68 glPushMatrix();
69 glRotatef(20.0, 1.0, 0.0, 0.0);
70
71 glPushMatrix();
72 glTranslatef(-0.75, 0.5, 0.0);
73 glRotatef(90.0, 1.0, 0.0, 0.0);
74 glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, red_mat );
75 glutSolidTorus(0.275, 0.85, 20, 20);
76 glPopMatrix();
77
78 glPushMatrix();
79 glTranslatef(-0.75, -0.5, 0.0);
80 glRotatef(270.0, 1.0, 0.0, 0.0);
81 glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, green_mat );
82 glutSolidCone(1.0, 2.0, 16, 1);
83 glPopMatrix();
84
85 glPushMatrix();
86 glTranslatef(0.75, 0.0, -1.0);
87 glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, blue_mat );
88 glutSolidSphere(1.0, 20, 20);
89 glPopMatrix();
90
91 glPopMatrix();
92
93 /* This is very important!!!
94 * Make sure buffered commands are finished!!!
95 */
96 glFinish();
97
98 Frames++;
99 if (perf) {
100 GLint t = glutGet(GLUT_ELAPSED_TIME);
101 if (t - T0 >= 5000) {
102 GLfloat seconds = (t - T0) / 1000.0;
103 GLfloat fps = Frames / seconds;
104 printf("%d frames in %6.3f seconds = %6.3f FPS\n", Frames, seconds, fps);
105 T0 = t;
106 Frames = 0;
107 }
108 }
109 }
110
111
112 #ifdef SAVE_TARGA
113
114 static void
115 write_targa(const char *filename, const GLubyte *buffer, int width, int height)
116 {
117 FILE *f = fopen( filename, "w" );
118 if (f) {
119 int i, x, y;
120 const GLubyte *ptr = buffer;
121 printf ("osdemo, writing tga file \n");
122 fputc (0x00, f); /* ID Length, 0 => No ID */
123 fputc (0x00, f); /* Color Map Type, 0 => No color map included */
124 fputc (0x02, f); /* Image Type, 2 => Uncompressed, True-color Image */
125 fputc (0x00, f); /* Next five bytes are about the color map entries */
126 fputc (0x00, f); /* 2 bytes Index, 2 bytes length, 1 byte size */
127 fputc (0x00, f);
128 fputc (0x00, f);
129 fputc (0x00, f);
130 fputc (0x00, f); /* X-origin of Image */
131 fputc (0x00, f);
132 fputc (0x00, f); /* Y-origin of Image */
133 fputc (0x00, f);
134 fputc (WIDTH & 0xff, f); /* Image Width */
135 fputc ((WIDTH>>8) & 0xff, f);
136 fputc (HEIGHT & 0xff, f); /* Image Height */
137 fputc ((HEIGHT>>8) & 0xff, f);
138 fputc (0x18, f); /* Pixel Depth, 0x18 => 24 Bits */
139 fputc (0x20, f); /* Image Descriptor */
140 fclose(f);
141 f = fopen( filename, "ab" ); /* reopen in binary append mode */
142 for (y=height-1; y>=0; y--) {
143 for (x=0; x<width; x++) {
144 i = (y*width + x) * 4;
145 fputc(ptr[i+2], f); /* write blue */
146 fputc(ptr[i+1], f); /* write green */
147 fputc(ptr[i], f); /* write red */
148 }
149 }
150 }
151 }
152
153 #else
154
155 static void
156 write_ppm(const char *filename, const GLubyte *buffer, int width, int height)
157 {
158 const int binary = 0;
159 FILE *f = fopen( filename, "w" );
160 if (f) {
161 int i, x, y;
162 const GLubyte *ptr = buffer;
163 if (binary) {
164 fprintf(f,"P6\n");
165 fprintf(f,"# ppm-file created by osdemo.c\n");
166 fprintf(f,"%i %i\n", width,height);
167 fprintf(f,"255\n");
168 fclose(f);
169 f = fopen( filename, "ab" ); /* reopen in binary append mode */
170 for (y=height-1; y>=0; y--) {
171 for (x=0; x<width; x++) {
172 i = (y*width + x) * 4;
173 fputc(ptr[i], f); /* write red */
174 fputc(ptr[i+1], f); /* write green */
175 fputc(ptr[i+2], f); /* write blue */
176 }
177 }
178 }
179 else {
180 /*ASCII*/
181 int counter = 0;
182 fprintf(f,"P3\n");
183 fprintf(f,"# ascii ppm file created by osdemo.c\n");
184 fprintf(f,"%i %i\n", width, height);
185 fprintf(f,"255\n");
186 for (y=height-1; y>=0; y--) {
187 for (x=0; x<width; x++) {
188 i = (y*width + x) * 4;
189 fprintf(f, " %3d %3d %3d", ptr[i], ptr[i+1], ptr[i+2]);
190 counter++;
191 if (counter % 5 == 0)
192 fprintf(f, "\n");
193 }
194 }
195 }
196 fclose(f);
197 }
198 }
199
200 #endif
201
202
203
204 int main( int argc, char *argv[] )
205 {
206 void *buffer;
207 int i;
208 char *filename = NULL;
209
210 /* Create an RGBA-mode context */
211 #if OSMESA_MAJOR_VERSION * 100 + OSMESA_MINOR_VERSION >= 305
212 /* specify Z, stencil, accum sizes */
213 OSMesaContext ctx = OSMesaCreateContextExt( OSMESA_RGBA, 16, 0, 0, NULL );
214 #else
215 OSMesaContext ctx = OSMesaCreateContext( OSMESA_RGBA, NULL );
216 #endif
217 if (!ctx) {
218 printf("OSMesaCreateContext failed!\n");
219 return 0;
220 }
221
222 for ( i=1; i<argc; i++ ) {
223 if (argv[i][0] != '-') filename = argv[i];
224 if (strcmp(argv[i], "-perf")==0) perf = 1;
225 }
226
227 /* Allocate the image buffer */
228 buffer = malloc( WIDTH * HEIGHT * 4 * sizeof(GLubyte) );
229 if (!buffer) {
230 printf("Alloc image buffer failed!\n");
231 return 0;
232 }
233
234 /* Bind the buffer to the context and make it current */
235 if (!OSMesaMakeCurrent( ctx, buffer, GL_UNSIGNED_BYTE, WIDTH, HEIGHT )) {
236 printf("OSMesaMakeCurrent failed!\n");
237 return 0;
238 }
239
240
241 {
242 int z, s, a;
243 glGetIntegerv(GL_DEPTH_BITS, &z);
244 glGetIntegerv(GL_STENCIL_BITS, &s);
245 glGetIntegerv(GL_ACCUM_RED_BITS, &a);
246 printf("Depth=%d Stencil=%d Accum=%d\n", z, s, a);
247 }
248
249 render_image();
250 if (perf)
251 for(i=0; i< 1000; i++)
252 render_image();
253
254 if (filename != NULL) {
255 #ifdef SAVE_TARGA
256 write_targa(filename, buffer, WIDTH, HEIGHT);
257 #else
258 write_ppm(filename, buffer, WIDTH, HEIGHT);
259 #endif
260 }
261 else {
262 printf("Specify a filename if you want to make an image file\n");
263 }
264
265 printf("all done\n");
266
267 /* free the image buffer */
268 free( buffer );
269
270 /* destroy the context */
271 OSMesaDestroyContext( ctx );
272
273 return 0;
274 }