home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / coders / mesa-1.2.8 / demos / osdemo.c < prev    next >
C/C++ Source or Header  |  1996-05-27  |  3KB  |  141 lines

  1. /* osdemo.c */
  2.  
  3. /* Demo of off-screen Mesa rendering */
  4.  
  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.  */
  17.  
  18.  
  19.  
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include "GL/osmesa.h"
  23. #include "glaux.h"
  24.  
  25.  
  26.  
  27. #define WIDTH 400
  28. #define HEIGHT 400
  29.  
  30.  
  31.  
  32. static void render_image( void )
  33. {
  34.    GLfloat light_ambient[] = { 0.0, 0.0, 0.0, 1.0 };
  35.    GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
  36.    GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
  37.    GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
  38.    GLfloat red_mat[]   = { 1.0, 0.2, 0.2, 1.0 };
  39.    GLfloat green_mat[] = { 0.2, 1.0, 0.2, 1.0 };
  40.    GLfloat blue_mat[]  = { 0.2, 0.2, 1.0, 1.0 };
  41.  
  42.  
  43.    glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
  44.    glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
  45.    glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
  46.    glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  47.     
  48.    glEnable(GL_LIGHTING);
  49.    glEnable(GL_LIGHT0);
  50.    glEnable(GL_DEPTH_TEST);
  51.  
  52.    glMatrixMode(GL_PROJECTION);
  53.    glLoadIdentity();
  54.    glOrtho(-2.5, 2.5, -2.5, 2.5, -10.0, 10.0);
  55.    glMatrixMode(GL_MODELVIEW);
  56.  
  57.    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  58.  
  59.    glPushMatrix();
  60.    glRotatef(20.0, 1.0, 0.0, 0.0);
  61.  
  62.    glPushMatrix();
  63.    glTranslatef(-0.75, 0.5, 0.0); 
  64.    glRotatef(90.0, 1.0, 0.0, 0.0);
  65.    glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, red_mat );
  66.    auxSolidTorus(0.275, 0.85);
  67.    glPopMatrix();
  68.  
  69.    glPushMatrix();
  70.    glTranslatef(-0.75, -0.5, 0.0); 
  71.    glRotatef(270.0, 1.0, 0.0, 0.0);
  72.    glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, green_mat );
  73.    auxSolidCone(1.0, 2.0);
  74.    glPopMatrix();
  75.  
  76.    glPushMatrix();
  77.    glTranslatef(0.75, 0.0, -1.0); 
  78.    glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, blue_mat );
  79.    auxSolidSphere(1.0);
  80.    glPopMatrix();
  81.  
  82.    glPopMatrix();
  83. }
  84.  
  85.  
  86.  
  87. int main( int argc, char *argv[] )
  88. {
  89.    OSMesaContext ctx;
  90.    void *buffer;
  91.  
  92.    /* Create an RGBA-mode context */
  93.    ctx = OSMesaCreateContext( GL_RGBA, NULL );
  94.  
  95.    /* Allocate the image buffer */
  96.    buffer = malloc( WIDTH * HEIGHT * 4 );
  97.  
  98.    /* Bind the buffer to the context and make it current */
  99.    OSMesaMakeCurrent( ctx, buffer, GL_UNSIGNED_BYTE, WIDTH, HEIGHT );
  100.  
  101.    render_image();
  102.  
  103.    if (argc>1) {
  104.       /* write PPM file */
  105.       FILE *f = fopen( argv[1], "w" );
  106.       if (f) {
  107.          int i, x, y;
  108.          GLubyte *ptr;
  109.          ptr = buffer;
  110.          fprintf(f,"P6\n");
  111.          fprintf(f,"# ppm-file created by %s\n",  argv[0]);
  112.          fprintf(f,"%i %i\n", WIDTH,HEIGHT);
  113.          fprintf(f,"255\n");
  114.          fclose(f);
  115.          f = fopen( argv[1], "ab" );  /* reopen in binary append mode */
  116.          for (y=HEIGHT-1; y>=0; y--) {
  117.             for (x=0; x<WIDTH; x++) {
  118.                i = (y*WIDTH + x) * 4;
  119.                fputc(ptr[i], f);   /* write red */
  120.                fputc(ptr[i+1], f); /* write green */
  121.                fputc(ptr[i+2], f); /* write blue */
  122.             }
  123.          }
  124.          fclose(f);
  125.       }
  126.    }
  127.    else {
  128.       printf("Specify a filename if you want to make a ppm file\n");
  129.    }
  130.  
  131.    printf("all done\n");
  132.  
  133.    /* free the image buffer */
  134.    free( buffer );
  135.  
  136.    /* destroy the context */
  137.    OSMesaDestroyContext( ctx );
  138.  
  139.    return 0;
  140. }
  141.