File: SWTri_DrawTriangle.cpp

package info (click to toggle)
libtuxcap 1.4.0.dfsg2-1.1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 4,172 kB
  • ctags: 5,897
  • sloc: cpp: 43,203; ansic: 3,095; python: 774; objc: 242; makefile: 100; xml: 87
file content (276 lines) | stat: -rw-r--r-- 7,070 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// This file is included by SWTri.cpp and should not be built directly by the project.

#if (TRI0==0) 
	#define PTYPE uint32_t
	#define PIXEL_INCLUDE "SWTri_Pixel8888.cpp"
	#define NAME0 8888
#elif (TRI0==1)
	#define PTYPE uint32_t
	#define PIXEL_INCLUDE "SWTri_Pixel888.cpp"
	#define NAME0 0888
#elif (TRI0==2)
	#define PTYPE unsigned short
	#define PIXEL_INCLUDE "SWTri_Pixel565.cpp"
	#define NAME0 0565
#elif (TRI0==3)
	#define PTYPE unsigned short
	#define PIXEL_INCLUDE "SWTri_Pixel555.cpp"
	#define NAME0 0555
#endif

#if (TRI1==1)
	#define TEXTURED
	#define NAME1 TEX1
#else
	#define NAME1 TEX0
#endif


#if (TRI2==1)
	#define TEX_ALPHA
	#define NAME2 TALPHA1
#else
	#define NAME2 TALPHA0
#endif

#if (TRI3==1)
	#define MOD_ARGB
	#define NAME3 MOD1
#else
	#define NAME3 MOD0
#endif

#if (TRI4==1)
	#define GLOBAL_ARGB
	#define NAME4 GLOB1
#else
	#define NAME4 GLOB0
#endif

#if (TRI5==1)
	#define LINEAR_BLEND
	#define NAME5 BLEND1
#else
	#define NAME5 BLEND0
#endif

