mesa: replace GET_SHINE_TAB_ENTRY() macro with an inline function
[mesa.git] / src / mesa / tnl / t_rasterpos.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.1
4 *
5 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
6 *
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:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
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.
23 */
24
25
26 #include "main/glheader.h"
27 #include "main/colormac.h"
28 #include "main/feedback.h"
29 #include "main/light.h"
30 #include "main/macros.h"
31 #include "main/simple_list.h"
32 #include "main/mtypes.h"
33
34 #include "math/m_matrix.h"
35 #include "tnl/tnl.h"
36
37
38
39 /**
40 * Clip a point against the view volume.
41 *
42 * \param v vertex vector describing the point to clip.
43 *
44 * \return zero if outside view volume, or one if inside.
45 */
46 static GLuint
47 viewclip_point_xy( const GLfloat v[] )
48 {
49 if ( v[0] > v[3] || v[0] < -v[3]
50 || v[1] > v[3] || v[1] < -v[3] ) {
51 return 0;
52 }
53 else {
54 return 1;
55 }
56 }
57
58
59 /**
60 * Clip a point against the far/near Z clipping planes.
61 *
62 * \param v vertex vector describing the point to clip.
63 *
64 * \return zero if outside view volume, or one if inside.
65 */
66 static GLuint
67 viewclip_point_z( const GLfloat v[] )
68 {
69 if (v[2] > v[3] || v[2] < -v[3] ) {
70 return 0;
71 }
72 else {
73 return 1;
74 }
75 }
76
77
78 /**
79 * Clip a point against the user clipping planes.
80 *
81 * \param ctx GL context.
82 * \param v vertex vector describing the point to clip.
83 *
84 * \return zero if the point was clipped, or one otherwise.
85 */
86 static GLuint
87 userclip_point( struct gl_context *ctx, const GLfloat v[] )
88 {
89 GLuint p;
90
91 for (p = 0; p < ctx->Const.MaxClipPlanes; p++) {
92 if (ctx->Transform.ClipPlanesEnabled & (1 << p)) {
93 GLfloat dot = v[0] * ctx->Transform._ClipUserPlane[p][0]
94 + v[1] * ctx->Transform._ClipUserPlane[p][1]
95 + v[2] * ctx->Transform._ClipUserPlane[p][2]
96 + v[3] * ctx->Transform._ClipUserPlane[p][3];
97 if (dot < 0.0F) {
98 return 0;
99 }
100 }
101 }
102
103 return 1;
104 }
105
106
107 /**
108 * Compute lighting for the raster position. RGB modes computed.
109 * \param ctx the context
110 * \param vertex vertex location
111 * \param normal normal vector
112 * \param Rcolor returned color
113 * \param Rspec returned specular color (if separate specular enabled)
114 */
115 static void
116 shade_rastpos(struct gl_context *ctx,
117 const GLfloat vertex[4],
118 const GLfloat normal[3],
119 GLfloat Rcolor[4],
120 GLfloat Rspec[4])
121 {
122 /*const*/ GLfloat (*base)[3] = ctx->Light._BaseColor;
123 const struct gl_light *light;
124 GLfloat diffuseColor[4], specularColor[4]; /* for RGB mode only */
125
126 _mesa_validate_all_lighting_tables( ctx );
127
128 COPY_3V(diffuseColor, base[0]);
129 diffuseColor[3] = CLAMP(
130 ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE][3], 0.0F, 1.0F );
131 ASSIGN_4V(specularColor, 0.0, 0.0, 0.0, 1.0);
132
133 foreach (light, &ctx->Light.EnabledList) {
134 GLfloat attenuation = 1.0;
135 GLfloat VP[3]; /* vector from vertex to light pos */
136 GLfloat n_dot_VP;
137 GLfloat diffuseContrib[3], specularContrib[3];
138
139 if (!(light->_Flags & LIGHT_POSITIONAL)) {
140 /* light at infinity */
141 COPY_3V(VP, light->_VP_inf_norm);
142 attenuation = light->_VP_inf_spot_attenuation;
143 }
144 else {
145 /* local/positional light */
146 GLfloat d;
147
148 /* VP = vector from vertex pos to light[i].pos */
149 SUB_3V(VP, light->_Position, vertex);
150 /* d = length(VP) */
151 d = (GLfloat) LEN_3FV( VP );
152 if (d > 1.0e-6) {
153 /* normalize VP */
154 GLfloat invd = 1.0F / d;
155 SELF_SCALE_SCALAR_3V(VP, invd);
156 }
157
158 /* atti */
159 attenuation = 1.0F / (light->ConstantAttenuation + d *
160 (light->LinearAttenuation + d *
161 light->QuadraticAttenuation));
162
163 if (light->_Flags & LIGHT_SPOT) {
164 GLfloat PV_dot_dir = - DOT3(VP, light->_NormSpotDirection);
165
166 if (PV_dot_dir<light->_CosCutoff) {
167 continue;
168 }
169 else {
170 GLfloat spot = powf(PV_dot_dir, light->SpotExponent);
171 attenuation *= spot;
172 }
173 }
174 }
175
176 if (attenuation < 1e-3)
177 continue;
178
179 n_dot_VP = DOT3( normal, VP );
180
181 if (n_dot_VP < 0.0F) {
182 ACC_SCALE_SCALAR_3V(diffuseColor, attenuation, light->_MatAmbient[0]);
183 continue;
184 }
185
186 /* Ambient + diffuse */
187 COPY_3V(diffuseContrib, light->_MatAmbient[0]);
188 ACC_SCALE_SCALAR_3V(diffuseContrib, n_dot_VP, light->_MatDiffuse[0]);
189
190 /* Specular */
191 {
192 const GLfloat *h;
193 GLfloat n_dot_h;
194
195 ASSIGN_3V(specularContrib, 0.0, 0.0, 0.0);
196
197 if (ctx->Light.Model.LocalViewer) {
198 GLfloat v[3];
199 COPY_3V(v, vertex);
200 NORMALIZE_3FV(v);
201 SUB_3V(VP, VP, v);
202 NORMALIZE_3FV(VP);
203 h = VP;
204 }
205 else if (light->_Flags & LIGHT_POSITIONAL) {
206 ACC_3V(VP, ctx->_EyeZDir);
207 NORMALIZE_3FV(VP);
208 h = VP;
209 }
210 else {
211 h = light->_h_inf_norm;
212 }
213
214 n_dot_h = DOT3(normal, h);
215
216 if (n_dot_h > 0.0F) {
217 GLfloat spec_coef = _mesa_lookup_shininess(ctx, 0, n_dot_h);
218
219 if (spec_coef > 1.0e-10) {
220 if (ctx->Light.Model.ColorControl==GL_SEPARATE_SPECULAR_COLOR) {
221 ACC_SCALE_SCALAR_3V( specularContrib, spec_coef,
222 light->_MatSpecular[0]);
223 }
224 else {
225 ACC_SCALE_SCALAR_3V( diffuseContrib, spec_coef,
226 light->_MatSpecular[0]);
227 }
228 }
229 }
230 }
231
232 ACC_SCALE_SCALAR_3V( diffuseColor, attenuation, diffuseContrib );
233 ACC_SCALE_SCALAR_3V( specularColor, attenuation, specularContrib );
234 }
235
236 Rcolor[0] = CLAMP(diffuseColor[0], 0.0F, 1.0F);
237 Rcolor[1] = CLAMP(diffuseColor[1], 0.0F, 1.0F);
238 Rcolor[2] = CLAMP(diffuseColor[2], 0.0F, 1.0F);
239 Rcolor[3] = CLAMP(diffuseColor[3], 0.0F, 1.0F);
240 Rspec[0] = CLAMP(specularColor[0], 0.0F, 1.0F);
241 Rspec[1] = CLAMP(specularColor[1], 0.0F, 1.0F);
242 Rspec[2] = CLAMP(specularColor[2], 0.0F, 1.0F);
243 Rspec[3] = CLAMP(specularColor[3], 0.0F, 1.0F);
244 }
245
246
247 /**
248 * Do texgen needed for glRasterPos.
249 * \param ctx rendering context
250 * \param vObj object-space vertex coordinate
251 * \param vEye eye-space vertex coordinate
252 * \param normal vertex normal
253 * \param unit texture unit number
254 * \param texcoord incoming texcoord and resulting texcoord
255 */
256 static void
257 compute_texgen(struct gl_context *ctx, const GLfloat vObj[4], const GLfloat vEye[4],
258 const GLfloat normal[3], GLuint unit, GLfloat texcoord[4])
259 {
260 const struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
261
262 /* always compute sphere map terms, just in case */
263 GLfloat u[3], two_nu, rx, ry, rz, m, mInv;
264 COPY_3V(u, vEye);
265 NORMALIZE_3FV(u);
266 two_nu = 2.0F * DOT3(normal, u);
267 rx = u[0] - normal[0] * two_nu;
268 ry = u[1] - normal[1] * two_nu;
269 rz = u[2] - normal[2] * two_nu;
270 m = rx * rx + ry * ry + (rz + 1.0F) * (rz + 1.0F);
271 if (m > 0.0F)
272 mInv = 0.5F * _mesa_inv_sqrtf(m);
273 else
274 mInv = 0.0F;
275
276 if (texUnit->TexGenEnabled & S_BIT) {
277 switch (texUnit->GenS.Mode) {
278 case GL_OBJECT_LINEAR:
279 texcoord[0] = DOT4(vObj, texUnit->GenS.ObjectPlane);
280 break;
281 case GL_EYE_LINEAR:
282 texcoord[0] = DOT4(vEye, texUnit->GenS.EyePlane);
283 break;
284 case GL_SPHERE_MAP:
285 texcoord[0] = rx * mInv + 0.5F;
286 break;
287 case GL_REFLECTION_MAP:
288 texcoord[0] = rx;
289 break;
290 case GL_NORMAL_MAP:
291 texcoord[0] = normal[0];
292 break;
293 default:
294 _mesa_problem(ctx, "Bad S texgen in compute_texgen()");
295 return;
296 }
297 }
298
299 if (texUnit->TexGenEnabled & T_BIT) {
300 switch (texUnit->GenT.Mode) {
301 case GL_OBJECT_LINEAR:
302 texcoord[1] = DOT4(vObj, texUnit->GenT.ObjectPlane);
303 break;
304 case GL_EYE_LINEAR:
305 texcoord[1] = DOT4(vEye, texUnit->GenT.EyePlane);
306 break;
307 case GL_SPHERE_MAP:
308 texcoord[1] = ry * mInv + 0.5F;
309 break;
310 case GL_REFLECTION_MAP:
311 texcoord[1] = ry;
312 break;
313 case GL_NORMAL_MAP:
314 texcoord[1] = normal[1];
315 break;
316 default:
317 _mesa_problem(ctx, "Bad T texgen in compute_texgen()");
318 return;
319 }
320 }
321
322 if (texUnit->TexGenEnabled & R_BIT) {
323 switch (texUnit->GenR.Mode) {
324 case GL_OBJECT_LINEAR:
325 texcoord[2] = DOT4(vObj, texUnit->GenR.ObjectPlane);
326 break;
327 case GL_EYE_LINEAR:
328 texcoord[2] = DOT4(vEye, texUnit->GenR.EyePlane);
329 break;
330 case GL_REFLECTION_MAP:
331 texcoord[2] = rz;
332 break;
333 case GL_NORMAL_MAP:
334 texcoord[2] = normal[2];
335 break;
336 default:
337 _mesa_problem(ctx, "Bad R texgen in compute_texgen()");
338 return;
339 }
340 }
341
342 if (texUnit->TexGenEnabled & Q_BIT) {
343 switch (texUnit->GenQ.Mode) {
344 case GL_OBJECT_LINEAR:
345 texcoord[3] = DOT4(vObj, texUnit->GenQ.ObjectPlane);
346 break;
347 case GL_EYE_LINEAR:
348 texcoord[3] = DOT4(vEye, texUnit->GenQ.EyePlane);
349 break;
350 default:
351 _mesa_problem(ctx, "Bad Q texgen in compute_texgen()");
352 return;
353 }
354 }
355 }
356
357
358 /**
359 * glRasterPos transformation. Typically called via ctx->Driver.RasterPos().
360 * XXX some of this code (such as viewport xform, clip testing and setting
361 * of ctx->Current.Raster* fields) could get lifted up into the
362 * main/rasterpos.c code.
363 *
364 * \param vObj vertex position in object space
365 */
366 void
367 _tnl_RasterPos(struct gl_context *ctx, const GLfloat vObj[4])
368 {
369 if (ctx->VertexProgram._Enabled) {
370 /* XXX implement this */
371 _mesa_problem(ctx, "Vertex programs not implemented for glRasterPos");
372 return;
373 }
374 else {
375 GLfloat eye[4], clip[4], ndc[3], d;
376 GLfloat *norm, eyenorm[3];
377 GLfloat *objnorm = ctx->Current.Attrib[VERT_ATTRIB_NORMAL];
378
379 /* apply modelview matrix: eye = MV * obj */
380 TRANSFORM_POINT( eye, ctx->ModelviewMatrixStack.Top->m, vObj );
381 /* apply projection matrix: clip = Proj * eye */
382 TRANSFORM_POINT( clip, ctx->ProjectionMatrixStack.Top->m, eye );
383
384 /* clip to view volume. */
385 if (!ctx->Transform.DepthClamp) {
386 if (viewclip_point_z(clip) == 0) {
387 ctx->Current.RasterPosValid = GL_FALSE;
388 return;
389 }
390 }
391 if (!ctx->Transform.RasterPositionUnclipped) {
392 if (viewclip_point_xy(clip) == 0) {
393 ctx->Current.RasterPosValid = GL_FALSE;
394 return;
395 }
396 }
397
398 /* clip to user clipping planes */
399 if (ctx->Transform.ClipPlanesEnabled && !userclip_point(ctx, clip)) {
400 ctx->Current.RasterPosValid = GL_FALSE;
401 return;
402 }
403
404 /* ndc = clip / W */
405 d = (clip[3] == 0.0F) ? 1.0F : 1.0F / clip[3];
406 ndc[0] = clip[0] * d;
407 ndc[1] = clip[1] * d;
408 ndc[2] = clip[2] * d;
409 /* wincoord = viewport_mapping(ndc) */
410 ctx->Current.RasterPos[0] = (ndc[0] * ctx->Viewport._WindowMap.m[MAT_SX]
411 + ctx->Viewport._WindowMap.m[MAT_TX]);
412 ctx->Current.RasterPos[1] = (ndc[1] * ctx->Viewport._WindowMap.m[MAT_SY]
413 + ctx->Viewport._WindowMap.m[MAT_TY]);
414 ctx->Current.RasterPos[2] = (ndc[2] * ctx->Viewport._WindowMap.m[MAT_SZ]
415 + ctx->Viewport._WindowMap.m[MAT_TZ])
416 / ctx->DrawBuffer->_DepthMaxF;
417 ctx->Current.RasterPos[3] = clip[3];
418
419 if (ctx->Transform.DepthClamp) {
420 ctx->Current.RasterPos[3] = CLAMP(ctx->Current.RasterPos[3],
421 ctx->Viewport.Near,
422 ctx->Viewport.Far);
423 }
424
425 /* compute raster distance */
426 if (ctx->Fog.FogCoordinateSource == GL_FOG_COORDINATE_EXT)
427 ctx->Current.RasterDistance = ctx->Current.Attrib[VERT_ATTRIB_FOG][0];
428 else
429 ctx->Current.RasterDistance =
430 SQRTF( eye[0]*eye[0] + eye[1]*eye[1] + eye[2]*eye[2] );
431
432 /* compute transformed normal vector (for lighting or texgen) */
433 if (ctx->_NeedEyeCoords) {
434 const GLfloat *inv = ctx->ModelviewMatrixStack.Top->inv;
435 TRANSFORM_NORMAL( eyenorm, objnorm, inv );
436 norm = eyenorm;
437 }
438 else {
439 norm = objnorm;
440 }
441
442 /* update raster color */
443 if (ctx->Light.Enabled) {
444 /* lighting */
445 shade_rastpos( ctx, vObj, norm,
446 ctx->Current.RasterColor,
447 ctx->Current.RasterSecondaryColor );
448 }
449 else {
450 /* use current color */
451 COPY_4FV(ctx->Current.RasterColor,
452 ctx->Current.Attrib[VERT_ATTRIB_COLOR0]);
453 COPY_4FV(ctx->Current.RasterSecondaryColor,
454 ctx->Current.Attrib[VERT_ATTRIB_COLOR1]);
455 }
456
457 /* texture coords */
458 {
459 GLuint u;
460 for (u = 0; u < ctx->Const.MaxTextureCoordUnits; u++) {
461 GLfloat tc[4];
462 COPY_4V(tc, ctx->Current.Attrib[VERT_ATTRIB_TEX0 + u]);
463 if (ctx->Texture.Unit[u].TexGenEnabled) {
464 compute_texgen(ctx, vObj, eye, norm, u, tc);
465 }
466 TRANSFORM_POINT(ctx->Current.RasterTexCoords[u],
467 ctx->TextureMatrixStack[u].Top->m, tc);
468 }
469 }
470
471 ctx->Current.RasterPosValid = GL_TRUE;
472 }
473
474 if (ctx->RenderMode == GL_SELECT) {
475 _mesa_update_hitflag( ctx, ctx->Current.RasterPos[2] );
476 }
477 }