home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / coders / mesa-1.2.8 / next / nextdemo6.m < prev    next >
Text File  |  1996-05-27  |  6KB  |  216 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. /*  texgen.c
  38.  *  This program draws a texture mapped teapot with 
  39.  *  automatically generated texture coordinates.  The
  40.  *  texture is rendered as stripes on the teapot.
  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. #define    stripeImageWidth 16
  62. GLubyte stripeImage[3*stripeImageWidth];
  63.  
  64. void makeStripeImage(void)
  65. {
  66.     int j;
  67.     
  68.     for (j = 0; j < stripeImageWidth; j++) {
  69.         stripeImage[3*j] = (j<=4) ? 255 : 0;
  70.         stripeImage[3*j+1] = (j>4) ? 255 : 0;
  71.         stripeImage[3*j+2] = 0;
  72.     }
  73. }
  74.  
  75. /* glTexGen stuff: */
  76.  
  77. GLfloat sgenparams[] = {1.0, 1.0, 1.0, 0.0};
  78.  
  79. static void render_image(void)
  80. {
  81.     glViewport(0, 0, WIDTH, HEIGHT);
  82.     glMatrixMode(GL_PROJECTION);
  83.     glLoadIdentity();
  84.     if (WIDTH <= HEIGHT)
  85.     glOrtho (-3.5, 3.5, -3.5*(GLfloat)HEIGHT/(GLfloat)WIDTH, 
  86.         3.5*(GLfloat)HEIGHT/(GLfloat)WIDTH, -3.5, 3.5);
  87.     else
  88.     glOrtho (-3.5*(GLfloat)WIDTH/(GLfloat)HEIGHT, 
  89.         3.5*(GLfloat)WIDTH/(GLfloat)HEIGHT, -3.5, 3.5, -3.5, 3.5);
  90.     glMatrixMode(GL_MODELVIEW);
  91.     glLoadIdentity();
  92.  
  93.     glClearColor (0.0, 0.0, 0.0, 0.0);
  94.  
  95.     makeStripeImage();
  96.     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  97.     glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  98.     glTexParameterf(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  99.     glTexParameterf(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  100.     glTexParameterf(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  101.     glTexImage1D(GL_TEXTURE_1D, 0, 3, stripeImageWidth, 0,
  102.          GL_RGB, GL_UNSIGNED_BYTE, stripeImage);
  103.  
  104.     glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
  105.     glTexGenfv(GL_S, GL_OBJECT_PLANE, sgenparams);
  106.  
  107.     glEnable(GL_DEPTH_TEST);
  108.     glDepthFunc(GL_LESS);
  109.     glEnable(GL_TEXTURE_GEN_S);
  110.     glEnable(GL_TEXTURE_1D);
  111.     glEnable(GL_CULL_FACE);
  112.     glEnable(GL_LIGHTING);
  113.     glEnable(GL_LIGHT0);
  114.     glEnable(GL_AUTO_NORMAL);
  115.     glEnable(GL_NORMALIZE);
  116.     glFrontFace(GL_CW);
  117.     glCullFace(GL_BACK);
  118.     glMaterialf (GL_FRONT, GL_SHININESS, 64.0);
  119.  
  120.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  121.     glPushMatrix ();
  122.     glRotatef(-45.0, -180.0, -40.0, 1.0);
  123.     auxSolidTeapot(3.0);
  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.  
  215.  
  216.