Consistent copyright info (version number, date) across all files.
[mesa.git] / src / mesa / swrast_setup / ss_tritmp.h
1 /* $Id: ss_tritmp.h,v 1.9 2001/03/12 00:48:43 gareth 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 GLchan (*vbcolor)[4] = VB->ColorPtr[1]->data;
65 GLchan (*vbspec)[4] = VB->SecondaryColorPtr[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 SS_SPEC(v[0]->specular, vbspec[e0]);
70 SS_SPEC(v[1]->specular, vbspec[e1]);
71 SS_SPEC(v[2]->specular, vbspec[e2]);
72 } else {
73 GLuint *vbindex = VB->IndexPtr[1]->data;
74 SS_IND(v[0]->index, vbindex[e0]);
75 SS_IND(v[1]->index, vbindex[e1]);
76 SS_IND(v[2]->index, vbindex[e2]);
77 }
78 }
79 }
80 }
81
82 if (IND & SS_OFFSET_BIT)
83 {
84 offset = ctx->Polygon.OffsetUnits;
85 z[0] = v[0]->win[2];
86 z[1] = v[1]->win[2];
87 z[2] = v[2]->win[2];
88 if (cc * cc > 1e-16) {
89 GLfloat ez = z[0] - z[2];
90 GLfloat fz = z[1] - z[2];
91 GLfloat a = ey*fz - ez*fy;
92 GLfloat b = ez*fx - ex*fz;
93 GLfloat ic = 1.0 / cc;
94 GLfloat ac = a * ic;
95 GLfloat bc = b * ic;
96 if (ac < 0.0f) ac = -ac;
97 if (bc < 0.0f) bc = -bc;
98 offset += MAX2(ac, bc) * ctx->Polygon.OffsetFactor;
99 }
100 offset *= ctx->MRD;
101 /*printf("offset %g\n", offset);*/
102 }
103 }
104
105 if (mode == GL_POINT) {
106 if ((IND & SS_OFFSET_BIT) && ctx->Polygon.OffsetPoint) {
107 v[0]->win[2] += offset;
108 v[1]->win[2] += offset;
109 v[2]->win[2] += offset;
110 }
111 _swsetup_render_point_tri( ctx, e0, e1, e2 );
112 } else if (mode == GL_LINE) {
113 if ((IND & SS_OFFSET_BIT) && ctx->Polygon.OffsetLine) {
114 v[0]->win[2] += offset;
115 v[1]->win[2] += offset;
116 v[2]->win[2] += offset;
117 }
118 _swsetup_render_line_tri( ctx, e0, e1, e2 );
119 } else {
120 if ((IND & SS_OFFSET_BIT) && ctx->Polygon.OffsetFill) {
121 v[0]->win[2] += offset;
122 v[1]->win[2] += offset;
123 v[2]->win[2] += offset;
124 }
125 _swrast_Triangle( ctx, v[0], v[1], v[2] );
126 }
127
128 if (IND & SS_OFFSET_BIT) {
129 v[0]->win[2] = z[0];
130 v[1]->win[2] = z[1];
131 v[2]->win[2] = z[2];
132 }
133
134 if (IND & SS_TWOSIDE_BIT) {
135 if (facing == 1) {
136 if (IND & SS_RGBA_BIT) {
137 GLchan (*vbcolor)[4] = VB->ColorPtr[0]->data;
138 GLchan (*vbspec)[4] = VB->SecondaryColorPtr[0]->data;
139 SS_COLOR(v[0]->color, vbcolor[e0]);
140 SS_COLOR(v[1]->color, vbcolor[e1]);
141 SS_COLOR(v[2]->color, vbcolor[e2]);
142 SS_SPEC(v[0]->specular, vbspec[e0]);
143 SS_SPEC(v[1]->specular, vbspec[e1]);
144 SS_SPEC(v[2]->specular, vbspec[e2]);
145 } else {
146 GLuint *vbindex = VB->IndexPtr[0]->data;
147 SS_IND(v[0]->index, vbindex[e0]);
148 SS_IND(v[1]->index, vbindex[e1]);
149 SS_IND(v[2]->index, vbindex[e2]);
150 }
151 }
152 }
153 }
154
155
156
157 /* Need to do something with edgeflags:
158 */
159 static void TAG(quad)( GLcontext *ctx, GLuint v0,
160 GLuint v1, GLuint v2, GLuint v3 )
161 {
162 if (IND & SS_UNFILLED_BIT) {
163 struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
164 GLubyte ef1 = VB->EdgeFlag[v1];
165 GLubyte ef3 = VB->EdgeFlag[v3];
166 VB->EdgeFlag[v1] = 0;
167 TAG(triangle)( ctx, v0, v1, v3 );
168 VB->EdgeFlag[v1] = ef1;
169 VB->EdgeFlag[v3] = 0;
170 TAG(triangle)( ctx, v1, v2, v3 );
171 VB->EdgeFlag[v3] = ef3;
172 } else {
173 TAG(triangle)( ctx, v0, v1, v3 );
174 TAG(triangle)( ctx, v1, v2, v3 );
175 }
176 }
177
178
179
180
181 static void TAG(init)( void )
182 {
183 tri_tab[IND] = TAG(triangle);
184 quad_tab[IND] = TAG(quad);
185 }
186
187
188 #undef IND
189 #undef TAG