#define funcname2(t0,t1,t2,t3,t4,t5) DrawTriangle_##t0##_##t1##_##t2##_##t3##_##t4##_##t5
#define funcname1(t0,t1,t2,t3,t4,t5) funcname2(t0,t1,t2,t3,t4,t5)
#define funcname funcname1(NAME0,NAME1,NAME2,NAME3,NAME4,NAME5)
void	Sexy::funcname(SWHelper::SWVertex * pVerts, void * pFrameBuffer, const unsigned int bytepitch, const SWHelper::SWTextureInfo * textureInfo, SWHelper::SWDiffuse & globalDiffuse)
{
	const int pitch = bytepitch/sizeof(PTYPE);
	const int tex_pitch = textureInfo->pitch;
	const int tex_height = textureInfo->height;
	const unsigned int tex_endpos = textureInfo->endpos;

	const int64_t bigOne = static_cast<int64_t>(1) << 48;
	#define  swap(a,b,type) {type tmp = a; a = b; b = tmp;}

	const unsigned int *	pTexture;
	unsigned int		vShift, uMask, vMask;
	
	#if defined(TEXTURED)
	{
		pTexture = textureInfo->pTexture;
		vShift = textureInfo->vShift;
		vMask = textureInfo->vMask;
		uMask = textureInfo->uMask;
	}
	#endif

	// Sort vertices by Y component

	SWHelper::SWVertex *	v0 = pVerts+0;
	SWHelper::SWVertex *	v1 = pVerts+1;
	SWHelper::SWVertex *	v2 = pVerts+2;

	if (v0->y > v1->y) swap(v0,v1,SWHelper::SWVertex *);
	if (v1->y > v2->y) swap(v1,v2,SWHelper::SWVertex *);
	if (v0->y > v1->y) swap(v0,v1,SWHelper::SWVertex *);

	#if defined(MOD_ARGB) && defined(GLOBAL_ARGB)
	{
		v0->a = (v0->a * globalDiffuse.a) >> 8;
		v0->r = (v0->r * globalDiffuse.r) >> 8;
		v0->g = (v0->g * globalDiffuse.g) >> 8;
		v0->b = (v0->b * globalDiffuse.b) >> 8;
		v1->a = (v1->a * globalDiffuse.a) >> 8;
		v1->r = (v1->r * globalDiffuse.r) >> 8;
		v1->g = (v1->g * globalDiffuse.g) >> 8;
		v1->b = (v1->b * globalDiffuse.b) >> 8;
		v2->a = (v2->a * globalDiffuse.a) >> 8;
		v2->r = (v2->r * globalDiffuse.r) >> 8;
		v2->g = (v2->g * globalDiffuse.g) >> 8;
		v2->b = (v2->b * globalDiffuse.b) >> 8;
	}
	#endif

	// Integer Y values (using a quick form of ceil() for positive values)

	int	y0 = (v0->y + 0xffff) >> 16;
	int	y2 = (v2->y + 0xffff) >> 16;
	if (y0 == y2) return;   // Null polygon (no height)?
	int	y1 = (v1->y + 0xffff) >> 16;

	// Calculate long-edge deltas

	int64_t	oneOverHeight = bigOne / (v2->y - v0->y);
	int		ldx, ldr, ldg, ldb, lda, ldu, ldv;
			ldx = static_cast<int>(((v2->x - v0->x) * oneOverHeight) >> 32);

	#if defined(MOD_ARGB)		
		lda = static_cast<int>(((v2->a - v0->a) * oneOverHeight) >> 32);
		ldr = static_cast<int>(((v2->r - v0->r) * oneOverHeight) >> 32);
		ldg = static_cast<int>(((v2->g - v0->g) * oneOverHeight) >> 32);
		ldb = static_cast<int>(((v2->b - v0->b) * oneOverHeight) >> 32);
	#endif
	
	#if defined(TEXTURED)
		ldu = static_cast<int>(((v2->u - v0->u) * oneOverHeight) >> 32);
		ldv = static_cast<int>(((v2->v - v0->v) * oneOverHeight) >> 32);
	#endif

	// Long-edge midpoint

	int64_t	topHeight = v1->y - v0->y;
	int		mid = v0->x + static_cast<int>((topHeight * ldx)>>16);

	if (v1->x == mid) return;   // Null polygon (no width)?

	// Edge variables (long)

	int64_t	subPix = (y0<<16) - v0->y;
	int		lx, lr, lg, lb, la, lu, lv;
			lx = v0->x + static_cast<int>((ldx * subPix)>>16);

	#if defined(MOD_ARGB)
		la = v0->a + static_cast<int>((lda * subPix)>>16);
		lr = v0->r + static_cast<int>((ldr * subPix)>>16);
		lg = v0->g + static_cast<int>((ldg * subPix)>>16);
		lb = v0->b + static_cast<int>((ldb * subPix)>>16);
	#endif
	
	#if defined(TEXTURED)
		lu = v0->u + static_cast<int>((ldu * subPix)>>16);
		lv = v0->v + static_cast<int>((ldv * subPix)>>16);
	#endif

	// Scanline deltas

	int64_t	oneOverWidth;
	int		dr, dg, db, da, du, dv;
	#if defined(TEXTURED) || defined(MOD_ARGB)	
		oneOverWidth = bigOne / (v1->x - mid);
	#endif
	
	#if defined (MOD_ARGB)
		da = static_cast<int>(((v1->a - (v0->a + ((topHeight * lda)>>16))) * oneOverWidth)>>32);
		dr = static_cast<int>(((v1->r - (v0->r + ((topHeight * ldr)>>16))) * oneOverWidth)>>32);
		dg = static_cast<int>(((v1->g - (v0->g + ((topHeight * ldg)>>16))) * oneOverWidth)>>32);
		db = static_cast<int>(((v1->b - (v0->b + ((topHeight * ldb)>>16))) * oneOverWidth)>>32);
	#endif
	
	#if defined (TEXTURED)
		du = static_cast<int>(((v1->u - (v0->u + ((topHeight * ldu)>>16))) * oneOverWidth)>>32);
		dv = static_cast<int>(((v1->v - (v0->v + ((topHeight * ldv)>>16))) * oneOverWidth)>>32);
	#endif

	// Screen info

	unsigned int	offset = y0 * pitch;
	PTYPE *		fb = reinterpret_cast<PTYPE *>(pFrameBuffer) + offset;
	int		iHeight = y1 - y0;

	if (iHeight)
	{
		// Short edge delta X

		oneOverHeight = bigOne / topHeight;
		int	sdx = static_cast<int>(((v1->x - v0->x) * oneOverHeight) >> 32);

		// Edge variables (short)

		int	sx = v0->x + static_cast<int>((sdx * subPix)>>16);

		// Scan-convert the top half

		if (mid < v1->x)
		{
			while(iHeight-- > 0)
			{
				// Integer (ceil()) left and right X components

				int		x0 = (lx + 0xffff) & 0xffff0000;
				int		x1 = (sx + 0xffff) & 0xffff0000;
				#include "SWTri_Loop.cpp"
			}
		}
		else if (mid > v1->x)
		{
			while(iHeight-- > 0)
			{
				// Integer (ceil()) left and right X components

				int	x0 = (sx + 0xffff) & 0xffff0000;
				int	x1 = (lx + 0xffff) & 0xffff0000;
				#include "SWTri_Loop.cpp"
			}
		}
	}

	// Done?

	iHeight = y2 - y1;
	if (!iHeight) return;

	// Short edge along bottom half

	oneOverHeight = bigOne / (v2->y - v1->y);
	int	sdx = static_cast<int>(((v2->x - v1->x) * oneOverHeight) >> 32);

	subPix = (y1<<16) - v1->y;
	int	sx = v1->x + static_cast<int>((sdx * subPix)>>16);

	// Scan-convert the bottom half

	if (mid < v1->x)
	{
		while(iHeight-- > 0)
		{
			// Integer (ceil()) left and right X components

			int	x0 = (lx + 0xffff) & 0xffff0000;
			int	x1 = (sx + 0xffff) & 0xffff0000;
			#include "SWTri_Loop.cpp"
		}
	}
	else if (mid > v1->x)
	{
		while(iHeight-- > 0)
		{
			// Integer (ceil()) left and right X components

			int	x0 = (sx + 0xffff) & 0xffff0000;
			int	x1 = (lx + 0xffff) & 0xffff0000;
			#include "SWTri_Loop.cpp"
		}
	}
}
#undef funcname2
#undef funcname1
#undef funcname
#undef PTYPE
#undef PIXEL_INCLUDE
#undef TEXTURED
#undef TEX_ALPHA
#undef MOD_ARGB
#undef GLOBAL_ARGB
#undef LINEAR_BLEND
#undef NAME0
#undef NAME1
#undef NAME2
#undef NAME3
#undef NAME4
#undef NAME5