blob: 66e821ad4e8698904d994b6edf1223664cdb360e [file] [log] [blame]
Brian Paul7d01c342004-09-24 23:00:52 +00001/*
2 * Demo of off-screen Mesa rendering with 32-bit float color channels.
3 * This requires the libOSMesa32.so library.
4 *
5 * Compile with something like this:
6 *
7 * gcc osdemo32.c -I../include -L../lib -lOSMesa32 -lm -o osdemo32
8 */
9
10
11#include <stdio.h>
12#include <stdlib.h>
13#include "GL/osmesa.h"
14#include "GL/glut.h"
15
16
17#define SAVE_TARGA
18
19
20#define WIDTH 400
21#define HEIGHT 400
22
23
24
25static void render_image( void )
26{
27 GLfloat light_ambient[] = { 0.0, 0.0, 0.0, 1.0 };
28 GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
29 GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
30 GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
31 GLfloat red_mat[] = { 1.0, 0.2, 0.2, 1.0 };
32 GLfloat green_mat[] = { 0.2, 1.0, 0.2, 0.5 };
33 GLfloat blue_mat[] = { 0.2, 0.2, 1.0, 1.0 };
34 GLfloat white_mat[] = { 1.0, 1.0, 1.0, 1.0 };
35 GLfloat purple_mat[] = { 1.0, 0.2, 1.0, 1.0 };
36 GLUquadricObj *qobj = gluNewQuadric();
37
38 glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
39 glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
40 glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
41 glLightfv(GL_LIGHT0, GL_POSITION, light_position);
42
43 glEnable(GL_LIGHTING);
44 glEnable(GL_LIGHT0);
45 glEnable(GL_DEPTH_TEST);
46
47 glMatrixMode(GL_PROJECTION);
48 glLoadIdentity();
49 glOrtho(-2.5, 2.5, -2.5, 2.5, -10.0, 10.0);
50 glMatrixMode(GL_MODELVIEW);
51
52 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
53
54 glPushMatrix();
55 glRotatef(20.0, 1.0, 0.0, 0.0);
56
57#if 0
58 glPushMatrix();
59 glTranslatef(-0.75, 0.5, 0.0);
60 glRotatef(90.0, 1.0, 0.0, 0.0);
61 glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, red_mat );
62 glutSolidTorus(0.275, 0.85, 20, 20);
63 glPopMatrix();
64#endif
65
66 /* red square */
67 glPushMatrix();
68 glTranslatef(0.0, -0.5, 0.0);
69 glRotatef(90, 1, 0.5, 0);
70 glScalef(3, 3, 3);
71 glDisable(GL_LIGHTING);
72 glColor4f(1, 0, 0, 0.5);
73 glBegin(GL_POLYGON);
74 glVertex2f(-1, -1);
75 glVertex2f( 1, -1);
76 glVertex2f( 1, 1);
77 glVertex2f(-1, 1);
78 glEnd();
79 glEnable(GL_LIGHTING);
80 glPopMatrix();
81
82#if 0
83 /* green square */
84 glPushMatrix();
85 glTranslatef(0.0, 0.5, 0.1);
86 glDisable(GL_LIGHTING);
87 glColor3f(0, 1, 0);
88 glBegin(GL_POLYGON);
89 glVertex2f(-1, -1);
90 glVertex2f( 1, -1);
91 glVertex2f( 1, 1);
92 glVertex2f(-1, 1);
93 glEnd();
94 glEnable(GL_LIGHTING);
95 glPopMatrix();
96
97 /* blue square */
98 glPushMatrix();
99 glTranslatef(0.75, 0.5, 0.3);
100 glDisable(GL_LIGHTING);
101 glColor3f(0, 0, 0.5);
102 glBegin(GL_POLYGON);
103 glVertex2f(-1, -1);
104 glVertex2f( 1, -1);
105 glVertex2f( 1, 1);
106 glVertex2f(-1, 1);
107 glEnd();
108 glEnable(GL_LIGHTING);
109 glPopMatrix();
110#endif
111 glPushMatrix();
112 glTranslatef(-0.75, -0.5, 0.0);
113 glRotatef(270.0, 1.0, 0.0, 0.0);
114 glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, green_mat );
115 glColor4f(0,1,0,0.5);
116 glEnable(GL_BLEND);
117 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
118 gluCylinder(qobj, 1.0, 0.0, 2.0, 16, 1);
119 glDisable(GL_BLEND);
120 glPopMatrix();
121
122 glPushMatrix();
123 glTranslatef(0.75, 1.0, 1.0);
124 glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, blue_mat );
125 gluSphere(qobj, 1.0, 20, 20);
126 glPopMatrix();
127
128 glPopMatrix();
129
130 gluDeleteQuadric(qobj);
131
132 {
133 GLint r, g, b, a;
134 glGetIntegerv(GL_RED_BITS, &r);
135 glGetIntegerv(GL_GREEN_BITS, &g);
136 glGetIntegerv(GL_BLUE_BITS, &b);
137 glGetIntegerv(GL_ALPHA_BITS, &a);
138 printf("channel sizes: %d %d %d %d\n", r, g, b, a);
139 }
140}
141
142
143
144static void
145write_targa(const char *filename, const GLfloat *buffer, int width, int height)
146{
147 FILE *f = fopen( filename, "w" );
148 if (f) {
149 int i, x, y;
150 const GLfloat *ptr = buffer;
151 printf ("osdemo, writing tga file \n");
152 fputc (0x00, f); /* ID Length, 0 => No ID */
153 fputc (0x00, f); /* Color Map Type, 0 => No color map included */
154 fputc (0x02, f); /* Image Type, 2 => Uncompressed, True-color Image */
155 fputc (0x00, f); /* Next five bytes are about the color map entries */
156 fputc (0x00, f); /* 2 bytes Index, 2 bytes length, 1 byte size */
157 fputc (0x00, f);
158 fputc (0x00, f);
159 fputc (0x00, f);
160 fputc (0x00, f); /* X-origin of Image */
161 fputc (0x00, f);
162 fputc (0x00, f); /* Y-origin of Image */
163 fputc (0x00, f);
164 fputc (WIDTH & 0xff, f); /* Image Width */
165 fputc ((WIDTH>>8) & 0xff, f);
166 fputc (HEIGHT & 0xff, f); /* Image Height */
167 fputc ((HEIGHT>>8) & 0xff, f);
168 fputc (0x18, f); /* Pixel Depth, 0x18 => 24 Bits */
169 fputc (0x20, f); /* Image Descriptor */
170 fclose(f);
171 f = fopen( filename, "ab" ); /* reopen in binary append mode */
172 for (y=height-1; y>=0; y--) {
173 for (x=0; x<width; x++) {
174 int r, g, b;
175 i = (y*width + x) * 4;
176 r = (int) (ptr[i+0] * 255.0);
177 g = (int) (ptr[i+1] * 255.0);
178 b = (int) (ptr[i+2] * 255.0);
179 if (r > 255) r = 255;
180 if (g > 255) g = 255;
181 if (b > 255) b = 255;
182 fputc(b, f); /* write blue */
183 fputc(g, f); /* write green */
184 fputc(r, f); /* write red */
185 }
186 }
187 }
188}
189
190
191static void
192write_ppm(const char *filename, const GLubyte *buffer, int width, int height)
193{
194 const int binary = 0;
195 FILE *f = fopen( filename, "w" );
196 if (f) {
197 int i, x, y;
198 const GLubyte *ptr = buffer;
199 if (binary) {
200 fprintf(f,"P6\n");
201 fprintf(f,"# ppm-file created by osdemo.c\n");
202 fprintf(f,"%i %i\n", width,height);
203 fprintf(f,"255\n");
204 fclose(f);
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 fputc(ptr[i], f); /* write red */
210 fputc(ptr[i+1], f); /* write green */
211 fputc(ptr[i+2], f); /* write blue */
212 }
213 }
214 }
215 else {
216 /*ASCII*/
217 int counter = 0;
218 fprintf(f,"P3\n");
219 fprintf(f,"# ascii ppm file created by osdemo.c\n");
220 fprintf(f,"%i %i\n", width, height);
221 fprintf(f,"255\n");
222 for (y=height-1; y>=0; y--) {
223 for (x=0; x<width; x++) {
224 i = (y*width + x) * 4;
225 fprintf(f, " %3d %3d %3d", ptr[i], ptr[i+1], ptr[i+2]);
226 counter++;
227 if (counter % 5 == 0)
228 fprintf(f, "\n");
229 }
230 }
231 }
232 fclose(f);
233 }
234}
235
236
237
238int main( int argc, char *argv[] )
239{
240 void *buffer;
241
242 /* Create an RGBA-mode context */
243#if OSMESA_MAJOR_VERSION * 100 + OSMESA_MINOR_VERSION >= 305
244 /* specify Z, stencil, accum sizes */
245 OSMesaContext ctx = OSMesaCreateContextExt( GL_RGBA, 16, 0, 0, NULL );
246#else
247 OSMesaContext ctx = OSMesaCreateContext( GL_RGBA, NULL );
248#endif
249 if (!ctx) {
250 printf("OSMesaCreateContext failed!\n");
251 return 0;
252 }
253
254 /* Allocate the image buffer */
255 buffer = malloc( WIDTH * HEIGHT * 4 * sizeof(GLfloat));
256 if (!buffer) {
257 printf("Alloc image buffer failed!\n");
258 return 0;
259 }
260
261 /* Bind the buffer to the context and make it current */
262 if (!OSMesaMakeCurrent( ctx, buffer, GL_FLOAT, WIDTH, HEIGHT )) {
263 printf("OSMesaMakeCurrent failed!\n");
264 return 0;
265 }
266
267 render_image();
268
269 if (argc>1) {
270#ifdef SAVE_TARGA
271 write_targa(argv[1], buffer, WIDTH, HEIGHT);
272#else
273 write_ppm(argv[1], buffer, WIDTH, HEIGHT);
274#endif
275 }
276 else {
277 printf("Specify a filename if you want to make an image file\n");
278 }
279
280 printf("all done\n");
281
282 /* free the image buffer */
283 free( buffer );
284
285 /* destroy the context */
286 OSMesaDestroyContext( ctx );
287
288 return 0;
289}