#include "glheader.h"
#include "colormac.h"
#include "context.h"
#include "macros.h"
#include "imports.h"
#include "mtypes.h"
#include "math/m_xform.h"
#include "t_context.h"
#include "t_pipeline.h"
struct normal_stage_data {
normal_func NormalTransform;
GLvector4f normal;
};
#define NORMAL_STAGE_DATA(stage) ((struct normal_stage_data *)stage->privatePtr)
static GLboolean
run_normal_stage(GLcontext *ctx, struct tnl_pipeline_stage *stage)
{
struct normal_stage_data *store = NORMAL_STAGE_DATA(stage);
struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
const GLfloat *lengths;
if (!store->NormalTransform)
return GL_TRUE;
if (_math_matrix_is_general_scale(ctx->ModelviewMatrixStack.Top))
lengths = NULL;
else
lengths = VB->NormalLengthPtr;
store->NormalTransform( ctx->ModelviewMatrixStack.Top,
ctx->_ModelViewInvScale,
VB->AttribPtr[_TNL_ATTRIB_NORMAL],
lengths,
&store->normal );
if (VB->AttribPtr[_TNL_ATTRIB_NORMAL]->count > 1) {
store->normal.stride = 4 * sizeof(GLfloat);
}
else {
store->normal.stride = 0;
}
VB->AttribPtr[_TNL_ATTRIB_NORMAL] = &store->normal;
VB->NormalPtr = &store->normal;
VB->NormalLengthPtr = NULL;
return GL_TRUE;
}
static void
validate_normal_stage(GLcontext *ctx, struct tnl_pipeline_stage *stage)
{
struct normal_stage_data *store = NORMAL_STAGE_DATA(stage);
if (ctx->VertexProgram._Current ||
(!ctx->Light.Enabled &&
!(ctx->Texture._GenFlags & TEXGEN_NEED_NORMALS))) {
store->NormalTransform = NULL;
return;
}
if (ctx->_NeedEyeCoords) {
GLuint transform = NORM_TRANSFORM_NO_ROT;
if (_math_matrix_has_rotation(ctx->ModelviewMatrixStack.Top)) {
transform = NORM_TRANSFORM;
}
if (ctx->Transform.Normalize) {
store->NormalTransform = _mesa_normal_tab[transform | NORM_NORMALIZE];
}
else if (ctx->Transform.RescaleNormals &&
ctx->_ModelViewInvScale != 1.0) {
store->NormalTransform = _mesa_normal_tab[transform | NORM_RESCALE];
}
else {
store->NormalTransform = _mesa_normal_tab[transform];
}
}
else {
if (ctx->Transform.Normalize) {
store->NormalTransform = _mesa_normal_tab[NORM_NORMALIZE];
}
else if (!ctx->Transform.RescaleNormals &&
ctx->_ModelViewInvScale != 1.0) {
store->NormalTransform = _mesa_normal_tab[NORM_RESCALE];
}
else {
store->NormalTransform = NULL;
}
}
}
static GLboolean
alloc_normal_data(GLcontext *ctx, struct tnl_pipeline_stage *stage)
{
TNLcontext *tnl = TNL_CONTEXT(ctx);
struct normal_stage_data *store;
stage->privatePtr = _mesa_malloc(sizeof(*store));
store = NORMAL_STAGE_DATA(stage);
if (!store)
return GL_FALSE;
_mesa_vector4f_alloc( &store->normal, 0, tnl->vb.Size, 32 );
return GL_TRUE;
}
static void
free_normal_data(struct tnl_pipeline_stage *stage)
{
struct normal_stage_data *store = NORMAL_STAGE_DATA(stage);
if (store) {
_mesa_vector4f_free( &store->normal );
_mesa_free( store );
stage->privatePtr = NULL;
}
}
const struct tnl_pipeline_stage _tnl_normal_transform_stage =
{
"normal transform",
NULL,
alloc_normal_data,
free_normal_data,
validate_normal_stage,
run_normal_stage
};