home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / coders / mesa-1.2.8 / next / nextdemo1.m < prev    next >
Text File  |  1996-05-27  |  4KB  |  169 lines

  1. /* nextdemo.m*/
  2.  
  3. /* Demo of NEXTSTEP 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.  * NEXTSTEP output provided by Pascal Thibaudeau 
  16.  * pthibaud@frbdx11.cribx1.u-bordeaux.fr
  17.  */
  18.  
  19. #import <dpsclient/wraps.h>
  20. #import <appkit/Application.h>
  21. #import <appkit/Window.h>
  22. #import <appkit/Menu.h>
  23. #import <appkit/View.h>
  24. #import <appkit/color.h>
  25. #import <appkit/NXBitmapImageRep.h>
  26.  
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include "GL/osmesa.h"
  30.  
  31. #define WIDTH 480
  32. #define HEIGHT 480 
  33.  
  34. static void render_image()
  35. {
  36.    GLfloat light_ambient[] = { 0.0, 0.0, 0.0, 1.0 };
  37.    GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
  38.    GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
  39.    GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
  40.    GLfloat red_mat[]   = { 1.0, 0.0, 0.0, 0.5 };
  41.    GLfloat green_mat[] = { 0.0, 1.0, 0.0, 0.5 };
  42.    GLfloat blue_mat[]  = { 0.0, 0.0, 1.0, 0.5};
  43.  
  44.    glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
  45.    glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
  46.    glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
  47.    glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  48.     
  49.    glEnable(GL_LIGHTING);
  50.    glEnable(GL_LIGHT0);
  51.    glEnable(GL_DEPTH_TEST);
  52.  
  53.    glMatrixMode(GL_PROJECTION);
  54.    glLoadIdentity();
  55.    glOrtho(-2.5, 2.5, -2.5, 2.5, -10.0, 10.0);
  56.    glMatrixMode(GL_MODELVIEW);
  57.  
  58.    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  59.  
  60.    glPushMatrix();
  61.    glRotatef(20.0, 1.0, 0.0, 0.0);
  62.  
  63.    glPushMatrix();
  64.    glTranslatef(-0.75, 0.5, 0.0); 
  65.    glRotatef(90.0, 1.0, 0.0, 0.0);
  66.    glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, red_mat );
  67.    auxSolidTorus(0.275, 0.85);
  68.    glPopMatrix();
  69.  
  70.    glPushMatrix();
  71.    glTranslatef(-0.75, -0.5, 0.0); 
  72.    glRotatef(270.0, 1.0, 0.0, 0.0);
  73.    glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, green_mat );
  74.    auxSolidCone(1.0, 2.0);
  75.    glPopMatrix();
  76.  
  77.    glPushMatrix();
  78.    glTranslatef(0.75, 0.0, -1.0); 
  79.    glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, blue_mat );
  80.    auxSolidSphere(1.0);
  81.    glPopMatrix();
  82.  
  83.    glPopMatrix();
  84. }
  85.  
  86.  
  87.  
  88. int main( int argc, char *argv[] )
  89. {
  90.    OSMesaContext ctx;
  91.    void *buffer;
  92.    id myWindow, myView, myMenu, image;
  93.    char name[50];
  94.    NXRect GR;
  95.    NXPoint position;
  96.      
  97.    NXApp=[Application new];
  98.  
  99.    /* Create an RGBA-mode context */
  100.    ctx = OSMesaCreateContext( GL_RGBA, NULL );
  101.  
  102.    /* Allocate the image buffer */
  103.    buffer = malloc( WIDTH * HEIGHT * 4 );
  104.    
  105.    /* Bind the buffer to the context and make it current */
  106.    OSMesaMakeCurrent( ctx, buffer, GL_UNSIGNED_BYTE, WIDTH, HEIGHT );
  107.  
  108.   image = [ [ NXBitmapImageRep alloc] 
  109.         initData: buffer
  110.         pixelsWide: WIDTH
  111.         pixelsHigh: HEIGHT
  112.         bitsPerSample: 8
  113.         samplesPerPixel: 4
  114.         hasAlpha: YES
  115.         isPlanar: NO
  116.         colorSpace: 2
  117.         bytesPerRow: WIDTH*4
  118.         bitsPerPixel: 32]; 
  119.  
  120.   NXSetRect(&GR,100,100,WIDTH,HEIGHT);
  121.  
  122.   myWindow = [ [ Window alloc]
  123.             initContent: &GR
  124.             style: NX_TITLEDSTYLE
  125.             backing: NX_BUFFERED
  126.             buttonMask: NX_MINIATURIZEBUTTONMASK
  127.             defer: NO];
  128.  
  129.   sprintf(name, "Graphic Rectangle %d", getpid());
  130.  
  131.    myView = [ [ View alloc] initFrame:&GR];
  132.    [myView setOpaque: NO];
  133.    [myView setDrawOrigin: -WIDTH: -HEIGHT];
  134.    [myView rotate: 180.0];
  135.  
  136.    myMenu = [ [ Menu alloc] initTitle: "NeXT-OpenGL"];
  137.    [myMenu addItem: "Quit" action: @selector(terminate:) keyEquivalent: 'q'];
  138.    [myMenu sizeToFit];
  139.  
  140.    [myWindow setTitle: name];
  141.    [myWindow display];
  142.    [myWindow setContentView: myView];
  143.    [myWindow makeKeyAndOrderFront: nil];
  144.  
  145.    [NXApp setMainMenu: myMenu];
  146.  
  147.    [myView lockFocus];
  148.  
  149.    /* here is the Mesa call */
  150.    render_image();
  151.  
  152.    [image draw];
  153.    [image free];
  154.  
  155.    [myWindow flushWindow];
  156.    [myView unlockFocus];
  157.  
  158.    /* free the image buffer */
  159.    free( buffer );
  160.  
  161.    /* destroy the context */
  162.    OSMesaDestroyContext( ctx );
  163.  
  164.    [NXApp run];
  165.    [NXApp free];
  166.  
  167.    return 0;
  168. }
  169.