Merge vtx-0-2-branch
[mesa.git] / src / mesa / swrast_setup / ss_tritmp.h
1
2 /*
3 * Mesa 3-D graphics library
4 * Version: 5.0
5 *
6 * Copyright (C) 1999-2002 Brian Paul All Rights Reserved.
7 *
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:
14 *
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
17 *
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.
24 *
25 * Authors:
26 * Keith Whitwell <keith@tungstengraphics.com>
27 */
28
29
30 static void TAG(triangle)(GLcontext *ctx, GLuint e0, GLuint e1, GLuint e2 )
31 {
32 struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
33 SWvertex *verts = SWSETUP_CONTEXT(ctx)->verts;
34 SWvertex *v[3];
35 GLfloat z[3];
36 GLfloat offset;
37 GLenum mode = GL_FILL;
38 GLuint facing = 0;
39
40 v[0] = &verts[e0];
41 v[1] = &verts[e1];
42 v[2] = &verts[e2];
43
44
45 if (IND & (SS_TWOSIDE_BIT | SS_OFFSET_BIT | SS_UNFILLED_BIT))
46 {
47 GLfloat ex = v[0]->win[0] - v[2]->win[0];
48 GLfloat ey = v[0]->win[1] - v[2]->win[1];
49 GLfloat fx = v[1]->win[0] - v[2]->win[0];
50 GLfloat fy = v[1]->win[1] - v[2]->win[1];
51 GLfloat cc = ex*fy - ey*fx;
52
53 if (IND & (SS_TWOSIDE_BIT | SS_UNFILLED_BIT))
54 {
55 facing = (cc < 0.0) ^ ctx->Polygon._FrontBit;
56 if (ctx->Stencil.TestTwoSide)
57 ctx->_Facing = facing; /* for 2-sided stencil test */
58
59 if (IND & SS_UNFILLED_BIT)
60 mode = facing ? ctx->Polygon.BackMode : ctx->Polygon.FrontMode;
61
62 if (facing == 1) {
63 if (IND & SS_TWOSIDE_BIT) {
64 if (IND & SS_RGBA_BIT) {
65 GLfloat (*vbcolor)[4] = VB->ColorPtr[1]->data;
66 SS_COLOR(v[0]->color, vbcolor[e0]);
67 SS_COLOR(v[1]->color, vbcolor[e1]);
68 SS_COLOR(v[2]->color, vbcolor[e2]);
69 if (VB->SecondaryColorPtr[1]) {
70 GLfloat (*vbspec)[4] = VB->SecondaryColorPtr[1]->data;
71 SS_SPEC(v[0]->specular, vbspec[e0]);
72 SS_SPEC(v[1]->specular, vbspec[e1]);
73 SS_SPEC(v[2]->specular, vbspec[e2]);
74 }
75 } else {
76 GLfloat *vbindex = (GLfloat *)VB->IndexPtr[1]->data;
77 SS_IND(v[0]->index, vbindex[e0]);
78 SS_IND(v[1]->index, vbindex[e1]);
79 SS_IND(v[2]->index, vbindex[e2]);
80 }
81 }
82 }
83 }
84
85 if (IND & SS_OFFSET_BIT)
86 {
87 offset = ctx->Polygon.OffsetUnits;
88 z[0] = v[0]->win[2];
89 z[1] = v[1]->win[2];
90 z[2] = v[2]->win[2];
91 if (cc * cc > 1e-16) {
92 GLfloat ez = z[0] - z[2];
93 GLfloat fz = z[1] - z[2];
94 GLfloat a = ey*fz - ez*fy;
95 GLfloat b = ez*fx - ex*fz;
96 GLfloat ic = 1.0F / cc;
97 GLfloat ac = a * ic;
98 GLfloat bc = b * ic;
99 if (ac < 0.0F) ac = -ac;
100 if (bc < 0.0F) bc = -bc;
101 offset += MAX2(ac, bc) * ctx->Polygon.OffsetFactor;
102 }
103 offset *= ctx->MRD;
104 /*printf("offset %g\n", offset);*/
105 }
106 }
107
108 if (mode == GL_POINT) {
109 if ((IND & SS_OFFSET_BIT) && ctx->Polygon.OffsetPoint) {
110 v[0]->win[2] += offset;
111 v[1]->win[2] += offset;
112 v[2]->win[2] += offset;
113 }
114 _swsetup_render_point_tri( ctx, e0, e1, e2, facing );
115 } else if (mode == GL_LINE) {
116 if ((IND & SS_OFFSET_BIT) && ctx->Polygon.OffsetLine) {
117 v[0]->win[2] += offset;
118 v[1]->win[2] += offset;
119 v[2]->win[2] += offset;
120 }
121 _swsetup_render_line_tri( ctx, e0, e1, e2, facing );
122 } else {
123 if ((IND & SS_OFFSET_BIT) && ctx->Polygon.OffsetFill) {
124 v[0]->win[2] += offset;
125 v[1]->win[2] += offset;
126 v[2]->win[2] += offset;
127 }
128 _swrast_Triangle( ctx, v[0], v[1], v[2] );
129 }
130
131 if (IND & SS_OFFSET_BIT) {
132 v[0]->win[2] = z[0];
133 v[1]->win[2] = z[1];
134 v[2]->win[2] = z[2];
135 }
136
137 if (IND & SS_TWOSIDE_BIT) {
138 if (facing == 1) {
139 if (IND & SS_RGBA_BIT) {
140 GLfloat (*vbcolor)[4] = VB->ColorPtr[0]->data;
141 SS_COLOR(v[0]->color, vbcolor[e0]);
142 SS_COLOR(v[1]->color, vbcolor[e1]);
143 SS_COLOR(v[2]->color, vbcolor[e2]);
144 if (VB->SecondaryColorPtr[0]) {
145 GLfloat (*vbspec)[4] = VB->SecondaryColorPtr[0]->data;
146 SS_SPEC(v[0]->specular, vbspec[e0]);
147 SS_SPEC(v[1]->specular, vbspec[e1]);
148 SS_SPEC(v[2]->specular, vbspec[e2]);
149 }
150 } else {
151 GLfloat *vbindex = (GLfloat *)VB->IndexPtr[0]->data;
152 SS_IND(v[0]->index, vbindex[e0]);
153 SS_IND(v[1]->index, vbindex[e1]);
154 SS_IND(v[2]->index, vbindex[e2]);
155 }
156 }
157 }
158 }
159
160
161
162 /* Need to fixup edgeflags when decomposing to triangles:
163 */
164 static void TAG(quadfunc)( GLcontext *ctx, GLuint v0,
165 GLuint v1, GLuint v2, GLuint v3 )
166 {
167 if (IND & SS_UNFILLED_BIT) {
168 struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
169 GLubyte ef1 = VB->EdgeFlag[v1];
170 GLubyte ef3 = VB->EdgeFlag[v3];
171 VB->EdgeFlag[v1] = 0;
172 TAG(triangle)( ctx, v0, v1, v3 );
173 VB->EdgeFlag[v1] = ef1;
174 VB->EdgeFlag[v3] = 0;
175 TAG(triangle)( ctx, v1, v2, v3 );
176 VB->EdgeFlag[v3] = ef3;
177 } else {
178 TAG(triangle)( ctx, v0, v1, v3 );
179 TAG(triangle)( ctx, v1, v2, v3 );
180 }
181 }
182
183
184
185
186 static void TAG(init)( void )
187 {
188 tri_tab[IND] = TAG(triangle);
189 quad_tab[IND] = TAG(quadfunc);
190 }
191
192
193 #undef IND
194 #undef TAG