home *** CD-ROM | disk | FTP | other *** search
/ Aminet 10 / aminetcdnumber101996.iso / Aminet / gfx / x11 / Mesa_Amiwin.lha / Mesa-Amiwin / src-glu / glu.c < prev    next >
C/C++ Source or Header  |  1995-11-29  |  9KB  |  350 lines

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