home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / coders / mesa-1.2.8 / next / nextdemo3.m < prev    next >
Text File  |  1996-05-27  |  9KB  |  258 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.  *  teapots.c
  39.  *  This program demonstrates lots of material properties.
  40.  *  A single light source illuminates the objects.
  41.  *
  42.  * NEXTSTEP output provided by Pascal Thibaudeau 
  43.  * pthibaud@frbdx11.cribx1.u-bordeaux.fr
  44.  */
  45. #import <dpsclient/wraps.h>
  46. #import <appkit/Application.h>
  47. #import <appkit/Window.h>
  48. #import <appkit/Menu.h>
  49. #import <appkit/View.h>
  50. #import <appkit/color.h>
  51. #import <appkit/NXBitmapImageRep.h>
  52.  
  53. #include <GL/gl.h>
  54. #include <GL/glu.h>
  55. #include <stdlib.h>
  56. #include "GL/osmesa.h"
  57.  
  58. #define WIDTH 400
  59. #define HEIGHT 400
  60.  
  61. /*  Move object into position.  Use 3rd through 12th parameters
  62.  *  to specify the material property.  Draw a teapot.
  63.  */
  64. void renderTeapot (GLfloat x, GLfloat y, 
  65.     GLfloat ambr, GLfloat ambg, GLfloat ambb, 
  66.     GLfloat difr, GLfloat difg, GLfloat difb, 
  67.     GLfloat specr, GLfloat specg, GLfloat specb, GLfloat shine)
  68. {
  69.     float mat[4];
  70.  
  71.     glPushMatrix();
  72.     glTranslatef (x, y, 0.0);
  73.     mat[0] = ambr; mat[1] = ambg; mat[2] = ambb; mat[3] = 1.0;    
  74.     glMaterialfv (GL_FRONT, GL_AMBIENT, mat);
  75.     mat[0] = difr; mat[1] = difg; mat[2] = difb;    
  76.     glMaterialfv (GL_FRONT, GL_DIFFUSE, mat);
  77.     mat[0] = specr; mat[1] = specg; mat[2] = specb;
  78.     glMaterialfv (GL_FRONT, GL_SPECULAR, mat);
  79.     glMaterialf (GL_FRONT, GL_SHININESS, shine*128.0);
  80.     auxSolidTeapot(1.0);
  81.     glPopMatrix();
  82. }
  83.  
  84.  
  85. /*  Initialize depth buffer, projection matrix, light source, 
  86.  *  and lighting model.  Do not specify a material property here.
  87.  */
  88. static void render_image(void)
  89. {
  90.     GLfloat ambient[] = { 0.0, 0.0, 0.0, 1.0 };
  91.     GLfloat diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
  92.     GLfloat specular[] = { 1.0, 1.0, 1.0, 1.0 };
  93.     GLfloat position[] = { 0.0, 3.0, 3.0, 0.0 };
  94.     
  95.     GLfloat lmodel_ambient[] = { 0.2, 0.2, 0.2, 1.0 };
  96.     GLfloat local_view[] = { 0.0 };
  97.  
  98.     glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
  99.     glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
  100.     glLightfv(GL_LIGHT0, GL_POSITION, position);
  101.     glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
  102.     glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, local_view);
  103.  
  104.     glFrontFace (GL_CW);
  105.     glEnable(GL_LIGHTING);
  106.     glEnable(GL_LIGHT0);
  107.     glEnable(GL_AUTO_NORMAL);
  108.     glEnable(GL_NORMALIZE);
  109.     glEnable(GL_DEPTH_TEST);
  110.     glDepthFunc(GL_LESS);
  111.  
  112.     glMatrixMode(GL_PROJECTION);
  113.     glLoadIdentity();
  114.     glOrtho (0.0, 16.0, 0.0, 16.0*(GLfloat)HEIGHT/(GLfloat)WIDTH, 
  115.         -10.0, 10.0);
  116.     glMatrixMode(GL_MODELVIEW);
  117.  
  118.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  119.     renderTeapot (2.0, 17.0, 0.0215, 0.1745, 0.0215, 
  120.     0.07568, 0.61424, 0.07568, 0.633, 0.727811, 0.633, 0.6);
  121.     renderTeapot (2.0, 14.0, 0.135, 0.2225, 0.1575,
  122.     0.54, 0.89, 0.63, 0.316228, 0.316228, 0.316228, 0.1);
  123.     renderTeapot (2.0, 11.0, 0.05375, 0.05, 0.06625,
  124.     0.18275, 0.17, 0.22525, 0.332741, 0.328634, 0.346435, 0.3);
  125.     renderTeapot (2.0, 8.0, 0.25, 0.20725, 0.20725,
  126.     1, 0.829, 0.829, 0.296648, 0.296648, 0.296648, 0.088);
  127.     renderTeapot (2.0, 5.0, 0.1745, 0.01175, 0.01175,
  128.     0.61424, 0.04136, 0.04136, 0.727811, 0.626959, 0.626959, 0.6);
  129.     renderTeapot (2.0, 2.0, 0.1, 0.18725, 0.1745,
  130.     0.396, 0.74151, 0.69102, 0.297254, 0.30829, 0.306678, 0.1);
  131.     renderTeapot (6.0, 17.0, 0.329412, 0.223529, 0.027451,
  132.     0.780392, 0.568627, 0.113725, 0.992157, 0.941176, 0.807843,
  133.     0.21794872);
  134.     renderTeapot (6.0, 14.0, 0.2125, 0.1275, 0.054,
  135.     0.714, 0.4284, 0.18144, 0.393548, 0.271906, 0.166721, 0.2);
  136.     renderTeapot (6.0, 11.0, 0.25, 0.25, 0.25, 
  137.     0.4, 0.4, 0.4, 0.774597, 0.774597, 0.774597, 0.6);
  138.     renderTeapot (6.0, 8.0, 0.19125, 0.0735, 0.0225,
  139.     0.7038, 0.27048, 0.0828, 0.256777, 0.137622, 0.086014, 0.1);
  140.     renderTeapot (6.0, 5.0, 0.24725, 0.1995, 0.0745,
  141.     0.75164, 0.60648, 0.22648, 0.628281, 0.555802, 0.366065, 0.4);
  142.     renderTeapot (6.0, 2.0, 0.19225, 0.19225, 0.19225,
  143.     0.50754, 0.50754, 0.50754, 0.508273, 0.508273, 0.508273, 0.4);
  144.     renderTeapot (10.0, 17.0, 0.0, 0.0, 0.0, 0.01, 0.01, 0.01,
  145.     0.50, 0.50, 0.50, .25);
  146.     renderTeapot (10.0, 14.0, 0.0, 0.1, 0.06, 0.0, 0.50980392, 0.50980392,
  147.     0.50196078, 0.50196078, 0.50196078, .25);
  148.     renderTeapot (10.0, 11.0, 0.0, 0.0, 0.0, 
  149.     0.1, 0.35, 0.1, 0.45, 0.55, 0.45, .25);
  150.     renderTeapot (10.0, 8.0, 0.0, 0.0, 0.0, 0.5, 0.0, 0.0,
  151.     0.7, 0.6, 0.6, .25);
  152.     renderTeapot (10.0, 5.0, 0.0, 0.0, 0.0, 0.55, 0.55, 0.55,
  153.     0.70, 0.70, 0.70, .25);
  154.     renderTeapot (10.0, 2.0, 0.0, 0.0, 0.0, 0.5, 0.5, 0.0,
  155.     0.60, 0.60, 0.50, .25);
  156.     renderTeapot (14.0, 17.0, 0.02, 0.02, 0.02, 0.01, 0.01, 0.01,
  157.     0.4, 0.4, 0.4, .078125);
  158.     renderTeapot (14.0, 14.0, 0.0, 0.05, 0.05, 0.4, 0.5, 0.5,
  159.     0.04, 0.7, 0.7, .078125);
  160.     renderTeapot (14.0, 11.0, 0.0, 0.05, 0.0, 0.4, 0.5, 0.4,
  161.     0.04, 0.7, 0.04, .078125);
  162.     renderTeapot (14.0, 8.0, 0.05, 0.0, 0.0, 0.5, 0.4, 0.4,
  163.     0.7, 0.04, 0.04, .078125);
  164.     renderTeapot (14.0, 5.0, 0.05, 0.05, 0.05, 0.5, 0.5, 0.5,
  165.     0.7, 0.7, 0.7, .078125);
  166.     renderTeapot (14.0, 2.0, 0.05, 0.05, 0.0, 0.5, 0.5, 0.4, 
  167.     0.7, 0.7, 0.04, .078125);
  168.     glFlush();
  169.  
  170. }
  171.  
  172. /*  Main Loop
  173.  *  Open window with initial window size, title bar, 
  174.  *  RGBA display mode, and handle input events.
  175.  */
  176. int main(int argc, char** argv)
  177. {
  178.  OSMesaContext ctx;
  179.    void *buffer;
  180.    id myWindow, myView, myMenu, image;
  181.    char name[50];
  182.    NXRect GR;
  183.    NXPoint position;
  184.    NXApp=[Application new];
  185.  
  186.    /* Create an RGBA-mode context */
  187.    ctx = OSMesaCreateContext( GL_RGBA, NULL );
  188.  
  189.    /* Allocate the image buffer */
  190.    buffer = malloc( WIDTH * HEIGHT * 4 );
  191.    
  192.    /* Bind the buffer to the context and make it current */
  193.    OSMesaMakeCurrent( ctx, buffer, GL_UNSIGNED_BYTE, WIDTH, HEIGHT );
  194.  
  195.   image = [ [ NXBitmapImageRep alloc] 
  196.         initData: buffer
  197.         pixelsWide: WIDTH
  198.         pixelsHigh: HEIGHT
  199.         bitsPerSample: 8
  200.         samplesPerPixel: 4
  201.         hasAlpha: YES
  202.         isPlanar: NO
  203.         colorSpace: 2
  204.         bytesPerRow: WIDTH*4
  205.         bitsPerPixel: 32]; 
  206.  
  207.   NXSetRect(&GR,100,100,WIDTH,HEIGHT);
  208.  
  209.   myWindow = [ [ Window alloc]
  210.             initContent: &GR
  211.             style: NX_TITLEDSTYLE
  212.             backing: NX_BUFFERED
  213.             buttonMask: NX_MINIATURIZEBUTTONMASK
  214.             defer: NO];
  215.  
  216.   sprintf(name, "Graphic Rectangle %d", getpid());
  217.  
  218.    myView = [ [ View alloc] initFrame:&GR];
  219.    [myView  setOpaque: NO];
  220.    [myView setDrawOrigin: -WIDTH: -HEIGHT];
  221.    [myView rotate: 180.0];
  222.  
  223.    myMenu = [ [ Menu alloc] initTitle: "NeXT-OpenGL"];
  224.    [myMenu addItem: "Quit" action: @selector(terminate:) keyEquivalent: 'q'];
  225.    [myMenu sizeToFit];
  226.  
  227.    [myWindow setTitle: name];
  228.    [myWindow display];
  229.    [myWindow setContentView: myView];
  230.    [myWindow makeKeyAndOrderFront: nil];
  231.  
  232.    [NXApp setMainMenu: myMenu];
  233.  
  234.    [myView lockFocus];
  235.  
  236.    /* here is the Mesa call */
  237.    render_image();
  238.  
  239.    [image draw];
  240.    [image free];
  241.  
  242.    [myWindow flushWindow];
  243.    [myView unlockFocus];
  244.  
  245.    /* free the image buffer */
  246.    free( buffer );
  247.  
  248.    /* destroy the context */
  249.    OSMesaDestroyContext( ctx );
  250.  
  251.    [NXApp run];
  252.    [NXApp free];
  253.  
  254.    return 0;
  255.  
  256.       }
  257.  
  258.