home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / coders / mesa-1.2.8 / next / nextdemo5.m < prev    next >
Text File  |  1996-05-27  |  12KB  |  358 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.  * material.c
  39.  * This program demonstrates the use of the GL lighting model.
  40.  * Several objects are drawn using different material characteristics.
  41.  * A single light source illuminates 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 600
  60. #define HEIGHT 450
  61.  
  62. /*  Initialize z-buffer, projection matrix, light source, 
  63.  *  and lighting model.  Do not specify a material property here.
  64.  */
  65. static void render_image(void)
  66. {
  67.     GLfloat ambient[] = { 0.0, 0.0, 0.0, 1.0 };
  68.     GLfloat diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
  69.     GLfloat specular[] = { 1.0, 1.0, 1.0, 1.0 };
  70.     GLfloat position[] = { 0.0, 3.0, 2.0, 0.0 };
  71.     GLfloat lmodel_ambient[] = { 0.4, 0.4, 0.4, 1.0 };
  72.     GLfloat local_view[] = { 0.0 };
  73.  
  74.     GLfloat no_mat[] = { 0.0, 0.0, 0.0, 1.0 };
  75.     GLfloat mat_ambient[] = { 0.7, 0.7, 0.7, 1.0 };
  76.     GLfloat mat_ambient_color[] = { 0.8, 0.8, 0.2, 1.0 };
  77.     GLfloat mat_diffuse[] = { 0.1, 0.5, 0.8, 1.0 };
  78.     GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
  79.     GLfloat no_shininess[] = { 0.0 };
  80.     GLfloat low_shininess[] = { 5.0 };
  81.     GLfloat high_shininess[] = { 100.0 };
  82.     GLfloat mat_emission[] = {0.3, 0.2, 0.2, 0.0};
  83.  
  84.     glEnable(GL_DEPTH_TEST);
  85.     glDepthFunc(GL_LESS);
  86.  
  87.     glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
  88.     glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
  89.     glLightfv(GL_LIGHT0, GL_POSITION, position);
  90.     glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
  91.     glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, local_view);
  92.  
  93.     glEnable(GL_LIGHTING);
  94.     glEnable(GL_LIGHT0);
  95.  
  96.     glClearColor(0.0, 0.1, 0.1, 0.0);
  97.  
  98.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  99.  
  100.     glViewport(0, 0, WIDTH, HEIGHT);
  101.     glMatrixMode(GL_PROJECTION);
  102.     glLoadIdentity();
  103.     if (WIDTH<= (HEIGHT * 2))
  104.     glOrtho (-6.0, 6.0, -3.0*((GLfloat)HEIGHT*2)/(GLfloat)WIDTH, 
  105.         3.0*((GLfloat)HEIGHT*2)/(GLfloat)WIDTH, -10.0, 10.0);
  106.     else
  107.     glOrtho (-6.0*(GLfloat)WIDTH/((GLfloat)HEIGHT*2), 
  108.         6.0*(GLfloat)WIDTH/((GLfloat)HEIGHT*2), -3.0, 3.0, -10.0, 10.0);
  109.     glMatrixMode(GL_MODELVIEW);
  110.  
  111.  
  112. /*  draw sphere in first row, first column
  113.  *  diffuse reflection only; no ambient or specular  
  114.  */
  115.     glPushMatrix();
  116.     glTranslatef (-3.75, 3.0, 0.0);
  117.     glMaterialfv(GL_FRONT, GL_AMBIENT, no_mat);
  118.     glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  119.     glMaterialfv(GL_FRONT, GL_SPECULAR, no_mat);
  120.     glMaterialfv(GL_FRONT, GL_SHININESS, no_shininess);
  121.     glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
  122.     auxSolidSphere(1.0);
  123.     glPopMatrix();
  124.  
  125. /*  draw sphere in first row, second column
  126.  *  diffuse and specular reflection; low shininess; no ambient
  127.  */
  128.     glPushMatrix();
  129.     glTranslatef (-1.25, 3.0, 0.0);
  130.     glMaterialfv(GL_FRONT, GL_AMBIENT, no_mat);
  131.     glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  132.     glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  133.     glMaterialfv(GL_FRONT, GL_SHININESS, low_shininess);
  134.     glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
  135.     auxSolidSphere(1.0);
  136.     glPopMatrix();
  137.  
  138. /*  draw sphere in first row, third column
  139.  *  diffuse and specular reflection; high shininess; no ambient
  140.  */
  141.     glPushMatrix();
  142.     glTranslatef (1.25, 3.0, 0.0);
  143.     glMaterialfv(GL_FRONT, GL_AMBIENT, no_mat);
  144.     glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  145.     glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  146.     glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
  147.     glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
  148.     auxSolidSphere(1.0);
  149.     glPopMatrix();
  150.  
  151. /*  draw sphere in first row, fourth column
  152.  *  diffuse reflection; emission; no ambient or specular reflection
  153.  */
  154.     glPushMatrix();
  155.     glTranslatef (3.75, 3.0, 0.0);
  156.     glMaterialfv(GL_FRONT, GL_AMBIENT, no_mat);
  157.     glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  158.     glMaterialfv(GL_FRONT, GL_SPECULAR, no_mat);
  159.     glMaterialfv(GL_FRONT, GL_SHININESS, no_shininess);
  160.     glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission);
  161.     auxSolidSphere(1.0);
  162.     glPopMatrix();
  163.  
  164. /*  draw sphere in second row, first column
  165.  *  ambient and diffuse reflection; no specular  
  166.  */
  167.     glPushMatrix();
  168.     glTranslatef (-3.75, 0.0, 0.0);
  169.     glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
  170.     glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  171.     glMaterialfv(GL_FRONT, GL_SPECULAR, no_mat);
  172.     glMaterialfv(GL_FRONT, GL_SHININESS, no_shininess);
  173.     glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
  174.     auxSolidSphere(1.0);
  175.     glPopMatrix();
  176.  
  177. /*  draw sphere in second row, second column
  178.  *  ambient, diffuse and specular reflection; low shininess
  179.  */
  180.     glPushMatrix();
  181.     glTranslatef (-1.25, 0.0, 0.0);
  182.     glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
  183.     glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  184.     glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  185.     glMaterialfv(GL_FRONT, GL_SHININESS, low_shininess);
  186.     glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
  187.     auxSolidSphere(1.0);
  188.     glPopMatrix();
  189.  
  190. /*  draw sphere in second row, third column
  191.  *  ambient, diffuse and specular reflection; high shininess
  192.  */
  193.     glPushMatrix();
  194.     glTranslatef (1.25, 0.0, 0.0);
  195.     glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
  196.     glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  197.     glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  198.     glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
  199.     glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
  200.     auxSolidSphere(1.0);
  201.     glPopMatrix();
  202.  
  203. /*  draw sphere in second row, fourth column
  204.  *  ambient and diffuse reflection; emission; no specular
  205.  */
  206.     glPushMatrix();
  207.     glTranslatef (3.75, 0.0, 0.0);
  208.     glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
  209.     glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  210.     glMaterialfv(GL_FRONT, GL_SPECULAR, no_mat);
  211.     glMaterialfv(GL_FRONT, GL_SHININESS, no_shininess);
  212.     glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission);
  213.     auxSolidSphere(1.0);
  214.     glPopMatrix();
  215.  
  216. /*  draw sphere in third row, first column
  217.  *  colored ambient and diffuse reflection; no specular  
  218.  */
  219.     glPushMatrix();
  220.     glTranslatef (-3.75, -3.0, 0.0);
  221.     glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient_color);
  222.     glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  223.     glMaterialfv(GL_FRONT, GL_SPECULAR, no_mat);
  224.     glMaterialfv(GL_FRONT, GL_SHININESS, no_shininess);
  225.     glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
  226.     auxSolidSphere(1.0);
  227.     glPopMatrix();
  228.  
  229. /*  draw sphere in third row, second column
  230.  *  colored ambient, diffuse and specular reflection; low shininess
  231.  */
  232.     glPushMatrix();
  233.     glTranslatef (-1.25, -3.0, 0.0);
  234.     glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient_color);
  235.     glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  236.     glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  237.     glMaterialfv(GL_FRONT, GL_SHININESS, low_shininess);
  238.     glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
  239.     auxSolidSphere(1.0);
  240.     glPopMatrix();
  241.  
  242. /*  draw sphere in third row, third column
  243.  *  colored ambient, diffuse and specular reflection; high shininess
  244.  */
  245.     glPushMatrix();
  246.     glTranslatef (1.25, -3.0, 0.0);
  247.     glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient_color);
  248.     glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  249.     glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  250.     glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
  251.     glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
  252.     auxSolidSphere(1.0);
  253.     glPopMatrix();
  254.  
  255. /*  draw sphere in third row, fourth column
  256.  *  colored ambient and diffuse reflection; emission; no specular
  257.  */
  258.     glPushMatrix();
  259.     glTranslatef (3.75, -3.0, 0.0);
  260.     glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient_color);
  261.     glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  262.     glMaterialfv(GL_FRONT, GL_SPECULAR, no_mat);
  263.     glMaterialfv(GL_FRONT, GL_SHININESS, no_shininess);
  264.     glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission);
  265.     auxSolidSphere(1.0);
  266.     glPopMatrix();
  267.  
  268.     glFlush();
  269. }
  270.  
  271. /*  Main Loop
  272.  *  Open window with initial window size, title bar, 
  273.  *  RGBA display mode, and handle input events.
  274.  */
  275. int main(int argc, char** argv)
  276. {
  277.    OSMesaContext ctx;
  278.    void *buffer;
  279.    id myWindow, myView, myMenu, image;
  280.    char name[50];
  281.    NXRect GR;
  282.    NXPoint position;
  283.    NXApp=[Application new];
  284.  
  285.    /* Create an RGBA-mode context */
  286.    ctx = OSMesaCreateContext( GL_RGBA, NULL );
  287.  
  288.    /* Allocate the image buffer */
  289.    buffer = malloc( WIDTH * HEIGHT * 4 );
  290.    
  291.    /* Bind the buffer to the context and make it current */
  292.    OSMesaMakeCurrent( ctx, buffer, GL_UNSIGNED_BYTE, WIDTH, HEIGHT );
  293.  
  294.   image = [ [ NXBitmapImageRep alloc] 
  295.         initData: buffer
  296.         pixelsWide: WIDTH
  297.         pixelsHigh: HEIGHT
  298.         bitsPerSample: 8
  299.         samplesPerPixel: 4
  300.         hasAlpha: YES
  301.         isPlanar: NO
  302.         colorSpace: 2
  303.         bytesPerRow: WIDTH*4
  304.         bitsPerPixel: 32]; 
  305.  
  306.   NXSetRect(&GR,100,100,WIDTH,HEIGHT);
  307.  
  308.   myWindow = [ [ Window alloc]
  309.             initContent: &GR
  310.             style: NX_TITLEDSTYLE
  311.             backing: NX_BUFFERED
  312.             buttonMask: NX_MINIATURIZEBUTTONMASK
  313.             defer: NO];
  314.  
  315.   sprintf(name, "OpenGL Graphic Rectangle %d", getpid());
  316.  
  317.    myView = [ [ View alloc] initFrame:&GR];
  318.    [myView  setOpaque: NO];
  319.    [myView setDrawOrigin: -WIDTH: -HEIGHT];
  320.    [myView rotate: 180.0];
  321.  
  322.    myMenu = [ [ Menu alloc] initTitle: "NeXT-Mesa"];
  323.    [myMenu addItem: "Quit" action: @selector(terminate:) keyEquivalent: 'q'];
  324.    [myMenu sizeToFit];
  325.  
  326.    [myWindow setTitle: name];
  327.    [myWindow display];
  328.    [myWindow setContentView: myView];
  329.    [myWindow makeKeyAndOrderFront: nil];
  330.  
  331.    [NXApp setMainMenu: myMenu];
  332.  
  333.    [myView lockFocus];
  334.  
  335.    /* here is the Mesa call */
  336.    render_image();
  337.  
  338.    [image draw];
  339.    [image free];
  340.  
  341.    [myWindow flushWindow];
  342.    [myView unlockFocus];
  343.  
  344.    /* free the image buffer */
  345.    free( buffer );
  346.  
  347.    /* destroy the context */
  348.    OSMesaDestroyContext( ctx );
  349.  
  350.    [NXApp run];
  351.    [NXApp free];
  352.  
  353.    return 0;
  354.  
  355.       }
  356.  
  357.  
  358.