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/light.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 */
51static GLuint
52viewclip_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 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 */
71static GLuint
72viewclip_point_near_z( const GLfloat v[] )
73{
74   if (v[2] < -v[3]) {
75      return 0;
76   }
77   else {
78      return 1;
79   }
80}
81
82
83/**
84 * Clip a point against the far Z clipping planes.
85 *
86 * \param v vertex vector describing the point to clip.
87 *
88 * \return zero if outside view volume, or one if inside.
89 */
90static GLuint
91viewclip_point_far_z( const GLfloat v[] )
92{
93   if (v[2] > v[3]) {
94      return 0;
95   }
96   else {
97      return 1;
98   }
99}
100
101
102/**
103 * Clip a point against the user clipping planes.
104 *
105 * \param ctx GL context.
106 * \param v vertex vector describing the point to clip.
107 *
108 * \return zero if the point was clipped, or one otherwise.
109 */
110static GLuint
111userclip_point( struct gl_context *ctx, const GLfloat v[] )
112{
113   GLbitfield mask = ctx->Transform.ClipPlanesEnabled;
114   while (mask) {
115      const int p = u_bit_scan(&mask);
116      GLfloat dot = v[0] * ctx->Transform._ClipUserPlane[p][0]
117         + v[1] * ctx->Transform._ClipUserPlane[p][1]
118         + v[2] * ctx->Transform._ClipUserPlane[p][2]
119         + v[3] * ctx->Transform._ClipUserPlane[p][3];
120
121      if (dot < 0.0F) {
122         return 0;
123      }
124   }
125
126   return 1;
127}
128
129
130/**
131 * Compute lighting for the raster position.  RGB modes computed.
132 * \param ctx the context
133 * \param vertex vertex location
134 * \param normal normal vector
135 * \param Rcolor returned color
136 * \param Rspec returned specular color (if separate specular enabled)
137 */
138static void
139shade_rastpos(struct gl_context *ctx,
140              const GLfloat vertex[4],
141              const GLfloat normal[3],
142              GLfloat Rcolor[4],
143              GLfloat Rspec[4])
144{
145   /*const*/ GLfloat (*base)[3] = ctx->Light._BaseColor;
146   GLbitfield mask;
147   GLfloat diffuseColor[4], specularColor[4];  /* for RGB mode only */
148
149   _mesa_update_light_materials(ctx);
150
151   COPY_3V(diffuseColor, base[0]);
152   diffuseColor[3] = CLAMP(
153      ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE][3], 0.0F, 1.0F );
154   ASSIGN_4V(specularColor, 0.0, 0.0, 0.0, 1.0);
155
156   mask = ctx->Light._EnabledLights;
157   while (mask) {
158      const int i = u_bit_scan(&mask);
159      struct gl_light *light = &ctx->Light.Light[i];
160      struct gl_light_uniforms *lu = &ctx->Light.LightSource[i];
161      GLfloat attenuation = 1.0;
162      GLfloat VP[3]; /* vector from vertex to light pos */
163      GLfloat n_dot_VP;
164      GLfloat diffuseContrib[3], specularContrib[3];
165
166      if (!(light->_Flags & LIGHT_POSITIONAL)) {
167         /* light at infinity */
168	 COPY_3V(VP, light->_VP_inf_norm);
169	 attenuation = light->_VP_inf_spot_attenuation;
170      }
171      else {
172         /* local/positional light */
173	 GLfloat d;
174
175         /* VP = vector from vertex pos to light[i].pos */
176	 SUB_3V(VP, light->_Position, vertex);
177         /* d = length(VP) */
178	 d = (GLfloat) LEN_3FV( VP );
179	 if (d > 1.0e-6F) {
180            /* normalize VP */
181	    GLfloat invd = 1.0F / d;
182	    SELF_SCALE_SCALAR_3V(VP, invd);
183	 }
184
185         /* atti */
186	 attenuation = 1.0F / (lu->ConstantAttenuation + d *
187			       (lu->LinearAttenuation + d *
188				lu->QuadraticAttenuation));
189
190	 if (light->_Flags & LIGHT_SPOT) {
191	    GLfloat PV_dot_dir = - DOT3(VP, light->_NormSpotDirection);
192
193	    if (PV_dot_dir<lu->_CosCutoff) {
194	       continue;
195	    }
196	    else {
197               GLfloat spot = powf(PV_dot_dir, lu->SpotExponent);
198	       attenuation *= spot;
199	    }
200	 }
201      }
202
203      if (attenuation < 1e-3F)
204	 continue;
205
206      n_dot_VP = DOT3( normal, VP );
207
208      if (n_dot_VP < 0.0F) {
209	 ACC_SCALE_SCALAR_3V(diffuseColor, attenuation, light->_MatAmbient[0]);
210	 continue;
211      }
212
213      /* Ambient + diffuse */
214      COPY_3V(diffuseContrib, light->_MatAmbient[0]);
215      ACC_SCALE_SCALAR_3V(diffuseContrib, n_dot_VP, light->_MatDiffuse[0]);
216
217      /* Specular */
218      {
219         const GLfloat *h;
220         GLfloat n_dot_h;
221
222         ASSIGN_3V(specularContrib, 0.0, 0.0, 0.0);
223
224	 if (ctx->Light.Model.LocalViewer) {
225	    GLfloat v[3];
226	    COPY_3V(v, vertex);
227	    NORMALIZE_3FV(v);
228	    SUB_3V(VP, VP, v);
229            NORMALIZE_3FV(VP);
230	    h = VP;
231	 }
232	 else if (light->_Flags & LIGHT_POSITIONAL) {
233	    ACC_3V(VP, ctx->_EyeZDir);
234            NORMALIZE_3FV(VP);
235	    h = VP;
236	 }
237         else {
238	    h = light->_h_inf_norm;
239	 }
240
241	 n_dot_h = DOT3(normal, h);
242
243	 if (n_dot_h > 0.0F) {
244	    GLfloat shine;
245	    GLfloat spec_coef;
246
247	    shine = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_SHININESS][0];
248	    spec_coef = powf(n_dot_h, shine);
249
250	    if (spec_coef > 1.0e-10F) {
251               if (ctx->Light.Model.ColorControl==GL_SEPARATE_SPECULAR_COLOR) {
252                  ACC_SCALE_SCALAR_3V( specularContrib, spec_coef,
253                                       light->_MatSpecular[0]);
254               }
255               else {
256                  ACC_SCALE_SCALAR_3V( diffuseContrib, spec_coef,
257                                       light->_MatSpecular[0]);
258               }
259	    }
260	 }
261      }
262
263      ACC_SCALE_SCALAR_3V( diffuseColor, attenuation, diffuseContrib );
264      ACC_SCALE_SCALAR_3V( specularColor, attenuation, specularContrib );
265   }
266
267   Rcolor[0] = CLAMP(diffuseColor[0], 0.0F, 1.0F);
268   Rcolor[1] = CLAMP(diffuseColor[1], 0.0F, 1.0F);
269   Rcolor[2] = CLAMP(diffuseColor[2], 0.0F, 1.0F);
270   Rcolor[3] = CLAMP(diffuseColor[3], 0.0F, 1.0F);
271   Rspec[0] = CLAMP(specularColor[0], 0.0F, 1.0F);
272   Rspec[1] = CLAMP(specularColor[1], 0.0F, 1.0F);
273   Rspec[2] = CLAMP(specularColor[2], 0.0F, 1.0F);
274   Rspec[3] = CLAMP(specularColor[3], 0.0F, 1.0F);
275}
276
277
278/**
279 * Do texgen needed for glRasterPos.
280 * \param ctx  rendering context
281 * \param vObj  object-space vertex coordinate
282 * \param vEye  eye-space vertex coordinate
283 * \param normal  vertex normal
284 * \param unit  texture unit number
285 * \param texcoord  incoming texcoord and resulting texcoord
286 */
287static void
288compute_texgen(struct gl_context *ctx, const GLfloat vObj[4], const GLfloat vEye[4],
289               const GLfloat normal[3], GLuint unit, GLfloat texcoord[4])
290{
291   const struct gl_fixedfunc_texture_unit *texUnit =
292      &ctx->Texture.FixedFuncUnit[unit];
293
294   /* always compute sphere map terms, just in case */
295   GLfloat u[3], two_nu, rx, ry, rz, m, mInv;
296   COPY_3V(u, vEye);
297   NORMALIZE_3FV(u);
298   two_nu = 2.0F * DOT3(normal, u);
299   rx = u[0] - normal[0] * two_nu;
300   ry = u[1] - normal[1] * two_nu;
301   rz = u[2] - normal[2] * two_nu;
302   m = rx * rx + ry * ry + (rz + 1.0F) * (rz + 1.0F);
303   if (m > 0.0F)
304      mInv = 0.5F * (1.0f / sqrtf(m));
305   else
306      mInv = 0.0F;
307
308   if (texUnit->TexGenEnabled & S_BIT) {
309      switch (texUnit->GenS.Mode) {
310         case GL_OBJECT_LINEAR:
311            texcoord[0] = DOT4(vObj, texUnit->ObjectPlane[GEN_S]);
312            break;
313         case GL_EYE_LINEAR:
314            texcoord[0] = DOT4(vEye, texUnit->EyePlane[GEN_S]);
315            break;
316         case GL_SPHERE_MAP:
317            texcoord[0] = rx * mInv + 0.5F;
318            break;
319         case GL_REFLECTION_MAP:
320            texcoord[0] = rx;
321            break;
322         case GL_NORMAL_MAP:
323            texcoord[0] = normal[0];
324            break;
325         default:
326            _mesa_problem(ctx, "Bad S texgen in compute_texgen()");
327            return;
328      }
329   }
330
331   if (texUnit->TexGenEnabled & T_BIT) {
332      switch (texUnit->GenT.Mode) {
333         case GL_OBJECT_LINEAR:
334            texcoord[1] = DOT4(vObj, texUnit->ObjectPlane[GEN_T]);
335            break;
336         case GL_EYE_LINEAR:
337            texcoord[1] = DOT4(vEye, texUnit->EyePlane[GEN_T]);
338            break;
339         case GL_SPHERE_MAP:
340            texcoord[1] = ry * mInv + 0.5F;
341            break;
342         case GL_REFLECTION_MAP:
343            texcoord[1] = ry;
344            break;
345         case GL_NORMAL_MAP:
346            texcoord[1] = normal[1];
347            break;
348         default:
349            _mesa_problem(ctx, "Bad T texgen in compute_texgen()");
350            return;
351      }
352   }
353
354   if (texUnit->TexGenEnabled & R_BIT) {
355      switch (texUnit->GenR.Mode) {
356         case GL_OBJECT_LINEAR:
357            texcoord[2] = DOT4(vObj, texUnit->ObjectPlane[GEN_R]);
358            break;
359         case GL_EYE_LINEAR:
360            texcoord[2] = DOT4(vEye, texUnit->EyePlane[GEN_R]);
361            break;
362         case GL_REFLECTION_MAP:
363            texcoord[2] = rz;
364            break;
365         case GL_NORMAL_MAP:
366            texcoord[2] = normal[2];
367            break;
368         default:
369            _mesa_problem(ctx, "Bad R texgen in compute_texgen()");
370            return;
371      }
372   }
373
374   if (texUnit->TexGenEnabled & Q_BIT) {
375      switch (texUnit->GenQ.Mode) {
376         case GL_OBJECT_LINEAR:
377            texcoord[3] = DOT4(vObj, texUnit->ObjectPlane[GEN_Q]);
378            break;
379         case GL_EYE_LINEAR:
380            texcoord[3] = DOT4(vEye, texUnit->EyePlane[GEN_Q]);
381            break;
382         default:
383            _mesa_problem(ctx, "Bad Q texgen in compute_texgen()");
384            return;
385      }
386   }
387}
388
389
390/**
391 * glRasterPos transformation.  Typically called via ctx->Driver.RasterPos().
392 *
393 * \param vObj  vertex position in object space
394 */
395void
396_mesa_RasterPos(struct gl_context *ctx, const GLfloat vObj[4])
397{
398   ctx->PopAttribState |= GL_CURRENT_BIT;
399
400   if (_mesa_arb_vertex_program_enabled(ctx)) {
401      /* XXX implement this */
402      _mesa_problem(ctx, "Vertex programs not implemented for glRasterPos");
403      return;
404   }
405   else {
406      GLfloat eye[4], clip[4], ndc[3], d;
407      GLfloat *norm, eyenorm[3];
408      GLfloat *objnorm = ctx->Current.Attrib[VERT_ATTRIB_NORMAL];
409      float scale[3], translate[3];
410
411      /* apply modelview matrix:  eye = MV * obj */
412      TRANSFORM_POINT( eye, ctx->ModelviewMatrixStack.Top->m, vObj );
413      /* apply projection matrix:  clip = Proj * eye */
414      TRANSFORM_POINT( clip, ctx->ProjectionMatrixStack.Top->m, eye );
415
416      /* clip to view volume. */
417      if (!ctx->Transform.DepthClampNear) {
418         if (viewclip_point_near_z(clip) == 0) {
419            ctx->Current.RasterPosValid = GL_FALSE;
420            return;
421         }
422      }
423      if (!ctx->Transform.DepthClampFar) {
424         if (viewclip_point_far_z(clip) == 0) {
425            ctx->Current.RasterPosValid = GL_FALSE;
426            return;
427         }
428      }
429      if (!ctx->Transform.RasterPositionUnclipped) {
430         if (viewclip_point_xy(clip) == 0) {
431            ctx->Current.RasterPosValid = GL_FALSE;
432            return;
433         }
434      }
435
436      /* clip to user clipping planes */
437      if (ctx->Transform.ClipPlanesEnabled && !userclip_point(ctx, clip)) {
438         ctx->Current.RasterPosValid = GL_FALSE;
439         return;
440      }
441
442      /* ndc = clip / W */
443      d = (clip[3] == 0.0F) ? 1.0F : 1.0F / clip[3];
444      ndc[0] = clip[0] * d;
445      ndc[1] = clip[1] * d;
446      ndc[2] = clip[2] * d;
447      /* wincoord = viewport_mapping(ndc) */
448      _mesa_get_viewport_xform(ctx, 0, scale, translate);
449      ctx->Current.RasterPos[0] = ndc[0] * scale[0] + translate[0];
450      ctx->Current.RasterPos[1] = ndc[1] * scale[1] + translate[1];
451      ctx->Current.RasterPos[2] = ndc[2] * scale[2] + translate[2];
452      ctx->Current.RasterPos[3] = clip[3];
453
454      if (ctx->Transform.DepthClampNear &&
455          ctx->Transform.DepthClampFar) {
456         ctx->Current.RasterPos[3] = CLAMP(ctx->Current.RasterPos[3],
457                                           ctx->ViewportArray[0].Near,
458                                           ctx->ViewportArray[0].Far);
459      } else {
460         /* Clamp against near and far plane separately */
461         if (ctx->Transform.DepthClampNear) {
462            ctx->Current.RasterPos[3] = MAX2(ctx->Current.RasterPos[3],
463                                             ctx->ViewportArray[0].Near);
464         }
465
466         if (ctx->Transform.DepthClampFar) {
467            ctx->Current.RasterPos[3] = MIN2(ctx->Current.RasterPos[3],
468                                             ctx->ViewportArray[0].Far);
469         }
470      }
471
472      /* compute raster distance */
473      if (ctx->Fog.FogCoordinateSource == GL_FOG_COORDINATE_EXT)
474         ctx->Current.RasterDistance = ctx->Current.Attrib[VERT_ATTRIB_FOG][0];
475      else
476         ctx->Current.RasterDistance =
477                        sqrtf( eye[0]*eye[0] + eye[1]*eye[1] + eye[2]*eye[2] );
478
479      /* compute transformed normal vector (for lighting or texgen) */
480      if (ctx->_NeedEyeCoords) {
481         const GLfloat *inv = ctx->ModelviewMatrixStack.Top->inv;
482         TRANSFORM_NORMAL( eyenorm, objnorm, inv );
483         norm = eyenorm;
484      }
485      else {
486         norm = objnorm;
487      }
488
489      /* update raster color */
490      if (ctx->Light.Enabled) {
491         /* lighting */
492         shade_rastpos( ctx, vObj, norm,
493                        ctx->Current.RasterColor,
494                        ctx->Current.RasterSecondaryColor );
495      }
496      else {
497         /* use current color */
498	 COPY_4FV(ctx->Current.RasterColor,
499		  ctx->Current.Attrib[VERT_ATTRIB_COLOR0]);
500	 COPY_4FV(ctx->Current.RasterSecondaryColor,
501		  ctx->Current.Attrib[VERT_ATTRIB_COLOR1]);
502      }
503
504      /* texture coords */
505      {
506         GLuint u;
507         for (u = 0; u < ctx->Const.MaxTextureCoordUnits; u++) {
508            GLfloat tc[4];
509            COPY_4V(tc, ctx->Current.Attrib[VERT_ATTRIB_TEX0 + u]);
510            if (ctx->Texture.FixedFuncUnit[u].TexGenEnabled) {
511               compute_texgen(ctx, vObj, eye, norm, u, tc);
512            }
513            TRANSFORM_POINT(ctx->Current.RasterTexCoords[u],
514                            ctx->TextureMatrixStack[u].Top->m, tc);
515         }
516      }
517
518      ctx->Current.RasterPosValid = GL_TRUE;
519   }
520
521   if (ctx->RenderMode == GL_SELECT) {
522      _mesa_update_hitflag( ctx, ctx->Current.RasterPos[2] );
523   }
524}
525
526
527/**
528 * Helper function for all the RasterPos functions.
529 */
530static void
531rasterpos(GLfloat x, GLfloat y, GLfloat z, GLfloat w)
532{
533   GET_CURRENT_CONTEXT(ctx);
534   GLfloat p[4];
535
536   p[0] = x;
537   p[1] = y;
538   p[2] = z;
539   p[3] = w;
540
541   FLUSH_VERTICES(ctx, 0, 0);
542   FLUSH_CURRENT(ctx, 0);
543
544   if (ctx->NewState)
545      _mesa_update_state( ctx );
546
547   ctx->Driver.RasterPos(ctx, p);
548}
549
550
551void GLAPIENTRY
552_mesa_RasterPos2d(GLdouble x, GLdouble y)
553{
554   rasterpos((GLfloat)x, (GLfloat)y, (GLfloat)0.0, (GLfloat)1.0);
555}
556
557void GLAPIENTRY
558_mesa_RasterPos2f(GLfloat x, GLfloat y)
559{
560   rasterpos(x, y, 0.0F, 1.0F);
561}
562
563void GLAPIENTRY
564_mesa_RasterPos2i(GLint x, GLint y)
565{
566   rasterpos((GLfloat) x, (GLfloat) y, 0.0F, 1.0F);
567}
568
569void GLAPIENTRY
570_mesa_RasterPos2s(GLshort x, GLshort y)
571{
572   rasterpos(x, y, 0.0F, 1.0F);
573}
574
575void GLAPIENTRY
576_mesa_RasterPos3d(GLdouble x, GLdouble y, GLdouble z)
577{
578   rasterpos((GLfloat) x, (GLfloat) y, (GLfloat) z, 1.0F);
579}
580
581void GLAPIENTRY
582_mesa_RasterPos3f(GLfloat x, GLfloat y, GLfloat z)
583{
584   rasterpos(x, y, z, 1.0F);
585}
586
587void GLAPIENTRY
588_mesa_RasterPos3i(GLint x, GLint y, GLint z)
589{
590   rasterpos((GLfloat) x, (GLfloat) y, (GLfloat) z, 1.0F);
591}
592
593void GLAPIENTRY
594_mesa_RasterPos3s(GLshort x, GLshort y, GLshort z)
595{
596   rasterpos(x, y, z, 1.0F);
597}
598
599void GLAPIENTRY
600_mesa_RasterPos4d(GLdouble x, GLdouble y, GLdouble z, GLdouble w)
601{
602   rasterpos((GLfloat) x, (GLfloat) y, (GLfloat) z, (GLfloat) w);
603}
604
605void GLAPIENTRY
606_mesa_RasterPos4f(GLfloat x, GLfloat y, GLfloat z, GLfloat w)
607{
608   rasterpos(x, y, z, w);
609}
610
611void GLAPIENTRY
612_mesa_RasterPos4i(GLint x, GLint y, GLint z, GLint w)
613{
614   rasterpos((GLfloat) x, (GLfloat) y, (GLfloat) z, (GLfloat) w);
615}
616
617void GLAPIENTRY
618_mesa_RasterPos4s(GLshort x, GLshort y, GLshort z, GLshort w)
619{
620   rasterpos(x, y, z, w);
621}
622
623void GLAPIENTRY
624_mesa_RasterPos2dv(const GLdouble *v)
625{
626   rasterpos((GLfloat) v[0], (GLfloat) v[1], 0.0F, 1.0F);
627}
628
629void GLAPIENTRY
630_mesa_RasterPos2fv(const GLfloat *v)
631{
632   rasterpos(v[0], v[1], 0.0F, 1.0F);
633}
634
635void GLAPIENTRY
636_mesa_RasterPos2iv(const GLint *v)
637{
638   rasterpos((GLfloat) v[0], (GLfloat) v[1], 0.0F, 1.0F);
639}
640
641void GLAPIENTRY
642_mesa_RasterPos2sv(const GLshort *v)
643{
644   rasterpos(v[0], v[1], 0.0F, 1.0F);
645}
646
647void GLAPIENTRY
648_mesa_RasterPos3dv(const GLdouble *v)
649{
650   rasterpos((GLfloat) v[0], (GLfloat) v[1], (GLfloat) v[2], 1.0F);
651}
652
653void GLAPIENTRY
654_mesa_RasterPos3fv(const GLfloat *v)
655{
656   rasterpos(v[0], v[1], v[2], 1.0F);
657}
658
659void GLAPIENTRY
660_mesa_RasterPos3iv(const GLint *v)
661{
662   rasterpos((GLfloat) v[0], (GLfloat) v[1], (GLfloat) v[2], 1.0F);
663}
664
665void GLAPIENTRY
666_mesa_RasterPos3sv(const GLshort *v)
667{
668   rasterpos(v[0], v[1], v[2], 1.0F);
669}
670
671void GLAPIENTRY
672_mesa_RasterPos4dv(const GLdouble *v)
673{
674   rasterpos((GLfloat) v[0], (GLfloat) v[1],
675		     (GLfloat) v[2], (GLfloat) v[3]);
676}
677
678void GLAPIENTRY
679_mesa_RasterPos4fv(const GLfloat *v)
680{
681   rasterpos(v[0], v[1], v[2], v[3]);
682}
683
684void GLAPIENTRY
685_mesa_RasterPos4iv(const GLint *v)
686{
687   rasterpos((GLfloat) v[0], (GLfloat) v[1],
688		     (GLfloat) v[2], (GLfloat) v[3]);
689}
690
691void GLAPIENTRY
692_mesa_RasterPos4sv(const GLshort *v)
693{
694   rasterpos(v[0], v[1], v[2], v[3]);
695}
696
697
698/**********************************************************************/
699/***           GL_ARB_window_pos / GL_MESA_window_pos               ***/
700/**********************************************************************/
701
702
703/**
704 * All glWindowPosMESA and glWindowPosARB commands call this function to
705 * update the current raster position.
706 */
707static void
708window_pos3f(GLfloat x, GLfloat y, GLfloat z)
709{
710   GET_CURRENT_CONTEXT(ctx);
711   GLfloat z2;
712
713   FLUSH_VERTICES(ctx, 0, GL_CURRENT_BIT);
714   FLUSH_CURRENT(ctx, 0);
715
716   z2 = CLAMP(z, 0.0F, 1.0F)
717      * (ctx->ViewportArray[0].Far - ctx->ViewportArray[0].Near)
718      + ctx->ViewportArray[0].Near;
719
720   /* set raster position */
721   ctx->Current.RasterPos[0] = x;
722   ctx->Current.RasterPos[1] = y;
723   ctx->Current.RasterPos[2] = z2;
724   ctx->Current.RasterPos[3] = 1.0F;
725
726   ctx->Current.RasterPosValid = GL_TRUE;
727
728   if (ctx->Fog.FogCoordinateSource == GL_FOG_COORDINATE_EXT)
729      ctx->Current.RasterDistance = ctx->Current.Attrib[VERT_ATTRIB_FOG][0];
730   else
731      ctx->Current.RasterDistance = 0.0;
732
733   /* raster color = current color or index */
734   ctx->Current.RasterColor[0]
735      = CLAMP(ctx->Current.Attrib[VERT_ATTRIB_COLOR0][0], 0.0F, 1.0F);
736   ctx->Current.RasterColor[1]
737      = CLAMP(ctx->Current.Attrib[VERT_ATTRIB_COLOR0][1], 0.0F, 1.0F);
738   ctx->Current.RasterColor[2]
739      = CLAMP(ctx->Current.Attrib[VERT_ATTRIB_COLOR0][2], 0.0F, 1.0F);
740   ctx->Current.RasterColor[3]
741      = CLAMP(ctx->Current.Attrib[VERT_ATTRIB_COLOR0][3], 0.0F, 1.0F);
742   ctx->Current.RasterSecondaryColor[0]
743      = CLAMP(ctx->Current.Attrib[VERT_ATTRIB_COLOR1][0], 0.0F, 1.0F);
744   ctx->Current.RasterSecondaryColor[1]
745      = CLAMP(ctx->Current.Attrib[VERT_ATTRIB_COLOR1][1], 0.0F, 1.0F);
746   ctx->Current.RasterSecondaryColor[2]
747      = CLAMP(ctx->Current.Attrib[VERT_ATTRIB_COLOR1][2], 0.0F, 1.0F);
748   ctx->Current.RasterSecondaryColor[3]
749      = CLAMP(ctx->Current.Attrib[VERT_ATTRIB_COLOR1][3], 0.0F, 1.0F);
750
751   /* raster texcoord = current texcoord */
752   {
753      GLuint texSet;
754      for (texSet = 0; texSet < ctx->Const.MaxTextureCoordUnits; texSet++) {
755         assert(texSet < ARRAY_SIZE(ctx->Current.RasterTexCoords));
756         COPY_4FV( ctx->Current.RasterTexCoords[texSet],
757                  ctx->Current.Attrib[VERT_ATTRIB_TEX0 + texSet] );
758      }
759   }
760
761   if (ctx->RenderMode==GL_SELECT) {
762      _mesa_update_hitflag( ctx, ctx->Current.RasterPos[2] );
763   }
764}
765
766
767/* This is just to support the GL_MESA_window_pos version */
768static void
769window_pos4f(GLfloat x, GLfloat y, GLfloat z, GLfloat w)
770{
771   GET_CURRENT_CONTEXT(ctx);
772   window_pos3f(x, y, z);
773   ctx->Current.RasterPos[3] = w;
774}
775
776
777void GLAPIENTRY
778_mesa_WindowPos2d(GLdouble x, GLdouble y)
779{
780   window_pos4f((GLfloat) x, (GLfloat) y, 0.0F, 1.0F);
781}
782
783void GLAPIENTRY
784_mesa_WindowPos2f(GLfloat x, GLfloat y)
785{
786   window_pos4f(x, y, 0.0F, 1.0F);
787}
788
789void GLAPIENTRY
790_mesa_WindowPos2i(GLint x, GLint y)
791{
792   window_pos4f((GLfloat) x, (GLfloat) y, 0.0F, 1.0F);
793}
794
795void GLAPIENTRY
796_mesa_WindowPos2s(GLshort x, GLshort y)
797{
798   window_pos4f(x, y, 0.0F, 1.0F);
799}
800
801void GLAPIENTRY
802_mesa_WindowPos3d(GLdouble x, GLdouble y, GLdouble z)
803{
804   window_pos4f((GLfloat) x, (GLfloat) y, (GLfloat) z, 1.0F);
805}
806
807void GLAPIENTRY
808_mesa_WindowPos3f(GLfloat x, GLfloat y, GLfloat z)
809{
810   window_pos4f(x, y, z, 1.0F);
811}
812
813void GLAPIENTRY
814_mesa_WindowPos3i(GLint x, GLint y, GLint z)
815{
816   window_pos4f((GLfloat) x, (GLfloat) y, (GLfloat) z, 1.0F);
817}
818
819void GLAPIENTRY
820_mesa_WindowPos3s(GLshort x, GLshort y, GLshort z)
821{
822   window_pos4f(x, y, z, 1.0F);
823}
824
825void GLAPIENTRY
826_mesa_WindowPos4dMESA(GLdouble x, GLdouble y, GLdouble z, GLdouble w)
827{
828   window_pos4f((GLfloat) x, (GLfloat) y, (GLfloat) z, (GLfloat) w);
829}
830
831void GLAPIENTRY
832_mesa_WindowPos4fMESA(GLfloat x, GLfloat y, GLfloat z, GLfloat w)
833{
834   window_pos4f(x, y, z, w);
835}
836
837void GLAPIENTRY
838_mesa_WindowPos4iMESA(GLint x, GLint y, GLint z, GLint w)
839{
840   window_pos4f((GLfloat) x, (GLfloat) y, (GLfloat) z, (GLfloat) w);
841}
842
843void GLAPIENTRY
844_mesa_WindowPos4sMESA(GLshort x, GLshort y, GLshort z, GLshort w)
845{
846   window_pos4f(x, y, z, w);
847}
848
849void GLAPIENTRY
850_mesa_WindowPos2dv(const GLdouble *v)
851{
852   window_pos4f((GLfloat) v[0], (GLfloat) v[1], 0.0F, 1.0F);
853}
854
855void GLAPIENTRY
856_mesa_WindowPos2fv(const GLfloat *v)
857{
858   window_pos4f(v[0], v[1], 0.0F, 1.0F);
859}
860
861void GLAPIENTRY
862_mesa_WindowPos2iv(const GLint *v)
863{
864   window_pos4f((GLfloat) v[0], (GLfloat) v[1], 0.0F, 1.0F);
865}
866
867void GLAPIENTRY
868_mesa_WindowPos2sv(const GLshort *v)
869{
870   window_pos4f(v[0], v[1], 0.0F, 1.0F);
871}
872
873void GLAPIENTRY
874_mesa_WindowPos3dv(const GLdouble *v)
875{
876   window_pos4f((GLfloat) v[0], (GLfloat) v[1], (GLfloat) v[2], 1.0F);
877}
878
879void GLAPIENTRY
880_mesa_WindowPos3fv(const GLfloat *v)
881{
882   window_pos4f(v[0], v[1], v[2], 1.0);
883}
884
885void GLAPIENTRY
886_mesa_WindowPos3iv(const GLint *v)
887{
888   window_pos4f((GLfloat) v[0], (GLfloat) v[1], (GLfloat) v[2], 1.0F);
889}
890
891void GLAPIENTRY
892_mesa_WindowPos3sv(const GLshort *v)
893{
894   window_pos4f(v[0], v[1], v[2], 1.0F);
895}
896
897void GLAPIENTRY
898_mesa_WindowPos4dvMESA(const GLdouble *v)
899{
900   window_pos4f((GLfloat) v[0], (GLfloat) v[1],
901			 (GLfloat) v[2], (GLfloat) v[3]);
902}
903
904void GLAPIENTRY
905_mesa_WindowPos4fvMESA(const GLfloat *v)
906{
907   window_pos4f(v[0], v[1], v[2], v[3]);
908}
909
910void GLAPIENTRY
911_mesa_WindowPos4ivMESA(const GLint *v)
912{
913   window_pos4f((GLfloat) v[0], (GLfloat) v[1],
914			 (GLfloat) v[2], (GLfloat) v[3]);
915}
916
917void GLAPIENTRY
918_mesa_WindowPos4svMESA(const GLshort *v)
919{
920   window_pos4f(v[0], v[1], v[2], v[3]);
921}
922
923
924#if 0
925
926/*
927 * OpenGL implementation of glWindowPos*MESA()
928 */
929void glWindowPos4fMESA( GLfloat x, GLfloat y, GLfloat z, GLfloat w )
930{
931   GLfloat fx, fy;
932
933   /* Push current matrix mode and viewport attributes */
934   glPushAttrib( GL_TRANSFORM_BIT | GL_VIEWPORT_BIT );
935
936   /* Setup projection parameters */
937   glMatrixMode( GL_PROJECTION );
938   glPushMatrix();
939   glLoadIdentity();
940   glMatrixMode( GL_MODELVIEW );
941   glPushMatrix();
942   glLoadIdentity();
943
944   glDepthRange( z, z );
945   glViewport( (int) x - 1, (int) y - 1, 2, 2 );
946
947   /* set the raster (window) position */
948   fx = x - (int) x;
949   fy = y - (int) y;
950   glRasterPos4f( fx, fy, 0.0, w );
951
952   /* restore matrices, viewport and matrix mode */
953   glPopMatrix();
954   glMatrixMode( GL_PROJECTION );
955   glPopMatrix();
956
957   glPopAttrib();
958}
959
960#endif
961
962
963/**********************************************************************/
964/** \name Initialization                                              */
965/**********************************************************************/
966/*@{*/
967
968/**
969 * Initialize the context current raster position information.
970 *
971 * \param ctx GL context.
972 *
973 * Initialize the current raster position information in
974 * __struct gl_contextRec::Current, and adds the extension entry points to the
975 * dispatcher.
976 */
977void _mesa_init_rastpos( struct gl_context * ctx )
978{
979   unsigned i;
980
981   ASSIGN_4V( ctx->Current.RasterPos, 0.0, 0.0, 0.0, 1.0 );
982   ctx->Current.RasterDistance = 0.0;
983   ASSIGN_4V( ctx->Current.RasterColor, 1.0, 1.0, 1.0, 1.0 );
984   ASSIGN_4V( ctx->Current.RasterSecondaryColor, 0.0, 0.0, 0.0, 1.0 );
985   for (i = 0; i < ARRAY_SIZE(ctx->Current.RasterTexCoords); i++)
986      ASSIGN_4V( ctx->Current.RasterTexCoords[i], 0.0, 0.0, 0.0, 1.0 );
987   ctx->Current.RasterPosValid = GL_TRUE;
988}
989
990/*@}*/
991