home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / Mesa-1.2.1 / src-glu / glu.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-05  |  6.6 KB  |  274 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.13 1995/06/09 21:48:05 brianp Exp $
  26.  
  27. $Log: glu.c,v $
  28.  * Revision 1.13  1995/06/09  21:48:05  brianp
  29.  * changed version string to 1.2.1
  30.  *
  31.  * Revision 1.12  1995/05/24  12:55:02  brianp
  32.  * changed gluGetString version to 1.2
  33.  *
  34.  * Revision 1.11  1995/05/22  16:56:20  brianp
  35.  * Release 1.2
  36.  *
  37.  * Revision 1.10  1995/05/16  19:17:21  brianp
  38.  * minor changes to allow compilation with real OpenGL headers
  39.  *
  40.  * Revision 1.9  1995/05/15  13:38:02  brianp
  41.  * fixed gluLookAt() bug per Michael Pichler
  42.  *
  43.  * Revision 1.8  1995/04/28  16:21:29  brianp
  44.  * added tesselation errors to gluErrorString()
  45.  *
  46.  * Revision 1.7  1995/04/18  15:51:21  brianp
  47.  * fixed warnings on Suns
  48.  *
  49.  * Revision 1.6  1995/04/17  13:45:03  brianp
  50.  * added gluGetString for GLU 1.1
  51.  *
  52.  * Revision 1.5  1995/03/16  20:37:44  brianp
  53.  * fixed gluPickMatrix
  54.  *
  55.  * Revision 1.4  1995/03/06  17:34:45  brianp
  56.  * fixed gluOrtho2D bug
  57.  * removed unused gluProject and gluUnproject functions
  58.  *
  59.  * Revision 1.3  1995/03/04  19:39:18  brianp
  60.  * version 1.1 beta
  61.  *
  62.  * Revision 1.2  1995/02/24  15:54:21  brianp
  63.  * ifdef'd out gluProject, gluUnproject stubs
  64.  *
  65.  * Revision 1.1  1995/02/24  15:45:01  brianp
  66.  * Initial revision
  67.  *
  68.  */
  69.  
  70.  
  71. #include <math.h>
  72. #include <stdio.h>
  73. #include <stdlib.h>
  74. #include "gluP.h"
  75.  
  76.  
  77.  
  78. /*
  79.  * Miscellaneous utility functions
  80.  */
  81.  
  82.  
  83.  
  84. #define PI 3.14159
  85. #define EPS 0.00001
  86.  
  87.  
  88.  
  89.  
  90. void gluLookAt( GLdouble eyex, GLdouble eyey, GLdouble eyez,
  91.         GLdouble centerx, GLdouble centery, GLdouble centerz,
  92.         GLdouble upx, GLdouble upy, GLdouble upz )
  93. {
  94.    GLdouble m[16];
  95.    GLdouble x[3], y[3], z[3];
  96.    GLdouble mag;
  97.  
  98.    /* Make rotation matrix */
  99.  
  100.    /* Z vector */
  101.    z[0] = eyex - centerx;
  102.    z[1] = eyey - centery;
  103.    z[2] = eyez - centerz;
  104.    mag = sqrt( z[0]*z[0] + z[1]*z[1] + z[2]*z[2] );
  105.    if (mag) {  /* mpichler, 19950515 */
  106.       z[0] /= mag;
  107.       z[1] /= mag;
  108.       z[2] /= mag;
  109.    }
  110.  
  111.    /* Y vector */
  112.    y[0] = upx;
  113.    y[1] = upy;
  114.    y[2] = upz;
  115.  
  116.    /* X vector = Y cross Z */
  117.    x[0] =  y[1]*z[2] - y[2]*z[1];
  118.    x[1] = -y[0]*z[2] + y[2]*z[0];
  119.    x[2] =  y[0]*z[1] - y[1]*z[0];
  120.  
  121.    /* Recompute Y = Z cross X */
  122.    y[0] =  z[1]*x[2] - z[2]*x[1];
  123.    y[1] = -z[0]*x[2] + z[2]*x[0];
  124.    y[2] =  z[0]*x[1] - z[1]*x[0];
  125.  
  126.    /* mpichler, 19950515 */
  127.    /* cross product gives area of parallelogram, which is < 1.0 for
  128.     * non-perpendicular unit-length vectors; so normalize x, y here
  129.     */
  130.  
  131.    mag = sqrt( x[0]*x[0] + x[1]*x[1] + x[2]*x[2] );
  132.    if (mag) {
  133.       x[0] /= mag;
  134.       x[1] /= mag;
  135.       x[2] /= mag;
  136.    }
  137.  
  138.    mag = sqrt( y[0]*y[0] + y[1]*y[1] + y[2]*y[2] );
  139.    if (mag) {
  140.       y[0] /= mag;
  141.       y[1] /= mag;
  142.       y[2] /= mag;
  143.    }
  144.  
  145. #define M(row,col)  m[col*4+row]
  146.    M(0,0) = x[0];  M(0,1) = x[1];  M(0,2) = x[2];  M(0,3) = 0.0;
  147.    M(1,0) = y[0];  M(1,1) = y[1];  M(1,2) = y[2];  M(1,3) = 0.0;
  148.    M(2,0) = z[0];  M(2,1) = z[1];  M(2,2) = z[2];  M(2,3) = 0.0;
  149.    M(3,0) = 0.0;   M(3,1) = 0.0;   M(3,2) = 0.0;   M(3,3) = 1.0;
  150. #undef M
  151.    glMultMatrixd( m );
  152.  
  153.    /* Translate Eye to Origin */
  154.    glTranslated( -eyex, -eyey, -eyez );
  155. }
  156.  
  157.  
  158.  
  159. void gluOrtho2D( GLdouble left, GLdouble right,
  160.          GLdouble bottom, GLdouble top )
  161. {
  162.    glOrtho( left, right, bottom, top, -1.0, 1.0 );
  163. }
  164.  
  165.  
  166.  
  167. void gluPerspective( GLdouble fovy, GLdouble aspect,
  168.              GLdouble zNear, GLdouble zFar )
  169. {
  170.    GLdouble xmin, xmax, ymin, ymax;
  171.  
  172.    ymax = zNear * tan( fovy * PI / 360.0 );
  173.    ymin = -ymax;
  174.  
  175.    xmin = ymin * aspect;
  176.    xmax = ymax * aspect;
  177.  
  178.    glFrustum( xmin, xmax, ymin, ymax, zNear, zFar );
  179. }
  180.  
  181.  
  182.  
  183. void gluPickMatrix( GLdouble x, GLdouble y,
  184.             GLdouble width, GLdouble height,
  185.             GLint viewport[4] )
  186. {
  187.    GLfloat m[16];
  188.    GLfloat sx, sy;
  189.    GLfloat tx, ty;
  190.  
  191.    sx = viewport[2] / width;
  192.    sy = viewport[3] / height;
  193.    tx = (viewport[2] + 2.0 * (viewport[0] - x)) / width;
  194.    ty = (viewport[3] + 2.0 * (viewport[1] - y)) / height;
  195.  
  196. #define M(row,col)  m[col*4+row]
  197.    M(0,0) = sx;   M(0,1) = 0.0;  M(0,2) = 0.0;  M(0,3) = tx;
  198.    M(1,0) = 0.0;  M(1,1) = sy;   M(1,2) = 0.0;  M(1,3) = ty;
  199.    M(2,0) = 0.0;  M(2,1) = 0.0;  M(2,2) = 1.0;  M(2,3) = 0.0;
  200.    M(3,0) = 0.0;  M(3,1) = 0.0;  M(3,2) = 0.0;  M(3,3) = 1.0;
  201. #undef M
  202.  
  203.    glMultMatrixf( m );
  204. }
  205.  
  206.  
  207.  
  208. const GLubyte* gluErrorString( GLUenum errorCode )
  209. {
  210.    static char *enum_err = "invalid enum";
  211.    static char *value_err = "invalid value";
  212.    static char *mem_err = "out of memory";
  213.    static char *tess_err1 = "missing gluEndPolygon";
  214.    static char *tess_err2 = "missing gluBeginPolygon";
  215.    static char *tess_err3 = "misoriented contour";
  216.    static char *tess_err4 = "vertex/edge intersection";
  217.    static char *tess_err5 = "misoriented or self-intersecting loops";
  218.    static char *tess_err6 = "coincident vertices";
  219.    static char *tess_err7 = "colinear vertices";
  220.    static char *tess_err8 = "intersecting edges";
  221.    static char *tess_err9 = "not coplanar contours";
  222.  
  223.    switch (errorCode) {
  224.     case GLU_INVALID_ENUM:
  225.         return (GLubyte *) enum_err;
  226.     case GLU_INVALID_VALUE:
  227.         return (GLubyte *) value_err;
  228.     case GLU_OUT_OF_MEMORY:
  229.         return (GLubyte *) mem_err;
  230.     case GLU_TESS_ERROR1:
  231.         return (GLubyte *) tess_err1;
  232.     case GLU_TESS_ERROR2:
  233.         return (GLubyte *) tess_err2;
  234.     case GLU_TESS_ERROR3:
  235.         return (GLubyte *) tess_err3;
  236.     case GLU_TESS_ERROR4:
  237.         return (GLubyte *) tess_err4;
  238.     case GLU_TESS_ERROR5:
  239.         return (GLubyte *) tess_err5;
  240.     case GLU_TESS_ERROR6:
  241.         return (GLubyte *) tess_err6;
  242.     case GLU_TESS_ERROR7:
  243.         return (GLubyte *) tess_err7;
  244.     case GLU_TESS_ERROR8:
  245.         return (GLubyte *) tess_err8;
  246.     case GLU_TESS_ERROR9:
  247.         return (GLubyte *) tess_err9;
  248.     default:
  249.         return NULL;
  250.    }
  251. }
  252.  
  253.  
  254.  
  255. /*
  256.  * New in GLU 1.1
  257.  */
  258.  
  259. const GLubyte* gluGetString( GLUenum name )
  260. {
  261.    static char *extensions = "";
  262.    static char *version = "1.2.1 Mesa";
  263.  
  264.    switch (name) {
  265.       case GLU_EXTENSIONS:
  266.          return (GLubyte *) extensions;
  267.       case GLU_VERSION:
  268.      return (GLubyte *) version;
  269.       default:
  270.      return NULL;
  271.    }
  272. }
  273.  
  274.