mesa: Use bitmask/ffs to iterate enabled lights
[mesa.git] / src / mesa / main / rastpos.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25
26 /**
27 * \file rastpos.c
28 * Raster position operations.
29 */
30
31 #include "glheader.h"
32 #include "context.h"
33 #include "feedback.h"
34 #include "macros.h"
35 #include "mtypes.h"
36 #include "rastpos.h"
37 #include "state.h"
38 #include "main/dispatch.h"
39 #include "main/viewport.h"
40 #include "util/bitscan.h"
41
42
43
44 /**
45 * Clip a point against the view volume.
46 *
47 * \param v vertex vector describing the point to clip.
48 *
49 * \return zero if outside view volume, or one if inside.
50 */
51 static GLuint
52 viewclip_point_xy( const GLfloat v[] )
53 {
54 if ( v[0] > v[3] || v[0] < -v[3]
55 || v[1] > v[3] || v[1] < -v[3] ) {
56 return 0;
57 }
58 else {
59 return 1;
60 }
61 }
62
63
64 /**
65 * Clip a point against the far/near Z clipping planes.
66 *
67 * \param v vertex vector describing the point to clip.
68 *
69 * \return zero if outside view volume, or one if inside.
70 */
71 static GLuint
72 viewclip_point_z( const GLfloat v[] )
73 {
74 if (v[2] > v[3] || v[2] < -v[3] ) {
75 return 0;
76 }
77 else {
78 return 1;
79 }
80 }
81
82
83 /**
84 * Clip a point against the user clipping planes.
85 *
86 * \param ctx GL context.
87 * \param v vertex vector describing the point to clip.
88 *
89 * \return zero if the point was clipped, or one otherwise.
90 */
91 static GLuint
92 userclip_point( struct gl_context *ctx, const GLfloat v[] )
93 {
94 GLuint p;
95
96 for (p = 0; p < ctx->Const.MaxClipPlanes; p++) {
97 if (ctx->Transform.ClipPlanesEnabled & (1 << p)) {
98 GLfloat dot = v[0] * ctx->Transform._ClipUserPlane[p][0]
99 + v[1] * ctx->Transform._ClipUserPlane[p][1]
100 + v[2] * ctx->Transform._ClipUserPlane[p][2]
101 + v[3] * ctx->Transform._ClipUserPlane[p][3];
102 if (dot < 0.0F) {
103 return 0;
104 }
105 }
106 }
107
108 return 1;
109 }
110
111
112 /**
113 * Compute lighting for the raster position. RGB modes computed.
114 * \param ctx the context
115 * \param vertex vertex location
116 * \param normal normal vector
117 * \param Rcolor returned color
118 * \param Rspec returned specular color (if separate specular enabled)
119 */
120 static void
121 shade_rastpos(struct gl_context *ctx,
122 const GLfloat vertex[4],
123 const GLfloat normal[3],
124 GLfloat Rcolor[4],
125 GLfloat Rspec[4])
126 {
127 /*const*/ GLfloat (*base)[3] = ctx->Light._BaseColor;
128 GLbitfield mask;
129 GLfloat diffuseColor[4], specularColor[4]; /* for RGB mode only */
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 mask = ctx->Light._EnabledLights;
137 while (mask) {
138 const int i = u_bit_scan(&mask);
139 struct gl_light *light = &ctx->Light.Light[i];
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-6F) {
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->_NormSpotDirection);
171
172 if (PV_dot_dir<light->_CosCutoff) {
173 continue;
174 }
175 else {
176 GLfloat spot = powf(PV_dot_dir, light->SpotExponent);
177 attenuation *= spot;
178 }
179 }
180 }
181
182 if (attenuation < 1e-3F)
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
196 /* Specular */
197 {
198 const GLfloat *h;
199 GLfloat n_dot_h;
200
201 ASSIGN_3V(specularContrib, 0.0, 0.0, 0.0);
202
203 if (ctx->Light.Model.LocalViewer) {
204 GLfloat v[3];
205 COPY_3V(v, vertex);
206 NORMALIZE_3FV(v);
207 SUB_3V(VP, VP, v);
208 NORMALIZE_3FV(VP);
209 h = VP;
210 }
211 else if (light->_Flags & LIGHT_POSITIONAL) {
212 ACC_3V(VP, ctx->_EyeZDir);
213 NORMALIZE_3FV(VP);
214 h = VP;
215 }
216 else {
217 h = light->_h_inf_norm;
218 }
219
220 n_dot_h = DOT3(normal, h);
221
222 if (n_dot_h > 0.0F) {
223 GLfloat shine;
224 GLfloat spec_coef;
225
226 shine = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_SHININESS][0];
227 spec_coef = powf(n_dot_h, shine);
228
229 if (spec_coef > 1.0e-10F) {
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 }
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(struct gl_context *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 * (1.0f / 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 *
371 * \param vObj vertex position in object space
372 */
373 void
374 _mesa_RasterPos(struct gl_context *ctx, const GLfloat vObj[4])
375 {
376 if (ctx->VertexProgram._Enabled) {
377 /* XXX implement this */
378 _mesa_problem(ctx, "Vertex programs not implemented for glRasterPos");
379 return;
380 }
381 else {
382 GLfloat eye[4], clip[4], ndc[3], d;
383 GLfloat *norm, eyenorm[3];
384 GLfloat *objnorm = ctx->Current.Attrib[VERT_ATTRIB_NORMAL];
385 float scale[3], translate[3];
386
387 /* apply modelview matrix: eye = MV * obj */
388 TRANSFORM_POINT( eye, ctx->ModelviewMatrixStack.Top->m, vObj );
389 /* apply projection matrix: clip = Proj * eye */
390 TRANSFORM_POINT( clip, ctx->ProjectionMatrixStack.Top->m, eye );
391
392 /* clip to view volume. */
393 if (!ctx->Transform.DepthClamp) {
394 if (viewclip_point_z(clip) == 0) {
395 ctx->Current.RasterPosValid = GL_FALSE;
396 return;
397 }
398 }
399 if (!ctx->Transform.RasterPositionUnclipped) {
400 if (viewclip_point_xy(clip) == 0) {
401 ctx->Current.RasterPosValid = GL_FALSE;
402 return;
403 }
404 }
405
406 /* clip to user clipping planes */
407 if (ctx->Transform.ClipPlanesEnabled && !userclip_point(ctx, clip)) {
408 ctx->Current.RasterPosValid = GL_FALSE;
409 return;
410 }
411
412 /* ndc = clip / W */
413 d = (clip[3] == 0.0F) ? 1.0F : 1.0F / clip[3];
414 ndc[0] = clip[0] * d;
415 ndc[1] = clip[1] * d;
416 ndc[2] = clip[2] * d;
417 /* wincoord = viewport_mapping(ndc) */
418 _mesa_get_viewport_xform(ctx, 0, scale, translate);
419 ctx->Current.RasterPos[0] = ndc[0] * scale[0] + translate[0];
420 ctx->Current.RasterPos[1] = ndc[1] * scale[1] + translate[1];
421 ctx->Current.RasterPos[2] = ndc[2] * scale[2] + translate[2];
422 ctx->Current.RasterPos[3] = clip[3];
423
424 if (ctx->Transform.DepthClamp) {
425 ctx->Current.RasterPos[3] = CLAMP(ctx->Current.RasterPos[3],
426 ctx->ViewportArray[0].Near,
427 ctx->ViewportArray[0].Far);
428 }
429
430 /* compute raster distance */
431 if (ctx->Fog.FogCoordinateSource == GL_FOG_COORDINATE_EXT)
432 ctx->Current.RasterDistance = ctx->Current.Attrib[VERT_ATTRIB_FOG][0];
433 else
434 ctx->Current.RasterDistance =
435 sqrtf( eye[0]*eye[0] + eye[1]*eye[1] + eye[2]*eye[2] );
436
437 /* compute transformed normal vector (for lighting or texgen) */
438 if (ctx->_NeedEyeCoords) {
439 const GLfloat *inv = ctx->ModelviewMatrixStack.Top->inv;
440 TRANSFORM_NORMAL( eyenorm, objnorm, inv );
441 norm = eyenorm;
442 }
443 else {
444 norm = objnorm;
445 }
446
447 /* update raster color */
448 if (ctx->Light.Enabled) {
449 /* lighting */
450 shade_rastpos( ctx, vObj, norm,
451 ctx->Current.RasterColor,
452 ctx->Current.RasterSecondaryColor );
453 }
454 else {
455 /* use current color */
456 COPY_4FV(ctx->Current.RasterColor,
457 ctx->Current.Attrib[VERT_ATTRIB_COLOR0]);
458 COPY_4FV(ctx->Current.RasterSecondaryColor,
459 ctx->Current.Attrib[VERT_ATTRIB_COLOR1]);
460 }
461
462 /* texture coords */
463 {
464 GLuint u;
465 for (u = 0; u < ctx->Const.MaxTextureCoordUnits; u++) {
466 GLfloat tc[4];
467 COPY_4V(tc, ctx->Current.Attrib[VERT_ATTRIB_TEX0 + u]);
468 if (ctx->Texture.Unit[u].TexGenEnabled) {
469 compute_texgen(ctx, vObj, eye, norm, u, tc);
470 }
471 TRANSFORM_POINT(ctx->Current.RasterTexCoords[u],
472 ctx->TextureMatrixStack[u].Top->m, tc);
473 }
474 }
475
476 ctx->Current.RasterPosValid = GL_TRUE;
477 }
478
479 if (ctx->RenderMode == GL_SELECT) {
480 _mesa_update_hitflag( ctx, ctx->Current.RasterPos[2] );
481 }
482 }
483
484
485 /**
486 * Helper function for all the RasterPos functions.
487 */
488 static void
489 rasterpos(GLfloat x, GLfloat y, GLfloat z, GLfloat w)
490 {
491 GET_CURRENT_CONTEXT(ctx);
492 GLfloat p[4];
493
494 p[0] = x;
495 p[1] = y;
496 p[2] = z;
497 p[3] = w;
498
499 FLUSH_VERTICES(ctx, 0);
500 FLUSH_CURRENT(ctx, 0);
501
502 if (ctx->NewState)
503 _mesa_update_state( ctx );
504
505 ctx->Driver.RasterPos(ctx, p);
506 }
507
508
509 void GLAPIENTRY
510 _mesa_RasterPos2d(GLdouble x, GLdouble y)
511 {
512 rasterpos((GLfloat)x, (GLfloat)y, (GLfloat)0.0, (GLfloat)1.0);
513 }
514
515 void GLAPIENTRY
516 _mesa_RasterPos2f(GLfloat x, GLfloat y)
517 {
518 rasterpos(x, y, 0.0F, 1.0F);
519 }
520
521 void GLAPIENTRY
522 _mesa_RasterPos2i(GLint x, GLint y)
523 {
524 rasterpos((GLfloat) x, (GLfloat) y, 0.0F, 1.0F);
525 }
526
527 void GLAPIENTRY
528 _mesa_RasterPos2s(GLshort x, GLshort y)
529 {
530 rasterpos(x, y, 0.0F, 1.0F);
531 }
532
533 void GLAPIENTRY
534 _mesa_RasterPos3d(GLdouble x, GLdouble y, GLdouble z)
535 {
536 rasterpos((GLfloat) x, (GLfloat) y, (GLfloat) z, 1.0F);
537 }
538
539 void GLAPIENTRY
540 _mesa_RasterPos3f(GLfloat x, GLfloat y, GLfloat z)
541 {
542 rasterpos(x, y, z, 1.0F);
543 }
544
545 void GLAPIENTRY
546 _mesa_RasterPos3i(GLint x, GLint y, GLint z)
547 {
548 rasterpos((GLfloat) x, (GLfloat) y, (GLfloat) z, 1.0F);
549 }
550
551 void GLAPIENTRY
552 _mesa_RasterPos3s(GLshort x, GLshort y, GLshort z)
553 {
554 rasterpos(x, y, z, 1.0F);
555 }
556
557 void GLAPIENTRY
558 _mesa_RasterPos4d(GLdouble x, GLdouble y, GLdouble z, GLdouble w)
559 {
560 rasterpos((GLfloat) x, (GLfloat) y, (GLfloat) z, (GLfloat) w);
561 }
562
563 void GLAPIENTRY
564 _mesa_RasterPos4f(GLfloat x, GLfloat y, GLfloat z, GLfloat w)
565 {
566 rasterpos(x, y, z, w);
567 }
568
569 void GLAPIENTRY
570 _mesa_RasterPos4i(GLint x, GLint y, GLint z, GLint w)
571 {
572 rasterpos((GLfloat) x, (GLfloat) y, (GLfloat) z, (GLfloat) w);
573 }
574
575 void GLAPIENTRY
576 _mesa_RasterPos4s(GLshort x, GLshort y, GLshort z, GLshort w)
577 {
578 rasterpos(x, y, z, w);
579 }
580
581 void GLAPIENTRY
582 _mesa_RasterPos2dv(const GLdouble *v)
583 {
584 rasterpos((GLfloat) v[0], (GLfloat) v[1], 0.0F, 1.0F);
585 }
586
587 void GLAPIENTRY
588 _mesa_RasterPos2fv(const GLfloat *v)
589 {
590 rasterpos(v[0], v[1], 0.0F, 1.0F);
591 }
592
593 void GLAPIENTRY
594 _mesa_RasterPos2iv(const GLint *v)
595 {
596 rasterpos((GLfloat) v[0], (GLfloat) v[1], 0.0F, 1.0F);
597 }
598
599 void GLAPIENTRY
600 _mesa_RasterPos2sv(const GLshort *v)
601 {
602 rasterpos(v[0], v[1], 0.0F, 1.0F);
603 }
604
605 void GLAPIENTRY
606 _mesa_RasterPos3dv(const GLdouble *v)
607 {
608 rasterpos((GLfloat) v[0], (GLfloat) v[1], (GLfloat) v[2], 1.0F);
609 }
610
611 void GLAPIENTRY
612 _mesa_RasterPos3fv(const GLfloat *v)
613 {
614 rasterpos(v[0], v[1], v[2], 1.0F);
615 }
616
617 void GLAPIENTRY
618 _mesa_RasterPos3iv(const GLint *v)
619 {
620 rasterpos((GLfloat) v[0], (GLfloat) v[1], (GLfloat) v[2], 1.0F);
621 }
622
623 void GLAPIENTRY
624 _mesa_RasterPos3sv(const GLshort *v)
625 {
626 rasterpos(v[0], v[1], v[2], 1.0F);
627 }
628
629 void GLAPIENTRY
630 _mesa_RasterPos4dv(const GLdouble *v)
631 {
632 rasterpos((GLfloat) v[0], (GLfloat) v[1],
633 (GLfloat) v[2], (GLfloat) v[3]);
634 }
635
636 void GLAPIENTRY
637 _mesa_RasterPos4fv(const GLfloat *v)
638 {
639 rasterpos(v[0], v[1], v[2], v[3]);
640 }
641
642 void GLAPIENTRY
643 _mesa_RasterPos4iv(const GLint *v)
644 {
645 rasterpos((GLfloat) v[0], (GLfloat) v[1],
646 (GLfloat) v[2], (GLfloat) v[3]);
647 }
648
649 void GLAPIENTRY
650 _mesa_RasterPos4sv(const GLshort *v)
651 {
652 rasterpos(v[0], v[1], v[2], v[3]);
653 }
654
655
656 /**********************************************************************/
657 /*** GL_ARB_window_pos / GL_MESA_window_pos ***/
658 /**********************************************************************/
659
660
661 /**
662 * All glWindowPosMESA and glWindowPosARB commands call this function to
663 * update the current raster position.
664 */
665 static void
666 window_pos3f(GLfloat x, GLfloat y, GLfloat z)
667 {
668 GET_CURRENT_CONTEXT(ctx);
669 GLfloat z2;
670
671 FLUSH_VERTICES(ctx, 0);
672 FLUSH_CURRENT(ctx, 0);
673
674 z2 = CLAMP(z, 0.0F, 1.0F)
675 * (ctx->ViewportArray[0].Far - ctx->ViewportArray[0].Near)
676 + ctx->ViewportArray[0].Near;
677
678 /* set raster position */
679 ctx->Current.RasterPos[0] = x;
680 ctx->Current.RasterPos[1] = y;
681 ctx->Current.RasterPos[2] = z2;
682 ctx->Current.RasterPos[3] = 1.0F;
683
684 ctx->Current.RasterPosValid = GL_TRUE;
685
686 if (ctx->Fog.FogCoordinateSource == GL_FOG_COORDINATE_EXT)
687 ctx->Current.RasterDistance = ctx->Current.Attrib[VERT_ATTRIB_FOG][0];
688 else
689 ctx->Current.RasterDistance = 0.0;
690
691 /* raster color = current color or index */
692 ctx->Current.RasterColor[0]
693 = CLAMP(ctx->Current.Attrib[VERT_ATTRIB_COLOR0][0], 0.0F, 1.0F);
694 ctx->Current.RasterColor[1]
695 = CLAMP(ctx->Current.Attrib[VERT_ATTRIB_COLOR0][1], 0.0F, 1.0F);
696 ctx->Current.RasterColor[2]
697 = CLAMP(ctx->Current.Attrib[VERT_ATTRIB_COLOR0][2], 0.0F, 1.0F);
698 ctx->Current.RasterColor[3]
699 = CLAMP(ctx->Current.Attrib[VERT_ATTRIB_COLOR0][3], 0.0F, 1.0F);
700 ctx->Current.RasterSecondaryColor[0]
701 = CLAMP(ctx->Current.Attrib[VERT_ATTRIB_COLOR1][0], 0.0F, 1.0F);
702 ctx->Current.RasterSecondaryColor[1]
703 = CLAMP(ctx->Current.Attrib[VERT_ATTRIB_COLOR1][1], 0.0F, 1.0F);
704 ctx->Current.RasterSecondaryColor[2]
705 = CLAMP(ctx->Current.Attrib[VERT_ATTRIB_COLOR1][2], 0.0F, 1.0F);
706 ctx->Current.RasterSecondaryColor[3]
707 = CLAMP(ctx->Current.Attrib[VERT_ATTRIB_COLOR1][3], 0.0F, 1.0F);
708
709 /* raster texcoord = current texcoord */
710 {
711 GLuint texSet;
712 for (texSet = 0; texSet < ctx->Const.MaxTextureCoordUnits; texSet++) {
713 assert(texSet < ARRAY_SIZE(ctx->Current.RasterTexCoords));
714 COPY_4FV( ctx->Current.RasterTexCoords[texSet],
715 ctx->Current.Attrib[VERT_ATTRIB_TEX0 + texSet] );
716 }
717 }
718
719 if (ctx->RenderMode==GL_SELECT) {
720 _mesa_update_hitflag( ctx, ctx->Current.RasterPos[2] );
721 }
722 }
723
724
725 /* This is just to support the GL_MESA_window_pos version */
726 static void
727 window_pos4f(GLfloat x, GLfloat y, GLfloat z, GLfloat w)
728 {
729 GET_CURRENT_CONTEXT(ctx);
730 window_pos3f(x, y, z);
731 ctx->Current.RasterPos[3] = w;
732 }
733
734
735 void GLAPIENTRY
736 _mesa_WindowPos2d(GLdouble x, GLdouble y)
737 {
738 window_pos4f((GLfloat) x, (GLfloat) y, 0.0F, 1.0F);
739 }
740
741 void GLAPIENTRY
742 _mesa_WindowPos2f(GLfloat x, GLfloat y)
743 {
744 window_pos4f(x, y, 0.0F, 1.0F);
745 }
746
747 void GLAPIENTRY
748 _mesa_WindowPos2i(GLint x, GLint y)
749 {
750 window_pos4f((GLfloat) x, (GLfloat) y, 0.0F, 1.0F);
751 }
752
753 void GLAPIENTRY
754 _mesa_WindowPos2s(GLshort x, GLshort y)
755 {
756 window_pos4f(x, y, 0.0F, 1.0F);
757 }
758
759 void GLAPIENTRY
760 _mesa_WindowPos3d(GLdouble x, GLdouble y, GLdouble z)
761 {
762 window_pos4f((GLfloat) x, (GLfloat) y, (GLfloat) z, 1.0F);
763 }
764
765 void GLAPIENTRY
766 _mesa_WindowPos3f(GLfloat x, GLfloat y, GLfloat z)
767 {
768 window_pos4f(x, y, z, 1.0F);
769 }
770
771 void GLAPIENTRY
772 _mesa_WindowPos3i(GLint x, GLint y, GLint z)
773 {
774 window_pos4f((GLfloat) x, (GLfloat) y, (GLfloat) z, 1.0F);
775 }
776
777 void GLAPIENTRY
778 _mesa_WindowPos3s(GLshort x, GLshort y, GLshort z)
779 {
780 window_pos4f(x, y, z, 1.0F);
781 }
782
783 void GLAPIENTRY
784 _mesa_WindowPos4dMESA(GLdouble x, GLdouble y, GLdouble z, GLdouble w)
785 {
786 window_pos4f((GLfloat) x, (GLfloat) y, (GLfloat) z, (GLfloat) w);
787 }
788
789 void GLAPIENTRY
790 _mesa_WindowPos4fMESA(GLfloat x, GLfloat y, GLfloat z, GLfloat w)
791 {
792 window_pos4f(x, y, z, w);
793 }
794
795 void GLAPIENTRY
796 _mesa_WindowPos4iMESA(GLint x, GLint y, GLint z, GLint w)
797 {
798 window_pos4f((GLfloat) x, (GLfloat) y, (GLfloat) z, (GLfloat) w);
799 }
800
801 void GLAPIENTRY
802 _mesa_WindowPos4sMESA(GLshort x, GLshort y, GLshort z, GLshort w)
803 {
804 window_pos4f(x, y, z, w);
805 }
806
807 void GLAPIENTRY
808 _mesa_WindowPos2dv(const GLdouble *v)
809 {
810 window_pos4f((GLfloat) v[0], (GLfloat) v[1], 0.0F, 1.0F);
811 }
812
813 void GLAPIENTRY
814 _mesa_WindowPos2fv(const GLfloat *v)
815 {
816 window_pos4f(v[0], v[1], 0.0F, 1.0F);
817 }
818
819 void GLAPIENTRY
820 _mesa_WindowPos2iv(const GLint *v)
821 {
822 window_pos4f((GLfloat) v[0], (GLfloat) v[1], 0.0F, 1.0F);
823 }
824
825 void GLAPIENTRY
826 _mesa_WindowPos2sv(const GLshort *v)
827 {
828 window_pos4f(v[0], v[1], 0.0F, 1.0F);
829 }
830
831 void GLAPIENTRY
832 _mesa_WindowPos3dv(const GLdouble *v)
833 {
834 window_pos4f((GLfloat) v[0], (GLfloat) v[1], (GLfloat) v[2], 1.0F);
835 }
836
837 void GLAPIENTRY
838 _mesa_WindowPos3fv(const GLfloat *v)
839 {
840 window_pos4f(v[0], v[1], v[2], 1.0);
841 }
842
843 void GLAPIENTRY
844 _mesa_WindowPos3iv(const GLint *v)
845 {
846 window_pos4f((GLfloat) v[0], (GLfloat) v[1], (GLfloat) v[2], 1.0F);
847 }
848
849 void GLAPIENTRY
850 _mesa_WindowPos3sv(const GLshort *v)
851 {
852 window_pos4f(v[0], v[1], v[2], 1.0F);
853 }
854
855 void GLAPIENTRY
856 _mesa_WindowPos4dvMESA(const GLdouble *v)
857 {
858 window_pos4f((GLfloat) v[0], (GLfloat) v[1],
859 (GLfloat) v[2], (GLfloat) v[3]);
860 }
861
862 void GLAPIENTRY
863 _mesa_WindowPos4fvMESA(const GLfloat *v)
864 {
865 window_pos4f(v[0], v[1], v[2], v[3]);
866 }
867
868 void GLAPIENTRY
869 _mesa_WindowPos4ivMESA(const GLint *v)
870 {
871 window_pos4f((GLfloat) v[0], (GLfloat) v[1],
872 (GLfloat) v[2], (GLfloat) v[3]);
873 }
874
875 void GLAPIENTRY
876 _mesa_WindowPos4svMESA(const GLshort *v)
877 {
878 window_pos4f(v[0], v[1], v[2], v[3]);
879 }
880
881
882 #if 0
883
884 /*
885 * OpenGL implementation of glWindowPos*MESA()
886 */
887 void glWindowPos4fMESA( GLfloat x, GLfloat y, GLfloat z, GLfloat w )
888 {
889 GLfloat fx, fy;
890
891 /* Push current matrix mode and viewport attributes */
892 glPushAttrib( GL_TRANSFORM_BIT | GL_VIEWPORT_BIT );
893
894 /* Setup projection parameters */
895 glMatrixMode( GL_PROJECTION );
896 glPushMatrix();
897 glLoadIdentity();
898 glMatrixMode( GL_MODELVIEW );
899 glPushMatrix();
900 glLoadIdentity();
901
902 glDepthRange( z, z );
903 glViewport( (int) x - 1, (int) y - 1, 2, 2 );
904
905 /* set the raster (window) position */
906 fx = x - (int) x;
907 fy = y - (int) y;
908 glRasterPos4f( fx, fy, 0.0, w );
909
910 /* restore matrices, viewport and matrix mode */
911 glPopMatrix();
912 glMatrixMode( GL_PROJECTION );
913 glPopMatrix();
914
915 glPopAttrib();
916 }
917
918 #endif
919
920
921 /**********************************************************************/
922 /** \name Initialization */
923 /**********************************************************************/
924 /*@{*/
925
926 /**
927 * Initialize the context current raster position information.
928 *
929 * \param ctx GL context.
930 *
931 * Initialize the current raster position information in
932 * __struct gl_contextRec::Current, and adds the extension entry points to the
933 * dispatcher.
934 */
935 void _mesa_init_rastpos( struct gl_context * ctx )
936 {
937 unsigned i;
938
939 ASSIGN_4V( ctx->Current.RasterPos, 0.0, 0.0, 0.0, 1.0 );
940 ctx->Current.RasterDistance = 0.0;
941 ASSIGN_4V( ctx->Current.RasterColor, 1.0, 1.0, 1.0, 1.0 );
942 ASSIGN_4V( ctx->Current.RasterSecondaryColor, 0.0, 0.0, 0.0, 1.0 );
943 for (i = 0; i < ARRAY_SIZE(ctx->Current.RasterTexCoords); i++)
944 ASSIGN_4V( ctx->Current.RasterTexCoords[i], 0.0, 0.0, 0.0, 1.0 );
945 ctx->Current.RasterPosValid = GL_TRUE;
946 }
947
948 /*@}*/