1848b8605Smrg/*
2848b8605Smrg * Mesa 3-D graphics library
3848b8605Smrg *
4848b8605Smrg * Copyright (C) 1999-2008  Brian Paul   All Rights Reserved.
5848b8605Smrg *
6848b8605Smrg * Permission is hereby granted, free of charge, to any person obtaining a
7848b8605Smrg * copy of this software and associated documentation files (the "Software"),
8848b8605Smrg * to deal in the Software without restriction, including without limitation
9848b8605Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10848b8605Smrg * and/or sell copies of the Software, and to permit persons to whom the
11848b8605Smrg * Software is furnished to do so, subject to the following conditions:
12848b8605Smrg *
13848b8605Smrg * The above copyright notice and this permission notice shall be included
14848b8605Smrg * in all copies or substantial portions of the Software.
15848b8605Smrg *
16848b8605Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17848b8605Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18848b8605Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19848b8605Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20848b8605Smrg * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21848b8605Smrg * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22848b8605Smrg * OTHER DEALINGS IN THE SOFTWARE.
23848b8605Smrg */
24848b8605Smrg
25848b8605Smrg
26848b8605Smrg#ifndef _M_XFORM_H
27848b8605Smrg#define _M_XFORM_H
28848b8605Smrg
29848b8605Smrg
30848b8605Smrg#include "main/compiler.h"
31848b8605Smrg#include "main/glheader.h"
32848b8605Smrg#include "math/m_matrix.h"
33848b8605Smrg#include "math/m_vector.h"
34848b8605Smrg
35848b8605Smrg
36848b8605Smrgextern void
37848b8605Smrg_math_init_transformation(void);
38848b8605Smrgextern void
39848b8605Smrginit_c_cliptest(void);
40848b8605Smrg
41848b8605Smrg/* KW: Clip functions now do projective divide as well.  The projected
42848b8605Smrg * coordinates are very useful to us because they let us cull
43848b8605Smrg * backfaces and eliminate vertices from lighting, fogging, etc
44848b8605Smrg * calculations.  Despite the fact that this divide could be done one
45848b8605Smrg * day in hardware, we would still have a reason to want to do it here
46848b8605Smrg * as long as those other calculations remain in software.
47848b8605Smrg *
48848b8605Smrg * Clipping is a convenient place to do the divide on x86 as it should be
49848b8605Smrg * possible to overlap with integer outcode calculations.
50848b8605Smrg *
51848b8605Smrg * There are two cases where we wouldn't want to do the divide in cliptest:
52848b8605Smrg *    - When we aren't clipping.  We still might want to cull backfaces
53848b8605Smrg *      so the divide should be done elsewhere.  This currently never
54848b8605Smrg *      happens.
55848b8605Smrg *
56848b8605Smrg *    - When culling isn't likely to help us, such as when the GL culling
57848b8605Smrg *      is disabled and we not lighting or are only lighting
58848b8605Smrg *      one-sided.  In this situation, backface determination provides
59848b8605Smrg *      us with no useful information.  A tricky case to detect is when
60848b8605Smrg *      all input data is already culled, although hopefully the
61848b8605Smrg *      application wouldn't turn on culling in such cases.
62848b8605Smrg *
63848b8605Smrg * We supply a buffer to hold the [x/w,y/w,z/w,1/w] values which
64848b8605Smrg * are the result of the projection.  This is only used in the
65848b8605Smrg * 4-vector case - in other cases, we just use the clip coordinates
66848b8605Smrg * as the projected coordinates - they are identical.
67848b8605Smrg *
68848b8605Smrg * This is doubly convenient because it means the Win[] array is now
69848b8605Smrg * of the same stride as all the others, so I can now turn map_vertices
70848b8605Smrg * into a straight-forward matrix transformation, with asm acceleration
71848b8605Smrg * automatically available.
72848b8605Smrg */
73848b8605Smrg
74848b8605Smrg/* Vertex buffer clipping flags
75848b8605Smrg */
76848b8605Smrg#define CLIP_RIGHT_SHIFT 	0
77848b8605Smrg#define CLIP_LEFT_SHIFT 	1
78848b8605Smrg#define CLIP_TOP_SHIFT  	2
79848b8605Smrg#define CLIP_BOTTOM_SHIFT       3
80848b8605Smrg#define CLIP_NEAR_SHIFT  	4
81848b8605Smrg#define CLIP_FAR_SHIFT  	5
82848b8605Smrg
83848b8605Smrg#define CLIP_RIGHT_BIT   0x01
84848b8605Smrg#define CLIP_LEFT_BIT    0x02
85848b8605Smrg#define CLIP_TOP_BIT     0x04
86848b8605Smrg#define CLIP_BOTTOM_BIT  0x08
87848b8605Smrg#define CLIP_NEAR_BIT    0x10
88848b8605Smrg#define CLIP_FAR_BIT     0x20
89848b8605Smrg#define CLIP_USER_BIT    0x40
90848b8605Smrg#define CLIP_CULL_BIT    0x80
91848b8605Smrg#define CLIP_FRUSTUM_BITS    0x3f
92848b8605Smrg
93848b8605Smrg
94b8e80941Smrgtypedef GLvector4f * (*clip_func)(GLvector4f *vClip,
95b8e80941Smrg                                  GLvector4f *vProj,
96b8e80941Smrg                                  GLubyte clipMask[],
97b8e80941Smrg                                  GLubyte *orMask,
98b8e80941Smrg                                  GLubyte *andMask,
99b8e80941Smrg                                  GLboolean viewport_z_clip);
100848b8605Smrg
101848b8605Smrgtypedef void (*dotprod_func)( GLfloat *out,
102848b8605Smrg			      GLuint out_stride,
103848b8605Smrg			      const GLvector4f *coord_vec,
104848b8605Smrg			      const GLfloat plane[4] );
105848b8605Smrg
106848b8605Smrgtypedef void (*vec_copy_func)( GLvector4f *to,
107848b8605Smrg			       const GLvector4f *from );
108848b8605Smrg
109848b8605Smrg
110848b8605Smrg
111848b8605Smrg/*
112848b8605Smrg * Functions for transformation of normals in the VB.
113848b8605Smrg */
114b8e80941Smrgtypedef void (*normal_func)(const GLmatrix *mat,
115b8e80941Smrg                            GLfloat scale,
116b8e80941Smrg                            const GLvector4f *in,
117b8e80941Smrg                            const GLfloat lengths[],
118b8e80941Smrg                            GLvector4f *dest);
119848b8605Smrg
120848b8605Smrg
121848b8605Smrg/* Flags for selecting a normal transformation function.
122848b8605Smrg */
123848b8605Smrg#define NORM_RESCALE   0x1		/* apply the scale factor */
124848b8605Smrg#define NORM_NORMALIZE 0x2		/* normalize */
125848b8605Smrg#define NORM_TRANSFORM 0x4		/* apply the transformation matrix */
126848b8605Smrg#define NORM_TRANSFORM_NO_ROT 0x8	/* apply the transformation matrix */
127848b8605Smrg
128848b8605Smrg
129848b8605Smrg
130848b8605Smrg
131848b8605Smrg/* KW: New versions of the transform function allow a mask array
132848b8605Smrg *     specifying that individual vector transform should be skipped
133848b8605Smrg *     when the mask byte is zero.  This is always present as a
134848b8605Smrg *     parameter, to allow a unified interface.
135848b8605Smrg */
136b8e80941Smrgtypedef void (*transform_func)(GLvector4f *to_vec,
137b8e80941Smrg                               const GLfloat m[16],
138b8e80941Smrg                               const GLvector4f *from_vec);
139848b8605Smrg
140848b8605Smrg
141848b8605Smrgextern dotprod_func  _mesa_dotprod_tab[5];
142848b8605Smrgextern vec_copy_func _mesa_copy_tab[0x10];
143848b8605Smrgextern vec_copy_func _mesa_copy_clean_tab[5];
144848b8605Smrgextern clip_func     _mesa_clip_tab[5];
145848b8605Smrgextern clip_func     _mesa_clip_np_tab[5];
146848b8605Smrgextern normal_func   _mesa_normal_tab[0xf];
147848b8605Smrg
148848b8605Smrg/* Use of 2 layers of linked 1-dimensional arrays to reduce
149848b8605Smrg * cost of lookup.
150848b8605Smrg */
151848b8605Smrgextern transform_func *_mesa_transform_tab[5];
152848b8605Smrg
153848b8605Smrg
154848b8605Smrg
155848b8605Smrg#define TransformRaw( to, mat, from ) \
156848b8605Smrg   ( _mesa_transform_tab[(from)->size][(mat)->type]( to, (mat)->m, from ), \
157848b8605Smrg     (to) )
158848b8605Smrg
159848b8605Smrg
160848b8605Smrg#endif
161