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