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