Cleaning up
[mesa.git] / src / mesa / main / rastpos.c
1 /* $Id: rastpos.c,v 1.1 1999/08/19 00:55:41 jtg Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 3.1
6 *
7 * Copyright (C) 1999 Brian Paul All Rights Reserved.
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 */
26
27
28
29
30
31 #ifdef PC_HEADER
32 #include "all.h"
33 #else
34 #include <assert.h>
35 #include "clip.h"
36 #include "feedback.h"
37 #include "light.h"
38 #include "macros.h"
39 #include "matrix.h"
40 #include "mmath.h"
41 #include "shade.h"
42 #include "types.h"
43 #include "xform.h"
44 #include "context.h"
45 #ifdef XFree86Server
46 #include "GL/xf86glx.h"
47 #endif
48 #endif
49
50
51
52 /*
53 * Caller: context->API.RasterPos4f
54 */
55 void gl_RasterPos4f( GLcontext *ctx,
56 GLfloat x, GLfloat y, GLfloat z, GLfloat w )
57 {
58 GLfloat v[4], eye[4], clip[4], ndc[3], d;
59
60 /* KW: Added this test, which is in the spec. We can't do this
61 * outside begin/end any more because the ctx->Current values
62 * aren't uptodate during that period.
63 */
64 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH( ctx, "glRasterPos" );
65
66 if (ctx->NewState)
67 gl_update_state( ctx );
68
69 ASSIGN_4V( v, x, y, z, w );
70 TRANSFORM_POINT( eye, ctx->ModelView.m, v );
71
72 /* raster color */
73 if (ctx->Light.Enabled)
74 {
75 /*GLfloat *vert;*/
76 GLfloat *norm, eyenorm[3];
77 GLfloat *objnorm = ctx->Current.Normal;
78
79 /* Not needed???
80 vert = (ctx->NeedEyeCoords ? eye : v);
81 */
82
83 if (ctx->NeedEyeNormals) {
84 GLfloat *inv = ctx->ModelView.inv;
85 TRANSFORM_NORMAL( eyenorm, objnorm, inv );
86 norm = eyenorm;
87 } else {
88 norm = objnorm;
89 }
90
91 gl_shade_rastpos( ctx, v, norm,
92 ctx->Current.RasterColor,
93 &ctx->Current.RasterIndex );
94
95 }
96 else {
97 /* use current color or index */
98 if (ctx->Visual->RGBAflag) {
99 UBYTE_RGBA_TO_FLOAT_RGBA(ctx->Current.RasterColor,
100 ctx->Current.ByteColor);
101 }
102 else {
103 ctx->Current.RasterIndex = ctx->Current.Index;
104 }
105 }
106
107 /* compute raster distance */
108 ctx->Current.RasterDistance =
109 GL_SQRT( eye[0]*eye[0] + eye[1]*eye[1] + eye[2]*eye[2] );
110
111 /* apply projection matrix: clip = Proj * eye */
112 TRANSFORM_POINT( clip, ctx->ProjectionMatrix.m, eye );
113
114 /* clip to view volume */
115 if (gl_viewclip_point( clip )==0) {
116 ctx->Current.RasterPosValid = GL_FALSE;
117 return;
118 }
119
120 /* clip to user clipping planes */
121 if ( ctx->Transform.AnyClip &&
122 gl_userclip_point(ctx, clip) == 0)
123 {
124 ctx->Current.RasterPosValid = GL_FALSE;
125 return;
126 }
127
128 /* ndc = clip / W */
129 ASSERT( clip[3]!=0.0 );
130 d = 1.0F / clip[3];
131 ndc[0] = clip[0] * d;
132 ndc[1] = clip[1] * d;
133 ndc[2] = clip[2] * d;
134
135 ctx->Current.RasterPos[0] = (ndc[0] * ctx->Viewport.WindowMap.m[MAT_SX] +
136 ctx->Viewport.WindowMap.m[MAT_TX]);
137 ctx->Current.RasterPos[1] = (ndc[1] * ctx->Viewport.WindowMap.m[MAT_SY] +
138 ctx->Viewport.WindowMap.m[MAT_TY]);
139 ctx->Current.RasterPos[2] = (ndc[2] * ctx->Viewport.WindowMap.m[MAT_SZ] +
140 ctx->Viewport.WindowMap.m[MAT_TZ]) / DEPTH_SCALE;
141 ctx->Current.RasterPos[3] = clip[3];
142 ctx->Current.RasterPosValid = GL_TRUE;
143
144 /* FOG??? */
145
146 {
147 GLuint texSet;
148 for (texSet=0; texSet<MAX_TEXTURE_UNITS; texSet++) {
149 COPY_4FV( ctx->Current.RasterMultiTexCoord[texSet],
150 ctx->Current.Texcoord[texSet] );
151 }
152 }
153
154 if (ctx->RenderMode==GL_SELECT) {
155 gl_update_hitflag( ctx, ctx->Current.RasterPos[2] );
156 }
157
158 }
159
160
161
162 /*
163 * This is a MESA extension function. Pretty much just like glRasterPos
164 * except we don't apply the modelview or projection matrices; specify a
165 * window coordinate directly.
166 * Caller: context->API.WindowPos4fMESA pointer.
167 */
168 void gl_windowpos( GLcontext *ctx, GLfloat x, GLfloat y, GLfloat z, GLfloat w )
169 {
170 /* KW: Assume that like rasterpos, this must be outside begin/end.
171 */
172 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH( ctx, "glWindowPosMESA" );
173
174 /* set raster position */
175 ctx->Current.RasterPos[0] = x;
176 ctx->Current.RasterPos[1] = y;
177 ctx->Current.RasterPos[2] = CLAMP( z, 0.0F, 1.0F );
178 ctx->Current.RasterPos[3] = w;
179
180 ctx->Current.RasterPosValid = GL_TRUE;
181
182 /* raster color */
183 if (0 && ctx->Light.Enabled) {
184
185 /* KW: I don't see how this can work - would have to take the
186 * inverse of the projection matrix or the combined
187 * modelProjection matrix, transform point and normal, and
188 * do the lighting. Those inverses are not used for
189 * anything else. This is not an object-space lighting
190 * issue - what this is trying to do is something like
191 * clip-space or window-space lighting...
192 *
193 * Anyway, since the implementation was never correct, I'm
194 * not fixing it now - just use the unlit color.
195 */
196
197 /* KW: As a reprise, we now *do* keep the inverse of the projection
198 * matrix, so it is not infeasible to try to swim up stream
199 * in this manner. I still don't want to implement it,
200 * however.
201 */
202 }
203 else {
204 /* use current color or index */
205 if (ctx->Visual->RGBAflag) {
206 UBYTE_RGBA_TO_FLOAT_RGBA(ctx->Current.RasterColor,
207 ctx->Current.ByteColor);
208 }
209 else {
210 ctx->Current.RasterIndex = ctx->Current.Index;
211 }
212 }
213
214 ctx->Current.RasterDistance = 0.0;
215
216 {
217 GLuint texSet;
218 for (texSet=0; texSet<MAX_TEXTURE_UNITS; texSet++) {
219 COPY_4FV( ctx->Current.RasterMultiTexCoord[texSet],
220 ctx->Current.Texcoord[texSet] );
221 }
222 }
223
224 if (ctx->RenderMode==GL_SELECT) {
225 gl_update_hitflag( ctx, ctx->Current.RasterPos[2] );
226 }
227 }