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