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