2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
26 #include "main/glheader.h"
27 #include "main/colormac.h"
28 #include "main/context.h"
29 #include "main/feedback.h"
30 #include "main/light.h"
31 #include "main/macros.h"
32 #include "main/simple_list.h"
33 #include "main/mtypes.h"
35 #include "math/m_matrix.h"
41 * Clip a point against the view volume.
43 * \param v vertex vector describing the point to clip.
45 * \return zero if outside view volume, or one if inside.
48 viewclip_point_xy( const GLfloat v
[] )
50 if ( v
[0] > v
[3] || v
[0] < -v
[3]
51 || v
[1] > v
[3] || v
[1] < -v
[3] ) {
61 * Clip a point against the far/near Z clipping planes.
63 * \param v vertex vector describing the point to clip.
65 * \return zero if outside view volume, or one if inside.
68 viewclip_point_z( const GLfloat v
[] )
70 if (v
[2] > v
[3] || v
[2] < -v
[3] ) {
80 * Clip a point against the user clipping planes.
82 * \param ctx GL context.
83 * \param v vertex vector describing the point to clip.
85 * \return zero if the point was clipped, or one otherwise.
88 userclip_point( GLcontext
*ctx
, const GLfloat v
[] )
92 for (p
= 0; p
< ctx
->Const
.MaxClipPlanes
; p
++) {
93 if (ctx
->Transform
.ClipPlanesEnabled
& (1 << p
)) {
94 GLfloat dot
= v
[0] * ctx
->Transform
._ClipUserPlane
[p
][0]
95 + v
[1] * ctx
->Transform
._ClipUserPlane
[p
][1]
96 + v
[2] * ctx
->Transform
._ClipUserPlane
[p
][2]
97 + v
[3] * ctx
->Transform
._ClipUserPlane
[p
][3];
109 * Compute lighting for the raster position. Both RGB and CI modes computed.
110 * \param ctx the context
111 * \param vertex vertex location
112 * \param normal normal vector
113 * \param Rcolor returned color
114 * \param Rspec returned specular color (if separate specular enabled)
115 * \param Rindex returned color index
118 shade_rastpos(GLcontext
*ctx
,
119 const GLfloat vertex
[4],
120 const GLfloat normal
[3],
125 /*const*/ GLfloat (*base
)[3] = ctx
->Light
._BaseColor
;
126 const struct gl_light
*light
;
127 GLfloat diffuseColor
[4], specularColor
[4]; /* for RGB mode only */
128 GLfloat diffuseCI
= 0.0, specularCI
= 0.0; /* for CI mode only */
130 _mesa_validate_all_lighting_tables( ctx
);
132 COPY_3V(diffuseColor
, base
[0]);
133 diffuseColor
[3] = CLAMP(
134 ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
][3], 0.0F
, 1.0F
);
135 ASSIGN_4V(specularColor
, 0.0, 0.0, 0.0, 1.0);
137 foreach (light
, &ctx
->Light
.EnabledList
) {
138 GLfloat attenuation
= 1.0;
139 GLfloat VP
[3]; /* vector from vertex to light pos */
141 GLfloat diffuseContrib
[3], specularContrib
[3];
143 if (!(light
->_Flags
& LIGHT_POSITIONAL
)) {
144 /* light at infinity */
145 COPY_3V(VP
, light
->_VP_inf_norm
);
146 attenuation
= light
->_VP_inf_spot_attenuation
;
149 /* local/positional light */
152 /* VP = vector from vertex pos to light[i].pos */
153 SUB_3V(VP
, light
->_Position
, vertex
);
155 d
= (GLfloat
) LEN_3FV( VP
);
158 GLfloat invd
= 1.0F
/ d
;
159 SELF_SCALE_SCALAR_3V(VP
, invd
);
163 attenuation
= 1.0F
/ (light
->ConstantAttenuation
+ d
*
164 (light
->LinearAttenuation
+ d
*
165 light
->QuadraticAttenuation
));
167 if (light
->_Flags
& LIGHT_SPOT
) {
168 GLfloat PV_dot_dir
= - DOT3(VP
, light
->_NormSpotDirection
);
170 if (PV_dot_dir
<light
->_CosCutoff
) {
174 double x
= PV_dot_dir
* (EXP_TABLE_SIZE
-1);
176 GLfloat spot
= (GLfloat
) (light
->_SpotExpTable
[k
][0]
177 + (x
-k
)*light
->_SpotExpTable
[k
][1]);
183 if (attenuation
< 1e-3)
186 n_dot_VP
= DOT3( normal
, VP
);
188 if (n_dot_VP
< 0.0F
) {
189 ACC_SCALE_SCALAR_3V(diffuseColor
, attenuation
, light
->_MatAmbient
[0]);
193 /* Ambient + diffuse */
194 COPY_3V(diffuseContrib
, light
->_MatAmbient
[0]);
195 ACC_SCALE_SCALAR_3V(diffuseContrib
, n_dot_VP
, light
->_MatDiffuse
[0]);
196 diffuseCI
+= n_dot_VP
* light
->_dli
* attenuation
;
203 ASSIGN_3V(specularContrib
, 0.0, 0.0, 0.0);
205 if (ctx
->Light
.Model
.LocalViewer
) {
213 else if (light
->_Flags
& LIGHT_POSITIONAL
) {
214 ACC_3V(VP
, ctx
->_EyeZDir
);
219 h
= light
->_h_inf_norm
;
222 n_dot_h
= DOT3(normal
, h
);
224 if (n_dot_h
> 0.0F
) {
226 GET_SHINE_TAB_ENTRY( ctx
->_ShineTable
[0], n_dot_h
, spec_coef
);
228 if (spec_coef
> 1.0e-10) {
229 if (ctx
->Light
.Model
.ColorControl
==GL_SEPARATE_SPECULAR_COLOR
) {
230 ACC_SCALE_SCALAR_3V( specularContrib
, spec_coef
,
231 light
->_MatSpecular
[0]);
234 ACC_SCALE_SCALAR_3V( diffuseContrib
, spec_coef
,
235 light
->_MatSpecular
[0]);
237 /*assert(light->_sli > 0.0);*/
238 specularCI
+= spec_coef
* light
->_sli
* attenuation
;
243 ACC_SCALE_SCALAR_3V( diffuseColor
, attenuation
, diffuseContrib
);
244 ACC_SCALE_SCALAR_3V( specularColor
, attenuation
, specularContrib
);
247 if (ctx
->Visual
.rgbMode
) {
248 Rcolor
[0] = CLAMP(diffuseColor
[0], 0.0F
, 1.0F
);
249 Rcolor
[1] = CLAMP(diffuseColor
[1], 0.0F
, 1.0F
);
250 Rcolor
[2] = CLAMP(diffuseColor
[2], 0.0F
, 1.0F
);
251 Rcolor
[3] = CLAMP(diffuseColor
[3], 0.0F
, 1.0F
);
252 Rspec
[0] = CLAMP(specularColor
[0], 0.0F
, 1.0F
);
253 Rspec
[1] = CLAMP(specularColor
[1], 0.0F
, 1.0F
);
254 Rspec
[2] = CLAMP(specularColor
[2], 0.0F
, 1.0F
);
255 Rspec
[3] = CLAMP(specularColor
[3], 0.0F
, 1.0F
);
258 GLfloat
*ind
= ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_INDEXES
];
259 GLfloat d_a
= ind
[MAT_INDEX_DIFFUSE
] - ind
[MAT_INDEX_AMBIENT
];
260 GLfloat s_a
= ind
[MAT_INDEX_SPECULAR
] - ind
[MAT_INDEX_AMBIENT
];
261 GLfloat i
= (ind
[MAT_INDEX_AMBIENT
]
262 + diffuseCI
* (1.0F
-specularCI
) * d_a
264 if (i
> ind
[MAT_INDEX_SPECULAR
]) {
265 i
= ind
[MAT_INDEX_SPECULAR
];
273 * Do texgen needed for glRasterPos.
274 * \param ctx rendering context
275 * \param vObj object-space vertex coordinate
276 * \param vEye eye-space vertex coordinate
277 * \param normal vertex normal
278 * \param unit texture unit number
279 * \param texcoord incoming texcoord and resulting texcoord
282 compute_texgen(GLcontext
*ctx
, const GLfloat vObj
[4], const GLfloat vEye
[4],
283 const GLfloat normal
[3], GLuint unit
, GLfloat texcoord
[4])
285 const struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[unit
];
287 /* always compute sphere map terms, just in case */
288 GLfloat u
[3], two_nu
, rx
, ry
, rz
, m
, mInv
;
291 two_nu
= 2.0F
* DOT3(normal
, u
);
292 rx
= u
[0] - normal
[0] * two_nu
;
293 ry
= u
[1] - normal
[1] * two_nu
;
294 rz
= u
[2] - normal
[2] * two_nu
;
295 m
= rx
* rx
+ ry
* ry
+ (rz
+ 1.0F
) * (rz
+ 1.0F
);
297 mInv
= 0.5F
* _mesa_inv_sqrtf(m
);
301 if (texUnit
->TexGenEnabled
& S_BIT
) {
302 switch (texUnit
->GenS
.Mode
) {
303 case GL_OBJECT_LINEAR
:
304 texcoord
[0] = DOT4(vObj
, texUnit
->GenS
.ObjectPlane
);
307 texcoord
[0] = DOT4(vEye
, texUnit
->GenS
.EyePlane
);
310 texcoord
[0] = rx
* mInv
+ 0.5F
;
312 case GL_REFLECTION_MAP
:
316 texcoord
[0] = normal
[0];
319 _mesa_problem(ctx
, "Bad S texgen in compute_texgen()");
324 if (texUnit
->TexGenEnabled
& T_BIT
) {
325 switch (texUnit
->GenT
.Mode
) {
326 case GL_OBJECT_LINEAR
:
327 texcoord
[1] = DOT4(vObj
, texUnit
->GenT
.ObjectPlane
);
330 texcoord
[1] = DOT4(vEye
, texUnit
->GenT
.EyePlane
);
333 texcoord
[1] = ry
* mInv
+ 0.5F
;
335 case GL_REFLECTION_MAP
:
339 texcoord
[1] = normal
[1];
342 _mesa_problem(ctx
, "Bad T texgen in compute_texgen()");
347 if (texUnit
->TexGenEnabled
& R_BIT
) {
348 switch (texUnit
->GenR
.Mode
) {
349 case GL_OBJECT_LINEAR
:
350 texcoord
[2] = DOT4(vObj
, texUnit
->GenR
.ObjectPlane
);
353 texcoord
[2] = DOT4(vEye
, texUnit
->GenR
.EyePlane
);
355 case GL_REFLECTION_MAP
:
359 texcoord
[2] = normal
[2];
362 _mesa_problem(ctx
, "Bad R texgen in compute_texgen()");
367 if (texUnit
->TexGenEnabled
& Q_BIT
) {
368 switch (texUnit
->GenQ
.Mode
) {
369 case GL_OBJECT_LINEAR
:
370 texcoord
[3] = DOT4(vObj
, texUnit
->GenQ
.ObjectPlane
);
373 texcoord
[3] = DOT4(vEye
, texUnit
->GenQ
.EyePlane
);
376 _mesa_problem(ctx
, "Bad Q texgen in compute_texgen()");
384 * glRasterPos transformation. Typically called via ctx->Driver.RasterPos().
385 * XXX some of this code (such as viewport xform, clip testing and setting
386 * of ctx->Current.Raster* fields) could get lifted up into the
387 * main/rasterpos.c code.
389 * \param vObj vertex position in object space
392 _tnl_RasterPos(GLcontext
*ctx
, const GLfloat vObj
[4])
394 if (ctx
->VertexProgram
._Enabled
) {
395 /* XXX implement this */
396 _mesa_problem(ctx
, "Vertex programs not implemented for glRasterPos");
400 GLfloat eye
[4], clip
[4], ndc
[3], d
;
401 GLfloat
*norm
, eyenorm
[3];
402 GLfloat
*objnorm
= ctx
->Current
.Attrib
[VERT_ATTRIB_NORMAL
];
404 /* apply modelview matrix: eye = MV * obj */
405 TRANSFORM_POINT( eye
, ctx
->ModelviewMatrixStack
.Top
->m
, vObj
);
406 /* apply projection matrix: clip = Proj * eye */
407 TRANSFORM_POINT( clip
, ctx
->ProjectionMatrixStack
.Top
->m
, eye
);
409 /* clip to view volume. */
410 if (!ctx
->Transform
.DepthClamp
) {
411 if (viewclip_point_z(clip
) == 0) {
412 ctx
->Current
.RasterPosValid
= GL_FALSE
;
416 if (!ctx
->Transform
.RasterPositionUnclipped
) {
417 if (viewclip_point_xy(clip
) == 0) {
418 ctx
->Current
.RasterPosValid
= GL_FALSE
;
423 /* clip to user clipping planes */
424 if (ctx
->Transform
.ClipPlanesEnabled
&& !userclip_point(ctx
, clip
)) {
425 ctx
->Current
.RasterPosValid
= GL_FALSE
;
430 d
= (clip
[3] == 0.0F
) ? 1.0F
: 1.0F
/ clip
[3];
431 ndc
[0] = clip
[0] * d
;
432 ndc
[1] = clip
[1] * d
;
433 ndc
[2] = clip
[2] * d
;
434 /* wincoord = viewport_mapping(ndc) */
435 ctx
->Current
.RasterPos
[0] = (ndc
[0] * ctx
->Viewport
._WindowMap
.m
[MAT_SX
]
436 + ctx
->Viewport
._WindowMap
.m
[MAT_TX
]);
437 ctx
->Current
.RasterPos
[1] = (ndc
[1] * ctx
->Viewport
._WindowMap
.m
[MAT_SY
]
438 + ctx
->Viewport
._WindowMap
.m
[MAT_TY
]);
439 ctx
->Current
.RasterPos
[2] = (ndc
[2] * ctx
->Viewport
._WindowMap
.m
[MAT_SZ
]
440 + ctx
->Viewport
._WindowMap
.m
[MAT_TZ
])
441 / ctx
->DrawBuffer
->_DepthMaxF
;
442 ctx
->Current
.RasterPos
[3] = clip
[3];
444 if (ctx
->Transform
.DepthClamp
) {
445 ctx
->Current
.RasterPos
[3] = CLAMP(ctx
->Current
.RasterPos
[3],
450 /* compute raster distance */
451 if (ctx
->Fog
.FogCoordinateSource
== GL_FOG_COORDINATE_EXT
)
452 ctx
->Current
.RasterDistance
= ctx
->Current
.Attrib
[VERT_ATTRIB_FOG
][0];
454 ctx
->Current
.RasterDistance
=
455 SQRTF( eye
[0]*eye
[0] + eye
[1]*eye
[1] + eye
[2]*eye
[2] );
457 /* compute transformed normal vector (for lighting or texgen) */
458 if (ctx
->_NeedEyeCoords
) {
459 const GLfloat
*inv
= ctx
->ModelviewMatrixStack
.Top
->inv
;
460 TRANSFORM_NORMAL( eyenorm
, objnorm
, inv
);
467 /* update raster color */
468 if (ctx
->Light
.Enabled
) {
470 shade_rastpos( ctx
, vObj
, norm
,
471 ctx
->Current
.RasterColor
,
472 ctx
->Current
.RasterSecondaryColor
,
473 &ctx
->Current
.RasterIndex
);
476 /* use current color or index */
477 if (ctx
->Visual
.rgbMode
) {
478 COPY_4FV(ctx
->Current
.RasterColor
,
479 ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
]);
480 COPY_4FV(ctx
->Current
.RasterSecondaryColor
,
481 ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR1
]);
484 ctx
->Current
.RasterIndex
485 = ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR_INDEX
][0];
492 for (u
= 0; u
< ctx
->Const
.MaxTextureCoordUnits
; u
++) {
494 COPY_4V(tc
, ctx
->Current
.Attrib
[VERT_ATTRIB_TEX0
+ u
]);
495 if (ctx
->Texture
.Unit
[u
].TexGenEnabled
) {
496 compute_texgen(ctx
, vObj
, eye
, norm
, u
, tc
);
498 TRANSFORM_POINT(ctx
->Current
.RasterTexCoords
[u
],
499 ctx
->TextureMatrixStack
[u
].Top
->m
, tc
);
503 ctx
->Current
.RasterPosValid
= GL_TRUE
;
506 if (ctx
->RenderMode
== GL_SELECT
) {
507 _mesa_update_hitflag( ctx
, ctx
->Current
.RasterPos
[2] );