2
* Demo of off-screen Mesa rendering with 16-bit color channels.
3
* This requires the libOSMesa16.so library.
5
* Compile with something like this:
7
* gcc osdemo16.c -I../../include -L../../lib -lglut -lGLU -lOSMesa16 -lm -o osdemo16
14
#include "GL/osmesa.h"
26
static void render_image( void )
28
GLfloat light_ambient[] = { 0.0, 0.0, 0.0, 1.0 };
29
GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
30
GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
31
GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
32
GLfloat red_mat[] = { 1.0, 0.2, 0.2, 1.0 };
33
GLfloat green_mat[] = { 0.2, 1.0, 0.2, 0.5 };
34
GLfloat blue_mat[] = { 0.2, 0.2, 1.0, 1.0 };
35
GLfloat white_mat[] = { 1.0, 1.0, 1.0, 1.0 };
36
GLfloat purple_mat[] = { 1.0, 0.2, 1.0, 1.0 };
37
GLUquadricObj *qobj = gluNewQuadric();
39
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
40
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
41
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
42
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
44
glEnable(GL_LIGHTING);
46
glEnable(GL_DEPTH_TEST);
48
glMatrixMode(GL_PROJECTION);
50
glOrtho(-2.5, 2.5, -2.5, 2.5, -10.0, 10.0);
51
glMatrixMode(GL_MODELVIEW);
53
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
56
glRotatef(20.0, 1.0, 0.0, 0.0);
60
glTranslatef(-0.75, 0.5, 0.0);
61
glRotatef(90.0, 1.0, 0.0, 0.0);
62
glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, red_mat );
63
glutSolidTorus(0.275, 0.85, 20, 20);
69
glTranslatef(0.0, -0.5, 0.0);
70
glRotatef(90, 1, 0.5, 0);
72
glDisable(GL_LIGHTING);
73
glColor4f(1, 0, 0, 0.5);
80
glEnable(GL_LIGHTING);
86
glTranslatef(0.0, 0.5, 0.1);
87
glDisable(GL_LIGHTING);
95
glEnable(GL_LIGHTING);
100
glTranslatef(0.75, 0.5, 0.3);
101
glDisable(GL_LIGHTING);
102
glColor3f(0, 0, 0.5);
109
glEnable(GL_LIGHTING);
113
glTranslatef(-0.75, -0.5, 0.0);
114
glRotatef(270.0, 1.0, 0.0, 0.0);
115
glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, green_mat );
116
glColor4f(0,1,0,0.5);
118
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
119
gluCylinder(qobj, 1.0, 0.0, 2.0, 16, 1);
124
glTranslatef(0.75, 1.0, 1.0);
125
glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, blue_mat );
126
gluSphere(qobj, 1.0, 20, 20);
131
/* This is very important!!!
132
* Make sure buffered commands are finished!!!
136
gluDeleteQuadric(qobj);
140
glGetIntegerv(GL_RED_BITS, &r);
141
glGetIntegerv(GL_GREEN_BITS, &g);
142
glGetIntegerv(GL_BLUE_BITS, &b);
143
glGetIntegerv(GL_ALPHA_BITS, &a);
144
printf("channel sizes: %d %d %d %d\n", r, g, b, a);
151
write_targa(const char *filename, const GLushort *buffer, int width, int height)
153
FILE *f = fopen( filename, "w" );
156
const GLushort *ptr = buffer;
157
printf ("osdemo, writing tga file \n");
158
fputc (0x00, f); /* ID Length, 0 => No ID */
159
fputc (0x00, f); /* Color Map Type, 0 => No color map included */
160
fputc (0x02, f); /* Image Type, 2 => Uncompressed, True-color Image */
161
fputc (0x00, f); /* Next five bytes are about the color map entries */
162
fputc (0x00, f); /* 2 bytes Index, 2 bytes length, 1 byte size */
166
fputc (0x00, f); /* X-origin of Image */
168
fputc (0x00, f); /* Y-origin of Image */
170
fputc (WIDTH & 0xff, f); /* Image Width */
171
fputc ((WIDTH>>8) & 0xff, f);
172
fputc (HEIGHT & 0xff, f); /* Image Height */
173
fputc ((HEIGHT>>8) & 0xff, f);
174
fputc (0x18, f); /* Pixel Depth, 0x18 => 24 Bits */
175
fputc (0x20, f); /* Image Descriptor */
177
f = fopen( filename, "ab" ); /* reopen in binary append mode */
178
for (y=height-1; y>=0; y--) {
179
for (x=0; x<width; x++) {
180
i = (y*width + x) * 4;
181
/* just write 8 high bits */
182
fputc(ptr[i+2] >> 8, f); /* write blue */
183
fputc(ptr[i+1] >> 8, f); /* write green */
184
fputc(ptr[i] >> 8, f); /* write red */
192
write_ppm(const char *filename, const GLushort *buffer, int width, int height)
194
const int binary = 0;
195
FILE *f = fopen( filename, "w" );
198
const GLushort *ptr = buffer;
201
fprintf(f,"# ppm-file created by osdemo.c\n");
202
fprintf(f,"%i %i\n", width,height);
205
f = fopen( filename, "ab" ); /* reopen in binary append mode */
206
for (y=height-1; y>=0; y--) {
207
for (x=0; x<width; x++) {
208
i = (y*width + x) * 4;
209
/* just write 8 high bits */
210
fputc(ptr[i] >> 8, f); /* write red */
211
fputc(ptr[i+1] >> 8, f); /* write green */
212
fputc(ptr[i+2] >> 8, f); /* write blue */
220
fprintf(f,"# ascii ppm file created by osdemo.c\n");
221
fprintf(f,"%i %i\n", width, height);
223
for (y=height-1; y>=0; y--) {
224
for (x=0; x<width; x++) {
225
i = (y*width + x) * 4;
226
/* just write 8 high bits */
227
fprintf(f, " %3d %3d %3d", ptr[i] >> 8, ptr[i+1] >> 8, ptr[i+2] >> 8);
229
if (counter % 5 == 0)
240
int main( int argc, char *argv[] )
244
/* Create an RGBA-mode context */
245
#if OSMESA_MAJOR_VERSION * 100 + OSMESA_MINOR_VERSION >= 305
246
/* specify Z, stencil, accum sizes */
247
OSMesaContext ctx = OSMesaCreateContextExt( GL_RGBA, 16, 0, 0, NULL );
249
OSMesaContext ctx = OSMesaCreateContext( GL_RGBA, NULL );
252
printf("OSMesaCreateContext failed!\n");
256
/* Allocate the image buffer */
257
buffer = (GLushort *) malloc( WIDTH * HEIGHT * 4 * sizeof(GLushort));
259
printf("Alloc image buffer failed!\n");
263
/* Bind the buffer to the context and make it current */
264
if (!OSMesaMakeCurrent( ctx, buffer, GL_UNSIGNED_SHORT, WIDTH, HEIGHT )) {
265
printf("OSMesaMakeCurrent failed!\n");
273
write_targa(argv[1], buffer, WIDTH, HEIGHT);
275
write_ppm(argv[1], buffer, WIDTH, HEIGHT);
279
printf("Specify a filename if you want to make an image file\n");
282
printf("all done\n");
284
/* free the image buffer */
287
/* destroy the context */
288
OSMesaDestroyContext( ctx );