1 // Copyright (C) 2002-2012 Nikolaus Gebhardt / Thomas Alten
2 // This file is part of the "Irrlicht Engine".
3 // For conditions of distribution and use, see copyright notice in irrlicht.h
4 
5 #include "IrrCompileConfig.h"
6 #include "IBurningShader.h"
7 
8 #ifdef _IRR_COMPILE_WITH_BURNINGSVIDEO_
9 
10 // compile flag for this file
11 #undef USE_ZBUFFER
12 #undef IPOL_Z
13 #undef CMP_Z
14 #undef WRITE_Z
15 
16 #undef IPOL_W
17 #undef CMP_W
18 #undef WRITE_W
19 
20 #undef SUBTEXEL
21 #undef INVERSE_W
22 
23 #undef IPOL_C0
24 #undef IPOL_T0
25 #undef IPOL_T1
26 
27 // define render case
28 #define SUBTEXEL
29 #define INVERSE_W
30 
31 #define USE_ZBUFFER
32 #define IPOL_W
33 #define CMP_W
34 //#define WRITE_W
35 
36 //#define IPOL_C0
37 #define IPOL_T0
38 //#define IPOL_T1
39 
40 // apply global override
41 #ifndef SOFTWARE_DRIVER_2_PERSPECTIVE_CORRECT
42 	#undef INVERSE_W
43 	#ifndef SOFTWARE_DRIVER_2_PERSPECTIVE_CORRECT
44 		#undef IPOL_W
45 	#endif
46 #endif
47 
48 #ifndef SOFTWARE_DRIVER_2_SUBTEXEL
49 	#undef SUBTEXEL
50 #endif
51 
52 #ifndef SOFTWARE_DRIVER_2_USE_VERTEX_COLOR
53 	#undef IPOL_C0
54 #endif
55 
56 #if !defined ( SOFTWARE_DRIVER_2_USE_WBUFFER ) && defined ( USE_ZBUFFER )
57 	#define IPOL_Z
58 
59 	#ifdef CMP_W
60 		#undef CMP_W
61 		#define CMP_Z
62 	#endif
63 
64 	#ifdef WRITE_W
65 		#undef WRITE_W
66 		#define WRITE_Z
67 	#endif
68 
69 #endif
70 
71 
72 namespace irr
73 {
74 
75 namespace video
76 {
77 
78 class CTRTextureGouraudAddNoZ2 : public IBurningShader
79 {
80 public:
81 
82 	//! constructor
83 	CTRTextureGouraudAddNoZ2(CBurningVideoDriver* driver);
84 
85 	//! draws an indexed triangle list
86 	virtual void drawTriangle ( const s4DVertex *a,const s4DVertex *b,const s4DVertex *c );
87 
88 
89 private:
90 	void scanline_bilinear ();
91 	sScanConvertData scan;
92 	sScanLineData line;
93 
94 };
95 
96 //! constructor
CTRTextureGouraudAddNoZ2(CBurningVideoDriver * driver)97 CTRTextureGouraudAddNoZ2::CTRTextureGouraudAddNoZ2(CBurningVideoDriver* driver)
98 : IBurningShader(driver)
99 {
100 	#ifdef _DEBUG
101 	setDebugName("CTRTextureGouraudAddNoZ2");
102 	#endif
103 }
104 
105 
106 
107 /*!
108 */
scanline_bilinear()109 void CTRTextureGouraudAddNoZ2::scanline_bilinear ()
110 {
111 	tVideoSample *dst;
112 
113 #ifdef USE_ZBUFFER
114 	fp24 *z;
115 #endif
116 
117 	s32 xStart;
118 	s32 xEnd;
119 	s32 dx;
120 
121 
122 #ifdef SUBTEXEL
123 	f32 subPixel;
124 #endif
125 
126 #ifdef IPOL_Z
127 	f32 slopeZ;
128 #endif
129 #ifdef IPOL_W
130 	fp24 slopeW;
131 #endif
132 #ifdef IPOL_C0
133 	sVec4 slopeC;
134 #endif
135 #ifdef IPOL_T0
136 	sVec2 slopeT[BURNING_MATERIAL_MAX_TEXTURES];
137 #endif
138 
139 	// apply top-left fill-convention, left
140 	xStart = core::ceil32( line.x[0] );
141 	xEnd = core::ceil32( line.x[1] ) - 1;
142 
143 	dx = xEnd - xStart;
144 
145 	if ( dx < 0 )
146 		return;
147 
148 	// slopes
149 	const f32 invDeltaX = core::reciprocal_approxim ( line.x[1] - line.x[0] );
150 
151 #ifdef IPOL_Z
152 	slopeZ = (line.z[1] - line.z[0]) * invDeltaX;
153 #endif
154 #ifdef IPOL_W
155 	slopeW = (line.w[1] - line.w[0]) * invDeltaX;
156 #endif
157 #ifdef IPOL_C0
158 	slopeC = (line.c[1] - line.c[0]) * invDeltaX;
159 #endif
160 #ifdef IPOL_T0
161 	slopeT[0] = (line.t[0][1] - line.t[0][0]) * invDeltaX;
162 #endif
163 #ifdef IPOL_T1
164 	slopeT[1] = (line.t[1][1] - line.t[1][0]) * invDeltaX;
165 #endif
166 
167 #ifdef SUBTEXEL
168 	subPixel = ( (f32) xStart ) - line.x[0];
169 #ifdef IPOL_Z
170 	line.z[0] += slopeZ * subPixel;
171 #endif
172 #ifdef IPOL_W
173 	line.w[0] += slopeW * subPixel;
174 #endif
175 #ifdef IPOL_C0
176 	line.c[0] += slopeC * subPixel;
177 #endif
178 #ifdef IPOL_T0
179 	line.t[0][0] += slopeT[0] * subPixel;
180 #endif
181 #ifdef IPOL_T1
182 	line.t[1][0] += slopeT[1] * subPixel;
183 #endif
184 #endif
185 
186 	dst = (tVideoSample*)RenderTarget->lock() + ( line.y * RenderTarget->getDimension().Width ) + xStart;
187 
188 #ifdef USE_ZBUFFER
189 	z = (fp24*) DepthBuffer->lock() + ( line.y * RenderTarget->getDimension().Width ) + xStart;
190 #endif
191 
192 
193 #ifdef IPOL_W
194 	f32 inversew;
195 #endif
196 
197 	tFixPoint tx0;
198 	tFixPoint ty0;
199 
200 	tFixPoint r0, g0, b0;
201 	tFixPoint r1, g1, b1;
202 
203 	for ( s32 i = 0; i <= dx; ++i )
204 	{
205 #ifdef CMP_Z
206 		if ( line.z[0] < z[i] )
207 #endif
208 #ifdef CMP_W
209 		if ( line.w[0] >= z[i] )
210 #endif
211 		{
212 #ifdef IPOL_W
213 			inversew = fix_inverse32 ( line.w[0] );
214 
215 			tx0 = tofix ( line.t[0][0].x,inversew);
216 			ty0 = tofix ( line.t[0][0].y,inversew);
217 #else
218 			tx0 = tofix ( line.t[0][0].x );
219 			ty0 = tofix ( line.t[0][0].y );
220 #endif
221 
222 			getSample_texture ( r0, g0, b0, &IT[0], tx0,ty0 );
223 
224 			color_to_fix ( r1, g1, b1, dst[i] );
225 
226 			dst[i] = fix_to_color ( clampfix_maxcolor ( r1 + (r0 >> 1 ) ),
227 									clampfix_maxcolor ( g1 + (g0 >> 1 ) ),
228 									clampfix_maxcolor ( b1 + (b0 >> 1) )
229 								);
230 
231 #ifdef WRITE_Z
232 			z[i] = line.z[0];
233 #endif
234 #ifdef WRITE_W
235 			z[i] = line.w[0];
236 #endif
237 		}
238 
239 #ifdef IPOL_Z
240 		line.z[0] += slopeZ;
241 #endif
242 #ifdef IPOL_W
243 		line.w[0] += slopeW;
244 #endif
245 #ifdef IPOL_C0
246 		line.c[0] += slopeC;
247 #endif
248 #ifdef IPOL_T0
249 		line.t[0][0] += slopeT[0];
250 #endif
251 #ifdef IPOL_T1
252 		line.t[1][0] += slopeT[1];
253 #endif
254 	}
255 
256 }
257 
drawTriangle(const s4DVertex * a,const s4DVertex * b,const s4DVertex * c)258 void CTRTextureGouraudAddNoZ2::drawTriangle ( const s4DVertex *a,const s4DVertex *b,const s4DVertex *c )
259 {
260 	// sort on height, y
261 	if ( F32_A_GREATER_B ( a->Pos.y , b->Pos.y ) ) swapVertexPointer(&a, &b);
262 	if ( F32_A_GREATER_B ( b->Pos.y , c->Pos.y ) ) swapVertexPointer(&b, &c);
263 	if ( F32_A_GREATER_B ( a->Pos.y , b->Pos.y ) ) swapVertexPointer(&a, &b);
264 
265 	const f32 ca = c->Pos.y - a->Pos.y;
266 	const f32 ba = b->Pos.y - a->Pos.y;
267 	const f32 cb = c->Pos.y - b->Pos.y;
268 	// calculate delta y of the edges
269 	scan.invDeltaY[0] = core::reciprocal( ca );
270 	scan.invDeltaY[1] = core::reciprocal( ba );
271 	scan.invDeltaY[2] = core::reciprocal( cb );
272 
273 	if ( F32_LOWER_EQUAL_0 ( scan.invDeltaY[0] ) )
274 		return;
275 
276 	// find if the major edge is left or right aligned
277 	f32 temp[4];
278 
279 	temp[0] = a->Pos.x - c->Pos.x;
280 	temp[1] = -ca;
281 	temp[2] = b->Pos.x - a->Pos.x;
282 	temp[3] = ba;
283 
284 	scan.left = ( temp[0] * temp[3] - temp[1] * temp[2] ) > 0.f ? 0 : 1;
285 	scan.right = 1 - scan.left;
286 
287 	// calculate slopes for the major edge
288 	scan.slopeX[0] = (c->Pos.x - a->Pos.x) * scan.invDeltaY[0];
289 	scan.x[0] = a->Pos.x;
290 
291 #ifdef IPOL_Z
292 	scan.slopeZ[0] = (c->Pos.z - a->Pos.z) * scan.invDeltaY[0];
293 	scan.z[0] = a->Pos.z;
294 #endif
295 
296 #ifdef IPOL_W
297 	scan.slopeW[0] = (c->Pos.w - a->Pos.w) * scan.invDeltaY[0];
298 	scan.w[0] = a->Pos.w;
299 #endif
300 
301 #ifdef IPOL_C0
302 	scan.slopeC[0] = (c->Color[0] - a->Color[0]) * scan.invDeltaY[0];
303 	scan.c[0] = a->Color[0];
304 #endif
305 
306 #ifdef IPOL_T0
307 	scan.slopeT[0][0] = (c->Tex[0] - a->Tex[0]) * scan.invDeltaY[0];
308 	scan.t[0][0] = a->Tex[0];
309 #endif
310 
311 #ifdef IPOL_T1
312 	scan.slopeT[1][0] = (c->Tex[1] - a->Tex[1]) * scan.invDeltaY[0];
313 	scan.t[1][0] = a->Tex[1];
314 #endif
315 
316 	// top left fill convention y run
317 	s32 yStart;
318 	s32 yEnd;
319 
320 #ifdef SUBTEXEL
321 	f32 subPixel;
322 #endif
323 
324 	// rasterize upper sub-triangle
325 	if ( (f32) 0.0 != scan.invDeltaY[1]  )
326 	{
327 		// calculate slopes for top edge
328 		scan.slopeX[1] = (b->Pos.x - a->Pos.x) * scan.invDeltaY[1];
329 		scan.x[1] = a->Pos.x;
330 
331 #ifdef IPOL_Z
332 		scan.slopeZ[1] = (b->Pos.z - a->Pos.z) * scan.invDeltaY[1];
333 		scan.z[1] = a->Pos.z;
334 #endif
335 
336 #ifdef IPOL_W
337 		scan.slopeW[1] = (b->Pos.w - a->Pos.w) * scan.invDeltaY[1];
338 		scan.w[1] = a->Pos.w;
339 #endif
340 
341 #ifdef IPOL_C0
342 		scan.slopeC[1] = (b->Color[0] - a->Color[0]) * scan.invDeltaY[1];
343 		scan.c[1] = a->Color[0];
344 #endif
345 
346 #ifdef IPOL_T0
347 		scan.slopeT[0][1] = (b->Tex[0] - a->Tex[0]) * scan.invDeltaY[1];
348 		scan.t[0][1] = a->Tex[0];
349 #endif
350 
351 #ifdef IPOL_T1
352 		scan.slopeT[1][1] = (b->Tex[1] - a->Tex[1]) * scan.invDeltaY[1];
353 		scan.t[1][1] = a->Tex[1];
354 #endif
355 
356 		// apply top-left fill convention, top part
357 		yStart = core::ceil32( a->Pos.y );
358 		yEnd = core::ceil32( b->Pos.y ) - 1;
359 
360 #ifdef SUBTEXEL
361 		subPixel = ( (f32) yStart ) - a->Pos.y;
362 
363 		// correct to pixel center
364 		scan.x[0] += scan.slopeX[0] * subPixel;
365 		scan.x[1] += scan.slopeX[1] * subPixel;
366 
367 #ifdef IPOL_Z
368 		scan.z[0] += scan.slopeZ[0] * subPixel;
369 		scan.z[1] += scan.slopeZ[1] * subPixel;
370 #endif
371 
372 #ifdef IPOL_W
373 		scan.w[0] += scan.slopeW[0] * subPixel;
374 		scan.w[1] += scan.slopeW[1] * subPixel;
375 #endif
376 
377 #ifdef IPOL_C0
378 		scan.c[0] += scan.slopeC[0] * subPixel;
379 		scan.c[1] += scan.slopeC[1] * subPixel;
380 #endif
381 
382 #ifdef IPOL_T0
383 		scan.t[0][0] += scan.slopeT[0][0] * subPixel;
384 		scan.t[0][1] += scan.slopeT[0][1] * subPixel;
385 #endif
386 
387 #ifdef IPOL_T1
388 		scan.t[1][0] += scan.slopeT[1][0] * subPixel;
389 		scan.t[1][1] += scan.slopeT[1][1] * subPixel;
390 #endif
391 
392 #endif
393 
394 		// rasterize the edge scanlines
395 		for( line.y = yStart; line.y <= yEnd; ++line.y)
396 		{
397 			line.x[scan.left] = scan.x[0];
398 			line.x[scan.right] = scan.x[1];
399 
400 #ifdef IPOL_Z
401 			line.z[scan.left] = scan.z[0];
402 			line.z[scan.right] = scan.z[1];
403 #endif
404 
405 #ifdef IPOL_W
406 			line.w[scan.left] = scan.w[0];
407 			line.w[scan.right] = scan.w[1];
408 #endif
409 
410 #ifdef IPOL_C0
411 			line.c[scan.left] = scan.c[0];
412 			line.c[scan.right] = scan.c[1];
413 #endif
414 
415 #ifdef IPOL_T0
416 			line.t[0][scan.left] = scan.t[0][0];
417 			line.t[0][scan.right] = scan.t[0][1];
418 #endif
419 
420 #ifdef IPOL_T1
421 			line.t[1][scan.left] = scan.t[1][0];
422 			line.t[1][scan.right] = scan.t[1][1];
423 #endif
424 
425 			// render a scanline
426 			scanline_bilinear ();
427 
428 			scan.x[0] += scan.slopeX[0];
429 			scan.x[1] += scan.slopeX[1];
430 
431 #ifdef IPOL_Z
432 			scan.z[0] += scan.slopeZ[0];
433 			scan.z[1] += scan.slopeZ[1];
434 #endif
435 
436 #ifdef IPOL_W
437 			scan.w[0] += scan.slopeW[0];
438 			scan.w[1] += scan.slopeW[1];
439 #endif
440 
441 #ifdef IPOL_C0
442 			scan.c[0] += scan.slopeC[0];
443 			scan.c[1] += scan.slopeC[1];
444 #endif
445 
446 #ifdef IPOL_T0
447 			scan.t[0][0] += scan.slopeT[0][0];
448 			scan.t[0][1] += scan.slopeT[0][1];
449 #endif
450 
451 #ifdef IPOL_T1
452 			scan.t[1][0] += scan.slopeT[1][0];
453 			scan.t[1][1] += scan.slopeT[1][1];
454 #endif
455 
456 		}
457 	}
458 
459 	// rasterize lower sub-triangle
460 	if ( (f32) 0.0 != scan.invDeltaY[2] )
461 	{
462 		// advance to middle point
463 		if( (f32) 0.0 != scan.invDeltaY[1] )
464 		{
465 			temp[0] = b->Pos.y - a->Pos.y;	// dy
466 
467 			scan.x[0] = a->Pos.x + scan.slopeX[0] * temp[0];
468 #ifdef IPOL_Z
469 			scan.z[0] = a->Pos.z + scan.slopeZ[0] * temp[0];
470 #endif
471 #ifdef IPOL_W
472 			scan.w[0] = a->Pos.w + scan.slopeW[0] * temp[0];
473 #endif
474 #ifdef IPOL_C0
475 			scan.c[0] = a->Color[0] + scan.slopeC[0] * temp[0];
476 #endif
477 #ifdef IPOL_T0
478 			scan.t[0][0] = a->Tex[0] + scan.slopeT[0][0] * temp[0];
479 #endif
480 #ifdef IPOL_T1
481 			scan.t[1][0] = a->Tex[1] + scan.slopeT[1][0] * temp[0];
482 #endif
483 
484 		}
485 
486 		// calculate slopes for bottom edge
487 		scan.slopeX[1] = (c->Pos.x - b->Pos.x) * scan.invDeltaY[2];
488 		scan.x[1] = b->Pos.x;
489 
490 #ifdef IPOL_Z
491 		scan.slopeZ[1] = (c->Pos.z - b->Pos.z) * scan.invDeltaY[2];
492 		scan.z[1] = b->Pos.z;
493 #endif
494 
495 #ifdef IPOL_W
496 		scan.slopeW[1] = (c->Pos.w - b->Pos.w) * scan.invDeltaY[2];
497 		scan.w[1] = b->Pos.w;
498 #endif
499 
500 #ifdef IPOL_C0
501 		scan.slopeC[1] = (c->Color[0] - b->Color[0]) * scan.invDeltaY[2];
502 		scan.c[1] = b->Color[0];
503 #endif
504 
505 #ifdef IPOL_T0
506 		scan.slopeT[0][1] = (c->Tex[0] - b->Tex[0]) * scan.invDeltaY[2];
507 		scan.t[0][1] = b->Tex[0];
508 #endif
509 
510 #ifdef IPOL_T1
511 		scan.slopeT[1][1] = (c->Tex[1] - b->Tex[1]) * scan.invDeltaY[2];
512 		scan.t[1][1] = b->Tex[1];
513 #endif
514 
515 		// apply top-left fill convention, top part
516 		yStart = core::ceil32( b->Pos.y );
517 		yEnd = core::ceil32( c->Pos.y ) - 1;
518 
519 #ifdef SUBTEXEL
520 
521 		subPixel = ( (f32) yStart ) - b->Pos.y;
522 
523 		// correct to pixel center
524 		scan.x[0] += scan.slopeX[0] * subPixel;
525 		scan.x[1] += scan.slopeX[1] * subPixel;
526 
527 #ifdef IPOL_Z
528 		scan.z[0] += scan.slopeZ[0] * subPixel;
529 		scan.z[1] += scan.slopeZ[1] * subPixel;
530 #endif
531 
532 #ifdef IPOL_W
533 		scan.w[0] += scan.slopeW[0] * subPixel;
534 		scan.w[1] += scan.slopeW[1] * subPixel;
535 #endif
536 
537 #ifdef IPOL_C0
538 		scan.c[0] += scan.slopeC[0] * subPixel;
539 		scan.c[1] += scan.slopeC[1] * subPixel;
540 #endif
541 
542 #ifdef IPOL_T0
543 		scan.t[0][0] += scan.slopeT[0][0] * subPixel;
544 		scan.t[0][1] += scan.slopeT[0][1] * subPixel;
545 #endif
546 
547 #ifdef IPOL_T1
548 		scan.t[1][0] += scan.slopeT[1][0] * subPixel;
549 		scan.t[1][1] += scan.slopeT[1][1] * subPixel;
550 #endif
551 
552 #endif
553 
554 		// rasterize the edge scanlines
555 		for( line.y = yStart; line.y <= yEnd; ++line.y)
556 		{
557 			line.x[scan.left] = scan.x[0];
558 			line.x[scan.right] = scan.x[1];
559 
560 #ifdef IPOL_Z
561 			line.z[scan.left] = scan.z[0];
562 			line.z[scan.right] = scan.z[1];
563 #endif
564 
565 #ifdef IPOL_W
566 			line.w[scan.left] = scan.w[0];
567 			line.w[scan.right] = scan.w[1];
568 #endif
569 
570 #ifdef IPOL_C0
571 			line.c[scan.left] = scan.c[0];
572 			line.c[scan.right] = scan.c[1];
573 #endif
574 
575 #ifdef IPOL_T0
576 			line.t[0][scan.left] = scan.t[0][0];
577 			line.t[0][scan.right] = scan.t[0][1];
578 #endif
579 
580 #ifdef IPOL_T1
581 			line.t[1][scan.left] = scan.t[1][0];
582 			line.t[1][scan.right] = scan.t[1][1];
583 #endif
584 
585 			// render a scanline
586 			scanline_bilinear ( );
587 
588 			scan.x[0] += scan.slopeX[0];
589 			scan.x[1] += scan.slopeX[1];
590 
591 #ifdef IPOL_Z
592 			scan.z[0] += scan.slopeZ[0];
593 			scan.z[1] += scan.slopeZ[1];
594 #endif
595 
596 #ifdef IPOL_W
597 			scan.w[0] += scan.slopeW[0];
598 			scan.w[1] += scan.slopeW[1];
599 #endif
600 
601 #ifdef IPOL_C0
602 			scan.c[0] += scan.slopeC[0];
603 			scan.c[1] += scan.slopeC[1];
604 #endif
605 
606 #ifdef IPOL_T0
607 			scan.t[0][0] += scan.slopeT[0][0];
608 			scan.t[0][1] += scan.slopeT[0][1];
609 #endif
610 
611 #ifdef IPOL_T1
612 			scan.t[1][0] += scan.slopeT[1][0];
613 			scan.t[1][1] += scan.slopeT[1][1];
614 #endif
615 
616 		}
617 	}
618 
619 }
620 
621 } // end namespace video
622 } // end namespace irr
623 
624 #endif // _IRR_COMPILE_WITH_BURNINGSVIDEO_
625 
626 namespace irr
627 {
628 namespace video
629 {
630 
631 //! creates a flat triangle renderer
createTRTextureGouraudAddNoZ2(CBurningVideoDriver * driver)632 IBurningShader* createTRTextureGouraudAddNoZ2(CBurningVideoDriver* driver)
633 {
634 	#ifdef _IRR_COMPILE_WITH_BURNINGSVIDEO_
635 	return new CTRTextureGouraudAddNoZ2(driver);
636 	#else
637 	return 0;
638 	#endif // _IRR_COMPILE_WITH_BURNINGSVIDEO_
639 }
640 
641 
642 } // end namespace video
643 } // end namespace irr
644 
645 
646