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