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