3 * Demo of off-screen Mesa rendering
5 * See Mesa/include/GL/osmesa.h for documentation of the OSMesa functions.
7 * If you want to render BIG images you'll probably have to increase
8 * MAX_WIDTH and MAX_HEIGHT in src/config.h.
10 * This program is in the public domain.
14 * PPM output provided by Joerg Schmalzl.
15 * ASCII PPM output added by Brian Paul.
17 * Usage: osdemo [-perf] [filename]
19 * -perf: Redraws the image 1000 times, displaying the FPS every 5 secs.
20 * filename: file to store the TGA or PPM output
27 #include "GL/osmesa.h"
38 static GLint Frames
= 0;
41 static void render_image( void )
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 };
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
);
57 glEnable(GL_LIGHTING
);
59 glEnable(GL_DEPTH_TEST
);
61 glMatrixMode(GL_PROJECTION
);
63 glOrtho(-2.5, 2.5, -2.5, 2.5, -10.0, 10.0);
64 glMatrixMode(GL_MODELVIEW
);
66 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
69 glRotatef(20.0, 1.0, 0.0, 0.0);
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);
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);
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);
93 /* This is very important!!!
94 * Make sure buffered commands are finished!!!
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
);
115 write_targa(const char *filename
, const GLubyte
*buffer
, int width
, int height
)
117 FILE *f
= fopen( filename
, "w" );
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 */
130 fputc (0x00, f
); /* X-origin of Image */
132 fputc (0x00, f
); /* Y-origin of Image */
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 */
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 */
156 write_ppm(const char *filename
, const GLubyte
*buffer
, int width
, int height
)
158 const int binary
= 0;
159 FILE *f
= fopen( filename
, "w" );
162 const GLubyte
*ptr
= buffer
;
165 fprintf(f
,"# ppm-file created by osdemo.c\n");
166 fprintf(f
,"%i %i\n", width
,height
);
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 */
183 fprintf(f
,"# ascii ppm file created by osdemo.c\n");
184 fprintf(f
,"%i %i\n", width
, height
);
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]);
191 if (counter
% 5 == 0)
204 int main( int argc
, char *argv
[] )
208 char *filename
= NULL
;
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
);
215 OSMesaContext ctx
= OSMesaCreateContext( OSMESA_RGBA
, NULL
);
218 printf("OSMesaCreateContext failed!\n");
222 for ( i
=1; i
<argc
; i
++ ) {
223 if (argv
[i
][0] != '-') filename
= argv
[i
];
224 if (strcmp(argv
[i
], "-perf")==0) perf
= 1;
227 /* Allocate the image buffer */
228 buffer
= malloc( WIDTH
* HEIGHT
* 4 * sizeof(GLubyte
) );
230 printf("Alloc image buffer failed!\n");
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");
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
);
251 for(i
=0; i
< 1000; i
++)
254 if (filename
!= NULL
) {
256 write_targa(filename
, buffer
, WIDTH
, HEIGHT
);
258 write_ppm(filename
, buffer
, WIDTH
, HEIGHT
);
262 printf("Specify a filename if you want to make an image file\n");
265 printf("all done\n");
267 /* free the image buffer */
270 /* destroy the context */
271 OSMesaDestroyContext( ctx
);