mesa: prefix #includes with main/
[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( const GLfloat v[] )
50 {
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] ) {
54 return 0;
55 }
56 else {
57 return 1;
58 }
59 }
60
61
62 /**
63 * Clip a point against the far/near Z clipping planes.
64 *
65 * \param v vertex vector describing the point to clip.
66 *
67 * \return zero if outside view volume, or one if inside.
68 */
69 static GLuint
70 viewclip_point_z( const GLfloat v[] )
71 {
72 if (v[2] > v[3] || v[2] < -v[3] ) {
73 return 0;
74 }
75 else {
76 return 1;
77 }
78 }
79
80
81 /**
82 * Clip a point against the user clipping planes.
83 *
84 * \param ctx GL context.
85 * \param v vertex vector describing the point to clip.
86 *
87 * \return zero if the point was clipped, or one otherwise.
88 */
89 static GLuint
90 userclip_point( GLcontext *ctx, const GLfloat v[] )
91 {
92 GLuint p;
93
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];
100 if (dot < 0.0F) {
101 return 0;
102 }
103 }
104 }
105
106 return 1;
107 }
108
109
110 /**
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
118 */
119 static void
120 shade_rastpos(GLcontext *ctx,
121 const GLfloat vertex[4],
122 const GLfloat normal[3],
123 GLfloat Rcolor[4],
124 GLfloat Rspec[4],
125 GLfloat *Rindex)
126 {
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 */
131
132 _mesa_validate_all_lighting_tables( ctx );
133
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);
138
139 foreach (light, &ctx->Light.EnabledList) {
140 GLfloat attenuation = 1.0;
141 GLfloat VP[3]; /* vector from vertex to light pos */
142 GLfloat n_dot_VP;
143 GLfloat diffuseContrib[3], specularContrib[3];
144
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;
149 }
150 else {
151 /* local/positional light */
152 GLfloat d;
153
154 /* VP = vector from vertex pos to light[i].pos */
155 SUB_3V(VP, light->_Position, vertex);
156 /* d = length(VP) */
157 d = (GLfloat) LEN_3FV( VP );
158 if (d > 1.0e-6) {
159 /* normalize VP */
160 GLfloat invd = 1.0F / d;
161 SELF_SCALE_SCALAR_3V(VP, invd);
162 }
163
164 /* atti */
165 attenuation = 1.0F / (light->ConstantAttenuation + d *
166 (light->LinearAttenuation + d *
167 light->QuadraticAttenuation));
168
169 if (light->_Flags & LIGHT_SPOT) {
170 GLfloat PV_dot_dir = - DOT3(VP, light->_NormDirection);
171
172 if (PV_dot_dir<light->_CosCutoff) {
173 continue;
174 }
175 else {
176 double x = PV_dot_dir * (EXP_TABLE_SIZE-1);
177 int k = (int) x;
178 GLfloat spot = (GLfloat) (light->_SpotExpTable[k][0]
179 + (x-k)*light->_SpotExpTable[k][1]);
180 attenuation *= spot;
181 }
182 }
183 }
184
185 if (attenuation < 1e-3)
186 continue;
187
188 n_dot_VP = DOT3( normal, VP );
189
190 if (n_dot_VP < 0.0F) {
191 ACC_SCALE_SCALAR_3V(diffuseColor, attenuation, light->_MatAmbient[0]);
192 continue;
193 }
194
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;
199
200 /* Specular */
201 {
202 const GLfloat *h;
203 GLfloat n_dot_h;
204
205 ASSIGN_3V(specularContrib, 0.0, 0.0, 0.0);
206
207 if (ctx->Light.Model.LocalViewer) {
208 GLfloat v[3];
209 COPY_3V(v, vertex);
210 NORMALIZE_3FV(v);
211 SUB_3V(VP, VP, v);
212 NORMALIZE_3FV(VP);
213 h = VP;
214 }
215 else if (light->_Flags & LIGHT_POSITIONAL) {
216 ACC_3V(VP, ctx->_EyeZDir);
217 NORMALIZE_3FV(VP);
218 h = VP;
219 }
220 else {
221 h = light->_h_inf_norm;
222 }
223
224 n_dot_h = DOT3(normal, h);
225
226 if (n_dot_h > 0.0F) {
227 GLfloat spec_coef;
228 GET_SHINE_TAB_ENTRY( ctx->_ShineTable[0], n_dot_h, spec_coef );
229
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]);
234 }
235 else {
236 ACC_SCALE_SCALAR_3V( diffuseContrib, spec_coef,
237 light->_MatSpecular[0]);
238 }
239 /*assert(light->_sli > 0.0);*/
240 specularCI += spec_coef * light->_sli * attenuation;
241 }
242 }
243 }
244
245 ACC_SCALE_SCALAR_3V( diffuseColor, attenuation, diffuseContrib );
246 ACC_SCALE_SCALAR_3V( specularColor, attenuation, specularContrib );
247 }
248
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);
258 }
259 else {
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
265 + specularCI * s_a);
266 if (i > ind[MAT_INDEX_SPECULAR]) {
267 i = ind[MAT_INDEX_SPECULAR];
268 }
269 *Rindex = i;
270 }
271 }
272
273
274 /**
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
282 */
283 static void
284 compute_texgen(GLcontext *ctx, const GLfloat vObj[4], const GLfloat vEye[4],
285 const GLfloat normal[3], GLuint unit, GLfloat texcoord[4])
286 {
287 const struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
288
289 /* always compute sphere map terms, just in case */
290 GLfloat u[3], two_nu, rx, ry, rz, m, mInv;
291 COPY_3V(u, vEye);
292 NORMALIZE_3FV(u);
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);
298 if (m > 0.0F)
299 mInv = 0.5F * _mesa_inv_sqrtf(m);
300 else
301 mInv = 0.0F;
302
303 if (texUnit->TexGenEnabled & S_BIT) {
304 switch (texUnit->GenModeS) {
305 case GL_OBJECT_LINEAR:
306 texcoord[0] = DOT4(vObj, texUnit->ObjectPlaneS);
307 break;
308 case GL_EYE_LINEAR:
309 texcoord[0] = DOT4(vEye, texUnit->EyePlaneS);
310 break;
311 case GL_SPHERE_MAP:
312 texcoord[0] = rx * mInv + 0.5F;
313 break;
314 case GL_REFLECTION_MAP:
315 texcoord[0] = rx;
316 break;
317 case GL_NORMAL_MAP:
318 texcoord[0] = normal[0];
319 break;
320 default:
321 _mesa_problem(ctx, "Bad S texgen in compute_texgen()");
322 return;
323 }
324 }
325
326 if (texUnit->TexGenEnabled & T_BIT) {
327 switch (texUnit->GenModeT) {
328 case GL_OBJECT_LINEAR:
329 texcoord[1] = DOT4(vObj, texUnit->ObjectPlaneT);
330 break;
331 case GL_EYE_LINEAR:
332 texcoord[1] = DOT4(vEye, texUnit->EyePlaneT);
333 break;
334 case GL_SPHERE_MAP:
335 texcoord[1] = ry * mInv + 0.5F;
336 break;
337 case GL_REFLECTION_MAP:
338 texcoord[1] = ry;
339 break;
340 case GL_NORMAL_MAP:
341 texcoord[1] = normal[1];
342 break;
343 default:
344 _mesa_problem(ctx, "Bad T texgen in compute_texgen()");
345 return;
346 }
347 }
348
349 if (texUnit->TexGenEnabled & R_BIT) {
350 switch (texUnit->GenModeR) {
351 case GL_OBJECT_LINEAR:
352 texcoord[2] = DOT4(vObj, texUnit->ObjectPlaneR);
353 break;
354 case GL_EYE_LINEAR:
355 texcoord[2] = DOT4(vEye, texUnit->EyePlaneR);
356 break;
357 case GL_REFLECTION_MAP:
358 texcoord[2] = rz;
359 break;
360 case GL_NORMAL_MAP:
361 texcoord[2] = normal[2];
362 break;
363 default:
364 _mesa_problem(ctx, "Bad R texgen in compute_texgen()");
365 return;
366 }
367 }
368
369 if (texUnit->TexGenEnabled & Q_BIT) {
370 switch (texUnit->GenModeQ) {
371 case GL_OBJECT_LINEAR:
372 texcoord[3] = DOT4(vObj, texUnit->ObjectPlaneQ);
373 break;
374 case GL_EYE_LINEAR:
375 texcoord[3] = DOT4(vEye, texUnit->EyePlaneQ);
376 break;
377 default:
378 _mesa_problem(ctx, "Bad Q texgen in compute_texgen()");
379 return;
380 }
381 }
382 }
383
384
385 /**
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.
390 *
391 * \param vObj vertex position in object space
392 */
393 void
394 _tnl_RasterPos(GLcontext *ctx, const GLfloat vObj[4])
395 {
396 if (ctx->VertexProgram._Enabled) {
397 /* XXX implement this */
398 _mesa_problem(ctx, "Vertex programs not implemented for glRasterPos");
399 return;
400 }
401 else {
402 GLfloat eye[4], clip[4], ndc[3], d;
403 GLfloat *norm, eyenorm[3];
404 GLfloat *objnorm = ctx->Current.Attrib[VERT_ATTRIB_NORMAL];
405
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 );
410
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;
416 return;
417 }
418 }
419 else if (viewclip_point(clip) == 0) {
420 /* Normal OpenGL behaviour */
421 ctx->Current.RasterPosValid = GL_FALSE;
422 return;
423 }
424
425 /* clip to user clipping planes */
426 if (ctx->Transform.ClipPlanesEnabled && !userclip_point(ctx, clip)) {
427 ctx->Current.RasterPosValid = GL_FALSE;
428 return;
429 }
430
431 /* ndc = clip / W */
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];
445
446 /* compute raster distance */
447 if (ctx->Fog.FogCoordinateSource == GL_FOG_COORDINATE_EXT)
448 ctx->Current.RasterDistance = ctx->Current.Attrib[VERT_ATTRIB_FOG][0];
449 else
450 ctx->Current.RasterDistance =
451 SQRTF( eye[0]*eye[0] + eye[1]*eye[1] + eye[2]*eye[2] );
452
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 );
457 norm = eyenorm;
458 }
459 else {
460 norm = objnorm;
461 }
462
463 /* update raster color */
464 if (ctx->Light.Enabled) {
465 /* lighting */
466 shade_rastpos( ctx, vObj, norm,
467 ctx->Current.RasterColor,
468 ctx->Current.RasterSecondaryColor,
469 &ctx->Current.RasterIndex );
470 }
471 else {
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]);
478 }
479 else {
480 ctx->Current.RasterIndex
481 = ctx->Current.Attrib[VERT_ATTRIB_COLOR_INDEX][0];
482 }
483 }
484
485 /* texture coords */
486 {
487 GLuint u;
488 for (u = 0; u < ctx->Const.MaxTextureCoordUnits; u++) {
489 GLfloat tc[4];
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);
493 }
494 TRANSFORM_POINT(ctx->Current.RasterTexCoords[u],
495 ctx->TextureMatrixStack[u].Top->m, tc);
496 }
497 }
498
499 ctx->Current.RasterPosValid = GL_TRUE;
500 }
501
502 if (ctx->RenderMode == GL_SELECT) {
503 _mesa_update_hitflag( ctx, ctx->Current.RasterPos[2] );
504 }
505 }