1
// Copyright (C) 2002-2011 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
5
#include "IrrCompileConfig.h"
6
#include "IBurningShader.h"
8
#ifdef _IRR_COMPILE_WITH_BURNINGSVIDEO_
10
// compile flag for this file
40
// apply global override
41
#ifndef SOFTWARE_DRIVER_2_PERSPECTIVE_CORRECT
45
#ifndef SOFTWARE_DRIVER_2_SUBTEXEL
49
#ifndef SOFTWARE_DRIVER_2_USE_VERTEX_COLOR
53
#if !defined ( SOFTWARE_DRIVER_2_USE_WBUFFER ) && defined ( USE_ZBUFFER )
54
#ifndef SOFTWARE_DRIVER_2_PERSPECTIVE_CORRECT
77
class CTRTextureLightMap2_Add : public IBurningShader
82
CTRTextureLightMap2_Add(CBurningVideoDriver* driver);
84
//! draws an indexed triangle list
85
virtual void drawTriangle ( const s4DVertex *a,const s4DVertex *b,const s4DVertex *c );
89
void scanline_bilinear ();
91
sScanConvertData scan;
97
CTRTextureLightMap2_Add::CTRTextureLightMap2_Add(CBurningVideoDriver* driver)
98
: IBurningShader(driver)
101
setDebugName("CTRTextureLightMap2_Add");
109
REALINLINE void CTRTextureLightMap2_Add::scanline_bilinear ()
136
sVec2 slopeT[BURNING_MATERIAL_MAX_TEXTURES];
139
// apply top-left fill-convention, left
140
xStart = core::ceil32( line.x[0] );
141
xEnd = core::ceil32( line.x[1] ) - 1;
149
const f32 invDeltaX = core::reciprocal_approxim ( line.x[1] - line.x[0] );
152
slopeZ = (line.z[1] - line.z[0]) * invDeltaX;
155
slopeW = (line.w[1] - line.w[0]) * invDeltaX;
158
slopeC = (line.c[1] - line.c[0]) * invDeltaX;
161
slopeT[0] = (line.t[0][1] - line.t[0][0]) * invDeltaX;
164
slopeT[1] = (line.t[1][1] - line.t[1][0]) * invDeltaX;
168
subPixel = ( (f32) xStart ) - line.x[0];
170
line.z[0] += slopeZ * subPixel;
173
line.w[0] += slopeW * subPixel;
176
line.c[0] += slopeC * subPixel;
179
line.t[0][0] += slopeT[0] * subPixel;
182
line.t[1][0] += slopeT[1] * subPixel;
186
dst = (tVideoSample*)RenderTarget->lock() + ( line.y * RenderTarget->getDimension().Width ) + xStart;
189
z = (fp24*) DepthBuffer->lock() + ( line.y * RenderTarget->getDimension().Width ) + xStart;
195
#ifdef BURNINGVIDEO_RENDERER_FAST
196
u32 dIndex = ( line.y & 3 ) << 2;
201
tFixPoint r0, g0, b0;
202
tFixPoint r1, g1, b1;
206
for ( s32 i = 0; i <= dx; i++ )
209
if ( line.z[0] < z[i] )
212
if ( line.w[0] >= z[i] )
223
#ifdef BURNINGVIDEO_RENDERER_FAST
227
const f32 inversew = fix_inverse32 ( line.w[0] );
229
const tFixPointu d = dithermask [ dIndex | ( i ) & 3 ];
231
dst[i] = PixelAdd32 (
232
getTexel_plain ( &IT[0], d + tofix ( line.t[0][0].x,inversew),
233
d + tofix ( line.t[0][0].y,inversew) ),
234
getTexel_plain ( &IT[1], d + tofix ( line.t[1][0].x,inversew),
235
d + tofix ( line.t[1][0].y,inversew) )
238
const tFixPointu d = dithermask [ dIndex | ( i ) & 3 ];
240
dst[i] = PixelAdd32 (
241
getTexel_plain ( &IT[0], d + tofix ( line.t[0][0].x),
242
d + tofix ( line.t[0][0].y) ),
243
getTexel_plain ( &IT[1], d + tofix ( line.t[1][0].x),
244
d + tofix ( line.t[1][0].y) )
250
const f32 inversew = fix_inverse32 ( line.w[0] );
252
getSample_texture ( r0, g0, b0, &IT[0], tofix ( line.t[0][0].x,inversew), tofix ( line.t[0][0].y,inversew) );
253
getSample_texture ( r1, g1, b1, &IT[1], tofix ( line.t[0][1].x,inversew), tofix ( line.t[0][1].y,inversew) );
255
dst[i] = fix_to_color ( clampfix_maxcolor ( r0 + r1 ),
256
clampfix_maxcolor ( g0 + g1 ),
257
clampfix_maxcolor ( b0 + b1 )
273
line.t[0][0] += slopeT[0];
276
line.t[1][0] += slopeT[1];
282
void CTRTextureLightMap2_Add::drawTriangle ( const s4DVertex *a,const s4DVertex *b,const s4DVertex *c )
285
if ( F32_A_GREATER_B ( a->Pos.y , b->Pos.y ) ) swapVertexPointer(&a, &b);
286
if ( F32_A_GREATER_B ( b->Pos.y , c->Pos.y ) ) swapVertexPointer(&b, &c);
287
if ( F32_A_GREATER_B ( a->Pos.y , b->Pos.y ) ) swapVertexPointer(&a, &b);
289
const f32 ca = c->Pos.y - a->Pos.y;
290
const f32 ba = b->Pos.y - a->Pos.y;
291
const f32 cb = c->Pos.y - b->Pos.y;
292
// calculate delta y of the edges
293
scan.invDeltaY[0] = core::reciprocal( ca );
294
scan.invDeltaY[1] = core::reciprocal( ba );
295
scan.invDeltaY[2] = core::reciprocal( cb );
297
if ( F32_LOWER_EQUAL_0 ( scan.invDeltaY[0] ) )
300
// find if the major edge is left or right aligned
303
temp[0] = a->Pos.x - c->Pos.x;
305
temp[2] = b->Pos.x - a->Pos.x;
308
scan.left = ( temp[0] * temp[3] - temp[1] * temp[2] ) > 0.f ? 0 : 1;
309
scan.right = 1 - scan.left;
311
// calculate slopes for the major edge
312
scan.slopeX[0] = (c->Pos.x - a->Pos.x) * scan.invDeltaY[0];
313
scan.x[0] = a->Pos.x;
316
scan.slopeZ[0] = (c->Pos.z - a->Pos.z) * scan.invDeltaY[0];
317
scan.z[0] = a->Pos.z;
321
scan.slopeW[0] = (c->Pos.w - a->Pos.w) * scan.invDeltaY[0];
322
scan.w[0] = a->Pos.w;
326
scan.slopeC[0] = (c->Color[0] - a->Color[0]) * scan.invDeltaY[0];
327
scan.c[0] = a->Color[0];
331
scan.slopeT[0][0] = (c->Tex[0] - a->Tex[0]) * scan.invDeltaY[0];
332
scan.t[0][0] = a->Tex[0];
336
scan.slopeT[1][0] = (c->Tex[1] - a->Tex[1]) * scan.invDeltaY[0];
337
scan.t[1][0] = a->Tex[1];
340
// top left fill convention y run
348
// rasterize upper sub-triangle
349
if ( (f32) 0.0 != scan.invDeltaY[1] )
351
// calculate slopes for top edge
352
scan.slopeX[1] = (b->Pos.x - a->Pos.x) * scan.invDeltaY[1];
353
scan.x[1] = a->Pos.x;
356
scan.slopeZ[1] = (b->Pos.z - a->Pos.z) * scan.invDeltaY[1];
357
scan.z[1] = a->Pos.z;
361
scan.slopeW[1] = (b->Pos.w - a->Pos.w) * scan.invDeltaY[1];
362
scan.w[1] = a->Pos.w;
366
scan.slopeC[1] = (b->Color[0] - a->Color[0]) * scan.invDeltaY[1];
367
scan.c[1] = a->Color[0];
371
scan.slopeT[0][1] = (b->Tex[0] - a->Tex[0]) * scan.invDeltaY[1];
372
scan.t[0][1] = a->Tex[0];
376
scan.slopeT[1][1] = (b->Tex[1] - a->Tex[1]) * scan.invDeltaY[1];
377
scan.t[1][1] = a->Tex[1];
380
// apply top-left fill convention, top part
381
yStart = core::ceil32( a->Pos.y );
382
yEnd = core::ceil32( b->Pos.y ) - 1;
385
subPixel = ( (f32) yStart ) - a->Pos.y;
387
// correct to pixel center
388
scan.x[0] += scan.slopeX[0] * subPixel;
389
scan.x[1] += scan.slopeX[1] * subPixel;
392
scan.z[0] += scan.slopeZ[0] * subPixel;
393
scan.z[1] += scan.slopeZ[1] * subPixel;
397
scan.w[0] += scan.slopeW[0] * subPixel;
398
scan.w[1] += scan.slopeW[1] * subPixel;
402
scan.c[0] += scan.slopeC[0] * subPixel;
403
scan.c[1] += scan.slopeC[1] * subPixel;
407
scan.t[0][0] += scan.slopeT[0][0] * subPixel;
408
scan.t[0][1] += scan.slopeT[0][1] * subPixel;
412
scan.t[1][0] += scan.slopeT[1][0] * subPixel;
413
scan.t[1][1] += scan.slopeT[1][1] * subPixel;
418
// rasterize the edge scanlines
419
for( line.y = yStart; line.y <= yEnd; ++line.y)
421
line.x[scan.left] = scan.x[0];
422
line.x[scan.right] = scan.x[1];
425
line.z[scan.left] = scan.z[0];
426
line.z[scan.right] = scan.z[1];
430
line.w[scan.left] = scan.w[0];
431
line.w[scan.right] = scan.w[1];
435
line.c[scan.left] = scan.c[0];
436
line.c[scan.right] = scan.c[1];
440
line.t[0][scan.left] = scan.t[0][0];
441
line.t[0][scan.right] = scan.t[0][1];
445
line.t[1][scan.left] = scan.t[1][0];
446
line.t[1][scan.right] = scan.t[1][1];
450
scanline_bilinear ();
452
scan.x[0] += scan.slopeX[0];
453
scan.x[1] += scan.slopeX[1];
456
scan.z[0] += scan.slopeZ[0];
457
scan.z[1] += scan.slopeZ[1];
461
scan.w[0] += scan.slopeW[0];
462
scan.w[1] += scan.slopeW[1];
466
scan.c[0] += scan.slopeC[0];
467
scan.c[1] += scan.slopeC[1];
471
scan.t[0][0] += scan.slopeT[0][0];
472
scan.t[0][1] += scan.slopeT[0][1];
476
scan.t[1][0] += scan.slopeT[1][0];
477
scan.t[1][1] += scan.slopeT[1][1];
483
// rasterize lower sub-triangle
484
if ( (f32) 0.0 != scan.invDeltaY[2] )
486
// advance to middle point
487
if( (f32) 0.0 != scan.invDeltaY[1] )
489
temp[0] = b->Pos.y - a->Pos.y; // dy
491
scan.x[0] = a->Pos.x + scan.slopeX[0] * temp[0];
493
scan.z[0] = a->Pos.z + scan.slopeZ[0] * temp[0];
496
scan.w[0] = a->Pos.w + scan.slopeW[0] * temp[0];
499
scan.c[0] = a->Color[0] + scan.slopeC[0] * temp[0];
502
scan.t[0][0] = a->Tex[0] + scan.slopeT[0][0] * temp[0];
505
scan.t[1][0] = a->Tex[1] + scan.slopeT[1][0] * temp[0];
510
// calculate slopes for bottom edge
511
scan.slopeX[1] = (c->Pos.x - b->Pos.x) * scan.invDeltaY[2];
512
scan.x[1] = b->Pos.x;
515
scan.slopeZ[1] = (c->Pos.z - b->Pos.z) * scan.invDeltaY[2];
516
scan.z[1] = b->Pos.z;
520
scan.slopeW[1] = (c->Pos.w - b->Pos.w) * scan.invDeltaY[2];
521
scan.w[1] = b->Pos.w;
525
scan.slopeC[1] = (c->Color[0] - b->Color[0]) * scan.invDeltaY[2];
526
scan.c[1] = b->Color[0];
530
scan.slopeT[0][1] = (c->Tex[0] - b->Tex[0]) * scan.invDeltaY[2];
531
scan.t[0][1] = b->Tex[0];
535
scan.slopeT[1][1] = (c->Tex[1] - b->Tex[1]) * scan.invDeltaY[2];
536
scan.t[1][1] = b->Tex[1];
539
// apply top-left fill convention, top part
540
yStart = core::ceil32( b->Pos.y );
541
yEnd = core::ceil32( c->Pos.y ) - 1;
545
subPixel = ( (f32) yStart ) - b->Pos.y;
547
// correct to pixel center
548
scan.x[0] += scan.slopeX[0] * subPixel;
549
scan.x[1] += scan.slopeX[1] * subPixel;
552
scan.z[0] += scan.slopeZ[0] * subPixel;
553
scan.z[1] += scan.slopeZ[1] * subPixel;
557
scan.w[0] += scan.slopeW[0] * subPixel;
558
scan.w[1] += scan.slopeW[1] * subPixel;
562
scan.c[0] += scan.slopeC[0] * subPixel;
563
scan.c[1] += scan.slopeC[1] * subPixel;
567
scan.t[0][0] += scan.slopeT[0][0] * subPixel;
568
scan.t[0][1] += scan.slopeT[0][1] * subPixel;
572
scan.t[1][0] += scan.slopeT[1][0] * subPixel;
573
scan.t[1][1] += scan.slopeT[1][1] * subPixel;
578
// rasterize the edge scanlines
579
for( line.y = yStart; line.y <= yEnd; ++line.y)
581
line.x[scan.left] = scan.x[0];
582
line.x[scan.right] = scan.x[1];
585
line.z[scan.left] = scan.z[0];
586
line.z[scan.right] = scan.z[1];
590
line.w[scan.left] = scan.w[0];
591
line.w[scan.right] = scan.w[1];
595
line.c[scan.left] = scan.c[0];
596
line.c[scan.right] = scan.c[1];
600
line.t[0][scan.left] = scan.t[0][0];
601
line.t[0][scan.right] = scan.t[0][1];
605
line.t[1][scan.left] = scan.t[1][0];
606
line.t[1][scan.right] = scan.t[1][1];
610
scanline_bilinear ();
612
scan.x[0] += scan.slopeX[0];
613
scan.x[1] += scan.slopeX[1];
616
scan.z[0] += scan.slopeZ[0];
617
scan.z[1] += scan.slopeZ[1];
621
scan.w[0] += scan.slopeW[0];
622
scan.w[1] += scan.slopeW[1];
626
scan.c[0] += scan.slopeC[0];
627
scan.c[1] += scan.slopeC[1];
631
scan.t[0][0] += scan.slopeT[0][0];
632
scan.t[0][1] += scan.slopeT[0][1];
636
scan.t[1][0] += scan.slopeT[1][0];
637
scan.t[1][1] += scan.slopeT[1][1];
647
} // end namespace video
648
} // end namespace irr
650
#endif // _IRR_COMPILE_WITH_BURNINGSVIDEO_
657
//! creates a flat triangle renderer
658
IBurningShader* createTriangleRendererTextureLightMap2_Add(CBurningVideoDriver* driver)
660
#ifdef _IRR_COMPILE_WITH_BURNINGSVIDEO_
661
return new CTRTextureLightMap2_Add(driver);
664
#endif // _IRR_COMPILE_WITH_BURNINGSVIDEO_
668
} // end namespace video
669
} // end namespace irr