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