16b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell/*
26b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell * Mesa 3-D graphics library
36b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell * Version:  7.1
46b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell *
56b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell * Copyright (C) 1999-2007  Brian Paul   All Rights Reserved.
66b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell *
76b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell * Permission is hereby granted, free of charge, to any person obtaining a
86b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell * copy of this software and associated documentation files (the "Software"),
96b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell * to deal in the Software without restriction, including without limitation
106b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell * the rights to use, copy, modify, merge, publish, distribute, sublicense,
116b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell * and/or sell copies of the Software, and to permit persons to whom the
126b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell * Software is furnished to do so, subject to the following conditions:
136b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell *
146b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell * The above copyright notice and this permission notice shall be included
156b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell * in all copies or substantial portions of the Software.
166b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell *
176b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
186b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
196b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
206b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
216b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
226b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
236b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell */
246b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell
256b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell
266b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell#include "main/glheader.h"
276b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell#include "main/colormac.h"
2887e44d9003d6fdd4b9be911ad1aa4de1f87068d9Brian Paul#include "main/feedback.h"
2987e44d9003d6fdd4b9be911ad1aa4de1f87068d9Brian Paul#include "main/light.h"
306b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell#include "main/macros.h"
3187e44d9003d6fdd4b9be911ad1aa4de1f87068d9Brian Paul#include "main/simple_list.h"
326b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell#include "main/mtypes.h"
336b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell
346b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell#include "math/m_matrix.h"
356b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell#include "tnl/tnl.h"
366b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell
376b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell
386b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell
396b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell/**
406b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell * Clip a point against the view volume.
416b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell *
426b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell * \param v vertex vector describing the point to clip.
436b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell *
446b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell * \return zero if outside view volume, or one if inside.
456b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell */
466b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwellstatic GLuint
47b4922b533155cc139ebafb111502bb55d2ad2ccfEric Anholtviewclip_point_xy( const GLfloat v[] )
486b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell{
496b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell   if (   v[0] > v[3] || v[0] < -v[3]
50b4922b533155cc139ebafb111502bb55d2ad2ccfEric Anholt       || v[1] > v[3] || v[1] < -v[3] ) {
516b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell      return 0;
526b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell   }
536b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell   else {
546b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell      return 1;
556b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell   }
566b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell}
576b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell
586b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell
596b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell/**
606b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell * Clip a point against the far/near Z clipping planes.
616b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell *
626b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell * \param v vertex vector describing the point to clip.
636b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell *
646b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell * \return zero if outside view volume, or one if inside.
656b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell */
666b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwellstatic GLuint
676b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwellviewclip_point_z( const GLfloat v[] )
686b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell{
696b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell   if (v[2] > v[3] || v[2] < -v[3] ) {
706b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell      return 0;
716b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell   }
726b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell   else {
736b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell      return 1;
746b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell   }
756b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell}
766b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell
776b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell
786b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell/**
796b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell * Clip a point against the user clipping planes.
806b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell *
816b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell * \param ctx GL context.
826b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell * \param v vertex vector describing the point to clip.
836b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell *
846b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell * \return zero if the point was clipped, or one otherwise.
856b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell */
866b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwellstatic GLuint
87f9995b30756140724f41daf963fa06167912be7fKristian Høgsberguserclip_point( struct gl_context *ctx, const GLfloat v[] )
886b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell{
896b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell   GLuint p;
906b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell
916b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell   for (p = 0; p < ctx->Const.MaxClipPlanes; p++) {
926b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell      if (ctx->Transform.ClipPlanesEnabled & (1 << p)) {
936b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell	 GLfloat dot = v[0] * ctx->Transform._ClipUserPlane[p][0]
946b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell		     + v[1] * ctx->Transform._ClipUserPlane[p][1]
956b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell		     + v[2] * ctx->Transform._ClipUserPlane[p][2]
966b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell		     + v[3] * ctx->Transform._ClipUserPlane[p][3];
976b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell         if (dot < 0.0F) {
986b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell            return 0;
996b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell         }
1006b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell      }
1016b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell   }
1026b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell
1036b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell   return 1;
1046b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell}
1056b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell
1066b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell
1076b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell/**
10831bf243a92454758fb4b8efc6bd8ccac99b67b6eMathias Fröhlich * Compute lighting for the raster position.  RGB modes computed.
1096b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell * \param ctx the context
1106b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell * \param vertex vertex location
1116b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell * \param normal normal vector
1126b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell * \param Rcolor returned color
1136b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell * \param Rspec returned specular color (if separate specular enabled)
1146b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell */
1156b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwellstatic void
116f9995b30756140724f41daf963fa06167912be7fKristian Høgsbergshade_rastpos(struct gl_context *ctx,
1176b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell              const GLfloat vertex[4],
1186b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell              const GLfloat normal[3],
1196b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell              GLfloat Rcolor[4],
120a9c1b3caf67f035df83c6a4e38709cfa395f4cc6Ian Romanick              GLfloat Rspec[4])
1216b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell{
1226b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell   /*const*/ GLfloat (*base)[3] = ctx->Light._BaseColor;
1236b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell   const struct gl_light *light;
1246b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell   GLfloat diffuseColor[4], specularColor[4];  /* for RGB mode only */
1256b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell
1266b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell   COPY_3V(diffuseColor, base[0]);
1276b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell   diffuseColor[3] = CLAMP(
1286b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell      ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE][3], 0.0F, 1.0F );
1296b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell   ASSIGN_4V(specularColor, 0.0, 0.0, 0.0, 1.0);
1306b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell
1316b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell   foreach (light, &ctx->Light.EnabledList) {
1326b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell      GLfloat attenuation = 1.0;
1336b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell      GLfloat VP[3]; /* vector from vertex to light pos */
1346b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell      GLfloat n_dot_VP;
1356b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell      GLfloat diffuseContrib[3], specularContrib[3];
1366b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell
1376b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell      if (!(light->_Flags & LIGHT_POSITIONAL)) {
1386b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell         /* light at infinity */
1396b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell	 COPY_3V(VP, light->_VP_inf_norm);
1406b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell	 attenuation = light->_VP_inf_spot_attenuation;
1416b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell      }
1426b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell      else {
1436b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell         /* local/positional light */
1446b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell	 GLfloat d;
1456b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell
1466b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell         /* VP = vector from vertex pos to light[i].pos */
1476b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell	 SUB_3V(VP, light->_Position, vertex);
1486b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell         /* d = length(VP) */
1496b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell	 d = (GLfloat) LEN_3FV( VP );
1506b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell	 if (d > 1.0e-6) {
1516b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell            /* normalize VP */
1526b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell	    GLfloat invd = 1.0F / d;
1536b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell	    SELF_SCALE_SCALAR_3V(VP, invd);
1546b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell	 }
1556b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell
1566b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell         /* atti */
1576b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell	 attenuation = 1.0F / (light->ConstantAttenuation + d *
1586b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell			       (light->LinearAttenuation + d *
1596b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell				light->QuadraticAttenuation));
1606b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell
1616b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell	 if (light->_Flags & LIGHT_SPOT) {
1627391ba1e9d81f15465059db25d1279eefdbeb1a9Brian Paul	    GLfloat PV_dot_dir = - DOT3(VP, light->_NormSpotDirection);
1636b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell
1646b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell	    if (PV_dot_dir<light->_CosCutoff) {
1656b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell	       continue;
1666b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell	    }
1676b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell	    else {
168ae509f88a54b9cc32f16099109330f2792593c83Brian Paul               GLfloat spot = powf(PV_dot_dir, light->SpotExponent);
1696b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell	       attenuation *= spot;
1706b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell	    }
1716b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell	 }
1726b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell      }
1736b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell
1746b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell      if (attenuation < 1e-3)
1756b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell	 continue;
1766b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell
1776b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell      n_dot_VP = DOT3( normal, VP );
1786b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell
1796b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell      if (n_dot_VP < 0.0F) {
1806b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell	 ACC_SCALE_SCALAR_3V(diffuseColor, attenuation, light->_MatAmbient[0]);
1816b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell	 continue;
1826b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell      }
1836b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell
1846b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell      /* Ambient + diffuse */
1856b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell      COPY_3V(diffuseContrib, light->_MatAmbient[0]);
1866b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell      ACC_SCALE_SCALAR_3V(diffuseContrib, n_dot_VP, light->_MatDiffuse[0]);
1876b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell
1886b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell      /* Specular */
1896b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell      {
1906b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell         const GLfloat *h;
1916b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell         GLfloat n_dot_h;
1926b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell
1936b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell         ASSIGN_3V(specularContrib, 0.0, 0.0, 0.0);
1946b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell
1956b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell	 if (ctx->Light.Model.LocalViewer) {
1966b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell	    GLfloat v[3];
1976b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell	    COPY_3V(v, vertex);
1986b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell	    NORMALIZE_3FV(v);
1996b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell	    SUB_3V(VP, VP, v);
2006b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell            NORMALIZE_3FV(VP);
2016b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell	    h = VP;
2026b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell	 }
2036b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell	 else if (light->_Flags & LIGHT_POSITIONAL) {
2046b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell	    ACC_3V(VP, ctx->_EyeZDir);
2056b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell            NORMALIZE_3FV(VP);
2066b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell	    h = VP;
2076b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell	 }
2086b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell         else {
2096b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell	    h = light->_h_inf_norm;
2106b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell	 }
2116b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell
2126b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell	 n_dot_h = DOT3(normal, h);
2136b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell
2146b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell	 if (n_dot_h > 0.0F) {
215a1b1f8ff866f8ce48cfc83c9d9dd9f636d05d2a7Mathias Fröhlich	    GLfloat shine;
216a1b1f8ff866f8ce48cfc83c9d9dd9f636d05d2a7Mathias Fröhlich	    GLfloat spec_coef;
217a1b1f8ff866f8ce48cfc83c9d9dd9f636d05d2a7Mathias Fröhlich
218a1b1f8ff866f8ce48cfc83c9d9dd9f636d05d2a7Mathias Fröhlich	    shine = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_SHININESS][0];
219a1b1f8ff866f8ce48cfc83c9d9dd9f636d05d2a7Mathias Fröhlich	    spec_coef = powf(n_dot_h, shine);
2206b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell
2216b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell	    if (spec_coef > 1.0e-10) {
2226b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell               if (ctx->Light.Model.ColorControl==GL_SEPARATE_SPECULAR_COLOR) {
2236b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell                  ACC_SCALE_SCALAR_3V( specularContrib, spec_coef,
2246b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell                                       light->_MatSpecular[0]);
2256b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell               }
2266b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell               else {
2276b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell                  ACC_SCALE_SCALAR_3V( diffuseContrib, spec_coef,
2286b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell                                       light->_MatSpecular[0]);
2296b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell               }
2306b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell	    }
2316b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell	 }
2326b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell      }
2336b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell
2346b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell      ACC_SCALE_SCALAR_3V( diffuseColor, attenuation, diffuseContrib );
2356b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell      ACC_SCALE_SCALAR_3V( specularColor, attenuation, specularContrib );
2366b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell   }
2376b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell
238a9c1b3caf67f035df83c6a4e38709cfa395f4cc6Ian Romanick   Rcolor[0] = CLAMP(diffuseColor[0], 0.0F, 1.0F);
239a9c1b3caf67f035df83c6a4e38709cfa395f4cc6Ian Romanick   Rcolor[1] = CLAMP(diffuseColor[1], 0.0F, 1.0F);
240a9c1b3caf67f035df83c6a4e38709cfa395f4cc6Ian Romanick   Rcolor[2] = CLAMP(diffuseColor[2], 0.0F, 1.0F);
241a9c1b3caf67f035df83c6a4e38709cfa395f4cc6Ian Romanick   Rcolor[3] = CLAMP(diffuseColor[3], 0.0F, 1.0F);
242a9c1b3caf67f035df83c6a4e38709cfa395f4cc6Ian Romanick   Rspec[0] = CLAMP(specularColor[0], 0.0F, 1.0F);
243a9c1b3caf67f035df83c6a4e38709cfa395f4cc6Ian Romanick   Rspec[1] = CLAMP(specularColor[1], 0.0F, 1.0F);
244a9c1b3caf67f035df83c6a4e38709cfa395f4cc6Ian Romanick   Rspec[2] = CLAMP(specularColor[2], 0.0F, 1.0F);
245a9c1b3caf67f035df83c6a4e38709cfa395f4cc6Ian Romanick   Rspec[3] = CLAMP(specularColor[3], 0.0F, 1.0F);
2466b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell}
2476b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell
2486b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell
2496b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell/**
2506b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell * Do texgen needed for glRasterPos.
2516b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell * \param ctx  rendering context
2526b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell * \param vObj  object-space vertex coordinate
2536b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell * \param vEye  eye-space vertex coordinate
2546b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell * \param normal  vertex normal
2556b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell * \param unit  texture unit number
2566b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell * \param texcoord  incoming texcoord and resulting texcoord
2576b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell */
2586b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwellstatic void
259f9995b30756140724f41daf963fa06167912be7fKristian Høgsbergcompute_texgen(struct gl_context *ctx, const GLfloat vObj[4], const GLfloat vEye[4],
2606b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell               const GLfloat normal[3], GLuint unit, GLfloat texcoord[4])
2616b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell{
2626b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell   const struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
2636b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell
2646b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell   /* always compute sphere map terms, just in case */
2656b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell   GLfloat u[3], two_nu, rx, ry, rz, m, mInv;
2666b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell   COPY_3V(u, vEye);
2676b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell   NORMALIZE_3FV(u);
2686b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell   two_nu = 2.0F * DOT3(normal, u);
2696b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell   rx = u[0] - normal[0] * two_nu;
2706b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell   ry = u[1] - normal[1] * two_nu;
2716b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell   rz = u[2] - normal[2] * two_nu;
2726b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell   m = rx * rx + ry * ry + (rz + 1.0F) * (rz + 1.0F);
2736b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell   if (m > 0.0F)
274f58ba6ca9147137c7a2d31a1014235f7077b7752Matt Turner      mInv = 0.5F * INV_SQRTF(m);
2756b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell   else
2766b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell      mInv = 0.0F;
2776b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell
2786b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell   if (texUnit->TexGenEnabled & S_BIT) {
2799705cff2033f1771a39ac3bb78eb5fcea522218aBrian Paul      switch (texUnit->GenS.Mode) {
2806b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell         case GL_OBJECT_LINEAR:
2819705cff2033f1771a39ac3bb78eb5fcea522218aBrian Paul            texcoord[0] = DOT4(vObj, texUnit->GenS.ObjectPlane);
2826b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell            break;
2836b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell         case GL_EYE_LINEAR:
2849705cff2033f1771a39ac3bb78eb5fcea522218aBrian Paul            texcoord[0] = DOT4(vEye, texUnit->GenS.EyePlane);
2856b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell            break;
2866b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell         case GL_SPHERE_MAP:
2876b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell            texcoord[0] = rx * mInv + 0.5F;
2886b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell            break;
2896b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell         case GL_REFLECTION_MAP:
2906b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell            texcoord[0] = rx;
2916b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell            break;
2926b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell         case GL_NORMAL_MAP:
2936b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell            texcoord[0] = normal[0];
2946b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell            break;
2956b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell         default:
2966b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell            _mesa_problem(ctx, "Bad S texgen in compute_texgen()");
2976b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell            return;
2986b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell      }
2996b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell   }
3006b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell
3016b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell   if (texUnit->TexGenEnabled & T_BIT) {
3029705cff2033f1771a39ac3bb78eb5fcea522218aBrian Paul      switch (texUnit->GenT.Mode) {
3036b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell         case GL_OBJECT_LINEAR:
3049705cff2033f1771a39ac3bb78eb5fcea522218aBrian Paul            texcoord[1] = DOT4(vObj, texUnit->GenT.ObjectPlane);
3056b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell            break;
3066b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell         case GL_EYE_LINEAR:
3079705cff2033f1771a39ac3bb78eb5fcea522218aBrian Paul            texcoord[1] = DOT4(vEye, texUnit->GenT.EyePlane);
3086b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell            break;
3096b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell         case GL_SPHERE_MAP:
3106b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell            texcoord[1] = ry * mInv + 0.5F;
3116b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell            break;
3126b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell         case GL_REFLECTION_MAP:
3136b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell            texcoord[1] = ry;
3146b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell            break;
3156b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell         case GL_NORMAL_MAP:
3166b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell            texcoord[1] = normal[1];
3176b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell            break;
3186b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell         default:
3196b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell            _mesa_problem(ctx, "Bad T texgen in compute_texgen()");
3206b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell            return;
3216b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell      }
3226b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell   }
3236b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell
3246b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell   if (texUnit->TexGenEnabled & R_BIT) {
3259705cff2033f1771a39ac3bb78eb5fcea522218aBrian Paul      switch (texUnit->GenR.Mode) {
3266b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell         case GL_OBJECT_LINEAR:
3279705cff2033f1771a39ac3bb78eb5fcea522218aBrian Paul            texcoord[2] = DOT4(vObj, texUnit->GenR.ObjectPlane);
3286b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell            break;
3296b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell         case GL_EYE_LINEAR:
3309705cff2033f1771a39ac3bb78eb5fcea522218aBrian Paul            texcoord[2] = DOT4(vEye, texUnit->GenR.EyePlane);
3316b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell            break;
3326b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell         case GL_REFLECTION_MAP:
3336b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell            texcoord[2] = rz;
3346b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell            break;
3356b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell         case GL_NORMAL_MAP:
3366b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell            texcoord[2] = normal[2];
3376b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell            break;
3386b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell         default:
3396b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell            _mesa_problem(ctx, "Bad R texgen in compute_texgen()");
3406b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell            return;
3416b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell      }
3426b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell   }
3436b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell
3446b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell   if (texUnit->TexGenEnabled & Q_BIT) {
3459705cff2033f1771a39ac3bb78eb5fcea522218aBrian Paul      switch (texUnit->GenQ.Mode) {
3466b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell         case GL_OBJECT_LINEAR:
3479705cff2033f1771a39ac3bb78eb5fcea522218aBrian Paul            texcoord[3] = DOT4(vObj, texUnit->GenQ.ObjectPlane);
3486b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell            break;
3496b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell         case GL_EYE_LINEAR:
3509705cff2033f1771a39ac3bb78eb5fcea522218aBrian Paul            texcoord[3] = DOT4(vEye, texUnit->GenQ.EyePlane);
3516b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell            break;
3526b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell         default:
3536b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell            _mesa_problem(ctx, "Bad Q texgen in compute_texgen()");
3546b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell            return;
3556b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell      }
3566b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell   }
3576b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell}
3586b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell
3596b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell
3606b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell/**
3616b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell * glRasterPos transformation.  Typically called via ctx->Driver.RasterPos().
3626b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell * XXX some of this code (such as viewport xform, clip testing and setting
3636b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell * of ctx->Current.Raster* fields) could get lifted up into the
3646b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell * main/rasterpos.c code.
3656b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell *
3666b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell * \param vObj  vertex position in object space
3676b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell */
3686b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwellvoid
369f9995b30756140724f41daf963fa06167912be7fKristian Høgsberg_tnl_RasterPos(struct gl_context *ctx, const GLfloat vObj[4])
3706b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell{
3716b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell   if (ctx->VertexProgram._Enabled) {
3726b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell      /* XXX implement this */
3736b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell      _mesa_problem(ctx, "Vertex programs not implemented for glRasterPos");
3746b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell      return;
3756b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell   }
3766b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell   else {
3776b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell      GLfloat eye[4], clip[4], ndc[3], d;
3786b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell      GLfloat *norm, eyenorm[3];
3796b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell      GLfloat *objnorm = ctx->Current.Attrib[VERT_ATTRIB_NORMAL];
3806b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell
3816b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell      /* apply modelview matrix:  eye = MV * obj */
3826b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell      TRANSFORM_POINT( eye, ctx->ModelviewMatrixStack.Top->m, vObj );
3836b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell      /* apply projection matrix:  clip = Proj * eye */
3846b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell      TRANSFORM_POINT( clip, ctx->ProjectionMatrixStack.Top->m, eye );
3856b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell
386b4922b533155cc139ebafb111502bb55d2ad2ccfEric Anholt      /* clip to view volume. */
387b4922b533155cc139ebafb111502bb55d2ad2ccfEric Anholt      if (!ctx->Transform.DepthClamp) {
3886b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell         if (viewclip_point_z(clip) == 0) {
3896b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell            ctx->Current.RasterPosValid = GL_FALSE;
3906b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell            return;
3916b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell         }
3926b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell      }
393b4922b533155cc139ebafb111502bb55d2ad2ccfEric Anholt      if (!ctx->Transform.RasterPositionUnclipped) {
394b4922b533155cc139ebafb111502bb55d2ad2ccfEric Anholt         if (viewclip_point_xy(clip) == 0) {
395b4922b533155cc139ebafb111502bb55d2ad2ccfEric Anholt            ctx->Current.RasterPosValid = GL_FALSE;
396b4922b533155cc139ebafb111502bb55d2ad2ccfEric Anholt            return;
397b4922b533155cc139ebafb111502bb55d2ad2ccfEric Anholt         }
3986b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell      }
3996b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell
4006b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell      /* clip to user clipping planes */
4016b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell      if (ctx->Transform.ClipPlanesEnabled && !userclip_point(ctx, clip)) {
4026b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell         ctx->Current.RasterPosValid = GL_FALSE;
4036b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell         return;
4046b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell      }
4056b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell
4066b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell      /* ndc = clip / W */
4076b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell      d = (clip[3] == 0.0F) ? 1.0F : 1.0F / clip[3];
4086b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell      ndc[0] = clip[0] * d;
4096b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell      ndc[1] = clip[1] * d;
4106b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell      ndc[2] = clip[2] * d;
4116b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell      /* wincoord = viewport_mapping(ndc) */
4126b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell      ctx->Current.RasterPos[0] = (ndc[0] * ctx->Viewport._WindowMap.m[MAT_SX]
4136b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell                                   + ctx->Viewport._WindowMap.m[MAT_TX]);
4146b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell      ctx->Current.RasterPos[1] = (ndc[1] * ctx->Viewport._WindowMap.m[MAT_SY]
4156b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell                                   + ctx->Viewport._WindowMap.m[MAT_TY]);
4166b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell      ctx->Current.RasterPos[2] = (ndc[2] * ctx->Viewport._WindowMap.m[MAT_SZ]
4176b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell                                   + ctx->Viewport._WindowMap.m[MAT_TZ])
4186b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell                                  / ctx->DrawBuffer->_DepthMaxF;
4196b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell      ctx->Current.RasterPos[3] = clip[3];
4206b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell
421b4922b533155cc139ebafb111502bb55d2ad2ccfEric Anholt      if (ctx->Transform.DepthClamp) {
422b4922b533155cc139ebafb111502bb55d2ad2ccfEric Anholt	 ctx->Current.RasterPos[3] = CLAMP(ctx->Current.RasterPos[3],
423b4922b533155cc139ebafb111502bb55d2ad2ccfEric Anholt					   ctx->Viewport.Near,
424b4922b533155cc139ebafb111502bb55d2ad2ccfEric Anholt					   ctx->Viewport.Far);
425b4922b533155cc139ebafb111502bb55d2ad2ccfEric Anholt      }
426b4922b533155cc139ebafb111502bb55d2ad2ccfEric Anholt
4276b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell      /* compute raster distance */
4286b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell      if (ctx->Fog.FogCoordinateSource == GL_FOG_COORDINATE_EXT)
4296b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell         ctx->Current.RasterDistance = ctx->Current.Attrib[VERT_ATTRIB_FOG][0];
4306b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell      else
4316b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell         ctx->Current.RasterDistance =
4326b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell                        SQRTF( eye[0]*eye[0] + eye[1]*eye[1] + eye[2]*eye[2] );
4336b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell
4346b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell      /* compute transformed normal vector (for lighting or texgen) */
4356b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell      if (ctx->_NeedEyeCoords) {
4366b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell         const GLfloat *inv = ctx->ModelviewMatrixStack.Top->inv;
4376b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell         TRANSFORM_NORMAL( eyenorm, objnorm, inv );
4386b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell         norm = eyenorm;
4396b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell      }
4406b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell      else {
4416b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell         norm = objnorm;
4426b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell      }
4436b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell
4446b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell      /* update raster color */
4456b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell      if (ctx->Light.Enabled) {
4466b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell         /* lighting */
4476b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell         shade_rastpos( ctx, vObj, norm,
4486b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell                        ctx->Current.RasterColor,
449a9c1b3caf67f035df83c6a4e38709cfa395f4cc6Ian Romanick                        ctx->Current.RasterSecondaryColor );
4506b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell      }
4516b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell      else {
452a9c1b3caf67f035df83c6a4e38709cfa395f4cc6Ian Romanick         /* use current color */
453a9c1b3caf67f035df83c6a4e38709cfa395f4cc6Ian Romanick	 COPY_4FV(ctx->Current.RasterColor,
454a9c1b3caf67f035df83c6a4e38709cfa395f4cc6Ian Romanick		  ctx->Current.Attrib[VERT_ATTRIB_COLOR0]);
455a9c1b3caf67f035df83c6a4e38709cfa395f4cc6Ian Romanick	 COPY_4FV(ctx->Current.RasterSecondaryColor,
456a9c1b3caf67f035df83c6a4e38709cfa395f4cc6Ian Romanick		  ctx->Current.Attrib[VERT_ATTRIB_COLOR1]);
4576b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell      }
4586b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell
4596b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell      /* texture coords */
4606b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell      {
4616b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell         GLuint u;
4626b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell         for (u = 0; u < ctx->Const.MaxTextureCoordUnits; u++) {
4636b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell            GLfloat tc[4];
4646b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell            COPY_4V(tc, ctx->Current.Attrib[VERT_ATTRIB_TEX0 + u]);
4656b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell            if (ctx->Texture.Unit[u].TexGenEnabled) {
4666b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell               compute_texgen(ctx, vObj, eye, norm, u, tc);
4676b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell            }
4686b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell            TRANSFORM_POINT(ctx->Current.RasterTexCoords[u],
4696b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell                            ctx->TextureMatrixStack[u].Top->m, tc);
4706b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell         }
4716b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell      }
4726b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell
4736b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell      ctx->Current.RasterPosValid = GL_TRUE;
4746b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell   }
4756b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell
4766b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell   if (ctx->RenderMode == GL_SELECT) {
4776b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell      _mesa_update_hitflag( ctx, ctx->Current.RasterPos[2] );
4786b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell   }
4796b146214dc16b441376d8dcaba21bcc4256a2402Keith Whitwell}
480