home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / coders / mesa-1.2.8 / src-glu / glu.c < prev    next >
C/C++ Source or Header  |  1996-05-27  |  9KB  |  357 lines

  1. /* $Id: glu.c,v 1.21 1996/05/15 18:36:22 brianp Exp $ */
  2.  
  3. /*
  4.  * Mesa 3-D graphics library
  5.  * Version:  1.2
  6.  * Copyright (C) 1995-1996  Brian Paul  (brianp@ssec.wisc.edu)
  7.  *
  8.  * This library is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU Library General Public
  10.  * License as published by the Free Software Foundation; either
  11.  * version 2 of the License, or (at your option) any later version.
  12.  *
  13.  * This library is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.  * Library General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU Library General Public
  19.  * License along with this library; if not, write to the Free
  20.  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  */
  22.  
  23.  
  24. /*
  25. $Log: glu.c,v $
  26.  * Revision 1.21  1996/05/15  18:36:22  brianp
  27.  * changed version string to 1.2.8
  28.  *
  29.  * Revision 1.20  1996/02/13  22:22:07  brianp
  30.  * changed version string to 1.2.7
  31.  *
  32.  * Revision 1.19  1996/01/22  16:14:05  brianp
  33.  * changed version string to 1.2.6
  34.  *
  35.  * Revision 1.18  1995/11/29  18:02:08  brianp
  36.  * changed version string to 1.2.5
  37.  *
  38.  * Revision 1.17  1995/10/19  16:34:40  brianp
  39.  * updated version string to 1.2.4
  40.  *
  41.  * Revision 1.16  1995/09/19  13:33:06  brianp
  42.  * change PI to M_PI
  43.  * incorporated Bogdan Sikorski's Sep 19 updates
  44.  *
  45.  * Revision 1.15  1995/07/28  21:35:49  brianp
  46.  * changed all GLUenum to GLenum
  47.  *
  48.  * Revision 1.14  1995/07/18  20:23:05  brianp
  49.  * much more complete gluErrorString() implementation
  50.  * updated gluGetString(GLU_VERSION) for 1.2.2
  51.  *
  52.  * Revision 1.13  1995/06/09  21:48:05  brianp
  53.  * changed version string to 1.2.1
  54.  *
  55.  * Revision 1.12  1995/05/24  12:55:02  brianp
  56.  * changed gluGetString version to 1.2
  57.  *
  58.  * Revision 1.11  1995/05/22  16:56:20  brianp
  59.  * Release 1.2
  60.  *
  61.  * Revision 1.10  1995/05/16  19:17:21  brianp
  62.  * minor changes to allow compilation with real OpenGL headers
  63.  *
  64.  * Revision 1.9  1995/05/15  13:38:02  brianp
  65.  * fixed gluLookAt() bug per Michael Pichler
  66.  *
  67.  * Revision 1.8  1995/04/28  16:21:29  brianp
  68.  * added tesselation errors to gluErrorString()
  69.  *
  70.  * Revision 1.7  1995/04/18  15:51:21  brianp
  71.  * fixed warnings on Suns
  72.  *
  73.  * Revision 1.6  1995/04/17  13:45:03  brianp
  74.  * added gluGetString for GLU 1.1
  75.  *
  76.  * Revision 1.5  1995/03/16  20:37:44  brianp
  77.  * fixed gluPickMatrix
  78.  *
  79.  * Revision 1.4  1995/03/06  17:34:45  brianp
  80.  * fixed gluOrtho2D bug
  81.  * removed unused gluProject and gluUnproject functions
  82.  *
  83.  * Revision 1.3  1995/03/04  19:39:18  brianp
  84.  * version 1.1 beta
  85.  *
  86.  * Revision 1.2  1995/02/24  15:54:21  brianp
  87.  * ifdef'd out gluProject, gluUnproject stubs
  88.  *
  89.  * Revision 1.1  1995/02/24  15:45:01  brianp
  90.  * Initial revision
  91.  *
  92.  */
  93.  
  94.  
  95. #include <math.h>
  96. #include <stdio.h>
  97. #include <stdlib.h>
  98. #include "gluP.h"
  99.  
  100.  
  101.  
  102. /*
  103.  * Miscellaneous utility functions
  104.  */
  105.  
  106.  
  107. #ifndef M_PI
  108. #define M_PI 3.1415926536
  109. #endif
  110. #define EPS 0.00001
  111.  
  112.  
  113.  
  114.  
  115. void gluLookAt( GLdouble eyex, GLdouble eyey, GLdouble eyez,
  116.         GLdouble centerx, GLdouble centery, GLdouble centerz,
  117.         GLdouble upx, GLdouble upy, GLdouble upz )
  118. {
  119.    GLdouble m[16];
  120.    GLdouble x[3], y[3], z[3];
  121.    GLdouble mag;
  122.  
  123.    /* Make rotation matrix */
  124.  
  125.    /* Z vector */
  126.    z[0] = eyex - centerx;
  127.    z[1] = eyey - centery;
  128.    z[2] = eyez - centerz;
  129.    mag = sqrt( z[0]*z[0] + z[1]*z[1] + z[2]*z[2] );
  130.    if (mag) {  /* mpichler, 19950515 */
  131.       z[0] /= mag;
  132.       z[1] /= mag;
  133.       z[2] /= mag;
  134.    }
  135.  
  136.    /* Y vector */
  137.    y[0] = upx;
  138.    y[1] = upy;
  139.    y[2] = upz;
  140.  
  141.    /* X vector = Y cross Z */
  142.    x[0] =  y[1]*z[2] - y[2]*z[1];
  143.    x[1] = -y[0]*z[2] + y[2]*z[0];
  144.    x[2] =  y[0]*z[1] - y[1]*z[0];
  145.  
  146.    /* Recompute Y = Z cross X */
  147.    y[0] =  z[1]*x[2] - z[2]*x[1];
  148.    y[1] = -z[0]*x[2] + z[2]*x[0];
  149.    y[2] =  z[0]*x[1] - z[1]*x[0];
  150.  
  151.    /* mpichler, 19950515 */
  152.    /* cross product gives area of parallelogram, which is < 1.0 for
  153.     * non-perpendicular unit-length vectors; so normalize x, y here
  154.     */
  155.  
  156.    mag = sqrt( x[0]*x[0] + x[1]*x[1] + x[2]*x[2] );
  157.    if (mag) {
  158.       x[0] /= mag;
  159.       x[1] /= mag;
  160.       x[2] /= mag;
  161.    }
  162.  
  163.    mag = sqrt( y[0]*y[0] + y[1]*y[1] + y[2]*y[2] );
  164.    if (mag) {
  165.       y[0] /= mag;
  166.       y[1] /= mag;
  167.       y[2] /= mag;
  168.    }
  169.  
  170. #define M(row,col)  m[col*4+row]
  171.    M(0,0) = x[0];  M(0,1) = x[1];  M(0,2) = x[2];  M(0,3) = 0.0;
  172.    M(1,0) = y[0];  M(1,1) = y[1];  M(1,2) = y[2];  M(1,3) = 0.0;
  173.    M(2,0) = z[0];  M(2,1) = z[1];  M(2,2) = z[2];  M(2,3) = 0.0;
  174.    M(3,0) = 0.0;   M(3,1) = 0.0;   M(3,2) = 0.0;   M(3,3) = 1.0;
  175. #undef M
  176.    glMultMatrixd( m );
  177.  
  178.    /* Translate Eye to Origin */
  179.    glTranslated( -eyex, -eyey, -eyez );
  180.  
  181. }
  182.  
  183.  
  184.  
  185. void gluOrtho2D( GLdouble left, GLdouble right,
  186.          GLdouble bottom, GLdouble top )
  187. {
  188.    glOrtho( left, right, bottom, top, -1.0, 1.0 );
  189. }
  190.  
  191.  
  192.  
  193. void gluPerspective( GLdouble fovy, GLdouble aspect,
  194.              GLdouble zNear, GLdouble zFar )
  195. {
  196.    GLdouble xmin, xmax, ymin, ymax;
  197.  
  198.    ymax = zNear * tan( fovy * M_PI / 360.0 );
  199.    ymin = -ymax;
  200.  
  201.    xmin = ymin * aspect;
  202.    xmax = ymax * aspect;
  203.  
  204.    glFrustum( xmin, xmax, ymin, ymax, zNear, zFar );
  205. }
  206.  
  207.  
  208.  
  209. void gluPickMatrix( GLdouble x, GLdouble y,
  210.             GLdouble width, GLdouble height,
  211.             GLint viewport[4] )
  212. {
  213.    GLfloat m[16];
  214.    GLfloat sx, sy;
  215.    GLfloat tx, ty;
  216.  
  217.    sx = viewport[2] / width;
  218.    sy = viewport[3] / height;
  219.    tx = (viewport[2] + 2.0 * (viewport[0] - x)) / width;
  220.    ty = (viewport[3] + 2.0 * (viewport[1] - y)) / height;
  221.  
  222. #define M(row,col)  m[col*4+row]
  223.    M(0,0) = sx;   M(0,1) = 0.0;  M(0,2) = 0.0;  M(0,3) = tx;
  224.    M(1,0) = 0.0;  M(1,1) = sy;   M(1,2) = 0.0;  M(1,3) = ty;
  225.    M(2,0) = 0.0;  M(2,1) = 0.0;  M(2,2) = 1.0;  M(2,3) = 0.0;
  226.    M(3,0) = 0.0;  M(3,1) = 0.0;  M(3,2) = 0.0;  M(3,3) = 1.0;
  227. #undef M
  228.  
  229.    glMultMatrixf( m );
  230. }
  231.  
  232.  
  233.  
  234. const GLubyte* gluErrorString( GLenum errorCode )
  235. {
  236.    static char *tess_error[] = {
  237.       "missing gluEndPolygon",
  238.       "missing gluBeginPolygon",
  239.       "misoriented contour",
  240.       "vertex/edge intersection",
  241.       "misoriented or self-intersecting loops",
  242.       "coincident vertices",
  243.       "colinear vertices",
  244.       "intersecting edges",
  245.       "not coplanar contours"
  246.    };
  247.    static char *nurbs_error[] = {
  248.       "spline order un-supported",
  249.       "too few knots",
  250.       "valid knot range is empty",
  251.       "decreasing knot sequence knot",
  252.       "knot multiplicity greater than order of spline",
  253.       "endcurve() must follow bgncurve()",
  254.       "bgncurve() must precede endcurve()",
  255.       "missing or extra geometric data",
  256.       "can't draw pwlcurves",
  257.       "missing bgncurve()",
  258.       "missing bgnsurface()",
  259.       "endtrim() must precede endsurface()",
  260.       "bgnsurface() must precede endsurface()",
  261.       "curve of improper type passed as trim curve",
  262.       "bgnsurface() must precede bgntrim()",
  263.       "endtrim() must follow bgntrim()",
  264.       "bgntrim() must precede endtrim()",
  265.       "invalid or missing trim curve",
  266.       "bgntrim() must precede pwlcurve()",
  267.       "pwlcurve referenced twice",
  268.       "pwlcurve and nurbscurve mixed",
  269.       "improper usage of trim data type",
  270.       "nurbscurve referenced twice",
  271.       "nurbscurve and pwlcurve mixed",
  272.       "nurbssurface referenced twice",
  273.       "invalid property",
  274.       "endsurface() must follow bgnsurface()",
  275.       "misoriented trim curves",
  276.       "intersecting trim curves",
  277.       "UNUSED",
  278.       "unconnected trim curves",
  279.       "unknown knot error",
  280.       "negative vertex count encountered",
  281.       "negative byte-stride encounteed",
  282.       "unknown type descriptor",
  283.       "null control array or knot vector",
  284.       "duplicate point on pwlcurve"
  285.    };
  286.  
  287.    /* GL Errors */
  288.    if (errorCode==GL_NO_ERROR) {
  289.       return (GLubyte *) "no error";
  290.    }
  291.    else if (errorCode==GL_INVALID_VALUE) {
  292.       return (GLubyte *) "invalid value";
  293.    }
  294.    else if (errorCode==GL_INVALID_ENUM) {
  295.       return (GLubyte *) "invalid enum";
  296.    }
  297.    else if (errorCode==GL_INVALID_OPERATION) {
  298.       return (GLubyte *) "invalid operation";
  299.    }
  300.    else if (errorCode==GL_STACK_OVERFLOW) {
  301.       return (GLubyte *) "stack overflow";
  302.    }
  303.    else if (errorCode==GL_STACK_UNDERFLOW) {
  304.       return (GLubyte *) "stack underflow";
  305.    }
  306.    else if (errorCode==GL_OUT_OF_MEMORY) {
  307.       return (GLubyte *) "out of memory";
  308.    }
  309.    /* GLU Errors */
  310.    else if (errorCode==GLU_NO_ERROR) {
  311.       return (GLubyte *) "no error";
  312.    }
  313.    else if (errorCode==GLU_INVALID_ENUM) {
  314.       return (GLubyte *) "invalid enum";
  315.    }
  316.    else if (errorCode==GLU_INVALID_VALUE) {
  317.       return (GLubyte *) "invalid value";
  318.    }
  319.    else if (errorCode==GLU_OUT_OF_MEMORY) {
  320.       return (GLubyte *) "out of memory";
  321.    }
  322.    else if (errorCode==GLU_INCOMPATIBLE_GL_VERSION) {
  323.       return (GLubyte *) "incompatible GL version";
  324.    }
  325.    else if (errorCode>=GLU_TESS_ERROR1 && errorCode<=GLU_TESS_ERROR9) {
  326.       return (GLubyte *) tess_error[errorCode-GLU_TESS_ERROR1];
  327.    }
  328.    else if (errorCode>=GLU_NURBS_ERROR1 && errorCode<=GLU_NURBS_ERROR37) {
  329.       return (GLubyte *) nurbs_error[errorCode-GLU_NURBS_ERROR1];
  330.    }
  331.    else {
  332.       return NULL;
  333.    }
  334. }
  335.  
  336.  
  337.  
  338. /*
  339.  * New in GLU 1.1
  340.  */
  341.  
  342. const GLubyte* gluGetString( GLenum name )
  343. {
  344.    static char *extensions = "";
  345.    static char *version = "1.2.8 Mesa";
  346.  
  347.    switch (name) {
  348.       case GLU_EXTENSIONS:
  349.          return (GLubyte *) extensions;
  350.       case GLU_VERSION:
  351.      return (GLubyte *) version;
  352.       default:
  353.      return NULL;
  354.    }
  355. }
  356.  
  357.