jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1 | /* $Id: osdemo.c,v 1.1 1999/08/19 00:55:40 jtg Exp $ */ |
| 2 | |
| 3 | /* |
| 4 | * Demo of off-screen Mesa rendering |
| 5 | * |
| 6 | * See Mesa/include/GL/osmesa.h for documentation of the OSMesa functions. |
| 7 | * |
| 8 | * If you want to render BIG images you'll probably have to increase |
| 9 | * MAX_WIDTH and MAX_HEIGHT in src/config.h. |
| 10 | * |
| 11 | * This program is in the public domain. |
| 12 | * |
| 13 | * Brian Paul |
| 14 | * |
| 15 | * PPM output provided by Joerg Schmalzl. |
| 16 | * ASCII PPM output added by Brian Paul. |
| 17 | */ |
| 18 | |
| 19 | |
| 20 | /* |
| 21 | * $Log: osdemo.c,v $ |
| 22 | * Revision 1.1 1999/08/19 00:55:40 jtg |
| 23 | * Initial revision |
| 24 | * |
| 25 | * Revision 3.0 1998/02/14 18:42:29 brianp |
| 26 | * initial rev |
| 27 | * |
| 28 | */ |
| 29 | |
| 30 | |
| 31 | |
| 32 | #include <stdio.h> |
| 33 | #include <stdlib.h> |
| 34 | #include "GL/osmesa.h" |
| 35 | #include "GL/glut.h" |
| 36 | |
| 37 | |
| 38 | |
| 39 | #define WIDTH 400 |
| 40 | #define HEIGHT 400 |
| 41 | |
| 42 | |
| 43 | |
| 44 | static void render_image( void ) |
| 45 | { |
| 46 | GLfloat light_ambient[] = { 0.0, 0.0, 0.0, 1.0 }; |
| 47 | GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 }; |
| 48 | GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 }; |
| 49 | GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 }; |
| 50 | GLfloat red_mat[] = { 1.0, 0.2, 0.2, 1.0 }; |
| 51 | GLfloat green_mat[] = { 0.2, 1.0, 0.2, 1.0 }; |
| 52 | GLfloat blue_mat[] = { 0.2, 0.2, 1.0, 1.0 }; |
| 53 | |
| 54 | |
| 55 | glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient); |
| 56 | glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); |
| 57 | glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular); |
| 58 | glLightfv(GL_LIGHT0, GL_POSITION, light_position); |
| 59 | |
| 60 | glEnable(GL_LIGHTING); |
| 61 | glEnable(GL_LIGHT0); |
| 62 | glEnable(GL_DEPTH_TEST); |
| 63 | |
| 64 | glMatrixMode(GL_PROJECTION); |
| 65 | glLoadIdentity(); |
| 66 | glOrtho(-2.5, 2.5, -2.5, 2.5, -10.0, 10.0); |
| 67 | glMatrixMode(GL_MODELVIEW); |
| 68 | |
| 69 | glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); |
| 70 | |
| 71 | glPushMatrix(); |
| 72 | glRotatef(20.0, 1.0, 0.0, 0.0); |
| 73 | |
| 74 | glPushMatrix(); |
| 75 | glTranslatef(-0.75, 0.5, 0.0); |
| 76 | glRotatef(90.0, 1.0, 0.0, 0.0); |
| 77 | glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, red_mat ); |
| 78 | glutSolidTorus(0.275, 0.85, 20, 20); |
| 79 | glPopMatrix(); |
| 80 | |
| 81 | glPushMatrix(); |
| 82 | glTranslatef(-0.75, -0.5, 0.0); |
| 83 | glRotatef(270.0, 1.0, 0.0, 0.0); |
| 84 | glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, green_mat ); |
| 85 | glutSolidCone(1.0, 2.0, 16, 1); |
| 86 | glPopMatrix(); |
| 87 | |
| 88 | glPushMatrix(); |
| 89 | glTranslatef(0.75, 0.0, -1.0); |
| 90 | glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, blue_mat ); |
| 91 | glutSolidSphere(1.0, 20, 20); |
| 92 | glPopMatrix(); |
| 93 | |
| 94 | glPopMatrix(); |
| 95 | } |
| 96 | |
| 97 | |
| 98 | |
| 99 | int main( int argc, char *argv[] ) |
| 100 | { |
| 101 | OSMesaContext ctx; |
| 102 | void *buffer; |
| 103 | |
| 104 | /* Create an RGBA-mode context */ |
| 105 | ctx = OSMesaCreateContext( GL_RGBA, NULL ); |
| 106 | |
| 107 | /* Allocate the image buffer */ |
| 108 | buffer = malloc( WIDTH * HEIGHT * 4 ); |
| 109 | |
| 110 | /* Bind the buffer to the context and make it current */ |
| 111 | OSMesaMakeCurrent( ctx, buffer, GL_UNSIGNED_BYTE, WIDTH, HEIGHT ); |
| 112 | |
| 113 | render_image(); |
| 114 | |
| 115 | if (argc>1) { |
| 116 | /* write PPM file */ |
| 117 | FILE *f = fopen( argv[1], "w" ); |
| 118 | if (f) { |
| 119 | int i, x, y; |
| 120 | GLubyte *ptr = (GLubyte *) buffer; |
| 121 | #define BINARY 0 |
| 122 | #if BINARY |
| 123 | fprintf(f,"P6\n"); |
| 124 | fprintf(f,"# ppm-file created by %s\n", argv[0]); |
| 125 | fprintf(f,"%i %i\n", WIDTH,HEIGHT); |
| 126 | fprintf(f,"255\n"); |
| 127 | fclose(f); |
| 128 | f = fopen( argv[1], "ab" ); /* reopen in binary append mode */ |
| 129 | for (y=HEIGHT-1; y>=0; y--) { |
| 130 | for (x=0; x<WIDTH; x++) { |
| 131 | i = (y*WIDTH + x) * 4; |
| 132 | fputc(ptr[i], f); /* write red */ |
| 133 | fputc(ptr[i+1], f); /* write green */ |
| 134 | fputc(ptr[i+2], f); /* write blue */ |
| 135 | } |
| 136 | } |
| 137 | #else /*ASCII*/ |
| 138 | int counter = 0; |
| 139 | fprintf(f,"P3\n"); |
| 140 | fprintf(f,"# ascii ppm file created by %s\n", argv[0]); |
| 141 | fprintf(f,"%i %i\n", WIDTH, HEIGHT); |
| 142 | fprintf(f,"255\n"); |
| 143 | for (y=HEIGHT-1; y>=0; y--) { |
| 144 | for (x=0; x<WIDTH; x++) { |
| 145 | i = (y*WIDTH + x) * 4; |
| 146 | fprintf(f, " %3d %3d %3d", ptr[i], ptr[i+1], ptr[i+2]); |
| 147 | counter++; |
| 148 | if (counter % 5 == 0) |
| 149 | fprintf(f, "\n"); |
| 150 | } |
| 151 | } |
| 152 | #endif |
| 153 | fclose(f); |
| 154 | } |
| 155 | } |
| 156 | else { |
| 157 | printf("Specify a filename if you want to make a ppm file\n"); |
| 158 | } |
| 159 | |
| 160 | printf("all done\n"); |
| 161 | |
| 162 | /* free the image buffer */ |
| 163 | free( buffer ); |
| 164 | |
| 165 | /* destroy the context */ |
| 166 | OSMesaDestroyContext( ctx ); |
| 167 | |
| 168 | return 0; |
| 169 | } |