rastpos.c revision cdc920a0
1/*
2 * Mesa 3-D graphics library
3 * Version:  6.5.3
4 *
5 * Copyright (C) 1999-2007  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 "context.h"
33#include "feedback.h"
34#include "macros.h"
35#include "rastpos.h"
36#include "state.h"
37#include "main/dispatch.h"
38
39
40#if FEATURE_rastpos
41
42
43/**
44 * Helper function for all the RasterPos functions.
45 */
46static void
47rasterpos(GLfloat x, GLfloat y, GLfloat z, GLfloat w)
48{
49   GET_CURRENT_CONTEXT(ctx);
50   GLfloat p[4];
51
52   p[0] = x;
53   p[1] = y;
54   p[2] = z;
55   p[3] = w;
56
57   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
58   FLUSH_CURRENT(ctx, 0);
59
60   if (ctx->NewState)
61      _mesa_update_state( ctx );
62
63   ctx->Driver.RasterPos(ctx, p);
64}
65
66
67static void GLAPIENTRY
68_mesa_RasterPos2d(GLdouble x, GLdouble y)
69{
70   rasterpos((GLfloat)x, (GLfloat)y, (GLfloat)0.0, (GLfloat)1.0);
71}
72
73static void GLAPIENTRY
74_mesa_RasterPos2f(GLfloat x, GLfloat y)
75{
76   rasterpos(x, y, 0.0F, 1.0F);
77}
78
79static void GLAPIENTRY
80_mesa_RasterPos2i(GLint x, GLint y)
81{
82   rasterpos((GLfloat) x, (GLfloat) y, 0.0F, 1.0F);
83}
84
85static void GLAPIENTRY
86_mesa_RasterPos2s(GLshort x, GLshort y)
87{
88   rasterpos(x, y, 0.0F, 1.0F);
89}
90
91static void GLAPIENTRY
92_mesa_RasterPos3d(GLdouble x, GLdouble y, GLdouble z)
93{
94   rasterpos((GLfloat) x, (GLfloat) y, (GLfloat) z, 1.0F);
95}
96
97static void GLAPIENTRY
98_mesa_RasterPos3f(GLfloat x, GLfloat y, GLfloat z)
99{
100   rasterpos(x, y, z, 1.0F);
101}
102
103static void GLAPIENTRY
104_mesa_RasterPos3i(GLint x, GLint y, GLint z)
105{
106   rasterpos((GLfloat) x, (GLfloat) y, (GLfloat) z, 1.0F);
107}
108
109static void GLAPIENTRY
110_mesa_RasterPos3s(GLshort x, GLshort y, GLshort z)
111{
112   rasterpos(x, y, z, 1.0F);
113}
114
115static void GLAPIENTRY
116_mesa_RasterPos4d(GLdouble x, GLdouble y, GLdouble z, GLdouble w)
117{
118   rasterpos((GLfloat) x, (GLfloat) y, (GLfloat) z, (GLfloat) w);
119}
120
121static void GLAPIENTRY
122_mesa_RasterPos4f(GLfloat x, GLfloat y, GLfloat z, GLfloat w)
123{
124   rasterpos(x, y, z, w);
125}
126
127static void GLAPIENTRY
128_mesa_RasterPos4i(GLint x, GLint y, GLint z, GLint w)
129{
130   rasterpos((GLfloat) x, (GLfloat) y, (GLfloat) z, (GLfloat) w);
131}
132
133static void GLAPIENTRY
134_mesa_RasterPos4s(GLshort x, GLshort y, GLshort z, GLshort w)
135{
136   rasterpos(x, y, z, w);
137}
138
139static void GLAPIENTRY
140_mesa_RasterPos2dv(const GLdouble *v)
141{
142   rasterpos((GLfloat) v[0], (GLfloat) v[1], 0.0F, 1.0F);
143}
144
145static void GLAPIENTRY
146_mesa_RasterPos2fv(const GLfloat *v)
147{
148   rasterpos(v[0], v[1], 0.0F, 1.0F);
149}
150
151static void GLAPIENTRY
152_mesa_RasterPos2iv(const GLint *v)
153{
154   rasterpos((GLfloat) v[0], (GLfloat) v[1], 0.0F, 1.0F);
155}
156
157static void GLAPIENTRY
158_mesa_RasterPos2sv(const GLshort *v)
159{
160   rasterpos(v[0], v[1], 0.0F, 1.0F);
161}
162
163static void GLAPIENTRY
164_mesa_RasterPos3dv(const GLdouble *v)
165{
166   rasterpos((GLfloat) v[0], (GLfloat) v[1], (GLfloat) v[2], 1.0F);
167}
168
169static void GLAPIENTRY
170_mesa_RasterPos3fv(const GLfloat *v)
171{
172   rasterpos(v[0], v[1], v[2], 1.0F);
173}
174
175static void GLAPIENTRY
176_mesa_RasterPos3iv(const GLint *v)
177{
178   rasterpos((GLfloat) v[0], (GLfloat) v[1], (GLfloat) v[2], 1.0F);
179}
180
181static void GLAPIENTRY
182_mesa_RasterPos3sv(const GLshort *v)
183{
184   rasterpos(v[0], v[1], v[2], 1.0F);
185}
186
187static void GLAPIENTRY
188_mesa_RasterPos4dv(const GLdouble *v)
189{
190   rasterpos((GLfloat) v[0], (GLfloat) v[1],
191		     (GLfloat) v[2], (GLfloat) v[3]);
192}
193
194static void GLAPIENTRY
195_mesa_RasterPos4fv(const GLfloat *v)
196{
197   rasterpos(v[0], v[1], v[2], v[3]);
198}
199
200static void GLAPIENTRY
201_mesa_RasterPos4iv(const GLint *v)
202{
203   rasterpos((GLfloat) v[0], (GLfloat) v[1],
204		     (GLfloat) v[2], (GLfloat) v[3]);
205}
206
207static void GLAPIENTRY
208_mesa_RasterPos4sv(const GLshort *v)
209{
210   rasterpos(v[0], v[1], v[2], v[3]);
211}
212
213
214/**********************************************************************/
215/***           GL_ARB_window_pos / GL_MESA_window_pos               ***/
216/**********************************************************************/
217
218
219/**
220 * All glWindowPosMESA and glWindowPosARB commands call this function to
221 * update the current raster position.
222 */
223static void
224window_pos3f(GLfloat x, GLfloat y, GLfloat z)
225{
226   GET_CURRENT_CONTEXT(ctx);
227   GLfloat z2;
228
229   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
230   FLUSH_CURRENT(ctx, 0);
231
232   z2 = CLAMP(z, 0.0F, 1.0F) * (ctx->Viewport.Far - ctx->Viewport.Near)
233      + ctx->Viewport.Near;
234
235   /* set raster position */
236   ctx->Current.RasterPos[0] = x;
237   ctx->Current.RasterPos[1] = y;
238   ctx->Current.RasterPos[2] = z2;
239   ctx->Current.RasterPos[3] = 1.0F;
240
241   ctx->Current.RasterPosValid = GL_TRUE;
242
243   if (ctx->Fog.FogCoordinateSource == GL_FOG_COORDINATE_EXT)
244      ctx->Current.RasterDistance = ctx->Current.Attrib[VERT_ATTRIB_FOG][0];
245   else
246      ctx->Current.RasterDistance = 0.0;
247
248   /* raster color = current color or index */
249   ctx->Current.RasterColor[0]
250      = CLAMP(ctx->Current.Attrib[VERT_ATTRIB_COLOR0][0], 0.0F, 1.0F);
251   ctx->Current.RasterColor[1]
252      = CLAMP(ctx->Current.Attrib[VERT_ATTRIB_COLOR0][1], 0.0F, 1.0F);
253   ctx->Current.RasterColor[2]
254      = CLAMP(ctx->Current.Attrib[VERT_ATTRIB_COLOR0][2], 0.0F, 1.0F);
255   ctx->Current.RasterColor[3]
256      = CLAMP(ctx->Current.Attrib[VERT_ATTRIB_COLOR0][3], 0.0F, 1.0F);
257   ctx->Current.RasterSecondaryColor[0]
258      = CLAMP(ctx->Current.Attrib[VERT_ATTRIB_COLOR1][0], 0.0F, 1.0F);
259   ctx->Current.RasterSecondaryColor[1]
260      = CLAMP(ctx->Current.Attrib[VERT_ATTRIB_COLOR1][1], 0.0F, 1.0F);
261   ctx->Current.RasterSecondaryColor[2]
262      = CLAMP(ctx->Current.Attrib[VERT_ATTRIB_COLOR1][2], 0.0F, 1.0F);
263   ctx->Current.RasterSecondaryColor[3]
264      = CLAMP(ctx->Current.Attrib[VERT_ATTRIB_COLOR1][3], 0.0F, 1.0F);
265
266   /* raster texcoord = current texcoord */
267   {
268      GLuint texSet;
269      for (texSet = 0; texSet < ctx->Const.MaxTextureCoordUnits; texSet++) {
270         assert(texSet < Elements(ctx->Current.RasterTexCoords));
271         COPY_4FV( ctx->Current.RasterTexCoords[texSet],
272                  ctx->Current.Attrib[VERT_ATTRIB_TEX0 + texSet] );
273      }
274   }
275
276   if (ctx->RenderMode==GL_SELECT) {
277      _mesa_update_hitflag( ctx, ctx->Current.RasterPos[2] );
278   }
279}
280
281
282/* This is just to support the GL_MESA_window_pos version */
283static void
284window_pos4f(GLfloat x, GLfloat y, GLfloat z, GLfloat w)
285{
286   GET_CURRENT_CONTEXT(ctx);
287   window_pos3f(x, y, z);
288   ctx->Current.RasterPos[3] = w;
289}
290
291
292static void GLAPIENTRY
293_mesa_WindowPos2dMESA(GLdouble x, GLdouble y)
294{
295   window_pos4f((GLfloat) x, (GLfloat) y, 0.0F, 1.0F);
296}
297
298static void GLAPIENTRY
299_mesa_WindowPos2fMESA(GLfloat x, GLfloat y)
300{
301   window_pos4f(x, y, 0.0F, 1.0F);
302}
303
304static void GLAPIENTRY
305_mesa_WindowPos2iMESA(GLint x, GLint y)
306{
307   window_pos4f((GLfloat) x, (GLfloat) y, 0.0F, 1.0F);
308}
309
310static void GLAPIENTRY
311_mesa_WindowPos2sMESA(GLshort x, GLshort y)
312{
313   window_pos4f(x, y, 0.0F, 1.0F);
314}
315
316static void GLAPIENTRY
317_mesa_WindowPos3dMESA(GLdouble x, GLdouble y, GLdouble z)
318{
319   window_pos4f((GLfloat) x, (GLfloat) y, (GLfloat) z, 1.0F);
320}
321
322static void GLAPIENTRY
323_mesa_WindowPos3fMESA(GLfloat x, GLfloat y, GLfloat z)
324{
325   window_pos4f(x, y, z, 1.0F);
326}
327
328static void GLAPIENTRY
329_mesa_WindowPos3iMESA(GLint x, GLint y, GLint z)
330{
331   window_pos4f((GLfloat) x, (GLfloat) y, (GLfloat) z, 1.0F);
332}
333
334static void GLAPIENTRY
335_mesa_WindowPos3sMESA(GLshort x, GLshort y, GLshort z)
336{
337   window_pos4f(x, y, z, 1.0F);
338}
339
340static void GLAPIENTRY
341_mesa_WindowPos4dMESA(GLdouble x, GLdouble y, GLdouble z, GLdouble w)
342{
343   window_pos4f((GLfloat) x, (GLfloat) y, (GLfloat) z, (GLfloat) w);
344}
345
346static void GLAPIENTRY
347_mesa_WindowPos4fMESA(GLfloat x, GLfloat y, GLfloat z, GLfloat w)
348{
349   window_pos4f(x, y, z, w);
350}
351
352static void GLAPIENTRY
353_mesa_WindowPos4iMESA(GLint x, GLint y, GLint z, GLint w)
354{
355   window_pos4f((GLfloat) x, (GLfloat) y, (GLfloat) z, (GLfloat) w);
356}
357
358static void GLAPIENTRY
359_mesa_WindowPos4sMESA(GLshort x, GLshort y, GLshort z, GLshort w)
360{
361   window_pos4f(x, y, z, w);
362}
363
364static void GLAPIENTRY
365_mesa_WindowPos2dvMESA(const GLdouble *v)
366{
367   window_pos4f((GLfloat) v[0], (GLfloat) v[1], 0.0F, 1.0F);
368}
369
370static void GLAPIENTRY
371_mesa_WindowPos2fvMESA(const GLfloat *v)
372{
373   window_pos4f(v[0], v[1], 0.0F, 1.0F);
374}
375
376static void GLAPIENTRY
377_mesa_WindowPos2ivMESA(const GLint *v)
378{
379   window_pos4f((GLfloat) v[0], (GLfloat) v[1], 0.0F, 1.0F);
380}
381
382static void GLAPIENTRY
383_mesa_WindowPos2svMESA(const GLshort *v)
384{
385   window_pos4f(v[0], v[1], 0.0F, 1.0F);
386}
387
388static void GLAPIENTRY
389_mesa_WindowPos3dvMESA(const GLdouble *v)
390{
391   window_pos4f((GLfloat) v[0], (GLfloat) v[1], (GLfloat) v[2], 1.0F);
392}
393
394static void GLAPIENTRY
395_mesa_WindowPos3fvMESA(const GLfloat *v)
396{
397   window_pos4f(v[0], v[1], v[2], 1.0);
398}
399
400static void GLAPIENTRY
401_mesa_WindowPos3ivMESA(const GLint *v)
402{
403   window_pos4f((GLfloat) v[0], (GLfloat) v[1], (GLfloat) v[2], 1.0F);
404}
405
406static void GLAPIENTRY
407_mesa_WindowPos3svMESA(const GLshort *v)
408{
409   window_pos4f(v[0], v[1], v[2], 1.0F);
410}
411
412static void GLAPIENTRY
413_mesa_WindowPos4dvMESA(const GLdouble *v)
414{
415   window_pos4f((GLfloat) v[0], (GLfloat) v[1],
416			 (GLfloat) v[2], (GLfloat) v[3]);
417}
418
419static void GLAPIENTRY
420_mesa_WindowPos4fvMESA(const GLfloat *v)
421{
422   window_pos4f(v[0], v[1], v[2], v[3]);
423}
424
425static void GLAPIENTRY
426_mesa_WindowPos4ivMESA(const GLint *v)
427{
428   window_pos4f((GLfloat) v[0], (GLfloat) v[1],
429			 (GLfloat) v[2], (GLfloat) v[3]);
430}
431
432static void GLAPIENTRY
433_mesa_WindowPos4svMESA(const GLshort *v)
434{
435   window_pos4f(v[0], v[1], v[2], v[3]);
436}
437
438
439#if 0
440
441/*
442 * OpenGL implementation of glWindowPos*MESA()
443 */
444void glWindowPos4fMESA( GLfloat x, GLfloat y, GLfloat z, GLfloat w )
445{
446   GLfloat fx, fy;
447
448   /* Push current matrix mode and viewport attributes */
449   glPushAttrib( GL_TRANSFORM_BIT | GL_VIEWPORT_BIT );
450
451   /* Setup projection parameters */
452   glMatrixMode( GL_PROJECTION );
453   glPushMatrix();
454   glLoadIdentity();
455   glMatrixMode( GL_MODELVIEW );
456   glPushMatrix();
457   glLoadIdentity();
458
459   glDepthRange( z, z );
460   glViewport( (int) x - 1, (int) y - 1, 2, 2 );
461
462   /* set the raster (window) position */
463   fx = x - (int) x;
464   fy = y - (int) y;
465   glRasterPos4f( fx, fy, 0.0, w );
466
467   /* restore matrices, viewport and matrix mode */
468   glPopMatrix();
469   glMatrixMode( GL_PROJECTION );
470   glPopMatrix();
471
472   glPopAttrib();
473}
474
475#endif
476
477
478void
479_mesa_init_rastpos_dispatch(struct _glapi_table *disp)
480{
481   SET_RasterPos2f(disp, _mesa_RasterPos2f);
482   SET_RasterPos2fv(disp, _mesa_RasterPos2fv);
483   SET_RasterPos2i(disp, _mesa_RasterPos2i);
484   SET_RasterPos2iv(disp, _mesa_RasterPos2iv);
485   SET_RasterPos2d(disp, _mesa_RasterPos2d);
486   SET_RasterPos2dv(disp, _mesa_RasterPos2dv);
487   SET_RasterPos2s(disp, _mesa_RasterPos2s);
488   SET_RasterPos2sv(disp, _mesa_RasterPos2sv);
489   SET_RasterPos3d(disp, _mesa_RasterPos3d);
490   SET_RasterPos3dv(disp, _mesa_RasterPos3dv);
491   SET_RasterPos3f(disp, _mesa_RasterPos3f);
492   SET_RasterPos3fv(disp, _mesa_RasterPos3fv);
493   SET_RasterPos3i(disp, _mesa_RasterPos3i);
494   SET_RasterPos3iv(disp, _mesa_RasterPos3iv);
495   SET_RasterPos3s(disp, _mesa_RasterPos3s);
496   SET_RasterPos3sv(disp, _mesa_RasterPos3sv);
497   SET_RasterPos4d(disp, _mesa_RasterPos4d);
498   SET_RasterPos4dv(disp, _mesa_RasterPos4dv);
499   SET_RasterPos4f(disp, _mesa_RasterPos4f);
500   SET_RasterPos4fv(disp, _mesa_RasterPos4fv);
501   SET_RasterPos4i(disp, _mesa_RasterPos4i);
502   SET_RasterPos4iv(disp, _mesa_RasterPos4iv);
503   SET_RasterPos4s(disp, _mesa_RasterPos4s);
504   SET_RasterPos4sv(disp, _mesa_RasterPos4sv);
505
506   /* 197. GL_MESA_window_pos */
507   SET_WindowPos2dMESA(disp, _mesa_WindowPos2dMESA);
508   SET_WindowPos2dvMESA(disp, _mesa_WindowPos2dvMESA);
509   SET_WindowPos2fMESA(disp, _mesa_WindowPos2fMESA);
510   SET_WindowPos2fvMESA(disp, _mesa_WindowPos2fvMESA);
511   SET_WindowPos2iMESA(disp, _mesa_WindowPos2iMESA);
512   SET_WindowPos2ivMESA(disp, _mesa_WindowPos2ivMESA);
513   SET_WindowPos2sMESA(disp, _mesa_WindowPos2sMESA);
514   SET_WindowPos2svMESA(disp, _mesa_WindowPos2svMESA);
515   SET_WindowPos3dMESA(disp, _mesa_WindowPos3dMESA);
516   SET_WindowPos3dvMESA(disp, _mesa_WindowPos3dvMESA);
517   SET_WindowPos3fMESA(disp, _mesa_WindowPos3fMESA);
518   SET_WindowPos3fvMESA(disp, _mesa_WindowPos3fvMESA);
519   SET_WindowPos3iMESA(disp, _mesa_WindowPos3iMESA);
520   SET_WindowPos3ivMESA(disp, _mesa_WindowPos3ivMESA);
521   SET_WindowPos3sMESA(disp, _mesa_WindowPos3sMESA);
522   SET_WindowPos3svMESA(disp, _mesa_WindowPos3svMESA);
523   SET_WindowPos4dMESA(disp, _mesa_WindowPos4dMESA);
524   SET_WindowPos4dvMESA(disp, _mesa_WindowPos4dvMESA);
525   SET_WindowPos4fMESA(disp, _mesa_WindowPos4fMESA);
526   SET_WindowPos4fvMESA(disp, _mesa_WindowPos4fvMESA);
527   SET_WindowPos4iMESA(disp, _mesa_WindowPos4iMESA);
528   SET_WindowPos4ivMESA(disp, _mesa_WindowPos4ivMESA);
529   SET_WindowPos4sMESA(disp, _mesa_WindowPos4sMESA);
530   SET_WindowPos4svMESA(disp, _mesa_WindowPos4svMESA);
531}
532
533
534#endif /* FEATURE_rastpos */
535
536
537/**********************************************************************/
538/** \name Initialization                                              */
539/**********************************************************************/
540/*@{*/
541
542/**
543 * Initialize the context current raster position information.
544 *
545 * \param ctx GL context.
546 *
547 * Initialize the current raster position information in
548 * __GLcontextRec::Current, and adds the extension entry points to the
549 * dispatcher.
550 */
551void _mesa_init_rastpos( GLcontext * ctx )
552{
553   int i;
554
555   ASSIGN_4V( ctx->Current.RasterPos, 0.0, 0.0, 0.0, 1.0 );
556   ctx->Current.RasterDistance = 0.0;
557   ASSIGN_4V( ctx->Current.RasterColor, 1.0, 1.0, 1.0, 1.0 );
558   ASSIGN_4V( ctx->Current.RasterSecondaryColor, 0.0, 0.0, 0.0, 1.0 );
559   for (i = 0; i < Elements(ctx->Current.RasterTexCoords); i++)
560      ASSIGN_4V( ctx->Current.RasterTexCoords[i], 0.0, 0.0, 0.0, 1.0 );
561   ctx->Current.RasterPosValid = GL_TRUE;
562}
563
564/*@}*/
565