home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / mesa5.zip / mesa5src.zip / MesaDLL / api_eval.cpp < prev    next >
C/C++ Source or Header  |  2002-12-08  |  11KB  |  320 lines

  1. /* $Id: api_eval.c,v 1.3 2002/10/29 20:28:42 brianp Exp $ */
  2.  
  3. /*
  4.  * Mesa 3-D graphics library
  5.  * Version:  3.5
  6.  *
  7.  * Copyright (C) 1999-2001  Brian Paul   All Rights Reserved.
  8.  *
  9.  * Permission is hereby granted, free of charge, to any person obtaining a
  10.  * copy of this software and associated documentation files (the "Software"),
  11.  * to deal in the Software without restriction, including without limitation
  12.  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  13.  * and/or sell copies of the Software, and to permit persons to whom the
  14.  * Software is furnished to do so, subject to the following conditions:
  15.  *
  16.  * The above copyright notice and this permission notice shall be included
  17.  * in all copies or substantial portions of the Software.
  18.  *
  19.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  20.  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  22.  * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  23.  * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  24.  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25.  *
  26.  * Authors:
  27.  *    Keith Whitwell <keith@tungstengraphics.com>
  28.  */
  29.  
  30. #include "glheader.h"
  31. #include "api_eval.h"
  32. #include "context.h"
  33. #include "macros.h"
  34. #include "mmath.h"
  35. #include "math/m_eval.h"
  36.  
  37. static void do_EvalCoord1f(GLcontext* ctx, GLfloat u)
  38. {
  39.    /** Color Index **/
  40.    if (ctx->Eval.Map1Index)
  41.    {
  42.       GLfloat findex;
  43.       struct gl_1d_map *map = &ctx->EvalMap.Map1Index;
  44.       GLfloat uu = (u - map->u1) * map->du;
  45.       _math_horner_bezier_curve(map->Points, &findex, uu, 1, map->Order);
  46.       glIndexi( (GLint) findex );
  47.    }
  48.  
  49.    /** Color **/
  50.    if (ctx->Eval.Map1Color4) {
  51.       GLfloat fcolor[4];
  52.       struct gl_1d_map *map = &ctx->EvalMap.Map1Color4;
  53.       GLfloat uu = (u - map->u1) * map->du;
  54.       _math_horner_bezier_curve(map->Points, fcolor, uu, 4, map->Order);
  55.       glColor4fv( fcolor );
  56.    }
  57.  
  58.    /** Normal Vector **/
  59.    if (ctx->Eval.Map1Normal) {
  60.       GLfloat normal[3];
  61.       struct gl_1d_map *map = &ctx->EvalMap.Map1Normal;
  62.       GLfloat uu = (u - map->u1) * map->du;
  63.       _math_horner_bezier_curve(map->Points, normal, uu, 3, map->Order);
  64.       glNormal3fv( normal );
  65.    }
  66.  
  67.    /** Texture Coordinates **/
  68.    if (ctx->Eval.Map1TextureCoord4) {
  69.       GLfloat texcoord[4];
  70.       struct gl_1d_map *map = &ctx->EvalMap.Map1Texture4;
  71.       GLfloat uu = (u - map->u1) * map->du;
  72.       _math_horner_bezier_curve(map->Points, texcoord, uu, 4, map->Order);
  73.       glTexCoord4fv( texcoord );
  74.    }
  75.    else if (ctx->Eval.Map1TextureCoord3) {
  76.       GLfloat texcoord[4];
  77.       struct gl_1d_map *map = &ctx->EvalMap.Map1Texture3;
  78.       GLfloat uu = (u - map->u1) * map->du;
  79.       _math_horner_bezier_curve(map->Points, texcoord, uu, 3, map->Order);
  80.       glTexCoord3fv( texcoord );
  81.    }
  82.    else if (ctx->Eval.Map1TextureCoord2) {
  83.       GLfloat texcoord[4];
  84.       struct gl_1d_map *map = &ctx->EvalMap.Map1Texture2;
  85.       GLfloat uu = (u - map->u1) * map->du;
  86.       _math_horner_bezier_curve(map->Points, texcoord, uu, 2, map->Order);
  87.       glTexCoord2fv( texcoord );
  88.    }
  89.    else if (ctx->Eval.Map1TextureCoord1) {
  90.       GLfloat texcoord[4];
  91.       struct gl_1d_map *map = &ctx->EvalMap.Map1Texture1;
  92.       GLfloat uu = (u - map->u1) * map->du;
  93.       _math_horner_bezier_curve(map->Points, texcoord, uu, 1, map->Order);
  94.       glTexCoord1fv( texcoord );
  95.    }
  96.  
  97.    /** Vertex **/
  98.    if (ctx->Eval.Map1Vertex4)
  99.    {
  100.       GLfloat vertex[4];
  101.       struct gl_1d_map *map = &ctx->EvalMap.Map1Vertex4;
  102.       GLfloat uu = (u - map->u1) * map->du;
  103.       _math_horner_bezier_curve(map->Points, vertex, uu, 4, map->Order);
  104.       glVertex4fv( vertex );
  105.    }
  106.    else if (ctx->Eval.Map1Vertex3)
  107.    {
  108.       GLfloat vertex[4];
  109.       struct gl_1d_map *map = &ctx->EvalMap.Map1Vertex3;
  110.       GLfloat uu = (u - map->u1) * map->du;
  111.       _math_horner_bezier_curve(map->Points, vertex, uu, 3, map->Order);
  112.       glVertex3fv( vertex );
  113.    }
  114. }
  115.  
  116. #define CROSS_PROD(n, u, v) \
  117.   (n)[0] = (u)[1]*(v)[2] - (u)[2]*(v)[1]; \
  118.   (n)[1] = (u)[2]*(v)[0] - (u)[0]*(v)[2]; \
  119.   (n)[2] = (u)[0]*(v)[1] - (u)[1]*(v)[0]
  120.  
  121.  
  122. static void do_EvalCoord2f( GLcontext* ctx, GLfloat u, GLfloat v )
  123. {  /** Color Index **/
  124.    if (ctx->Eval.Map2Index) {
  125.       GLfloat findex;
  126.       struct gl_2d_map *map = &ctx->EvalMap.Map2Index;
  127.       GLfloat uu = (u - map->u1) * map->du;
  128.       GLfloat vv = (v - map->v1) * map->dv;
  129.       _math_horner_bezier_surf(map->Points, &findex, uu, vv, 1,
  130.                          map->Uorder, map->Vorder);
  131.       glIndexi( (GLuint) (GLint) findex );
  132.    }
  133.  
  134.    /** Color **/
  135.    if (ctx->Eval.Map2Color4) {
  136.       GLfloat fcolor[4];
  137.       struct gl_2d_map *map = &ctx->EvalMap.Map2Color4;
  138.       GLfloat uu = (u - map->u1) * map->du;
  139.       GLfloat vv = (v - map->v1) * map->dv;
  140.       _math_horner_bezier_surf(map->Points, fcolor, uu, vv, 4,
  141.                          map->Uorder, map->Vorder);
  142.       glColor4fv( fcolor );
  143.    }
  144.  
  145.    /** Normal **/
  146.    if (ctx->Eval.Map2Normal &&
  147.        (!ctx->Eval.AutoNormal || (!ctx->Eval.Map2Vertex3 &&
  148.                                  !ctx->Eval.Map2Vertex4))) {
  149.       GLfloat normal[3];
  150.       struct gl_2d_map *map = &ctx->EvalMap.Map2Normal;
  151.       GLfloat uu = (u - map->u1) * map->du;
  152.       GLfloat vv = (v - map->v1) * map->dv;
  153.       _math_horner_bezier_surf(map->Points, normal, uu, vv, 3,
  154.                         map->Uorder, map->Vorder);
  155.       glNormal3fv( normal );
  156.    }
  157.  
  158.    /** Texture Coordinates **/
  159.    if (ctx->Eval.Map2TextureCoord4) {
  160.       GLfloat texcoord[4];
  161.       struct gl_2d_map *map = &ctx->EvalMap.Map2Texture4;
  162.       GLfloat uu = (u - map->u1) * map->du;
  163.       GLfloat vv = (v - map->v1) * map->dv;
  164.       _math_horner_bezier_surf(map->Points, texcoord, uu, vv, 4,
  165.                          map->Uorder, map->Vorder);
  166.       glTexCoord4fv( texcoord );
  167.    }
  168.    else if (ctx->Eval.Map2TextureCoord3) {
  169.       GLfloat texcoord[4];
  170.       struct gl_2d_map *map = &ctx->EvalMap.Map2Texture3;
  171.       GLfloat uu = (u - map->u1) * map->du;
  172.       GLfloat vv = (v - map->v1) * map->dv;
  173.       _math_horner_bezier_surf(map->Points, texcoord, uu, vv, 3,
  174.                          map->Uorder, map->Vorder);
  175.       glTexCoord3fv( texcoord );
  176.    }
  177.    else if (ctx->Eval.Map2TextureCoord2) {
  178.       GLfloat texcoord[4];
  179.       struct gl_2d_map *map = &ctx->EvalMap.Map2Texture2;
  180.       GLfloat uu = (u - map->u1) * map->du;
  181.       GLfloat vv = (v - map->v1) * map->dv;
  182.       _math_horner_bezier_surf(map->Points, texcoord, uu, vv, 2,
  183.                          map->Uorder, map->Vorder);
  184.       glTexCoord2fv( texcoord );
  185.    }
  186.    else if (ctx->Eval.Map2TextureCoord1) {
  187.       GLfloat texcoord[4];
  188.       struct gl_2d_map *map = &ctx->EvalMap.Map2Texture1;
  189.       GLfloat uu = (u - map->u1) * map->du;
  190.       GLfloat vv = (v - map->v1) * map->dv;
  191.       _math_horner_bezier_surf(map->Points, texcoord, uu, vv, 1,
  192.                          map->Uorder, map->Vorder);
  193.       glTexCoord1fv( texcoord );
  194.    }
  195.  
  196.    /** Vertex **/
  197.    if(ctx->Eval.Map2Vertex4) {
  198.       GLfloat vertex[4];
  199.       GLfloat normal[3];
  200.       struct gl_2d_map *map = &ctx->EvalMap.Map2Vertex4;
  201.       GLfloat uu = (u - map->u1) * map->du;
  202.       GLfloat vv = (v - map->v1) * map->dv;
  203.  
  204.       if (ctx->Eval.AutoNormal) {
  205.          GLfloat du[4], dv[4];
  206.  
  207.          _math_de_casteljau_surf(map->Points, vertex, du, dv, uu, vv, 4,
  208.                                 map->Uorder, map->Vorder);
  209.  
  210.          CROSS_PROD(normal, du, dv);
  211.          NORMALIZE_3FV(normal);
  212.       }
  213.       else {
  214.          _math_horner_bezier_surf(map->Points, vertex, uu, vv, 4,
  215.                             map->Uorder, map->Vorder);
  216.       }
  217.    }
  218.    else if (ctx->Eval.Map2Vertex3) {
  219.       GLfloat vertex[4];
  220.       struct gl_2d_map *map = &ctx->EvalMap.Map2Vertex3;
  221.       GLfloat uu = (u - map->u1) * map->du;
  222.       GLfloat vv = (v - map->v1) * map->dv;
  223.       if (ctx->Eval.AutoNormal) {
  224.          GLfloat du[3], dv[3];
  225.         GLfloat normal[3];
  226.          _math_de_casteljau_surf(map->Points, vertex, du, dv, uu, vv, 3,
  227.                                 map->Uorder, map->Vorder);
  228.          CROSS_PROD(normal, du, dv);
  229.          NORMALIZE_3FV(normal);
  230.         glNormal3fv( normal );
  231.         glVertex3fv( vertex );
  232.       }
  233.       else {
  234.          _math_horner_bezier_surf(map->Points, vertex, uu, vv, 3,
  235.                             map->Uorder, map->Vorder);
  236.         glVertex3fv( vertex );
  237.       }
  238.    }
  239. }
  240.  
  241.  
  242. void _mesa_EvalPoint1( GLint i )
  243. {
  244.    GET_CURRENT_CONTEXT( ctx );
  245.    GLfloat du = ((ctx->Eval.MapGrid1u2 - ctx->Eval.MapGrid1u1) /
  246.                 (GLfloat) ctx->Eval.MapGrid1un);
  247.    GLfloat u = i * du + ctx->Eval.MapGrid1u1;
  248.  
  249.    glEvalCoord1f( u );
  250. }
  251.  
  252. void _mesa_EvalPoint2( GLint i, GLint j )
  253. {
  254.    GET_CURRENT_CONTEXT( ctx );
  255.    GLfloat du = ((ctx->Eval.MapGrid2u2 - ctx->Eval.MapGrid2u1) /
  256.                 (GLfloat) ctx->Eval.MapGrid2un);
  257.    GLfloat dv = ((ctx->Eval.MapGrid2v2 - ctx->Eval.MapGrid2v1) /
  258.                 (GLfloat) ctx->Eval.MapGrid2vn);
  259.    GLfloat u = i * du + ctx->Eval.MapGrid2u1;
  260.    GLfloat v = j * dv + ctx->Eval.MapGrid2v1;
  261.  
  262.    glEvalCoord2f( u, v );
  263. }
  264.  
  265. /* Wierd thing about eval is that it doesn't affect 'current' values.
  266.  * This technique of saving and resetting current values requires
  267.  * that:
  268.  *
  269.  *   1) Current values are updated immediately in the glColor,
  270.  *   etc. functions.
  271.  *
  272.  *   2) Hardware color values are stored seperately from ctx->Current,
  273.  *      for example in dma buffers, or direct emit to registers.
  274.  */
  275. void _mesa_EvalCoord1f( GLfloat u )
  276. {
  277.    GET_CURRENT_CONTEXT( ctx );
  278.    GLfloat normal[3], texcoord[4], color[4];
  279.    GLuint index;
  280.  
  281.    COPY_3FV( normal, ctx->Current.Attrib[VERT_ATTRIB_NORMAL] );
  282.    COPY_4FV( texcoord, ctx->Current.Attrib[VERT_ATTRIB_TEX0] );
  283.    COPY_4FV( color, ctx->Current.Attrib[VERT_ATTRIB_COLOR0] );
  284.    index = ctx->Current.Index;
  285.  
  286.    do_EvalCoord1f( ctx, u );
  287.  
  288.    COPY_3FV( ctx->Current.Attrib[VERT_ATTRIB_NORMAL], normal );
  289.    COPY_4FV( ctx->Current.Attrib[VERT_ATTRIB_TEX0], texcoord );
  290.    COPY_4FV( ctx->Current.Attrib[VERT_ATTRIB_COLOR0], color );
  291.    ctx->Current.Index = index;
  292. }
  293.  
  294. void _mesa_EvalCoord2f( GLfloat u, GLfloat v )
  295. {
  296.    GET_CURRENT_CONTEXT( ctx );
  297.    GLfloat normal[3], texcoord[4], color[4];
  298.    GLuint index;
  299.  
  300.    COPY_3FV( normal, ctx->Current.Attrib[VERT_ATTRIB_NORMAL] );
  301.    COPY_4FV( texcoord, ctx->Current.Attrib[VERT_ATTRIB_TEX0] );
  302.    COPY_4FV( color, ctx->Current.Attrib[VERT_ATTRIB_COLOR0] );
  303.    index = ctx->Current.Index;
  304.  
  305.    do_EvalCoord2f( ctx, u, v );
  306.  
  307.    COPY_3FV( ctx->Current.Attrib[VERT_ATTRIB_NORMAL], normal );
  308.    COPY_4FV( ctx->Current.Attrib[VERT_ATTRIB_TEX0], texcoord );
  309.    COPY_4FV( ctx->Current.Attrib[VERT_ATTRIB_COLOR0], color );
  310.    ctx->Current.Index = index;
  311. }
  312.  
  313. void _mesa_EvalCoord1fv( const GLfloat *u )
  314. {  glEvalCoord1f( u[0] );
  315. }
  316.  
  317. void _mesa_EvalCoord2fv( const GLfloat *u )
  318. {  glEvalCoord2f( u[0], u[1] );
  319. }
  320.