Home | History | Annotate | Download | only in tnl
      1 /*
      2  * Mesa 3-D graphics library
      3  *
      4  * Copyright (C) 1999-2006  Brian Paul   All Rights Reserved.
      5  *
      6  * Permission is hereby granted, free of charge, to any person obtaining a
      7  * copy of this software and associated documentation files (the "Software"),
      8  * to deal in the Software without restriction, including without limitation
      9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     10  * and/or sell copies of the Software, and to permit persons to whom the
     11  * Software is furnished to do so, subject to the following conditions:
     12  *
     13  * The above copyright notice and this permission notice shall be included
     14  * in all copies or substantial portions of the Software.
     15  *
     16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
     17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
     20  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
     21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
     22  * OTHER DEALINGS IN THE SOFTWARE.
     23  *
     24  * Authors:
     25  *    Keith Whitwell <keithw (at) vmware.com>
     26  */
     27 
     28 
     29 #include "main/glheader.h"
     30 #include "main/macros.h"
     31 #include "main/imports.h"
     32 #include "main/mtypes.h"
     33 
     34 #include "math/m_xform.h"
     35 
     36 #include "t_context.h"
     37 #include "t_pipeline.h"
     38 
     39 
     40 struct normal_stage_data {
     41    normal_func NormalTransform;
     42    GLvector4f normal;
     43 };
     44 
     45 #define NORMAL_STAGE_DATA(stage) ((struct normal_stage_data *)stage->privatePtr)
     46 
     47 
     48 static GLboolean
     49 run_normal_stage(struct gl_context *ctx, struct tnl_pipeline_stage *stage)
     50 {
     51    struct normal_stage_data *store = NORMAL_STAGE_DATA(stage);
     52    struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
     53    const GLfloat *lengths;
     54 
     55    if (!store->NormalTransform)
     56       return GL_TRUE;
     57 
     58    /* We can only use the display list's saved normal lengths if we've
     59     * got a transformation matrix with uniform scaling.
     60     */
     61    if (_math_matrix_is_general_scale(ctx->ModelviewMatrixStack.Top))
     62       lengths = NULL;
     63    else
     64       lengths = VB->NormalLengthPtr;
     65 
     66    store->NormalTransform( ctx->ModelviewMatrixStack.Top,
     67 			   ctx->_ModelViewInvScale,
     68 			   VB->AttribPtr[_TNL_ATTRIB_NORMAL],  /* input normals */
     69 			   lengths,
     70 			   &store->normal ); /* resulting normals */
     71 
     72    if (VB->AttribPtr[_TNL_ATTRIB_NORMAL]->count > 1) {
     73       store->normal.stride = 4 * sizeof(GLfloat);
     74    }
     75    else {
     76       store->normal.stride = 0;
     77    }
     78 
     79    VB->AttribPtr[_TNL_ATTRIB_NORMAL] = &store->normal;
     80 
     81    VB->NormalLengthPtr = NULL;	/* no longer valid */
     82    return GL_TRUE;
     83 }
     84 
     85 
     86 /**
     87  * Examine current GL state and set the store->NormalTransform pointer
     88  * to point to the appropriate normal transformation routine.
     89  */
     90 static void
     91 validate_normal_stage(struct gl_context *ctx, struct tnl_pipeline_stage *stage)
     92 {
     93    struct normal_stage_data *store = NORMAL_STAGE_DATA(stage);
     94 
     95    if (ctx->VertexProgram._Current ||
     96        (!ctx->Light.Enabled &&
     97 	!(ctx->Texture._GenFlags & TEXGEN_NEED_NORMALS))) {
     98       store->NormalTransform = NULL;
     99       return;
    100    }
    101 
    102    if (ctx->_NeedEyeCoords) {
    103       /* Eye coordinates are needed, for whatever reasons.
    104        * Do lighting in eye coordinates, as the GL spec says.
    105        */
    106       GLuint transform = NORM_TRANSFORM_NO_ROT;
    107 
    108       if (_math_matrix_has_rotation(ctx->ModelviewMatrixStack.Top)) {
    109          /* need to do full (3x3) matrix transform */
    110 	 transform = NORM_TRANSFORM;
    111       }
    112 
    113       if (ctx->Transform.Normalize) {
    114 	 store->NormalTransform = _mesa_normal_tab[transform | NORM_NORMALIZE];
    115       }
    116       else if (ctx->Transform.RescaleNormals &&
    117                ctx->_ModelViewInvScale != 1.0F) {
    118 	 store->NormalTransform = _mesa_normal_tab[transform | NORM_RESCALE];
    119       }
    120       else {
    121 	 store->NormalTransform = _mesa_normal_tab[transform];
    122       }
    123    }
    124    else {
    125       /* We don't need eye coordinates.
    126        * Do lighting in object coordinates.  Thus, we don't need to fully
    127        * transform normal vectors (just leave them in object coordinates)
    128        * but we still need to do normalization/rescaling if enabled.
    129        */
    130       if (ctx->Transform.Normalize) {
    131 	 store->NormalTransform = _mesa_normal_tab[NORM_NORMALIZE];
    132       }
    133       else if (!ctx->Transform.RescaleNormals &&
    134 	       ctx->_ModelViewInvScale != 1.0F) {
    135 	 store->NormalTransform = _mesa_normal_tab[NORM_RESCALE];
    136       }
    137       else {
    138 	 store->NormalTransform = NULL;
    139       }
    140    }
    141 }
    142 
    143 
    144 /**
    145  * Allocate stage's private data (storage for transformed normals).
    146  */
    147 static GLboolean
    148 alloc_normal_data(struct gl_context *ctx, struct tnl_pipeline_stage *stage)
    149 {
    150    TNLcontext *tnl = TNL_CONTEXT(ctx);
    151    struct normal_stage_data *store;
    152 
    153    stage->privatePtr = malloc(sizeof(*store));
    154    store = NORMAL_STAGE_DATA(stage);
    155    if (!store)
    156       return GL_FALSE;
    157 
    158    _mesa_vector4f_alloc( &store->normal, 0, tnl->vb.Size, 32 );
    159    return GL_TRUE;
    160 }
    161 
    162 
    163 /**
    164  * Free stage's private data.
    165  */
    166 static void
    167 free_normal_data(struct tnl_pipeline_stage *stage)
    168 {
    169    struct normal_stage_data *store = NORMAL_STAGE_DATA(stage);
    170    if (store) {
    171       _mesa_vector4f_free( &store->normal );
    172       free( store );
    173       stage->privatePtr = NULL;
    174    }
    175 }
    176 
    177 
    178 const struct tnl_pipeline_stage _tnl_normal_transform_stage =
    179 {
    180    "normal transform",		/* name */
    181    NULL,			/* privatePtr */
    182    alloc_normal_data,		/* create */
    183    free_normal_data,		/* destroy */
    184    validate_normal_stage,	/* validate */
    185    run_normal_stage             /* run */
    186 };
    187