fix for-loop in _mesa_GetDouble to avoid out of bounds memory read
[mesa.git] / src / mesa / main / rastpos.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.3
4 *
5 * Copyright (C) 1999-2004 Brian Paul All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25
26 /**
27 * \file rastpos.c
28 * Raster position operations.
29 */
30
31 #include "glheader.h"
32 #include "colormac.h"
33 #include "context.h"
34 #include "feedback.h"
35 #include "light.h"
36 #include "macros.h"
37 #include "rastpos.h"
38 #include "state.h"
39 #include "simple_list.h"
40 #include "mtypes.h"
41
42 #include "math/m_matrix.h"
43
44
45 /**
46 * Clip a point against the view volume.
47 *
48 * \param v vertex vector describing the point to clip.
49 *
50 * \return zero if outside view volume, or one if inside.
51 */
52 static GLuint
53 viewclip_point( const GLfloat v[] )
54 {
55 if ( v[0] > v[3] || v[0] < -v[3]
56 || v[1] > v[3] || v[1] < -v[3]
57 || v[2] > v[3] || v[2] < -v[3] ) {
58 return 0;
59 }
60 else {
61 return 1;
62 }
63 }
64
65
66 /**
67 * Clip a point against the far/near Z clipping planes.
68 *
69 * \param v vertex vector describing the point to clip.
70 *
71 * \return zero if outside view volume, or one if inside.
72 */
73 static GLuint
74 viewclip_point_z( const GLfloat v[] )
75 {
76 if (v[2] > v[3] || v[2] < -v[3] ) {
77 return 0;
78 }
79 else {
80 return 1;
81 }
82 }
83
84
85 /**
86 * Clip a point against the user clipping planes.
87 *
88 * \param ctx GL context.
89 * \param v vertex vector describing the point to clip.
90 *
91 * \return zero if the point was clipped, or one otherwise.
92 */
93 static GLuint
94 userclip_point( GLcontext *ctx, const GLfloat v[] )
95 {
96 GLuint p;
97
98 for (p = 0; p < ctx->Const.MaxClipPlanes; p++) {
99 if (ctx->Transform.ClipPlanesEnabled & (1 << p)) {
100 GLfloat dot = v[0] * ctx->Transform._ClipUserPlane[p][0]
101 + v[1] * ctx->Transform._ClipUserPlane[p][1]
102 + v[2] * ctx->Transform._ClipUserPlane[p][2]
103 + v[3] * ctx->Transform._ClipUserPlane[p][3];
104 if (dot < 0.0F) {
105 return 0;
106 }
107 }
108 }
109
110 return 1;
111 }
112
113
114 /**
115 * This has been split off to allow the normal shade routines to
116 * get a little closer to the vertex buffer, and to use the
117 * GLvector objects directly.
118 * \param ctx the context
119 * \param vertex vertex location
120 * \param normal normal vector
121 * \param Rcolor returned color
122 * \param Rspec returned specular color (if separate specular enabled)
123 * \param Rindex returned color index
124 */
125 static void
126 shade_rastpos(GLcontext *ctx,
127 const GLfloat vertex[4],
128 const GLfloat normal[3],
129 GLfloat Rcolor[4],
130 GLfloat Rspec[4],
131 GLfloat *Rindex)
132 {
133 GLfloat (*base)[3] = ctx->Light._BaseColor;
134 struct gl_light *light;
135 GLfloat diffuseColor[4], specularColor[4];
136 GLfloat diffuse = 0, specular = 0;
137
138 if (!ctx->_ShineTable[0] || !ctx->_ShineTable[1])
139 _mesa_validate_all_lighting_tables( ctx );
140
141 COPY_3V(diffuseColor, base[0]);
142 diffuseColor[3] = CLAMP(
143 ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE][3], 0.0F, 1.0F );
144 ASSIGN_4V(specularColor, 0.0, 0.0, 0.0, 0.0);
145
146 foreach (light, &ctx->Light.EnabledList) {
147 GLfloat n_dot_h;
148 GLfloat attenuation = 1.0;
149 GLfloat VP[3];
150 GLfloat n_dot_VP;
151 GLfloat *h;
152 GLfloat diffuseContrib[3], specularContrib[3];
153 GLboolean normalized;
154
155 if (!(light->_Flags & LIGHT_POSITIONAL)) {
156 COPY_3V(VP, light->_VP_inf_norm);
157 attenuation = light->_VP_inf_spot_attenuation;
158 }
159 else {
160 GLfloat d;
161
162 SUB_3V(VP, light->_Position, vertex);
163 d = (GLfloat) LEN_3FV( VP );
164
165 if ( d > 1e-6) {
166 GLfloat invd = 1.0F / d;
167 SELF_SCALE_SCALAR_3V(VP, invd);
168 }
169 attenuation = 1.0F / (light->ConstantAttenuation + d *
170 (light->LinearAttenuation + d *
171 light->QuadraticAttenuation));
172
173 if (light->_Flags & LIGHT_SPOT) {
174 GLfloat PV_dot_dir = - DOT3(VP, light->_NormDirection);
175
176 if (PV_dot_dir<light->_CosCutoff) {
177 continue;
178 }
179 else {
180 double x = PV_dot_dir * (EXP_TABLE_SIZE-1);
181 int k = (int) x;
182 GLfloat spot = (GLfloat) (light->_SpotExpTable[k][0]
183 + (x-k)*light->_SpotExpTable[k][1]);
184 attenuation *= spot;
185 }
186 }
187 }
188
189 if (attenuation < 1e-3)
190 continue;
191
192 n_dot_VP = DOT3( normal, VP );
193
194 if (n_dot_VP < 0.0F) {
195 ACC_SCALE_SCALAR_3V(diffuseColor, attenuation, light->_MatAmbient[0]);
196 continue;
197 }
198
199 COPY_3V(diffuseContrib, light->_MatAmbient[0]);
200 ACC_SCALE_SCALAR_3V(diffuseContrib, n_dot_VP, light->_MatDiffuse[0]);
201 diffuse += n_dot_VP * light->_dli * attenuation;
202 ASSIGN_3V(specularContrib, 0.0, 0.0, 0.0);
203
204 {
205 if (ctx->Light.Model.LocalViewer) {
206 GLfloat v[3];
207 COPY_3V(v, vertex);
208 NORMALIZE_3FV(v);
209 SUB_3V(VP, VP, v);
210 h = VP;
211 normalized = 0;
212 }
213 else if (light->_Flags & LIGHT_POSITIONAL) {
214 h = VP;
215 ACC_3V(h, ctx->_EyeZDir);
216 normalized = 0;
217 }
218 else {
219 h = light->_h_inf_norm;
220 normalized = 1;
221 }
222
223 n_dot_h = DOT3(normal, h);
224
225 if (n_dot_h > 0.0F) {
226 GLfloat (*mat)[4] = ctx->Light.Material.Attrib;
227 GLfloat spec_coef;
228 GLfloat shininess = mat[MAT_ATTRIB_FRONT_SHININESS][0];
229
230 if (!normalized) {
231 n_dot_h *= n_dot_h;
232 n_dot_h /= LEN_SQUARED_3FV( h );
233 shininess *= .5;
234 }
235
236 GET_SHINE_TAB_ENTRY( ctx->_ShineTable[0], n_dot_h, spec_coef );
237
238 if (spec_coef > 1.0e-10) {
239 if (ctx->Light.Model.ColorControl==GL_SEPARATE_SPECULAR_COLOR) {
240 ACC_SCALE_SCALAR_3V( specularContrib, spec_coef,
241 light->_MatSpecular[0]);
242 }
243 else {
244 ACC_SCALE_SCALAR_3V( diffuseContrib, spec_coef,
245 light->_MatSpecular[0]);
246 }
247 specular += spec_coef * light->_sli * attenuation;
248 }
249 }
250 }
251
252 ACC_SCALE_SCALAR_3V( diffuseColor, attenuation, diffuseContrib );
253 ACC_SCALE_SCALAR_3V( specularColor, attenuation, specularContrib );
254 }
255
256 if (ctx->Visual.rgbMode) {
257 Rcolor[0] = CLAMP(diffuseColor[0], 0.0F, 1.0F);
258 Rcolor[1] = CLAMP(diffuseColor[1], 0.0F, 1.0F);
259 Rcolor[2] = CLAMP(diffuseColor[2], 0.0F, 1.0F);
260 Rcolor[3] = CLAMP(diffuseColor[3], 0.0F, 1.0F);
261 Rspec[0] = CLAMP(specularColor[0], 0.0F, 1.0F);
262 Rspec[1] = CLAMP(specularColor[1], 0.0F, 1.0F);
263 Rspec[2] = CLAMP(specularColor[2], 0.0F, 1.0F);
264 Rspec[3] = CLAMP(specularColor[3], 0.0F, 1.0F);
265 }
266 else {
267 GLfloat *ind = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_INDEXES];
268 GLfloat d_a = ind[MAT_INDEX_DIFFUSE] - ind[MAT_INDEX_AMBIENT];
269 GLfloat s_a = ind[MAT_INDEX_SPECULAR] - ind[MAT_INDEX_AMBIENT];
270 GLfloat i = (ind[MAT_INDEX_AMBIENT]
271 + diffuse * (1.0F-specular) * d_a
272 + specular * s_a);
273 if (i > ind[MAT_INDEX_SPECULAR]) {
274 i = ind[MAT_INDEX_SPECULAR];
275 }
276 *Rindex = i;
277 }
278 }
279
280
281 /**
282 * Do texgen needed for glRasterPos.
283 * \param ctx rendering context
284 * \param vObj object-space vertex coordinate
285 * \param vEye eye-space vertex coordinate
286 * \param normal vertex normal
287 * \param unit texture unit number
288 * \param texcoord incoming texcoord and resulting texcoord
289 */
290 static void
291 compute_texgen(GLcontext *ctx, const GLfloat vObj[4], const GLfloat vEye[4],
292 const GLfloat normal[3], GLuint unit, GLfloat texcoord[4])
293 {
294 const struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
295
296 /* always compute sphere map terms, just in case */
297 GLfloat u[3], two_nu, rx, ry, rz, m, mInv;
298 COPY_3V(u, vEye);
299 NORMALIZE_3FV(u);
300 two_nu = 2.0F * DOT3(normal, u);
301 rx = u[0] - normal[0] * two_nu;
302 ry = u[1] - normal[1] * two_nu;
303 rz = u[2] - normal[2] * two_nu;
304 m = rx * rx + ry * ry + (rz + 1.0F) * (rz + 1.0F);
305 if (m > 0.0F)
306 mInv = 0.5F * _mesa_inv_sqrtf(m);
307 else
308 mInv = 0.0F;
309
310 if (texUnit->TexGenEnabled & S_BIT) {
311 switch (texUnit->GenModeS) {
312 case GL_OBJECT_LINEAR:
313 texcoord[0] = DOT4(vObj, texUnit->ObjectPlaneS);
314 break;
315 case GL_EYE_LINEAR:
316 texcoord[0] = DOT4(vEye, texUnit->EyePlaneS);
317 break;
318 case GL_SPHERE_MAP:
319 texcoord[0] = rx * mInv + 0.5F;
320 break;
321 case GL_REFLECTION_MAP:
322 texcoord[0] = rx;
323 break;
324 case GL_NORMAL_MAP:
325 texcoord[0] = normal[0];
326 break;
327 default:
328 _mesa_problem(ctx, "Bad S texgen in compute_texgen()");
329 return;
330 }
331 }
332
333 if (texUnit->TexGenEnabled & T_BIT) {
334 switch (texUnit->GenModeT) {
335 case GL_OBJECT_LINEAR:
336 texcoord[1] = DOT4(vObj, texUnit->ObjectPlaneT);
337 break;
338 case GL_EYE_LINEAR:
339 texcoord[1] = DOT4(vEye, texUnit->EyePlaneT);
340 break;
341 case GL_SPHERE_MAP:
342 texcoord[1] = ry * mInv + 0.5F;
343 break;
344 case GL_REFLECTION_MAP:
345 texcoord[1] = ry;
346 break;
347 case GL_NORMAL_MAP:
348 texcoord[1] = normal[1];
349 break;
350 default:
351 _mesa_problem(ctx, "Bad T texgen in compute_texgen()");
352 return;
353 }
354 }
355
356 if (texUnit->TexGenEnabled & R_BIT) {
357 switch (texUnit->GenModeR) {
358 case GL_OBJECT_LINEAR:
359 texcoord[2] = DOT4(vObj, texUnit->ObjectPlaneR);
360 break;
361 case GL_EYE_LINEAR:
362 texcoord[2] = DOT4(vEye, texUnit->EyePlaneR);
363 break;
364 case GL_REFLECTION_MAP:
365 texcoord[2] = rz;
366 break;
367 case GL_NORMAL_MAP:
368 texcoord[2] = normal[2];
369 break;
370 default:
371 _mesa_problem(ctx, "Bad R texgen in compute_texgen()");
372 return;
373 }
374 }
375
376 if (texUnit->TexGenEnabled & Q_BIT) {
377 switch (texUnit->GenModeQ) {
378 case GL_OBJECT_LINEAR:
379 texcoord[3] = DOT4(vObj, texUnit->ObjectPlaneQ);
380 break;
381 case GL_EYE_LINEAR:
382 texcoord[3] = DOT4(vEye, texUnit->EyePlaneQ);
383 break;
384 default:
385 _mesa_problem(ctx, "Bad Q texgen in compute_texgen()");
386 return;
387 }
388 }
389 }
390
391
392
393 /**
394 * Set the raster position for pixel operations.
395 *
396 * All glRasterPos command call this function to update the current
397 * raster position.
398 *
399 * \param ctx GL context.
400 * \param x x coordinate for the raster position.
401 * \param y y coordinate for the raster position.
402 * \param z z coordinate for the raster position.
403 * \param w w coordinate for the raster position.
404 *
405 * \sa Called by _mesa_RasterPos4f().
406 *
407 * Flushes the vertices, transforms and clips the vertex coordinates, and
408 * finally sets the current raster position and associated data in
409 * __GLcontextRec::Current. When in selection mode calls
410 * _mesa_update_hitflag() with the current raster position.
411 */
412 static void
413 raster_pos4f(GLcontext *ctx, GLfloat x, GLfloat y, GLfloat z, GLfloat w)
414 {
415 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
416 FLUSH_CURRENT(ctx, 0);
417
418 if (ctx->NewState)
419 _mesa_update_state( ctx );
420
421 if (ctx->VertexProgram._Enabled) {
422 /* XXX implement this */
423 _mesa_problem(ctx, "Vertex programs not implemented for glRasterPos");
424 return;
425 }
426 else {
427 GLfloat obj[4], eye[4], clip[4], ndc[3], d;
428 GLfloat *norm, eyenorm[3];
429 GLfloat *objnorm = ctx->Current.Attrib[VERT_ATTRIB_NORMAL];
430
431 ASSIGN_4V( obj, x, y, z, w );
432 /* apply modelview matrix: eye = MV * obj */
433 TRANSFORM_POINT( eye, ctx->ModelviewMatrixStack.Top->m, obj );
434 /* apply projection matrix: clip = Proj * eye */
435 TRANSFORM_POINT( clip, ctx->ProjectionMatrixStack.Top->m, eye );
436
437 /* clip to view volume */
438 if (ctx->Transform.RasterPositionUnclipped) {
439 /* GL_IBM_rasterpos_clip: only clip against Z */
440 if (viewclip_point_z(clip) == 0) {
441 ctx->Current.RasterPosValid = GL_FALSE;
442 return;
443 }
444 }
445 else if (viewclip_point(clip) == 0) {
446 /* Normal OpenGL behaviour */
447 ctx->Current.RasterPosValid = GL_FALSE;
448 return;
449 }
450
451 /* clip to user clipping planes */
452 if (ctx->Transform.ClipPlanesEnabled && !userclip_point(ctx, clip)) {
453 ctx->Current.RasterPosValid = GL_FALSE;
454 return;
455 }
456
457 /* ndc = clip / W */
458 d = (clip[3] == 0.0F) ? 1.0F : 1.0F / clip[3];
459 ndc[0] = clip[0] * d;
460 ndc[1] = clip[1] * d;
461 ndc[2] = clip[2] * d;
462 /* wincoord = viewport_mapping(ndc) */
463 ctx->Current.RasterPos[0] = (ndc[0] * ctx->Viewport._WindowMap.m[MAT_SX]
464 + ctx->Viewport._WindowMap.m[MAT_TX]);
465 ctx->Current.RasterPos[1] = (ndc[1] * ctx->Viewport._WindowMap.m[MAT_SY]
466 + ctx->Viewport._WindowMap.m[MAT_TY]);
467 ctx->Current.RasterPos[2] = (ndc[2] * ctx->Viewport._WindowMap.m[MAT_SZ]
468 + ctx->Viewport._WindowMap.m[MAT_TZ])
469 / ctx->DrawBuffer->_DepthMaxF;
470 ctx->Current.RasterPos[3] = clip[3];
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, obj, norm,
493 ctx->Current.RasterColor,
494 ctx->Current.RasterSecondaryColor,
495 &ctx->Current.RasterIndex );
496 }
497 else {
498 /* use current color or index */
499 if (ctx->Visual.rgbMode) {
500 COPY_4FV(ctx->Current.RasterColor,
501 ctx->Current.Attrib[VERT_ATTRIB_COLOR0]);
502 COPY_4FV(ctx->Current.RasterSecondaryColor,
503 ctx->Current.Attrib[VERT_ATTRIB_COLOR1]);
504 }
505 else {
506 ctx->Current.RasterIndex = ctx->Current.Index;
507 }
508 }
509
510 /* texture coords */
511 {
512 GLuint u;
513 for (u = 0; u < ctx->Const.MaxTextureCoordUnits; u++) {
514 GLfloat tc[4];
515 COPY_4V(tc, ctx->Current.Attrib[VERT_ATTRIB_TEX0 + u]);
516 if (ctx->Texture.Unit[u].TexGenEnabled) {
517 compute_texgen(ctx, obj, eye, norm, u, tc);
518 }
519 TRANSFORM_POINT(ctx->Current.RasterTexCoords[u],
520 ctx->TextureMatrixStack[u].Top->m, tc);
521 }
522 }
523
524 ctx->Current.RasterPosValid = GL_TRUE;
525 }
526
527 if (ctx->RenderMode == GL_SELECT) {
528 _mesa_update_hitflag( ctx, ctx->Current.RasterPos[2] );
529 }
530 }
531
532
533 /** Calls _mesa_RasterPos4f() */
534 void GLAPIENTRY
535 _mesa_RasterPos2d(GLdouble x, GLdouble y)
536 {
537 _mesa_RasterPos4f((GLfloat) x, (GLfloat) y, 0.0F, 1.0F);
538 }
539
540 /** Calls _mesa_RasterPos4f() */
541 void GLAPIENTRY
542 _mesa_RasterPos2f(GLfloat x, GLfloat y)
543 {
544 _mesa_RasterPos4f(x, y, 0.0F, 1.0F);
545 }
546
547 /** Calls _mesa_RasterPos4f() */
548 void GLAPIENTRY
549 _mesa_RasterPos2i(GLint x, GLint y)
550 {
551 _mesa_RasterPos4f((GLfloat) x, (GLfloat) y, 0.0F, 1.0F);
552 }
553
554 /** Calls _mesa_RasterPos4f() */
555 void GLAPIENTRY
556 _mesa_RasterPos2s(GLshort x, GLshort y)
557 {
558 _mesa_RasterPos4f(x, y, 0.0F, 1.0F);
559 }
560
561 /** Calls _mesa_RasterPos4f() */
562 void GLAPIENTRY
563 _mesa_RasterPos3d(GLdouble x, GLdouble y, GLdouble z)
564 {
565 _mesa_RasterPos4f((GLfloat) x, (GLfloat) y, (GLfloat) z, 1.0F);
566 }
567
568 /** Calls _mesa_RasterPos4f() */
569 void GLAPIENTRY
570 _mesa_RasterPos3f(GLfloat x, GLfloat y, GLfloat z)
571 {
572 _mesa_RasterPos4f(x, y, z, 1.0F);
573 }
574
575 /** Calls _mesa_RasterPos4f() */
576 void GLAPIENTRY
577 _mesa_RasterPos3i(GLint x, GLint y, GLint z)
578 {
579 _mesa_RasterPos4f((GLfloat) x, (GLfloat) y, (GLfloat) z, 1.0F);
580 }
581
582 /** Calls _mesa_RasterPos4f() */
583 void GLAPIENTRY
584 _mesa_RasterPos3s(GLshort x, GLshort y, GLshort z)
585 {
586 _mesa_RasterPos4f(x, y, z, 1.0F);
587 }
588
589 /** Calls _mesa_RasterPos4f() */
590 void GLAPIENTRY
591 _mesa_RasterPos4d(GLdouble x, GLdouble y, GLdouble z, GLdouble w)
592 {
593 _mesa_RasterPos4f((GLfloat) x, (GLfloat) y, (GLfloat) z, (GLfloat) w);
594 }
595
596 /** Calls raster_pos4f() */
597 void GLAPIENTRY
598 _mesa_RasterPos4f(GLfloat x, GLfloat y, GLfloat z, GLfloat w)
599 {
600 GET_CURRENT_CONTEXT(ctx);
601 raster_pos4f(ctx, x, y, z, w);
602 }
603
604 /** Calls _mesa_RasterPos4f() */
605 void GLAPIENTRY
606 _mesa_RasterPos4i(GLint x, GLint y, GLint z, GLint w)
607 {
608 _mesa_RasterPos4f((GLfloat) x, (GLfloat) y, (GLfloat) z, (GLfloat) w);
609 }
610
611 /** Calls _mesa_RasterPos4f() */
612 void GLAPIENTRY
613 _mesa_RasterPos4s(GLshort x, GLshort y, GLshort z, GLshort w)
614 {
615 _mesa_RasterPos4f(x, y, z, w);
616 }
617
618 /** Calls _mesa_RasterPos4f() */
619 void GLAPIENTRY
620 _mesa_RasterPos2dv(const GLdouble *v)
621 {
622 _mesa_RasterPos4f((GLfloat) v[0], (GLfloat) v[1], 0.0F, 1.0F);
623 }
624
625 /** Calls _mesa_RasterPos4f() */
626 void GLAPIENTRY
627 _mesa_RasterPos2fv(const GLfloat *v)
628 {
629 _mesa_RasterPos4f(v[0], v[1], 0.0F, 1.0F);
630 }
631
632 /** Calls _mesa_RasterPos4f() */
633 void GLAPIENTRY
634 _mesa_RasterPos2iv(const GLint *v)
635 {
636 _mesa_RasterPos4f((GLfloat) v[0], (GLfloat) v[1], 0.0F, 1.0F);
637 }
638
639 /** Calls _mesa_RasterPos4f() */
640 void GLAPIENTRY
641 _mesa_RasterPos2sv(const GLshort *v)
642 {
643 _mesa_RasterPos4f(v[0], v[1], 0.0F, 1.0F);
644 }
645
646 /** Calls _mesa_RasterPos4f() */
647 void GLAPIENTRY
648 _mesa_RasterPos3dv(const GLdouble *v)
649 {
650 _mesa_RasterPos4f((GLfloat) v[0], (GLfloat) v[1], (GLfloat) v[2], 1.0F);
651 }
652
653 /** Calls _mesa_RasterPos4f() */
654 void GLAPIENTRY
655 _mesa_RasterPos3fv(const GLfloat *v)
656 {
657 _mesa_RasterPos4f(v[0], v[1], v[2], 1.0F);
658 }
659
660 /** Calls _mesa_RasterPos4f() */
661 void GLAPIENTRY
662 _mesa_RasterPos3iv(const GLint *v)
663 {
664 _mesa_RasterPos4f((GLfloat) v[0], (GLfloat) v[1], (GLfloat) v[2], 1.0F);
665 }
666
667 /** Calls _mesa_RasterPos4f() */
668 void GLAPIENTRY
669 _mesa_RasterPos3sv(const GLshort *v)
670 {
671 _mesa_RasterPos4f(v[0], v[1], v[2], 1.0F);
672 }
673
674 /** Calls _mesa_RasterPos4f() */
675 void GLAPIENTRY
676 _mesa_RasterPos4dv(const GLdouble *v)
677 {
678 _mesa_RasterPos4f((GLfloat) v[0], (GLfloat) v[1],
679 (GLfloat) v[2], (GLfloat) v[3]);
680 }
681
682 /** Calls _mesa_RasterPos4f() */
683 void GLAPIENTRY
684 _mesa_RasterPos4fv(const GLfloat *v)
685 {
686 _mesa_RasterPos4f(v[0], v[1], v[2], v[3]);
687 }
688
689 /** Calls _mesa_RasterPos4f() */
690 void GLAPIENTRY
691 _mesa_RasterPos4iv(const GLint *v)
692 {
693 _mesa_RasterPos4f((GLfloat) v[0], (GLfloat) v[1],
694 (GLfloat) v[2], (GLfloat) v[3]);
695 }
696
697 /** Calls _mesa_RasterPos4f() */
698 void GLAPIENTRY
699 _mesa_RasterPos4sv(const GLshort *v)
700 {
701 _mesa_RasterPos4f(v[0], v[1], v[2], v[3]);
702 }
703
704
705 /**********************************************************************/
706 /*** GL_ARB_window_pos / GL_MESA_window_pos ***/
707 /**********************************************************************/
708
709 #if FEATURE_windowpos
710 /**
711 * All glWindowPosMESA and glWindowPosARB commands call this function to
712 * update the current raster position.
713 */
714 static void
715 window_pos3f(GLfloat x, GLfloat y, GLfloat z)
716 {
717 GET_CURRENT_CONTEXT(ctx);
718 GLfloat z2;
719
720 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
721 FLUSH_CURRENT(ctx, 0);
722
723 z2 = CLAMP(z, 0.0F, 1.0F) * (ctx->Viewport.Far - ctx->Viewport.Near)
724 + ctx->Viewport.Near;
725
726 /* set raster position */
727 ctx->Current.RasterPos[0] = x;
728 ctx->Current.RasterPos[1] = y;
729 ctx->Current.RasterPos[2] = z2;
730 ctx->Current.RasterPos[3] = 1.0F;
731
732 ctx->Current.RasterPosValid = GL_TRUE;
733
734 if (ctx->Fog.FogCoordinateSource == GL_FOG_COORDINATE_EXT)
735 ctx->Current.RasterDistance = ctx->Current.Attrib[VERT_ATTRIB_FOG][0];
736 else
737 ctx->Current.RasterDistance = 0.0;
738
739 /* raster color = current color or index */
740 if (ctx->Visual.rgbMode) {
741 ctx->Current.RasterColor[0]
742 = CLAMP(ctx->Current.Attrib[VERT_ATTRIB_COLOR0][0], 0.0F, 1.0F);
743 ctx->Current.RasterColor[1]
744 = CLAMP(ctx->Current.Attrib[VERT_ATTRIB_COLOR0][1], 0.0F, 1.0F);
745 ctx->Current.RasterColor[2]
746 = CLAMP(ctx->Current.Attrib[VERT_ATTRIB_COLOR0][2], 0.0F, 1.0F);
747 ctx->Current.RasterColor[3]
748 = CLAMP(ctx->Current.Attrib[VERT_ATTRIB_COLOR0][3], 0.0F, 1.0F);
749 ctx->Current.RasterSecondaryColor[0]
750 = CLAMP(ctx->Current.Attrib[VERT_ATTRIB_COLOR1][0], 0.0F, 1.0F);
751 ctx->Current.RasterSecondaryColor[1]
752 = CLAMP(ctx->Current.Attrib[VERT_ATTRIB_COLOR1][1], 0.0F, 1.0F);
753 ctx->Current.RasterSecondaryColor[2]
754 = CLAMP(ctx->Current.Attrib[VERT_ATTRIB_COLOR1][2], 0.0F, 1.0F);
755 ctx->Current.RasterSecondaryColor[3]
756 = CLAMP(ctx->Current.Attrib[VERT_ATTRIB_COLOR1][3], 0.0F, 1.0F);
757 }
758 else {
759 ctx->Current.RasterIndex = ctx->Current.Index;
760 }
761
762 /* raster texcoord = current texcoord */
763 {
764 GLuint texSet;
765 for (texSet = 0; texSet < ctx->Const.MaxTextureCoordUnits; texSet++) {
766 COPY_4FV( ctx->Current.RasterTexCoords[texSet],
767 ctx->Current.Attrib[VERT_ATTRIB_TEX0 + texSet] );
768 }
769 }
770
771 if (ctx->RenderMode==GL_SELECT) {
772 _mesa_update_hitflag( ctx, ctx->Current.RasterPos[2] );
773 }
774 }
775
776
777 /* This is just to support the GL_MESA_window_pos version */
778 static void
779 window_pos4f(GLfloat x, GLfloat y, GLfloat z, GLfloat w)
780 {
781 GET_CURRENT_CONTEXT(ctx);
782 window_pos3f(x, y, z);
783 ctx->Current.RasterPos[3] = w;
784 }
785
786
787 void GLAPIENTRY
788 _mesa_WindowPos2dMESA(GLdouble x, GLdouble y)
789 {
790 window_pos4f((GLfloat) x, (GLfloat) y, 0.0F, 1.0F);
791 }
792
793 void GLAPIENTRY
794 _mesa_WindowPos2fMESA(GLfloat x, GLfloat y)
795 {
796 window_pos4f(x, y, 0.0F, 1.0F);
797 }
798
799 void GLAPIENTRY
800 _mesa_WindowPos2iMESA(GLint x, GLint y)
801 {
802 window_pos4f((GLfloat) x, (GLfloat) y, 0.0F, 1.0F);
803 }
804
805 void GLAPIENTRY
806 _mesa_WindowPos2sMESA(GLshort x, GLshort y)
807 {
808 window_pos4f(x, y, 0.0F, 1.0F);
809 }
810
811 void GLAPIENTRY
812 _mesa_WindowPos3dMESA(GLdouble x, GLdouble y, GLdouble z)
813 {
814 window_pos4f((GLfloat) x, (GLfloat) y, (GLfloat) z, 1.0F);
815 }
816
817 void GLAPIENTRY
818 _mesa_WindowPos3fMESA(GLfloat x, GLfloat y, GLfloat z)
819 {
820 window_pos4f(x, y, z, 1.0F);
821 }
822
823 void GLAPIENTRY
824 _mesa_WindowPos3iMESA(GLint x, GLint y, GLint z)
825 {
826 window_pos4f((GLfloat) x, (GLfloat) y, (GLfloat) z, 1.0F);
827 }
828
829 void GLAPIENTRY
830 _mesa_WindowPos3sMESA(GLshort x, GLshort y, GLshort z)
831 {
832 window_pos4f(x, y, z, 1.0F);
833 }
834
835 void GLAPIENTRY
836 _mesa_WindowPos4dMESA(GLdouble x, GLdouble y, GLdouble z, GLdouble w)
837 {
838 window_pos4f((GLfloat) x, (GLfloat) y, (GLfloat) z, (GLfloat) w);
839 }
840
841 void GLAPIENTRY
842 _mesa_WindowPos4fMESA(GLfloat x, GLfloat y, GLfloat z, GLfloat w)
843 {
844 window_pos4f(x, y, z, w);
845 }
846
847 void GLAPIENTRY
848 _mesa_WindowPos4iMESA(GLint x, GLint y, GLint z, GLint w)
849 {
850 window_pos4f((GLfloat) x, (GLfloat) y, (GLfloat) z, (GLfloat) w);
851 }
852
853 void GLAPIENTRY
854 _mesa_WindowPos4sMESA(GLshort x, GLshort y, GLshort z, GLshort w)
855 {
856 window_pos4f(x, y, z, w);
857 }
858
859 void GLAPIENTRY
860 _mesa_WindowPos2dvMESA(const GLdouble *v)
861 {
862 window_pos4f((GLfloat) v[0], (GLfloat) v[1], 0.0F, 1.0F);
863 }
864
865 void GLAPIENTRY
866 _mesa_WindowPos2fvMESA(const GLfloat *v)
867 {
868 window_pos4f(v[0], v[1], 0.0F, 1.0F);
869 }
870
871 void GLAPIENTRY
872 _mesa_WindowPos2ivMESA(const GLint *v)
873 {
874 window_pos4f((GLfloat) v[0], (GLfloat) v[1], 0.0F, 1.0F);
875 }
876
877 void GLAPIENTRY
878 _mesa_WindowPos2svMESA(const GLshort *v)
879 {
880 window_pos4f(v[0], v[1], 0.0F, 1.0F);
881 }
882
883 void GLAPIENTRY
884 _mesa_WindowPos3dvMESA(const GLdouble *v)
885 {
886 window_pos4f((GLfloat) v[0], (GLfloat) v[1], (GLfloat) v[2], 1.0F);
887 }
888
889 void GLAPIENTRY
890 _mesa_WindowPos3fvMESA(const GLfloat *v)
891 {
892 window_pos4f(v[0], v[1], v[2], 1.0);
893 }
894
895 void GLAPIENTRY
896 _mesa_WindowPos3ivMESA(const GLint *v)
897 {
898 window_pos4f((GLfloat) v[0], (GLfloat) v[1], (GLfloat) v[2], 1.0F);
899 }
900
901 void GLAPIENTRY
902 _mesa_WindowPos3svMESA(const GLshort *v)
903 {
904 window_pos4f(v[0], v[1], v[2], 1.0F);
905 }
906
907 void GLAPIENTRY
908 _mesa_WindowPos4dvMESA(const GLdouble *v)
909 {
910 window_pos4f((GLfloat) v[0], (GLfloat) v[1],
911 (GLfloat) v[2], (GLfloat) v[3]);
912 }
913
914 void GLAPIENTRY
915 _mesa_WindowPos4fvMESA(const GLfloat *v)
916 {
917 window_pos4f(v[0], v[1], v[2], v[3]);
918 }
919
920 void GLAPIENTRY
921 _mesa_WindowPos4ivMESA(const GLint *v)
922 {
923 window_pos4f((GLfloat) v[0], (GLfloat) v[1],
924 (GLfloat) v[2], (GLfloat) v[3]);
925 }
926
927 void GLAPIENTRY
928 _mesa_WindowPos4svMESA(const GLshort *v)
929 {
930 window_pos4f(v[0], v[1], v[2], v[3]);
931 }
932
933 #endif
934
935 #if 0
936
937 /*
938 * OpenGL implementation of glWindowPos*MESA()
939 */
940 void glWindowPos4fMESA( GLfloat x, GLfloat y, GLfloat z, GLfloat w )
941 {
942 GLfloat fx, fy;
943
944 /* Push current matrix mode and viewport attributes */
945 glPushAttrib( GL_TRANSFORM_BIT | GL_VIEWPORT_BIT );
946
947 /* Setup projection parameters */
948 glMatrixMode( GL_PROJECTION );
949 glPushMatrix();
950 glLoadIdentity();
951 glMatrixMode( GL_MODELVIEW );
952 glPushMatrix();
953 glLoadIdentity();
954
955 glDepthRange( z, z );
956 glViewport( (int) x - 1, (int) y - 1, 2, 2 );
957
958 /* set the raster (window) position */
959 fx = x - (int) x;
960 fy = y - (int) y;
961 glRasterPos4f( fx, fy, 0.0, w );
962
963 /* restore matrices, viewport and matrix mode */
964 glPopMatrix();
965 glMatrixMode( GL_PROJECTION );
966 glPopMatrix();
967
968 glPopAttrib();
969 }
970
971 #endif
972
973
974 /**********************************************************************/
975 /** \name Initialization */
976 /**********************************************************************/
977 /*@{*/
978
979 /**
980 * Initialize the context current raster position information.
981 *
982 * \param ctx GL context.
983 *
984 * Initialize the current raster position information in
985 * __GLcontextRec::Current, and adds the extension entry points to the
986 * dispatcher.
987 */
988 void _mesa_init_rastpos( GLcontext * ctx )
989 {
990 int i;
991
992 ASSIGN_4V( ctx->Current.RasterPos, 0.0, 0.0, 0.0, 1.0 );
993 ctx->Current.RasterDistance = 0.0;
994 ASSIGN_4V( ctx->Current.RasterColor, 1.0, 1.0, 1.0, 1.0 );
995 ctx->Current.RasterIndex = 1.0;
996 for (i=0; i<MAX_TEXTURE_UNITS; i++)
997 ASSIGN_4V( ctx->Current.RasterTexCoords[i], 0.0, 0.0, 0.0, 1.0 );
998 ctx->Current.RasterPosValid = GL_TRUE;
999 }
1000
1001 /*@}*/