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
78
class CTRGouraud2 : public IBurningShader
83
CTRGouraud2(CBurningVideoDriver* driver);
85
//! draws an indexed triangle list
86
virtual void drawTriangle ( const s4DVertex *a,const s4DVertex *b,const s4DVertex *c );
90
void scanline_bilinear ();
91
sScanConvertData scan;
97
CTRGouraud2::CTRGouraud2(CBurningVideoDriver* driver)
98
: IBurningShader(driver)
101
setDebugName("CTRGouraud2");
109
void CTRGouraud2::scanline_bilinear ()
135
sVec2 slopeT[BURNING_MATERIAL_MAX_TEXTURES];
138
// apply top-left fill-convention, left
139
xStart = core::ceil32( line.x[0] );
140
xEnd = core::ceil32( line.x[1] ) - 1;
148
const f32 invDeltaX = core::reciprocal_approxim ( line.x[1] - line.x[0] );
151
slopeZ = (line.z[1] - line.z[0]) * invDeltaX;
154
slopeW = (line.w[1] - line.w[0]) * invDeltaX;
157
slopeC = (line.c[0][1] - line.c[0][0]) * invDeltaX;
160
slopeT[0] = (line.t[0][1] - line.t[0][0]) * invDeltaX;
163
slopeT[1] = (line.t[1][1] - line.t[1][0]) * invDeltaX;
167
subPixel = ( (f32) xStart ) - line.x[0];
169
line.z[0] += slopeZ * subPixel;
172
line.w[0] += slopeW * subPixel;
175
line.c[0][0] += slopeC * subPixel;
178
line.t[0][0] += slopeT[0] * subPixel;
181
line.t[1][0] += slopeT[1] * subPixel;
185
dst = (tVideoSample*)RenderTarget->lock() + ( line.y * RenderTarget->getDimension().Width ) + xStart;
188
z = (fp24*) DepthBuffer->lock() + ( line.y * RenderTarget->getDimension().Width ) + xStart;
194
tFixPoint r0, g0, b0;
202
for ( s32 i = 0; i <= dx; ++i )
205
if ( line.z[0] < z[i] )
208
if ( line.w[0] >= z[i] )
214
inversew = core::reciprocal ( line.w[0] );
216
getSample_color ( r0, g0, b0, line.c[0][0] * inversew );
218
getSample_color ( r0, g0, b0, line.c[0][0] );
221
dst[i] = fix_to_color ( r0, g0, b0 );
223
dst[i] = COLOR_BRIGHT_WHITE;
242
line.c[0][0] += slopeC;
245
line.t[0][0] += slopeT[0];
248
line.t[1][0] += slopeT[1];
254
void CTRGouraud2::drawTriangle ( const s4DVertex *a,const s4DVertex *b,const s4DVertex *c )
257
if ( a->Pos.y > b->Pos.y ) swapVertexPointer(&a, &b);
258
if ( a->Pos.y > c->Pos.y ) swapVertexPointer(&a, &c);
259
if ( b->Pos.y > c->Pos.y ) swapVertexPointer(&b, &c);
261
const f32 ca = c->Pos.y - a->Pos.y;
262
const f32 ba = b->Pos.y - a->Pos.y;
263
const f32 cb = c->Pos.y - b->Pos.y;
264
// calculate delta y of the edges
265
scan.invDeltaY[0] = core::reciprocal( ca );
266
scan.invDeltaY[1] = core::reciprocal( ba );
267
scan.invDeltaY[2] = core::reciprocal( cb );
269
if ( F32_LOWER_EQUAL_0 ( scan.invDeltaY[0] ) )
272
// find if the major edge is left or right aligned
275
temp[0] = a->Pos.x - c->Pos.x;
277
temp[2] = b->Pos.x - a->Pos.x;
280
scan.left = ( temp[0] * temp[3] - temp[1] * temp[2] ) > 0.f ? 0 : 1;
281
scan.right = 1 - scan.left;
283
// calculate slopes for the major edge
284
scan.slopeX[0] = (c->Pos.x - a->Pos.x) * scan.invDeltaY[0];
285
scan.x[0] = a->Pos.x;
288
scan.slopeZ[0] = (c->Pos.z - a->Pos.z) * scan.invDeltaY[0];
289
scan.z[0] = a->Pos.z;
293
scan.slopeW[0] = (c->Pos.w - a->Pos.w) * scan.invDeltaY[0];
294
scan.w[0] = a->Pos.w;
298
scan.slopeC[0][0] = (c->Color[0] - a->Color[0]) * scan.invDeltaY[0];
299
scan.c[0][0] = a->Color[0];
303
scan.slopeT[0][0] = (c->Tex[0] - a->Tex[0]) * scan.invDeltaY[0];
304
scan.t[0][0] = a->Tex[0];
308
scan.slopeT[1][0] = (c->Tex[1] - a->Tex[1]) * scan.invDeltaY[0];
309
scan.t[1][0] = a->Tex[1];
312
// top left fill convention y run
321
// rasterize upper sub-triangle
322
if ( (f32) 0.0 != scan.invDeltaY[1] )
324
// calculate slopes for top edge
325
scan.slopeX[1] = (b->Pos.x - a->Pos.x) * scan.invDeltaY[1];
326
scan.x[1] = a->Pos.x;
329
scan.slopeZ[1] = (b->Pos.z - a->Pos.z) * scan.invDeltaY[1];
330
scan.z[1] = a->Pos.z;
334
scan.slopeW[1] = (b->Pos.w - a->Pos.w) * scan.invDeltaY[1];
335
scan.w[1] = a->Pos.w;
339
scan.slopeC[0][1] = (b->Color[0] - a->Color[0]) * scan.invDeltaY[1];
340
scan.c[0][1] = a->Color[0];
344
scan.slopeT[0][1] = (b->Tex[0] - a->Tex[0]) * scan.invDeltaY[1];
345
scan.t[0][1] = a->Tex[0];
349
scan.slopeT[1][1] = (b->Tex[1] - a->Tex[1]) * scan.invDeltaY[1];
350
scan.t[1][1] = a->Tex[1];
353
// apply top-left fill convention, top part
354
yStart = core::ceil32( a->Pos.y );
355
yEnd = core::ceil32( b->Pos.y ) - 1;
358
subPixel = ( (f32) yStart ) - a->Pos.y;
360
// correct to pixel center
361
scan.x[0] += scan.slopeX[0] * subPixel;
362
scan.x[1] += scan.slopeX[1] * subPixel;
365
scan.z[0] += scan.slopeZ[0] * subPixel;
366
scan.z[1] += scan.slopeZ[1] * subPixel;
370
scan.w[0] += scan.slopeW[0] * subPixel;
371
scan.w[1] += scan.slopeW[1] * subPixel;
375
scan.c[0][0] += scan.slopeC[0][0] * subPixel;
376
scan.c[0][1] += scan.slopeC[0][1] * subPixel;
380
scan.t[0][0] += scan.slopeT[0][0] * subPixel;
381
scan.t[0][1] += scan.slopeT[0][1] * subPixel;
385
scan.t[1][0] += scan.slopeT[1][0] * subPixel;
386
scan.t[1][1] += scan.slopeT[1][1] * subPixel;
391
// rasterize the edge scanlines
392
for( line.y = yStart; line.y <= yEnd; ++line.y)
394
line.x[scan.left] = scan.x[0];
395
line.x[scan.right] = scan.x[1];
398
line.z[scan.left] = scan.z[0];
399
line.z[scan.right] = scan.z[1];
403
line.w[scan.left] = scan.w[0];
404
line.w[scan.right] = scan.w[1];
408
line.c[0][scan.left] = scan.c[0][0];
409
line.c[0][scan.right] = scan.c[0][1];
413
line.t[0][scan.left] = scan.t[0][0];
414
line.t[0][scan.right] = scan.t[0][1];
418
line.t[1][scan.left] = scan.t[1][0];
419
line.t[1][scan.right] = scan.t[1][1];
423
scanline_bilinear ();
425
scan.x[0] += scan.slopeX[0];
426
scan.x[1] += scan.slopeX[1];
429
scan.z[0] += scan.slopeZ[0];
430
scan.z[1] += scan.slopeZ[1];
434
scan.w[0] += scan.slopeW[0];
435
scan.w[1] += scan.slopeW[1];
439
scan.c[0][0] += scan.slopeC[0][0];
440
scan.c[0][1] += scan.slopeC[0][1];
444
scan.t[0][0] += scan.slopeT[0][0];
445
scan.t[0][1] += scan.slopeT[0][1];
449
scan.t[1][0] += scan.slopeT[1][0];
450
scan.t[1][1] += scan.slopeT[1][1];
456
// rasterize lower sub-triangle
457
if ( (f32) 0.0 != scan.invDeltaY[2] )
459
// advance to middle point
460
if( (f32) 0.0 != scan.invDeltaY[1] )
462
temp[0] = b->Pos.y - a->Pos.y; // dy
464
scan.x[0] = a->Pos.x + scan.slopeX[0] * temp[0];
466
scan.z[0] = a->Pos.z + scan.slopeZ[0] * temp[0];
469
scan.w[0] = a->Pos.w + scan.slopeW[0] * temp[0];
472
scan.c[0][0] = a->Color[0] + scan.slopeC[0][0] * temp[0];
475
scan.t[0][0] = a->Tex[0] + scan.slopeT[0][0] * temp[0];
478
scan.t[1][0] = a->Tex[1] + scan.slopeT[1][0] * temp[0];
483
// calculate slopes for bottom edge
484
scan.slopeX[1] = (c->Pos.x - b->Pos.x) * scan.invDeltaY[2];
485
scan.x[1] = b->Pos.x;
488
scan.slopeZ[1] = (c->Pos.z - b->Pos.z) * scan.invDeltaY[2];
489
scan.z[1] = b->Pos.z;
493
scan.slopeW[1] = (c->Pos.w - b->Pos.w) * scan.invDeltaY[2];
494
scan.w[1] = b->Pos.w;
498
scan.slopeC[0][1] = (c->Color[0] - b->Color[0]) * scan.invDeltaY[2];
499
scan.c[0][1] = b->Color[0];
503
scan.slopeT[0][1] = (c->Tex[0] - b->Tex[0]) * scan.invDeltaY[2];
504
scan.t[0][1] = b->Tex[0];
508
scan.slopeT[1][1] = (c->Tex[1] - b->Tex[1]) * scan.invDeltaY[2];
509
scan.t[1][1] = b->Tex[1];
512
// apply top-left fill convention, top part
513
yStart = core::ceil32( b->Pos.y );
514
yEnd = core::ceil32( c->Pos.y ) - 1;
518
subPixel = ( (f32) yStart ) - b->Pos.y;
520
// correct to pixel center
521
scan.x[0] += scan.slopeX[0] * subPixel;
522
scan.x[1] += scan.slopeX[1] * subPixel;
525
scan.z[0] += scan.slopeZ[0] * subPixel;
526
scan.z[1] += scan.slopeZ[1] * subPixel;
530
scan.w[0] += scan.slopeW[0] * subPixel;
531
scan.w[1] += scan.slopeW[1] * subPixel;
535
scan.c[0][0] += scan.slopeC[0][0] * subPixel;
536
scan.c[0][1] += scan.slopeC[0][1] * subPixel;
540
scan.t[0][0] += scan.slopeT[0][0] * subPixel;
541
scan.t[0][1] += scan.slopeT[0][1] * subPixel;
545
scan.t[1][0] += scan.slopeT[1][0] * subPixel;
546
scan.t[1][1] += scan.slopeT[1][1] * subPixel;
551
// rasterize the edge scanlines
552
for( line.y = yStart; line.y <= yEnd; ++line.y)
554
line.x[scan.left] = scan.x[0];
555
line.x[scan.right] = scan.x[1];
558
line.z[scan.left] = scan.z[0];
559
line.z[scan.right] = scan.z[1];
563
line.w[scan.left] = scan.w[0];
564
line.w[scan.right] = scan.w[1];
568
line.c[0][scan.left] = scan.c[0][0];
569
line.c[0][scan.right] = scan.c[0][1];
573
line.t[0][scan.left] = scan.t[0][0];
574
line.t[0][scan.right] = scan.t[0][1];
578
line.t[1][scan.left] = scan.t[1][0];
579
line.t[1][scan.right] = scan.t[1][1];
583
scanline_bilinear ();
585
scan.x[0] += scan.slopeX[0];
586
scan.x[1] += scan.slopeX[1];
589
scan.z[0] += scan.slopeZ[0];
590
scan.z[1] += scan.slopeZ[1];
594
scan.w[0] += scan.slopeW[0];
595
scan.w[1] += scan.slopeW[1];
599
scan.c[0][0] += scan.slopeC[0][0];
600
scan.c[0][1] += scan.slopeC[0][1];
604
scan.t[0][0] += scan.slopeT[0][0];
605
scan.t[0][1] += scan.slopeT[0][1];
609
scan.t[1][0] += scan.slopeT[1][0];
610
scan.t[1][1] += scan.slopeT[1][1];
620
} // end namespace video
621
} // end namespace irr
623
#endif // _IRR_COMPILE_WITH_BURNINGSVIDEO_
630
//! creates a flat triangle renderer
631
IBurningShader* createTriangleRendererGouraud2(CBurningVideoDriver* driver)
633
#ifdef _IRR_COMPILE_WITH_BURNINGSVIDEO_
634
return new CTRGouraud2(driver);
637
#endif // _IRR_COMPILE_WITH_BURNINGSVIDEO_
641
} // end namespace video
642
} // end namespace irr