3 * Mesa 3-D graphics library
6 * Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 #include "math/m_vector.h"
37 #include "math/m_matrix.h"
40 #define _XFORMAPI _ASMAPI
41 #define _XFORMAPIP _ASMAPIP
48 * Transform a point (column vector) by a matrix: Q = M * P
50 #define TRANSFORM_POINT( Q, M, P ) \
51 Q[0] = M[0] * P[0] + M[4] * P[1] + M[8] * P[2] + M[12] * P[3]; \
52 Q[1] = M[1] * P[0] + M[5] * P[1] + M[9] * P[2] + M[13] * P[3]; \
53 Q[2] = M[2] * P[0] + M[6] * P[1] + M[10] * P[2] + M[14] * P[3]; \
54 Q[3] = M[3] * P[0] + M[7] * P[1] + M[11] * P[2] + M[15] * P[3];
57 #define TRANSFORM_POINT3( Q, M, P ) \
58 Q[0] = M[0] * P[0] + M[4] * P[1] + M[8] * P[2] + M[12]; \
59 Q[1] = M[1] * P[0] + M[5] * P[1] + M[9] * P[2] + M[13]; \
60 Q[2] = M[2] * P[0] + M[6] * P[1] + M[10] * P[2] + M[14]; \
61 Q[3] = M[3] * P[0] + M[7] * P[1] + M[11] * P[2] + M[15];
65 * Transform a normal (row vector) by a matrix: [NX NY NZ] = N * MAT
67 #define TRANSFORM_NORMAL( TO, N, MAT ) \
69 TO[0] = N[0] * MAT[0] + N[1] * MAT[1] + N[2] * MAT[2]; \
70 TO[1] = N[0] * MAT[4] + N[1] * MAT[5] + N[2] * MAT[6]; \
71 TO[2] = N[0] * MAT[8] + N[1] * MAT[9] + N[2] * MAT[10]; \
75 extern void _mesa_transform_vector( GLfloat u
[4],
77 CONST GLfloat m
[16] );
81 _math_init_transformation( void );
84 /* KW: Clip functions now do projective divide as well. The projected
85 * coordinates are very useful to us because they let us cull
86 * backfaces and eliminate vertices from lighting, fogging, etc
87 * calculations. Despite the fact that this divide could be done one
88 * day in hardware, we would still have a reason to want to do it here
89 * as long as those other calculations remain in software.
91 * Clipping is a convenient place to do the divide on x86 as it should be
92 * possible to overlap with integer outcode calculations.
94 * There are two cases where we wouldn't want to do the divide in cliptest:
95 * - When we aren't clipping. We still might want to cull backfaces
96 * so the divide should be done elsewhere. This currently never
99 * - When culling isn't likely to help us, such as when the GL culling
100 * is disabled and we not lighting or are only lighting
101 * one-sided. In this situation, backface determination provides
102 * us with no useful information. A tricky case to detect is when
103 * all input data is already culled, although hopefully the
104 * application wouldn't turn on culling in such cases.
106 * We supply a buffer to hold the [x/w,y/w,z/w,1/w] values which
107 * are the result of the projection. This is only used in the
108 * 4-vector case - in other cases, we just use the clip coordinates
109 * as the projected coordinates - they are identical.
111 * This is doubly convenient because it means the Win[] array is now
112 * of the same stride as all the others, so I can now turn map_vertices
113 * into a straight-forward matrix transformation, with asm acceleration
114 * automatically available.
117 /* Vertex buffer clipping flags
119 #define CLIP_RIGHT_SHIFT 0
120 #define CLIP_LEFT_SHIFT 1
121 #define CLIP_TOP_SHIFT 2
122 #define CLIP_BOTTOM_SHIFT 3
123 #define CLIP_NEAR_SHIFT 4
124 #define CLIP_FAR_SHIFT 5
126 #define CLIP_RIGHT_BIT 0x01
127 #define CLIP_LEFT_BIT 0x02
128 #define CLIP_TOP_BIT 0x04
129 #define CLIP_BOTTOM_BIT 0x08
130 #define CLIP_NEAR_BIT 0x10
131 #define CLIP_FAR_BIT 0x20
132 #define CLIP_USER_BIT 0x40
133 #define CLIP_ALL_BITS 0x3f
136 typedef GLvector4f
* (_XFORMAPIP clip_func
)( GLvector4f
*vClip
,
142 typedef void (*dotprod_func
)( GLfloat
*out
,
144 CONST GLvector4f
*coord_vec
,
145 CONST GLfloat plane
[4] );
147 typedef void (*vec_copy_func
)( GLvector4f
*to
,
148 CONST GLvector4f
*from
);
153 * Functions for transformation of normals in the VB.
155 typedef void (_NORMAPIP normal_func
)( CONST GLmatrix
*mat
,
157 CONST GLvector4f
*in
,
158 CONST GLfloat lengths
[],
162 /* Flags for selecting a normal transformation function.
164 #define NORM_RESCALE 0x1 /* apply the scale factor */
165 #define NORM_NORMALIZE 0x2 /* normalize */
166 #define NORM_TRANSFORM 0x4 /* apply the transformation matrix */
167 #define NORM_TRANSFORM_NO_ROT 0x8 /* apply the transformation matrix */
172 /* KW: New versions of the transform function allow a mask array
173 * specifying that individual vector transform should be skipped
174 * when the mask byte is zero. This is always present as a
175 * parameter, to allow a unified interface.
177 typedef void (_XFORMAPIP transform_func
)( GLvector4f
*to_vec
,
179 CONST GLvector4f
*from_vec
);
182 extern GLvector4f
*_mesa_project_points( GLvector4f
*to
,
183 CONST GLvector4f
*from
);
185 extern void _mesa_transform_bounds3( GLubyte
*orMask
, GLubyte
*andMask
,
187 CONST GLfloat src
[][3] );
189 extern void _mesa_transform_bounds2( GLubyte
*orMask
, GLubyte
*andMask
,
191 CONST GLfloat src
[][3] );
194 extern dotprod_func _mesa_dotprod_tab
[5];
195 extern vec_copy_func _mesa_copy_tab
[0x10];
196 extern vec_copy_func _mesa_copy_clean_tab
[5];
197 extern clip_func _mesa_clip_tab
[5];
198 extern clip_func _mesa_clip_np_tab
[5];
199 extern normal_func _mesa_normal_tab
[0xf];
201 /* Use of 2 layers of linked 1-dimensional arrays to reduce
204 extern transform_func
*_mesa_transform_tab
[5];
207 extern void _mesa_transform_point_sz( GLfloat Q
[4], CONST GLfloat M
[16],
208 CONST GLfloat P
[4], GLuint sz
);
211 #define TransformRaw( to, mat, from ) \
212 ( _mesa_transform_tab[(from)->size][(mat)->type]( to, (mat)->m, from ), \