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.
33 #include "simple_list.h"
36 #include "math/m_matrix.h"
42 * Clip a point against the view volume.
44 * \param v vertex vector describing the point to clip.
46 * \return zero if outside view volume, or one if inside.
49 viewclip_point( const GLfloat v
[] )
51 if ( v
[0] > v
[3] || v
[0] < -v
[3]
52 || v
[1] > v
[3] || v
[1] < -v
[3]
53 || v
[2] > v
[3] || v
[2] < -v
[3] ) {
63 * Clip a point against the far/near Z clipping planes.
65 * \param v vertex vector describing the point to clip.
67 * \return zero if outside view volume, or one if inside.
70 viewclip_point_z( const GLfloat v
[] )
72 if (v
[2] > v
[3] || v
[2] < -v
[3] ) {
82 * Clip a point against the user clipping planes.
84 * \param ctx GL context.
85 * \param v vertex vector describing the point to clip.
87 * \return zero if the point was clipped, or one otherwise.
90 userclip_point( GLcontext
*ctx
, const GLfloat v
[] )
94 for (p
= 0; p
< ctx
->Const
.MaxClipPlanes
; p
++) {
95 if (ctx
->Transform
.ClipPlanesEnabled
& (1 << p
)) {
96 GLfloat dot
= v
[0] * ctx
->Transform
._ClipUserPlane
[p
][0]
97 + v
[1] * ctx
->Transform
._ClipUserPlane
[p
][1]
98 + v
[2] * ctx
->Transform
._ClipUserPlane
[p
][2]
99 + v
[3] * ctx
->Transform
._ClipUserPlane
[p
][3];
111 * Compute lighting for the raster position. Both RGB and CI modes computed.
112 * \param ctx the context
113 * \param vertex vertex location
114 * \param normal normal vector
115 * \param Rcolor returned color
116 * \param Rspec returned specular color (if separate specular enabled)
117 * \param Rindex returned color index
120 shade_rastpos(GLcontext
*ctx
,
121 const GLfloat vertex
[4],
122 const GLfloat normal
[3],
127 /*const*/ GLfloat (*base
)[3] = ctx
->Light
._BaseColor
;
128 const struct gl_light
*light
;
129 GLfloat diffuseColor
[4], specularColor
[4]; /* for RGB mode only */
130 GLfloat diffuseCI
= 0.0, specularCI
= 0.0; /* for CI mode only */
132 _mesa_validate_all_lighting_tables( ctx
);
134 COPY_3V(diffuseColor
, base
[0]);
135 diffuseColor
[3] = CLAMP(
136 ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
][3], 0.0F
, 1.0F
);
137 ASSIGN_4V(specularColor
, 0.0, 0.0, 0.0, 1.0);
139 foreach (light
, &ctx
->Light
.EnabledList
) {
140 GLfloat attenuation
= 1.0;
141 GLfloat VP
[3]; /* vector from vertex to light pos */
143 GLfloat diffuseContrib
[3], specularContrib
[3];
145 if (!(light
->_Flags
& LIGHT_POSITIONAL
)) {
146 /* light at infinity */
147 COPY_3V(VP
, light
->_VP_inf_norm
);
148 attenuation
= light
->_VP_inf_spot_attenuation
;
151 /* local/positional light */
154 /* VP = vector from vertex pos to light[i].pos */
155 SUB_3V(VP
, light
->_Position
, vertex
);
157 d
= (GLfloat
) LEN_3FV( VP
);
160 GLfloat invd
= 1.0F
/ d
;
161 SELF_SCALE_SCALAR_3V(VP
, invd
);
165 attenuation
= 1.0F
/ (light
->ConstantAttenuation
+ d
*
166 (light
->LinearAttenuation
+ d
*
167 light
->QuadraticAttenuation
));
169 if (light
->_Flags
& LIGHT_SPOT
) {
170 GLfloat PV_dot_dir
= - DOT3(VP
, light
->_NormDirection
);
172 if (PV_dot_dir
<light
->_CosCutoff
) {
176 double x
= PV_dot_dir
* (EXP_TABLE_SIZE
-1);
178 GLfloat spot
= (GLfloat
) (light
->_SpotExpTable
[k
][0]
179 + (x
-k
)*light
->_SpotExpTable
[k
][1]);
185 if (attenuation
< 1e-3)
188 n_dot_VP
= DOT3( normal
, VP
);
190 if (n_dot_VP
< 0.0F
) {
191 ACC_SCALE_SCALAR_3V(diffuseColor
, attenuation
, light
->_MatAmbient
[0]);
195 /* Ambient + diffuse */
196 COPY_3V(diffuseContrib
, light
->_MatAmbient
[0]);
197 ACC_SCALE_SCALAR_3V(diffuseContrib
, n_dot_VP
, light
->_MatDiffuse
[0]);
198 diffuseCI
+= n_dot_VP
* light
->_dli
* attenuation
;
205 ASSIGN_3V(specularContrib
, 0.0, 0.0, 0.0);
207 if (ctx
->Light
.Model
.LocalViewer
) {
215 else if (light
->_Flags
& LIGHT_POSITIONAL
) {
216 ACC_3V(VP
, ctx
->_EyeZDir
);
221 h
= light
->_h_inf_norm
;
224 n_dot_h
= DOT3(normal
, h
);
226 if (n_dot_h
> 0.0F
) {
228 GET_SHINE_TAB_ENTRY( ctx
->_ShineTable
[0], n_dot_h
, spec_coef
);
230 if (spec_coef
> 1.0e-10) {
231 if (ctx
->Light
.Model
.ColorControl
==GL_SEPARATE_SPECULAR_COLOR
) {
232 ACC_SCALE_SCALAR_3V( specularContrib
, spec_coef
,
233 light
->_MatSpecular
[0]);
236 ACC_SCALE_SCALAR_3V( diffuseContrib
, spec_coef
,
237 light
->_MatSpecular
[0]);
239 /*assert(light->_sli > 0.0);*/
240 specularCI
+= spec_coef
* light
->_sli
* attenuation
;
245 ACC_SCALE_SCALAR_3V( diffuseColor
, attenuation
, diffuseContrib
);
246 ACC_SCALE_SCALAR_3V( specularColor
, attenuation
, specularContrib
);
249 if (ctx
->Visual
.rgbMode
) {
250 Rcolor
[0] = CLAMP(diffuseColor
[0], 0.0F
, 1.0F
);
251 Rcolor
[1] = CLAMP(diffuseColor
[1], 0.0F
, 1.0F
);
252 Rcolor
[2] = CLAMP(diffuseColor
[2], 0.0F
, 1.0F
);
253 Rcolor
[3] = CLAMP(diffuseColor
[3], 0.0F
, 1.0F
);
254 Rspec
[0] = CLAMP(specularColor
[0], 0.0F
, 1.0F
);
255 Rspec
[1] = CLAMP(specularColor
[1], 0.0F
, 1.0F
);
256 Rspec
[2] = CLAMP(specularColor
[2], 0.0F
, 1.0F
);
257 Rspec
[3] = CLAMP(specularColor
[3], 0.0F
, 1.0F
);
260 GLfloat
*ind
= ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_INDEXES
];
261 GLfloat d_a
= ind
[MAT_INDEX_DIFFUSE
] - ind
[MAT_INDEX_AMBIENT
];
262 GLfloat s_a
= ind
[MAT_INDEX_SPECULAR
] - ind
[MAT_INDEX_AMBIENT
];
263 GLfloat i
= (ind
[MAT_INDEX_AMBIENT
]
264 + diffuseCI
* (1.0F
-specularCI
) * d_a
266 if (i
> ind
[MAT_INDEX_SPECULAR
]) {
267 i
= ind
[MAT_INDEX_SPECULAR
];
275 * Do texgen needed for glRasterPos.
276 * \param ctx rendering context
277 * \param vObj object-space vertex coordinate
278 * \param vEye eye-space vertex coordinate
279 * \param normal vertex normal
280 * \param unit texture unit number
281 * \param texcoord incoming texcoord and resulting texcoord
284 compute_texgen(GLcontext
*ctx
, const GLfloat vObj
[4], const GLfloat vEye
[4],
285 const GLfloat normal
[3], GLuint unit
, GLfloat texcoord
[4])
287 const struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[unit
];
289 /* always compute sphere map terms, just in case */
290 GLfloat u
[3], two_nu
, rx
, ry
, rz
, m
, mInv
;
293 two_nu
= 2.0F
* DOT3(normal
, u
);
294 rx
= u
[0] - normal
[0] * two_nu
;
295 ry
= u
[1] - normal
[1] * two_nu
;
296 rz
= u
[2] - normal
[2] * two_nu
;
297 m
= rx
* rx
+ ry
* ry
+ (rz
+ 1.0F
) * (rz
+ 1.0F
);
299 mInv
= 0.5F
* _mesa_inv_sqrtf(m
);
303 if (texUnit
->TexGenEnabled
& S_BIT
) {
304 switch (texUnit
->GenModeS
) {
305 case GL_OBJECT_LINEAR
:
306 texcoord
[0] = DOT4(vObj
, texUnit
->ObjectPlaneS
);
309 texcoord
[0] = DOT4(vEye
, texUnit
->EyePlaneS
);
312 texcoord
[0] = rx
* mInv
+ 0.5F
;
314 case GL_REFLECTION_MAP
:
318 texcoord
[0] = normal
[0];
321 _mesa_problem(ctx
, "Bad S texgen in compute_texgen()");
326 if (texUnit
->TexGenEnabled
& T_BIT
) {
327 switch (texUnit
->GenModeT
) {
328 case GL_OBJECT_LINEAR
:
329 texcoord
[1] = DOT4(vObj
, texUnit
->ObjectPlaneT
);
332 texcoord
[1] = DOT4(vEye
, texUnit
->EyePlaneT
);
335 texcoord
[1] = ry
* mInv
+ 0.5F
;
337 case GL_REFLECTION_MAP
:
341 texcoord
[1] = normal
[1];
344 _mesa_problem(ctx
, "Bad T texgen in compute_texgen()");
349 if (texUnit
->TexGenEnabled
& R_BIT
) {
350 switch (texUnit
->GenModeR
) {
351 case GL_OBJECT_LINEAR
:
352 texcoord
[2] = DOT4(vObj
, texUnit
->ObjectPlaneR
);
355 texcoord
[2] = DOT4(vEye
, texUnit
->EyePlaneR
);
357 case GL_REFLECTION_MAP
:
361 texcoord
[2] = normal
[2];
364 _mesa_problem(ctx
, "Bad R texgen in compute_texgen()");
369 if (texUnit
->TexGenEnabled
& Q_BIT
) {
370 switch (texUnit
->GenModeQ
) {
371 case GL_OBJECT_LINEAR
:
372 texcoord
[3] = DOT4(vObj
, texUnit
->ObjectPlaneQ
);
375 texcoord
[3] = DOT4(vEye
, texUnit
->EyePlaneQ
);
378 _mesa_problem(ctx
, "Bad Q texgen in compute_texgen()");
386 * glRasterPos transformation. Typically called via ctx->Driver.RasterPos().
387 * XXX some of this code (such as viewport xform, clip testing and setting
388 * of ctx->Current.Raster* fields) could get lifted up into the
389 * main/rasterpos.c code.
391 * \param vObj vertex position in object space
394 _tnl_RasterPos(GLcontext
*ctx
, const GLfloat vObj
[4])
396 if (ctx
->VertexProgram
._Enabled
) {
397 /* XXX implement this */
398 _mesa_problem(ctx
, "Vertex programs not implemented for glRasterPos");
402 GLfloat eye
[4], clip
[4], ndc
[3], d
;
403 GLfloat
*norm
, eyenorm
[3];
404 GLfloat
*objnorm
= ctx
->Current
.Attrib
[VERT_ATTRIB_NORMAL
];
406 /* apply modelview matrix: eye = MV * obj */
407 TRANSFORM_POINT( eye
, ctx
->ModelviewMatrixStack
.Top
->m
, vObj
);
408 /* apply projection matrix: clip = Proj * eye */
409 TRANSFORM_POINT( clip
, ctx
->ProjectionMatrixStack
.Top
->m
, eye
);
411 /* clip to view volume */
412 if (ctx
->Transform
.RasterPositionUnclipped
) {
413 /* GL_IBM_rasterpos_clip: only clip against Z */
414 if (viewclip_point_z(clip
) == 0) {
415 ctx
->Current
.RasterPosValid
= GL_FALSE
;
419 else if (viewclip_point(clip
) == 0) {
420 /* Normal OpenGL behaviour */
421 ctx
->Current
.RasterPosValid
= GL_FALSE
;
425 /* clip to user clipping planes */
426 if (ctx
->Transform
.ClipPlanesEnabled
&& !userclip_point(ctx
, clip
)) {
427 ctx
->Current
.RasterPosValid
= GL_FALSE
;
432 d
= (clip
[3] == 0.0F
) ? 1.0F
: 1.0F
/ clip
[3];
433 ndc
[0] = clip
[0] * d
;
434 ndc
[1] = clip
[1] * d
;
435 ndc
[2] = clip
[2] * d
;
436 /* wincoord = viewport_mapping(ndc) */
437 ctx
->Current
.RasterPos
[0] = (ndc
[0] * ctx
->Viewport
._WindowMap
.m
[MAT_SX
]
438 + ctx
->Viewport
._WindowMap
.m
[MAT_TX
]);
439 ctx
->Current
.RasterPos
[1] = (ndc
[1] * ctx
->Viewport
._WindowMap
.m
[MAT_SY
]
440 + ctx
->Viewport
._WindowMap
.m
[MAT_TY
]);
441 ctx
->Current
.RasterPos
[2] = (ndc
[2] * ctx
->Viewport
._WindowMap
.m
[MAT_SZ
]
442 + ctx
->Viewport
._WindowMap
.m
[MAT_TZ
])
443 / ctx
->DrawBuffer
->_DepthMaxF
;
444 ctx
->Current
.RasterPos
[3] = clip
[3];
446 /* compute raster distance */
447 if (ctx
->Fog
.FogCoordinateSource
== GL_FOG_COORDINATE_EXT
)
448 ctx
->Current
.RasterDistance
= ctx
->Current
.Attrib
[VERT_ATTRIB_FOG
][0];
450 ctx
->Current
.RasterDistance
=
451 SQRTF( eye
[0]*eye
[0] + eye
[1]*eye
[1] + eye
[2]*eye
[2] );
453 /* compute transformed normal vector (for lighting or texgen) */
454 if (ctx
->_NeedEyeCoords
) {
455 const GLfloat
*inv
= ctx
->ModelviewMatrixStack
.Top
->inv
;
456 TRANSFORM_NORMAL( eyenorm
, objnorm
, inv
);
463 /* update raster color */
464 if (ctx
->Light
.Enabled
) {
466 shade_rastpos( ctx
, vObj
, norm
,
467 ctx
->Current
.RasterColor
,
468 ctx
->Current
.RasterSecondaryColor
,
469 &ctx
->Current
.RasterIndex
);
472 /* use current color or index */
473 if (ctx
->Visual
.rgbMode
) {
474 COPY_4FV(ctx
->Current
.RasterColor
,
475 ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
]);
476 COPY_4FV(ctx
->Current
.RasterSecondaryColor
,
477 ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR1
]);
480 ctx
->Current
.RasterIndex
481 = ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR_INDEX
][0];
488 for (u
= 0; u
< ctx
->Const
.MaxTextureCoordUnits
; u
++) {
490 COPY_4V(tc
, ctx
->Current
.Attrib
[VERT_ATTRIB_TEX0
+ u
]);
491 if (ctx
->Texture
.Unit
[u
].TexGenEnabled
) {
492 compute_texgen(ctx
, vObj
, eye
, norm
, u
, tc
);
494 TRANSFORM_POINT(ctx
->Current
.RasterTexCoords
[u
],
495 ctx
->TextureMatrixStack
[u
].Top
->m
, tc
);
499 ctx
->Current
.RasterPosValid
= GL_TRUE
;
502 if (ctx
->RenderMode
== GL_SELECT
) {
503 _mesa_update_hitflag( ctx
, ctx
->Current
.RasterPos
[2] );