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

  1. /*
  2.  * (c) Copyright 1993, Silicon Graphics, Inc.
  3.  * ALL RIGHTS RESERVED 
  4.  * Permission to use, copy, modify, and distribute this software for 
  5.  * any purpose and without fee is hereby granted, provided that the above
  6.  * copyright notice appear in all copies and that both the copyright notice
  7.  * and this permission notice appear in supporting documentation, and that 
  8.  * the name of Silicon Graphics, Inc. not be used in advertising
  9.  * or publicity pertaining to distribution of the software without specific,
  10.  * written prior permission. 
  11.  *
  12.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  13.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  14.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  15.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  16.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  17.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  18.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  19.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  20.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  21.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  22.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  23.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  24.  * 
  25.  * US Government Users Restricted Rights 
  26.  * Use, duplication, or disclosure by the Government is subject to
  27.  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  28.  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  29.  * clause at DFARS 252.227-7013 and/or in similar or successor
  30.  * clauses in the FAR or the DOD or NASA FAR Supplement.
  31.  * Unpublished-- rights reserved under the copyright laws of the
  32.  * United States.  Contractor/manufacturer is Silicon Graphics,
  33.  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  34.  *
  35.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  36.  */
  37. /*
  38.  *  original name: tea.c
  39.  *  This program demonstrates two-sided lighting and compares
  40.  *  it with one-sided lighting.  Three teapots are drawn, with
  41.  *  a clipping plane to expose the interior of the objects.
  42.  * 
  43.  * NEXTSTEP output provided by Pascal Thibaudeau 
  44.  * pthibaud@frbdx11.cribx1.u-bordeaux.fr
  45.  */
  46. #import <dpsclient/wraps.h>
  47. #import <appkit/Application.h>
  48. #import <appkit/Window.h>
  49. #import <appkit/Menu.h>
  50. #import <appkit/View.h>
  51. #import <appkit/color.h>
  52. #import <appkit/NXBitmapImageRep.h>
  53.  
  54. #include <GL/gl.h>
  55. #include <GL/glu.h>
  56. #include <stdlib.h>
  57. #include "GL/osmesa.h"
  58.  
  59. #define WIDTH 400
  60. #define HEIGHT 400
  61.  
  62. static void render_image(void)
  63. {
  64.     GLfloat light_ambient[] = { 0.0, 0.0, 0.0, 1.0 };
  65.     GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
  66.     GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
  67.  
  68. /*    light_position is NOT default value    */
  69.     GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
  70.     GLdouble eqn[4] = {1.0, 0.0, -1.0, 1.0};
  71.     GLfloat two_side_on[] = { GL_TRUE };
  72.     GLfloat two_side_off[] = { GL_FALSE };
  73.     GLfloat mat_diffuse[] = { 0.8, 0.8, 0.8, 1.0 };
  74.     GLfloat back_diffuse[] = { 0.8, 0.2, 0.8, 1.0 };
  75.  
  76.     glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
  77.     glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
  78.     glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
  79.     glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  80.     
  81.     glFrontFace (GL_CW);
  82.     glEnable(GL_LIGHTING);
  83.     glEnable(GL_LIGHT0);
  84.     glEnable(GL_AUTO_NORMAL);
  85.     glEnable(GL_NORMALIZE);
  86.     glDepthFunc(GL_LESS);
  87.     glEnable(GL_DEPTH_TEST);
  88.  
  89.     glMatrixMode(GL_PROJECTION);
  90.     glLoadIdentity();
  91.     glOrtho (-4.0, 4.0, -4.0*(GLfloat)HEIGHT/(GLfloat)WIDTH, 
  92.         4.0*(GLfloat)HEIGHT/(GLfloat)WIDTH, -10.0, 10.0);
  93.     glMatrixMode(GL_MODELVIEW);
  94.  
  95.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  96.  
  97.     glPushMatrix ();
  98.     glClipPlane (GL_CLIP_PLANE0, eqn);    /*  slice objects   */
  99.     glEnable (GL_CLIP_PLANE0); 
  100.  
  101.     glPushMatrix ();
  102.     glTranslatef (0.0, 2.0, 0.0);
  103.     auxSolidTeapot(1.0);    /*  one-sided lighting    */
  104.     glPopMatrix ();
  105.  
  106.     /*  two-sided lighting, but same material    */
  107.     glLightModelf (GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
  108.     glMaterialfv (GL_FRONT_AND_BACK, GL_DIFFUSE, mat_diffuse);
  109.     glPushMatrix ();
  110.     glTranslatef (0.0, 0.0, 0.0);
  111.     auxSolidTeapot(1.0);
  112.     glPopMatrix ();
  113.  
  114.     /*  two-sided lighting, two different materials    */
  115.     glMaterialfv (GL_FRONT, GL_DIFFUSE, mat_diffuse);
  116.     glMaterialfv (GL_BACK, GL_DIFFUSE, back_diffuse);
  117.     glPushMatrix ();
  118.     glTranslatef (0.0, -2.0, 0.0);
  119.     auxSolidTeapot(1.0);
  120.     glPopMatrix ();
  121.  
  122.     glLightModelf (GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE);
  123.     glDisable (GL_CLIP_PLANE0);
  124.     glPopMatrix ();
  125.     glFlush();
  126. }
  127.  
  128. /*  Main Loop
  129.  *  Open window with initial window size, title bar, 
  130.  *  RGBA display mode, and handle input events.
  131.  */
  132. int main(int argc, char** argv)
  133. {
  134.    OSMesaContext ctx;
  135.    void *buffer;
  136.    id myWindow, myView, myMenu, image;
  137.    char name[50];
  138.    NXRect GR;
  139.    NXPoint position;
  140.    NXApp=[Application new];
  141.  
  142.    /* Create an RGBA-mode context */
  143.    ctx = OSMesaCreateContext( GL_RGBA, NULL );
  144.  
  145.    /* Allocate the image buffer */
  146.    buffer = malloc( WIDTH * HEIGHT * 4 );
  147.    
  148.    /* Bind the buffer to the context and make it current */
  149.    OSMesaMakeCurrent( ctx, buffer, GL_UNSIGNED_BYTE, WIDTH, HEIGHT );
  150.  
  151.   image = [ [ NXBitmapImageRep alloc] 
  152.         initData: buffer
  153.         pixelsWide: WIDTH
  154.         pixelsHigh: HEIGHT
  155.         bitsPerSample: 8
  156.         samplesPerPixel: 4
  157.         hasAlpha: YES
  158.         isPlanar: NO
  159.         colorSpace: 2
  160.         bytesPerRow: WIDTH*4
  161.         bitsPerPixel: 32]; 
  162.  
  163.   NXSetRect(&GR,100,100,WIDTH,HEIGHT);
  164.  
  165.   myWindow = [ [ Window alloc]
  166.             initContent: &GR
  167.             style: NX_TITLEDSTYLE
  168.             backing: NX_BUFFERED
  169.             buttonMask: NX_MINIATURIZEBUTTONMASK
  170.             defer: NO];
  171.  
  172.   sprintf(name, "OpenGL Graphic Rectangle %d", getpid());
  173.  
  174.    myView = [ [ View alloc] initFrame:&GR];
  175.    [myView  setOpaque: NO];
  176.    [myView setDrawOrigin: -WIDTH: -HEIGHT];
  177.    [myView rotate: 180.0];
  178.  
  179.    myMenu = [ [ Menu alloc] initTitle: "NeXT-Mesa"];
  180.    [myMenu addItem: "Quit" action: @selector(terminate:) keyEquivalent: 'q'];
  181.    [myMenu sizeToFit];
  182.  
  183.    [myWindow setTitle: name];
  184.    [myWindow display];
  185.    [myWindow setContentView: myView];
  186.    [myWindow makeKeyAndOrderFront: nil];
  187.  
  188.    [NXApp setMainMenu: myMenu];
  189.  
  190.    [myView lockFocus];
  191.  
  192.    /* here is the Mesa call */
  193.    render_image();
  194.  
  195.    [image draw];
  196.    [image free];
  197.  
  198.    [myWindow flushWindow];
  199.    [myView unlockFocus];
  200.  
  201.    /* free the image buffer */
  202.    free( buffer );
  203.  
  204.    /* destroy the context */
  205.    OSMesaDestroyContext( ctx );
  206.  
  207.    [NXApp run];
  208.    [NXApp free];
  209.  
  210.    return 0;
  211.  
  212.       }
  213.  
  214.