Home | History | Annotate | Download | only in tnl
      1 /*
      2  * Mesa 3-D graphics library
      3  * Version:  7.1
      4  *
      5  * Copyright (C) 1999-2007  Brian Paul   All Rights Reserved.
      6  *
      7  * Permission is hereby granted, free of charge, to any person obtaining a
      8  * copy of this software and associated documentation files (the "Software"),
      9  * to deal in the Software without restriction, including without limitation
     10  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     11  * and/or sell copies of the Software, and to permit persons to whom the
     12  * Software is furnished to do so, subject to the following conditions:
     13  *
     14  * The above copyright notice and this permission notice shall be included
     15  * in all copies or substantial portions of the Software.
     16  *
     17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
     18  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     20  * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
     21  * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
     22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     23  */
     24 
     25 
     26 #include "main/glheader.h"
     27 #include "main/colormac.h"
     28 #include "main/feedback.h"
     29 #include "main/light.h"
     30 #include "main/macros.h"
     31 #include "main/simple_list.h"
     32 #include "main/mtypes.h"
     33 
     34 #include "math/m_matrix.h"
     35 #include "tnl/tnl.h"
     36 
     37 
     38 
     39 /**
     40  * Clip a point against the view volume.
     41  *
     42  * \param v vertex vector describing the point to clip.
     43  *
     44  * \return zero if outside view volume, or one if inside.
     45  */
     46 static GLuint
     47 viewclip_point_xy( const GLfloat v[] )
     48 {
     49    if (   v[0] > v[3] || v[0] < -v[3]
     50        || v[1] > v[3] || v[1] < -v[3] ) {
     51       return 0;
     52    }
     53    else {
     54       return 1;
     55    }
     56 }
     57 
     58 
     59 /**
     60  * Clip a point against the far/near Z clipping planes.
     61  *
     62  * \param v vertex vector describing the point to clip.
     63  *
     64  * \return zero if outside view volume, or one if inside.
     65  */
     66 static GLuint
     67 viewclip_point_z( const GLfloat v[] )
     68 {
     69    if (v[2] > v[3] || v[2] < -v[3] ) {
     70       return 0;
     71    }
     72    else {
     73       return 1;
     74    }
     75 }
     76 
     77 
     78 /**
     79  * Clip a point against the user clipping planes.
     80  *
     81  * \param ctx GL context.
     82  * \param v vertex vector describing the point to clip.
     83  *
     84  * \return zero if the point was clipped, or one otherwise.
     85  */
     86 static GLuint
     87 userclip_point( struct gl_context *ctx, const GLfloat v[] )
     88 {
     89    GLuint p;
     90 
     91    for (p = 0; p < ctx->Const.MaxClipPlanes; p++) {
     92       if (ctx->Transform.ClipPlanesEnabled & (1 << p)) {
     93 	 GLfloat dot = v[0] * ctx->Transform._ClipUserPlane[p][0]
     94 		     + v[1] * ctx->Transform._ClipUserPlane[p][1]
     95 		     + v[2] * ctx->Transform._ClipUserPlane[p][2]
     96 		     + v[3] * ctx->Transform._ClipUserPlane[p][3];
     97          if (dot < 0.0F) {
     98             return 0;
     99          }
    100       }
    101    }
    102 
    103    return 1;
    104 }
    105 
    106 
    107 /**
    108  * Compute lighting for the raster position.  RGB modes computed.
    109  * \param ctx the context
    110  * \param vertex vertex location
    111  * \param normal normal vector
    112  * \param Rcolor returned color
    113  * \param Rspec returned specular color (if separate specular enabled)
    114  */
    115 static void
    116 shade_rastpos(struct gl_context *ctx,
    117               const GLfloat vertex[4],
    118               const GLfloat normal[3],
    119               GLfloat Rcolor[4],
    120               GLfloat Rspec[4])
    121 {
    122    /*const*/ GLfloat (*base)[3] = ctx->Light._BaseColor;
    123    const struct gl_light *light;
    124    GLfloat diffuseColor[4], specularColor[4];  /* for RGB mode only */
    125 
    126    COPY_3V(diffuseColor, base[0]);
    127    diffuseColor[3] = CLAMP(
    128       ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE][3], 0.0F, 1.0F );
    129    ASSIGN_4V(specularColor, 0.0, 0.0, 0.0, 1.0);
    130 
    131    foreach (light, &ctx->Light.EnabledList) {
    132       GLfloat attenuation = 1.0;
    133       GLfloat VP[3]; /* vector from vertex to light pos */
    134       GLfloat n_dot_VP;
    135       GLfloat diffuseContrib[3], specularContrib[3];
    136 
    137       if (!(light->_Flags & LIGHT_POSITIONAL)) {
    138          /* light at infinity */
    139 	 COPY_3V(VP, light->_VP_inf_norm);
    140 	 attenuation = light->_VP_inf_spot_attenuation;
    141       }
    142       else {
    143          /* local/positional light */
    144 	 GLfloat d;
    145 
    146          /* VP = vector from vertex pos to light[i].pos */
    147 	 SUB_3V(VP, light->_Position, vertex);
    148          /* d = length(VP) */
    149 	 d = (GLfloat) LEN_3FV( VP );
    150 	 if (d > 1.0e-6) {
    151             /* normalize VP */
    152 	    GLfloat invd = 1.0F / d;
    153 	    SELF_SCALE_SCALAR_3V(VP, invd);
    154 	 }
    155 
    156          /* atti */
    157 	 attenuation = 1.0F / (light->ConstantAttenuation + d *
    158 			       (light->LinearAttenuation + d *
    159 				light->QuadraticAttenuation));
    160 
    161 	 if (light->_Flags & LIGHT_SPOT) {
    162 	    GLfloat PV_dot_dir = - DOT3(VP, light->_NormSpotDirection);
    163 
    164 	    if (PV_dot_dir<light->_CosCutoff) {
    165 	       continue;
    166 	    }
    167 	    else {
    168                GLfloat spot = powf(PV_dot_dir, light->SpotExponent);
    169 	       attenuation *= spot;
    170 	    }
    171 	 }
    172       }
    173 
    174       if (attenuation < 1e-3)
    175 	 continue;
    176 
    177       n_dot_VP = DOT3( normal, VP );
    178 
    179       if (n_dot_VP < 0.0F) {
    180 	 ACC_SCALE_SCALAR_3V(diffuseColor, attenuation, light->_MatAmbient[0]);
    181 	 continue;
    182       }
    183 
    184       /* Ambient + diffuse */
    185       COPY_3V(diffuseContrib, light->_MatAmbient[0]);
    186       ACC_SCALE_SCALAR_3V(diffuseContrib, n_dot_VP, light->_MatDiffuse[0]);
    187 
    188       /* Specular */
    189       {
    190          const GLfloat *h;
    191          GLfloat n_dot_h;
    192 
    193          ASSIGN_3V(specularContrib, 0.0, 0.0, 0.0);
    194 
    195 	 if (ctx->Light.Model.LocalViewer) {
    196 	    GLfloat v[3];
    197 	    COPY_3V(v, vertex);
    198 	    NORMALIZE_3FV(v);
    199 	    SUB_3V(VP, VP, v);
    200             NORMALIZE_3FV(VP);
    201 	    h = VP;
    202 	 }
    203 	 else if (light->_Flags & LIGHT_POSITIONAL) {
    204 	    ACC_3V(VP, ctx->_EyeZDir);
    205             NORMALIZE_3FV(VP);
    206 	    h = VP;
    207 	 }
    208          else {
    209 	    h = light->_h_inf_norm;
    210 	 }
    211 
    212 	 n_dot_h = DOT3(normal, h);
    213 
    214 	 if (n_dot_h > 0.0F) {
    215 	    GLfloat shine;
    216 	    GLfloat spec_coef;
    217 
    218 	    shine = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_SHININESS][0];
    219 	    spec_coef = powf(n_dot_h, shine);
    220 
    221 	    if (spec_coef > 1.0e-10) {
    222                if (ctx->Light.Model.ColorControl==GL_SEPARATE_SPECULAR_COLOR) {
    223                   ACC_SCALE_SCALAR_3V( specularContrib, spec_coef,
    224                                        light->_MatSpecular[0]);
    225                }
    226                else {
    227                   ACC_SCALE_SCALAR_3V( diffuseContrib, spec_coef,
    228                                        light->_MatSpecular[0]);
    229                }
    230 	    }
    231 	 }
    232       }
    233 
    234       ACC_SCALE_SCALAR_3V( diffuseColor, attenuation, diffuseContrib );
    235       ACC_SCALE_SCALAR_3V( specularColor, attenuation, specularContrib );
    236    }
    237 
    238    Rcolor[0] = CLAMP(diffuseColor[0], 0.0F, 1.0F);
    239    Rcolor[1] = CLAMP(diffuseColor[1], 0.0F, 1.0F);
    240    Rcolor[2] = CLAMP(diffuseColor[2], 0.0F, 1.0F);
    241    Rcolor[3] = CLAMP(diffuseColor[3], 0.0F, 1.0F);
    242    Rspec[0] = CLAMP(specularColor[0], 0.0F, 1.0F);
    243    Rspec[1] = CLAMP(specularColor[1], 0.0F, 1.0F);
    244    Rspec[2] = CLAMP(specularColor[2], 0.0F, 1.0F);
    245    Rspec[3] = CLAMP(specularColor[3], 0.0F, 1.0F);
    246 }
    247 
    248 
    249 /**
    250  * Do texgen needed for glRasterPos.
    251  * \param ctx  rendering context
    252  * \param vObj  object-space vertex coordinate
    253  * \param vEye  eye-space vertex coordinate
    254  * \param normal  vertex normal
    255  * \param unit  texture unit number
    256  * \param texcoord  incoming texcoord and resulting texcoord
    257  */
    258 static void
    259 compute_texgen(struct gl_context *ctx, const GLfloat vObj[4], const GLfloat vEye[4],
    260                const GLfloat normal[3], GLuint unit, GLfloat texcoord[4])
    261 {
    262    const struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
    263 
    264    /* always compute sphere map terms, just in case */
    265    GLfloat u[3], two_nu, rx, ry, rz, m, mInv;
    266    COPY_3V(u, vEye);
    267    NORMALIZE_3FV(u);
    268    two_nu = 2.0F * DOT3(normal, u);
    269    rx = u[0] - normal[0] * two_nu;
    270    ry = u[1] - normal[1] * two_nu;
    271    rz = u[2] - normal[2] * two_nu;
    272    m = rx * rx + ry * ry + (rz + 1.0F) * (rz + 1.0F);
    273    if (m > 0.0F)
    274       mInv = 0.5F * INV_SQRTF(m);
    275    else
    276       mInv = 0.0F;
    277 
    278    if (texUnit->TexGenEnabled & S_BIT) {
    279       switch (texUnit->GenS.Mode) {
    280          case GL_OBJECT_LINEAR:
    281             texcoord[0] = DOT4(vObj, texUnit->GenS.ObjectPlane);
    282             break;
    283          case GL_EYE_LINEAR:
    284             texcoord[0] = DOT4(vEye, texUnit->GenS.EyePlane);
    285             break;
    286          case GL_SPHERE_MAP:
    287             texcoord[0] = rx * mInv + 0.5F;
    288             break;
    289          case GL_REFLECTION_MAP:
    290             texcoord[0] = rx;
    291             break;
    292          case GL_NORMAL_MAP:
    293             texcoord[0] = normal[0];
    294             break;
    295          default:
    296             _mesa_problem(ctx, "Bad S texgen in compute_texgen()");
    297             return;
    298       }
    299    }
    300 
    301    if (texUnit->TexGenEnabled & T_BIT) {
    302       switch (texUnit->GenT.Mode) {
    303          case GL_OBJECT_LINEAR:
    304             texcoord[1] = DOT4(vObj, texUnit->GenT.ObjectPlane);
    305             break;
    306          case GL_EYE_LINEAR:
    307             texcoord[1] = DOT4(vEye, texUnit->GenT.EyePlane);
    308             break;
    309          case GL_SPHERE_MAP:
    310             texcoord[1] = ry * mInv + 0.5F;
    311             break;
    312          case GL_REFLECTION_MAP:
    313             texcoord[1] = ry;
    314             break;
    315          case GL_NORMAL_MAP:
    316             texcoord[1] = normal[1];
    317             break;
    318          default:
    319             _mesa_problem(ctx, "Bad T texgen in compute_texgen()");
    320             return;
    321       }
    322    }
    323 
    324    if (texUnit->TexGenEnabled & R_BIT) {
    325       switch (texUnit->GenR.Mode) {
    326          case GL_OBJECT_LINEAR:
    327             texcoord[2] = DOT4(vObj, texUnit->GenR.ObjectPlane);
    328             break;
    329          case GL_EYE_LINEAR:
    330             texcoord[2] = DOT4(vEye, texUnit->GenR.EyePlane);
    331             break;
    332          case GL_REFLECTION_MAP:
    333             texcoord[2] = rz;
    334             break;
    335          case GL_NORMAL_MAP:
    336             texcoord[2] = normal[2];
    337             break;
    338          default:
    339             _mesa_problem(ctx, "Bad R texgen in compute_texgen()");
    340             return;
    341       }
    342    }
    343 
    344    if (texUnit->TexGenEnabled & Q_BIT) {
    345       switch (texUnit->GenQ.Mode) {
    346          case GL_OBJECT_LINEAR:
    347             texcoord[3] = DOT4(vObj, texUnit->GenQ.ObjectPlane);
    348             break;
    349          case GL_EYE_LINEAR:
    350             texcoord[3] = DOT4(vEye, texUnit->GenQ.EyePlane);
    351             break;
    352          default:
    353             _mesa_problem(ctx, "Bad Q texgen in compute_texgen()");
    354             return;
    355       }
    356    }
    357 }
    358 
    359 
    360 /**
    361  * glRasterPos transformation.  Typically called via ctx->Driver.RasterPos().
    362  * XXX some of this code (such as viewport xform, clip testing and setting
    363  * of ctx->Current.Raster* fields) could get lifted up into the
    364  * main/rasterpos.c code.
    365  *
    366  * \param vObj  vertex position in object space
    367  */
    368 void
    369 _tnl_RasterPos(struct gl_context *ctx, const GLfloat vObj[4])
    370 {
    371    if (ctx->VertexProgram._Enabled) {
    372       /* XXX implement this */
    373       _mesa_problem(ctx, "Vertex programs not implemented for glRasterPos");
    374       return;
    375    }
    376    else {
    377       GLfloat eye[4], clip[4], ndc[3], d;
    378       GLfloat *norm, eyenorm[3];
    379       GLfloat *objnorm = ctx->Current.Attrib[VERT_ATTRIB_NORMAL];
    380 
    381       /* apply modelview matrix:  eye = MV * obj */
    382       TRANSFORM_POINT( eye, ctx->ModelviewMatrixStack.Top->m, vObj );
    383       /* apply projection matrix:  clip = Proj * eye */
    384       TRANSFORM_POINT( clip, ctx->ProjectionMatrixStack.Top->m, eye );
    385 
    386       /* clip to view volume. */
    387       if (!ctx->Transform.DepthClamp) {
    388          if (viewclip_point_z(clip) == 0) {
    389             ctx->Current.RasterPosValid = GL_FALSE;
    390             return;
    391          }
    392       }
    393       if (!ctx->Transform.RasterPositionUnclipped) {
    394          if (viewclip_point_xy(clip) == 0) {
    395             ctx->Current.RasterPosValid = GL_FALSE;
    396             return;
    397          }
    398       }
    399 
    400       /* clip to user clipping planes */
    401       if (ctx->Transform.ClipPlanesEnabled && !userclip_point(ctx, clip)) {
    402          ctx->Current.RasterPosValid = GL_FALSE;
    403          return;
    404       }
    405 
    406       /* ndc = clip / W */
    407       d = (clip[3] == 0.0F) ? 1.0F : 1.0F / clip[3];
    408       ndc[0] = clip[0] * d;
    409       ndc[1] = clip[1] * d;
    410       ndc[2] = clip[2] * d;
    411       /* wincoord = viewport_mapping(ndc) */
    412       ctx->Current.RasterPos[0] = (ndc[0] * ctx->Viewport._WindowMap.m[MAT_SX]
    413                                    + ctx->Viewport._WindowMap.m[MAT_TX]);
    414       ctx->Current.RasterPos[1] = (ndc[1] * ctx->Viewport._WindowMap.m[MAT_SY]
    415                                    + ctx->Viewport._WindowMap.m[MAT_TY]);
    416       ctx->Current.RasterPos[2] = (ndc[2] * ctx->Viewport._WindowMap.m[MAT_SZ]
    417                                    + ctx->Viewport._WindowMap.m[MAT_TZ])
    418                                   / ctx->DrawBuffer->_DepthMaxF;
    419       ctx->Current.RasterPos[3] = clip[3];
    420 
    421       if (ctx->Transform.DepthClamp) {
    422 	 ctx->Current.RasterPos[3] = CLAMP(ctx->Current.RasterPos[3],
    423 					   ctx->Viewport.Near,
    424 					   ctx->Viewport.Far);
    425       }
    426 
    427       /* compute raster distance */
    428       if (ctx->Fog.FogCoordinateSource == GL_FOG_COORDINATE_EXT)
    429          ctx->Current.RasterDistance = ctx->Current.Attrib[VERT_ATTRIB_FOG][0];
    430       else
    431          ctx->Current.RasterDistance =
    432                         SQRTF( eye[0]*eye[0] + eye[1]*eye[1] + eye[2]*eye[2] );
    433 
    434       /* compute transformed normal vector (for lighting or texgen) */
    435       if (ctx->_NeedEyeCoords) {
    436          const GLfloat *inv = ctx->ModelviewMatrixStack.Top->inv;
    437          TRANSFORM_NORMAL( eyenorm, objnorm, inv );
    438          norm = eyenorm;
    439       }
    440       else {
    441          norm = objnorm;
    442       }
    443 
    444       /* update raster color */
    445       if (ctx->Light.Enabled) {
    446          /* lighting */
    447          shade_rastpos( ctx, vObj, norm,
    448                         ctx->Current.RasterColor,
    449                         ctx->Current.RasterSecondaryColor );
    450       }
    451       else {
    452          /* use current color */
    453 	 COPY_4FV(ctx->Current.RasterColor,
    454 		  ctx->Current.Attrib[VERT_ATTRIB_COLOR0]);
    455 	 COPY_4FV(ctx->Current.RasterSecondaryColor,
    456 		  ctx->Current.Attrib[VERT_ATTRIB_COLOR1]);
    457       }
    458 
    459       /* texture coords */
    460       {
    461          GLuint u;
    462          for (u = 0; u < ctx->Const.MaxTextureCoordUnits; u++) {
    463             GLfloat tc[4];
    464             COPY_4V(tc, ctx->Current.Attrib[VERT_ATTRIB_TEX0 + u]);
    465             if (ctx->Texture.Unit[u].TexGenEnabled) {
    466                compute_texgen(ctx, vObj, eye, norm, u, tc);
    467             }
    468             TRANSFORM_POINT(ctx->Current.RasterTexCoords[u],
    469                             ctx->TextureMatrixStack[u].Top->m, tc);
    470          }
    471       }
    472 
    473       ctx->Current.RasterPosValid = GL_TRUE;
    474    }
    475 
    476    if (ctx->RenderMode == GL_SELECT) {
    477       _mesa_update_hitflag( ctx, ctx->Current.RasterPos[2] );
    478    }
    479 }
    480