home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / coders / mesa-1.2.8 / next / nextdemo4.m < prev    next >
Text File  |  1996-05-27  |  6KB  |  207 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. /*  chess.c
  38.  *  This program texture maps a checkerboard image onto
  39.  *  two rectangles.  The texture coordinates for the 
  40.  *  rectangles are 0.0 to 3.0.
  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 <math.h>
  57. #include "GL/osmesa.h"
  58.  
  59. #define WIDTH 500
  60. #define HEIGHT 500
  61.  
  62. #define    checkImageWidth 64
  63. #define    checkImageHeight 64
  64. GLubyte checkImage[checkImageWidth][checkImageHeight][3];
  65.  
  66. void makecheckimage(void)
  67. {
  68.     int i, j, r, c;
  69.     
  70.     for (i = 0; i < checkImageWidth; i++) {
  71.     for (j = 0; j < checkImageHeight; j++) {
  72.         c = ((((i&0x8)==0)^((j&0x8))==0))*255;
  73.         checkImage[i][j][0] = (GLubyte) c;
  74.         checkImage[i][j][1] = (GLubyte) c;
  75.         checkImage[i][j][2] = (GLubyte) c;
  76.     }
  77.     }
  78. }
  79.  
  80. static void render_image(void)
  81. {
  82.     glEnable(GL_DEPTH_TEST);
  83.     glDepthFunc(GL_LESS);
  84.  
  85.     makecheckimage();
  86.     glTexImage2D(GL_TEXTURE_2D, 0, 3, 
  87.     checkImageWidth, checkImageHeight, 0,
  88.     GL_RGB, GL_UNSIGNED_BYTE, &checkImage[0][0][0]);
  89.     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
  90.     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
  91.     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  92.     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  93.     glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
  94.     glEnable(GL_TEXTURE_2D);
  95.     glShadeModel(GL_FLAT);
  96.  
  97.     glViewport(0, 0, WIDTH, HEIGHT);
  98.     glMatrixMode(GL_PROJECTION);
  99.     glLoadIdentity();
  100.     gluPerspective(60.0, 1.0*(GLfloat)WIDTH/(GLfloat)HEIGHT, 1.0, 30.0);
  101.     glMatrixMode(GL_MODELVIEW);
  102.     glLoadIdentity();
  103.     glTranslatef(0.0, 0.0, -3.6);
  104.  
  105.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  106.     glBegin(GL_QUADS);
  107.     glTexCoord2f(0.0, 0.0); glVertex3f(-2.0, -1.0, 0.0);
  108.     glTexCoord2f(0.0, 3.0); glVertex3f(-2.0, 1.0, 0.0);
  109.     glTexCoord2f(3.0, 3.0); glVertex3f(0.0, 1.0, 0.0);
  110.     glTexCoord2f(3.0, 0.0); glVertex3f(0.0, -1.0, 0.0);
  111.  
  112.     glTexCoord2f(0.0, 0.0); glVertex3f(1.0, -1.0, 0.0);
  113.     glTexCoord2f(0.0, 3.0); glVertex3f(1.0, 1.0, 0.0);
  114.     glTexCoord2f(3.0, 3.0); glVertex3f(2.41421, 1.0, -1.41421);
  115.     glTexCoord2f(3.0, 0.0); glVertex3f(2.41421, -1.0, -1.41421);
  116.     glEnd();
  117.     glFlush();
  118.  
  119. }
  120.  
  121. /*  Main Loop
  122.  *  Open window with initial window size, title bar, 
  123.  *  RGBA display mode, and handle input events.
  124.  */
  125. int main(int argc, char** argv)
  126. {
  127.    OSMesaContext ctx;
  128.    void *buffer;
  129.    id myWindow, myView, myMenu, image;
  130.    char name[50];
  131.    NXRect GR;
  132.    NXPoint position;
  133.    NXApp=[Application new];
  134.  
  135.    /* Create an RGBA-mode context */
  136.    ctx = OSMesaCreateContext( GL_RGBA, NULL );
  137.  
  138.    /* Allocate the image buffer */
  139.    buffer = malloc( WIDTH * HEIGHT * 4 );
  140.    
  141.    /* Bind the buffer to the context and make it current */
  142.    OSMesaMakeCurrent( ctx, buffer, GL_UNSIGNED_BYTE, WIDTH, HEIGHT );
  143.  
  144.   image = [ [ NXBitmapImageRep alloc] 
  145.         initData: buffer
  146.         pixelsWide: WIDTH
  147.         pixelsHigh: HEIGHT
  148.         bitsPerSample: 8
  149.         samplesPerPixel: 4
  150.         hasAlpha: YES
  151.         isPlanar: NO
  152.         colorSpace: 2
  153.         bytesPerRow: WIDTH*4
  154.         bitsPerPixel: 32]; 
  155.  
  156.   NXSetRect(&GR,100,100,WIDTH,HEIGHT);
  157.  
  158.   myWindow = [ [ Window alloc]
  159.             initContent: &GR
  160.             style: NX_TITLEDSTYLE
  161.             backing: NX_BUFFERED
  162.             buttonMask: NX_MINIATURIZEBUTTONMASK
  163.             defer: NO];
  164.  
  165.   sprintf(name, "OpenGL Graphic Rectangle %d", getpid());
  166.  
  167.    myView = [ [ View alloc] initFrame:&GR];
  168.    [myView  setOpaque: NO];
  169.    [myView setDrawOrigin: -WIDTH: -HEIGHT];
  170.    [myView rotate: 180.0];
  171.  
  172.    myMenu = [ [ Menu alloc] initTitle: "NeXT-Mesa"];
  173.    [myMenu addItem: "Quit" action: @selector(terminate:) keyEquivalent: 'q'];
  174.    [myMenu sizeToFit];
  175.  
  176.    [myWindow setTitle: name];
  177.    [myWindow display];
  178.    [myWindow setContentView: myView];
  179.    [myWindow makeKeyAndOrderFront: nil];
  180.  
  181.    [NXApp setMainMenu: myMenu];
  182.  
  183.    [myView lockFocus];
  184.  
  185.    /* here is the Mesa call */
  186.    render_image();
  187.  
  188.    [image draw];
  189.    [image free];
  190.  
  191.    [myWindow flushWindow];
  192.    [myView unlockFocus];
  193.  
  194.    /* free the image buffer */
  195.    free( buffer );
  196.  
  197.    /* destroy the context */
  198.    OSMesaDestroyContext( ctx );
  199.  
  200.    [NXApp run];
  201.    [NXApp free];
  202.  
  203.    return 0;
  204.  
  205.       }
  206.  
  207.