badd3e7b37131fc04d683d0724cdf3dacaadd77f
1 /* $Id: rastpos.c,v 1.19 2001/01/03 15:56:41 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2000 Brian Paul All Rights Reserved.
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
41 #include "simple_list.h"
44 #include "math/m_matrix.h"
45 #include "math/m_xform.h"
50 * Clip a point against the view volume.
51 * Input: v - vertex-vector describing the point to clip
52 * Return: 0 = outside view volume
53 * 1 = inside view volume
56 viewclip_point( const GLfloat v
[] )
58 if ( v
[0] > v
[3] || v
[0] < -v
[3]
59 || v
[1] > v
[3] || v
[1] < -v
[3]
60 || v
[2] > v
[3] || v
[2] < -v
[3] ) {
70 * Clip a point against the user clipping planes.
71 * Input: v - vertex-vector describing the point to clip.
72 * Return: 0 = point was clipped
73 * 1 = point not clipped
76 userclip_point( GLcontext
* ctx
, const GLfloat v
[] )
80 for (p
= 0; p
< ctx
->Const
.MaxClipPlanes
; p
++) {
81 if (ctx
->Transform
.ClipEnabled
[p
]) {
82 GLfloat dot
= v
[0] * ctx
->Transform
._ClipUserPlane
[p
][0]
83 + v
[1] * ctx
->Transform
._ClipUserPlane
[p
][1]
84 + v
[2] * ctx
->Transform
._ClipUserPlane
[p
][2]
85 + v
[3] * ctx
->Transform
._ClipUserPlane
[p
][3];
96 /* This has been split off to allow the normal shade routines to
97 * get a little closer to the vertex buffer, and to use the
98 * GLvector objects directly.
101 shade_rastpos(GLcontext
*ctx
,
102 const GLfloat vertex
[4],
103 const GLfloat normal
[3],
107 GLfloat (*base
)[3] = ctx
->Light
._BaseColor
;
108 const GLchan
*sumA
= ctx
->Light
._BaseAlpha
;
109 struct gl_light
*light
;
111 GLfloat diffuse
= 0, specular
= 0;
113 COPY_3V(color
, base
[0]);
114 color
[3] = CHAN_TO_FLOAT( sumA
[0] );
116 foreach (light
, &ctx
->Light
.EnabledList
) {
118 GLfloat attenuation
= 1.0;
123 GLboolean normalized
;
125 if (!(light
->_Flags
& LIGHT_POSITIONAL
)) {
126 COPY_3V(VP
, light
->_VP_inf_norm
);
127 attenuation
= light
->_VP_inf_spot_attenuation
;
132 SUB_3V(VP
, light
->_Position
, vertex
);
136 GLfloat invd
= 1.0F
/ d
;
137 SELF_SCALE_SCALAR_3V(VP
, invd
);
139 attenuation
= 1.0F
/ (light
->ConstantAttenuation
+ d
*
140 (light
->LinearAttenuation
+ d
*
141 light
->QuadraticAttenuation
));
143 if (light
->_Flags
& LIGHT_SPOT
) {
144 GLfloat PV_dot_dir
= - DOT3(VP
, light
->_NormDirection
);
146 if (PV_dot_dir
<light
->_CosCutoff
) {
150 double x
= PV_dot_dir
* (EXP_TABLE_SIZE
-1);
152 GLfloat spot
= (GLfloat
) (light
->_SpotExpTable
[k
][0]
153 + (x
-k
)*light
->_SpotExpTable
[k
][1]);
159 if (attenuation
< 1e-3)
162 n_dot_VP
= DOT3( normal
, VP
);
164 if (n_dot_VP
< 0.0F
) {
165 ACC_SCALE_SCALAR_3V(color
, attenuation
, light
->_MatAmbient
[0]);
169 COPY_3V(contrib
, light
->_MatAmbient
[0]);
170 ACC_SCALE_SCALAR_3V(contrib
, n_dot_VP
, light
->_MatDiffuse
[0]);
171 diffuse
+= n_dot_VP
* light
->_dli
* attenuation
;
174 if (ctx
->Light
.Model
.LocalViewer
) {
182 else if (light
->_Flags
& LIGHT_POSITIONAL
) {
184 ACC_3V(h
, ctx
->_EyeZDir
);
188 h
= light
->_h_inf_norm
;
192 n_dot_h
= DOT3(normal
, h
);
194 if (n_dot_h
> 0.0F
) {
195 struct gl_material
*mat
= &ctx
->Light
.Material
[0];
197 GLfloat shininess
= mat
->Shininess
;
201 n_dot_h
/= LEN_SQUARED_3FV( h
);
205 GET_SHINE_TAB_ENTRY( ctx
->_ShineTable
[0], n_dot_h
, spec_coef
);
207 if (spec_coef
> 1.0e-10) {
208 ACC_SCALE_SCALAR_3V( contrib
, spec_coef
,
209 light
->_MatSpecular
[0]);
210 specular
+= spec_coef
* light
->_sli
* attenuation
;
215 ACC_SCALE_SCALAR_3V( color
, attenuation
, contrib
);
218 if (ctx
->Visual
.RGBAflag
) {
219 Rcolor
[0] = CLAMP(color
[0], 0.0F
, 1.0F
);
220 Rcolor
[1] = CLAMP(color
[1], 0.0F
, 1.0F
);
221 Rcolor
[2] = CLAMP(color
[2], 0.0F
, 1.0F
);
222 Rcolor
[3] = CLAMP(color
[3], 0.0F
, 1.0F
);
225 struct gl_material
*mat
= &ctx
->Light
.Material
[0];
226 GLfloat d_a
= mat
->DiffuseIndex
- mat
->AmbientIndex
;
227 GLfloat s_a
= mat
->SpecularIndex
- mat
->AmbientIndex
;
228 GLfloat ind
= mat
->AmbientIndex
229 + diffuse
* (1.0F
-specular
) * d_a
231 if (ind
> mat
->SpecularIndex
) {
232 ind
= mat
->SpecularIndex
;
234 *index
= (GLuint
) (GLint
) ind
;
240 * Caller: context->API.RasterPos4f
243 raster_pos4f(GLcontext
*ctx
, GLfloat x
, GLfloat y
, GLfloat z
, GLfloat w
)
245 GLfloat v
[4], eye
[4], clip
[4], ndc
[3], d
;
246 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
247 FLUSH_CURRENT(ctx
, 0);
250 gl_update_state( ctx
);
252 ASSIGN_4V( v
, x
, y
, z
, w
);
253 TRANSFORM_POINT( eye
, ctx
->ModelView
.m
, v
);
256 if (ctx
->Light
.Enabled
) {
257 GLfloat
*norm
, eyenorm
[3];
258 GLfloat
*objnorm
= ctx
->Current
.Normal
;
260 if (ctx
->_NeedEyeCoords
) {
261 GLfloat
*inv
= ctx
->ModelView
.inv
;
262 TRANSFORM_NORMAL( eyenorm
, objnorm
, inv
);
269 shade_rastpos( ctx
, v
, norm
,
270 ctx
->Current
.RasterColor
,
271 &ctx
->Current
.RasterIndex
);
275 /* use current color or index */
276 if (ctx
->Visual
.RGBAflag
) {
277 ctx
->Current
.RasterColor
[0] = CHAN_TO_FLOAT(ctx
->Current
.Color
[0]);
278 ctx
->Current
.RasterColor
[1] = CHAN_TO_FLOAT(ctx
->Current
.Color
[1]);
279 ctx
->Current
.RasterColor
[2] = CHAN_TO_FLOAT(ctx
->Current
.Color
[2]);
280 ctx
->Current
.RasterColor
[3] = CHAN_TO_FLOAT(ctx
->Current
.Color
[3]);
283 ctx
->Current
.RasterIndex
= ctx
->Current
.Index
;
287 /* compute raster distance */
288 ctx
->Current
.RasterDistance
= (GLfloat
)
289 GL_SQRT( eye
[0]*eye
[0] + eye
[1]*eye
[1] + eye
[2]*eye
[2] );
291 /* apply projection matrix: clip = Proj * eye */
292 TRANSFORM_POINT( clip
, ctx
->ProjectionMatrix
.m
, eye
);
294 /* clip to view volume */
295 if (viewclip_point( clip
)==0) {
296 ctx
->Current
.RasterPosValid
= GL_FALSE
;
300 /* clip to user clipping planes */
301 if (ctx
->Transform
._AnyClip
&&
302 userclip_point(ctx
, clip
) == 0) {
303 ctx
->Current
.RasterPosValid
= GL_FALSE
;
308 ASSERT( clip
[3]!=0.0 );
310 ndc
[0] = clip
[0] * d
;
311 ndc
[1] = clip
[1] * d
;
312 ndc
[2] = clip
[2] * d
;
314 ctx
->Current
.RasterPos
[0] = (ndc
[0] * ctx
->Viewport
._WindowMap
.m
[MAT_SX
] +
315 ctx
->Viewport
._WindowMap
.m
[MAT_TX
]);
316 ctx
->Current
.RasterPos
[1] = (ndc
[1] * ctx
->Viewport
._WindowMap
.m
[MAT_SY
] +
317 ctx
->Viewport
._WindowMap
.m
[MAT_TY
]);
318 ctx
->Current
.RasterPos
[2] = (ndc
[2] * ctx
->Viewport
._WindowMap
.m
[MAT_SZ
] +
319 ctx
->Viewport
._WindowMap
.m
[MAT_TZ
]) / ctx
->Visual
.DepthMaxF
;
320 ctx
->Current
.RasterPos
[3] = clip
[3];
321 ctx
->Current
.RasterPosValid
= GL_TRUE
;
327 for (texSet
= 0; texSet
< ctx
->Const
.MaxTextureUnits
; texSet
++) {
328 COPY_4FV( ctx
->Current
.RasterMultiTexCoord
[texSet
],
329 ctx
->Current
.Texcoord
[texSet
] );
333 if (ctx
->RenderMode
==GL_SELECT
) {
334 gl_update_hitflag( ctx
, ctx
->Current
.RasterPos
[2] );
342 _mesa_RasterPos2d(GLdouble x
, GLdouble y
)
344 _mesa_RasterPos4f(x
, y
, 0.0F
, 1.0F
);
348 _mesa_RasterPos2f(GLfloat x
, GLfloat y
)
350 _mesa_RasterPos4f(x
, y
, 0.0F
, 1.0F
);
354 _mesa_RasterPos2i(GLint x
, GLint y
)
356 _mesa_RasterPos4f(x
, y
, 0.0F
, 1.0F
);
360 _mesa_RasterPos2s(GLshort x
, GLshort y
)
362 _mesa_RasterPos4f(x
, y
, 0.0F
, 1.0F
);
366 _mesa_RasterPos3d(GLdouble x
, GLdouble y
, GLdouble z
)
368 _mesa_RasterPos4f(x
, y
, z
, 1.0F
);
372 _mesa_RasterPos3f(GLfloat x
, GLfloat y
, GLfloat z
)
374 _mesa_RasterPos4f(x
, y
, z
, 1.0F
);
378 _mesa_RasterPos3i(GLint x
, GLint y
, GLint z
)
380 _mesa_RasterPos4f(x
, y
, z
, 1.0F
);
384 _mesa_RasterPos3s(GLshort x
, GLshort y
, GLshort z
)
386 _mesa_RasterPos4f(x
, y
, z
, 1.0F
);
390 _mesa_RasterPos4d(GLdouble x
, GLdouble y
, GLdouble z
, GLdouble w
)
392 _mesa_RasterPos4f(x
, y
, z
, w
);
396 _mesa_RasterPos4f(GLfloat x
, GLfloat y
, GLfloat z
, GLfloat w
)
398 GET_CURRENT_CONTEXT(ctx
);
399 raster_pos4f(ctx
, x
, y
, z
, w
);
403 _mesa_RasterPos4i(GLint x
, GLint y
, GLint z
, GLint w
)
405 _mesa_RasterPos4f(x
, y
, z
, w
);
409 _mesa_RasterPos4s(GLshort x
, GLshort y
, GLshort z
, GLshort w
)
411 _mesa_RasterPos4f(x
, y
, z
, w
);
415 _mesa_RasterPos2dv(const GLdouble
*v
)
417 _mesa_RasterPos4f(v
[0], v
[1], 0.0F
, 1.0F
);
421 _mesa_RasterPos2fv(const GLfloat
*v
)
423 _mesa_RasterPos4f(v
[0], v
[1], 0.0F
, 1.0F
);
427 _mesa_RasterPos2iv(const GLint
*v
)
429 _mesa_RasterPos4f(v
[0], v
[1], 0.0F
, 1.0F
);
433 _mesa_RasterPos2sv(const GLshort
*v
)
435 _mesa_RasterPos4f(v
[0], v
[1], 0.0F
, 1.0F
);
439 _mesa_RasterPos3dv(const GLdouble
*v
)
441 _mesa_RasterPos4f(v
[0], v
[1], v
[2], 1.0F
);
445 _mesa_RasterPos3fv(const GLfloat
*v
)
447 _mesa_RasterPos4f(v
[0], v
[1], v
[2], 1.0F
);
451 _mesa_RasterPos3iv(const GLint
*v
)
453 _mesa_RasterPos4f(v
[0], v
[1], v
[2], 1.0F
);
457 _mesa_RasterPos3sv(const GLshort
*v
)
459 _mesa_RasterPos4f(v
[0], v
[1], v
[2], 1.0F
);
463 _mesa_RasterPos4dv(const GLdouble
*v
)
465 _mesa_RasterPos4f(v
[0], v
[1], v
[2], v
[3]);
469 _mesa_RasterPos4fv(const GLfloat
*v
)
471 _mesa_RasterPos4f(v
[0], v
[1], v
[2], v
[3]);
475 _mesa_RasterPos4iv(const GLint
*v
)
477 _mesa_RasterPos4f(v
[0], v
[1], v
[2], v
[3]);
481 _mesa_RasterPos4sv(const GLshort
*v
)
483 _mesa_RasterPos4f(v
[0], v
[1], v
[2], v
[3]);