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
28
#ifdef BURNINGVIDEO_RENDERER_FAST
45
#ifndef SOFTWARE_DRIVER_2_USE_VERTEX_COLOR
49
// apply global override
50
#ifndef SOFTWARE_DRIVER_2_PERSPECTIVE_CORRECT
52
#ifndef SOFTWARE_DRIVER_2_PERSPECTIVE_CORRECT
57
#ifndef SOFTWARE_DRIVER_2_SUBTEXEL
61
#if !defined ( SOFTWARE_DRIVER_2_USE_WBUFFER ) && defined ( USE_ZBUFFER )
83
class CTRTextureGouraudNoZ2 : public IBurningShader
88
CTRTextureGouraudNoZ2(CBurningVideoDriver* driver);
90
//! draws an indexed triangle list
91
virtual void drawTriangle ( const s4DVertex *a,const s4DVertex *b,const s4DVertex *c );
95
void scanline_bilinear ();
96
sScanConvertData scan;
102
CTRTextureGouraudNoZ2::CTRTextureGouraudNoZ2(CBurningVideoDriver* driver)
103
: IBurningShader(driver)
106
setDebugName("CTRTextureGouraudNoZ2");
114
void CTRTextureGouraudNoZ2::scanline_bilinear ( )
141
sVec2 slopeT[BURNING_MATERIAL_MAX_TEXTURES];
144
// apply top-left fill-convention, left
145
xStart = core::ceil32( line.x[0] );
146
xEnd = core::ceil32( line.x[1] ) - 1;
154
const f32 invDeltaX = core::reciprocal_approxim ( line.x[1] - line.x[0] );
157
slopeZ = (line.z[1] - line.z[0]) * invDeltaX;
160
slopeW = (line.w[1] - line.w[0]) * invDeltaX;
163
slopeC = (line.c[1] - line.c[0]) * invDeltaX;
166
slopeT[0] = (line.t[0][1] - line.t[0][0]) * invDeltaX;
169
slopeT[1] = (line.t[1][1] - line.t[1][0]) * invDeltaX;
173
subPixel = ( (f32) xStart ) - line.x[0];
175
line.z[0] += slopeZ * subPixel;
178
line.w[0] += slopeW * subPixel;
181
line.c[0] += slopeC * subPixel;
184
line.t[0][0] += slopeT[0] * subPixel;
187
line.t[1][0] += slopeT[1] * subPixel;
191
dst = (tVideoSample*)RenderTarget->lock() + ( line.y * RenderTarget->getDimension().Width ) + xStart;
194
z = (fp24*) DepthBuffer->lock() + ( line.y * RenderTarget->getDimension().Width ) + xStart;
206
for ( s32 i = 0; i <= dx; ++i )
209
if ( line.z[0] < z[i] )
212
if ( line.w[0] >= z[i] )
217
inversew = fix_inverse32 ( line.w[0] );
219
tx0 = tofix ( line.t[0][0].x,inversew);
220
ty0 = tofix ( line.t[0][0].y,inversew);
222
tx0 = tofix ( line.t[0][0].x );
223
ty0 = tofix ( line.t[0][0].y );
225
dst[i] = getTexel_plain ( &IT[0], tx0, ty0 );
228
getSample_texture ( r0, g0, b0, &IT[0], tx0, ty0 );
229
dst[i] = fix_to_color ( r0, g0, b0 );
249
line.t[0][0] += slopeT[0];
252
line.t[1][0] += slopeT[1];
258
void CTRTextureGouraudNoZ2::drawTriangle ( const s4DVertex *a,const s4DVertex *b,const s4DVertex *c )
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);
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 );
273
if ( F32_LOWER_EQUAL_0 ( scan.invDeltaY[0] ) )
276
// find if the major edge is left or right aligned
279
temp[0] = a->Pos.x - c->Pos.x;
281
temp[2] = b->Pos.x - a->Pos.x;
284
scan.left = ( temp[0] * temp[3] - temp[1] * temp[2] ) > 0.f ? 0 : 1;
285
scan.right = 1 - scan.left;
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;
292
scan.slopeZ[0] = (c->Pos.z - a->Pos.z) * scan.invDeltaY[0];
293
scan.z[0] = a->Pos.z;
297
scan.slopeW[0] = (c->Pos.w - a->Pos.w) * scan.invDeltaY[0];
298
scan.w[0] = a->Pos.w;
302
scan.slopeC[0] = (c->Color[0] - a->Color[0]) * scan.invDeltaY[0];
303
scan.c[0] = a->Color[0];
307
scan.slopeT[0][0] = (c->Tex[0] - a->Tex[0]) * scan.invDeltaY[0];
308
scan.t[0][0] = a->Tex[0];
312
scan.slopeT[1][0] = (c->Tex[1] - a->Tex[1]) * scan.invDeltaY[0];
313
scan.t[1][0] = a->Tex[1];
316
// top left fill convention y run
324
// rasterize upper sub-triangle
325
if ( (f32) 0.0 != scan.invDeltaY[1] )
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;
332
scan.slopeZ[1] = (b->Pos.z - a->Pos.z) * scan.invDeltaY[1];
333
scan.z[1] = a->Pos.z;
337
scan.slopeW[1] = (b->Pos.w - a->Pos.w) * scan.invDeltaY[1];
338
scan.w[1] = a->Pos.w;
342
scan.slopeC[1] = (b->Color[0] - a->Color[0]) * scan.invDeltaY[1];
343
scan.c[1] = a->Color[0];
347
scan.slopeT[0][1] = (b->Tex[0] - a->Tex[0]) * scan.invDeltaY[1];
348
scan.t[0][1] = a->Tex[0];
352
scan.slopeT[1][1] = (b->Tex[1] - a->Tex[1]) * scan.invDeltaY[1];
353
scan.t[1][1] = a->Tex[1];
356
// apply top-left fill convention, top part
357
yStart = core::ceil32( a->Pos.y );
358
yEnd = core::ceil32( b->Pos.y ) - 1;
361
subPixel = ( (f32) yStart ) - a->Pos.y;
363
// correct to pixel center
364
scan.x[0] += scan.slopeX[0] * subPixel;
365
scan.x[1] += scan.slopeX[1] * subPixel;
368
scan.z[0] += scan.slopeZ[0] * subPixel;
369
scan.z[1] += scan.slopeZ[1] * subPixel;
373
scan.w[0] += scan.slopeW[0] * subPixel;
374
scan.w[1] += scan.slopeW[1] * subPixel;
378
scan.c[0] += scan.slopeC[0] * subPixel;
379
scan.c[1] += scan.slopeC[1] * subPixel;
383
scan.t[0][0] += scan.slopeT[0][0] * subPixel;
384
scan.t[0][1] += scan.slopeT[0][1] * subPixel;
388
scan.t[1][0] += scan.slopeT[1][0] * subPixel;
389
scan.t[1][1] += scan.slopeT[1][1] * subPixel;
394
// rasterize the edge scanlines
395
for( line.y = yStart; line.y <= yEnd; ++line.y)
397
line.x[scan.left] = scan.x[0];
398
line.x[scan.right] = scan.x[1];
401
line.z[scan.left] = scan.z[0];
402
line.z[scan.right] = scan.z[1];
406
line.w[scan.left] = scan.w[0];
407
line.w[scan.right] = scan.w[1];
411
line.c[scan.left] = scan.c[0];
412
line.c[scan.right] = scan.c[1];
416
line.t[0][scan.left] = scan.t[0][0];
417
line.t[0][scan.right] = scan.t[0][1];
421
line.t[1][scan.left] = scan.t[1][0];
422
line.t[1][scan.right] = scan.t[1][1];
426
scanline_bilinear ( );
428
scan.x[0] += scan.slopeX[0];
429
scan.x[1] += scan.slopeX[1];
432
scan.z[0] += scan.slopeZ[0];
433
scan.z[1] += scan.slopeZ[1];
437
scan.w[0] += scan.slopeW[0];
438
scan.w[1] += scan.slopeW[1];
442
scan.c[0] += scan.slopeC[0];
443
scan.c[1] += scan.slopeC[1];
447
scan.t[0][0] += scan.slopeT[0][0];
448
scan.t[0][1] += scan.slopeT[0][1];
452
scan.t[1][0] += scan.slopeT[1][0];
453
scan.t[1][1] += scan.slopeT[1][1];
459
// rasterize lower sub-triangle
460
if ( (f32) 0.0 != scan.invDeltaY[2] )
462
// advance to middle point
463
if( (f32) 0.0 != scan.invDeltaY[1] )
465
temp[0] = b->Pos.y - a->Pos.y; // dy
467
scan.x[0] = a->Pos.x + scan.slopeX[0] * temp[0];
469
scan.z[0] = a->Pos.z + scan.slopeZ[0] * temp[0];
472
scan.w[0] = a->Pos.w + scan.slopeW[0] * temp[0];
475
scan.c[0] = a->Color[0] + scan.slopeC[0] * temp[0];
478
scan.t[0][0] = a->Tex[0] + scan.slopeT[0][0] * temp[0];
481
scan.t[1][0] = a->Tex[1] + scan.slopeT[1][0] * temp[0];
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;
491
scan.slopeZ[1] = (c->Pos.z - b->Pos.z) * scan.invDeltaY[2];
492
scan.z[1] = b->Pos.z;
496
scan.slopeW[1] = (c->Pos.w - b->Pos.w) * scan.invDeltaY[2];
497
scan.w[1] = b->Pos.w;
501
scan.slopeC[1] = (c->Color[0] - b->Color[0]) * scan.invDeltaY[2];
502
scan.c[1] = b->Color[0];
506
scan.slopeT[0][1] = (c->Tex[0] - b->Tex[0]) * scan.invDeltaY[2];
507
scan.t[0][1] = b->Tex[0];
511
scan.slopeT[1][1] = (c->Tex[1] - b->Tex[1]) * scan.invDeltaY[2];
512
scan.t[1][1] = b->Tex[1];
515
// apply top-left fill convention, top part
516
yStart = core::ceil32( b->Pos.y );
517
yEnd = core::ceil32( c->Pos.y ) - 1;
521
subPixel = ( (f32) yStart ) - b->Pos.y;
523
// correct to pixel center
524
scan.x[0] += scan.slopeX[0] * subPixel;
525
scan.x[1] += scan.slopeX[1] * subPixel;
528
scan.z[0] += scan.slopeZ[0] * subPixel;
529
scan.z[1] += scan.slopeZ[1] * subPixel;
533
scan.w[0] += scan.slopeW[0] * subPixel;
534
scan.w[1] += scan.slopeW[1] * subPixel;
538
scan.c[0] += scan.slopeC[0] * subPixel;
539
scan.c[1] += scan.slopeC[1] * subPixel;
543
scan.t[0][0] += scan.slopeT[0][0] * subPixel;
544
scan.t[0][1] += scan.slopeT[0][1] * subPixel;
548
scan.t[1][0] += scan.slopeT[1][0] * subPixel;
549
scan.t[1][1] += scan.slopeT[1][1] * subPixel;
554
// rasterize the edge scanlines
555
for( line.y = yStart; line.y <= yEnd; ++line.y)
557
line.x[scan.left] = scan.x[0];
558
line.x[scan.right] = scan.x[1];
561
line.z[scan.left] = scan.z[0];
562
line.z[scan.right] = scan.z[1];
566
line.w[scan.left] = scan.w[0];
567
line.w[scan.right] = scan.w[1];
571
line.c[scan.left] = scan.c[0];
572
line.c[scan.right] = scan.c[1];
576
line.t[0][scan.left] = scan.t[0][0];
577
line.t[0][scan.right] = scan.t[0][1];
581
line.t[1][scan.left] = scan.t[1][0];
582
line.t[1][scan.right] = scan.t[1][1];
586
scanline_bilinear ();
588
scan.x[0] += scan.slopeX[0];
589
scan.x[1] += scan.slopeX[1];
592
scan.z[0] += scan.slopeZ[0];
593
scan.z[1] += scan.slopeZ[1];
597
scan.w[0] += scan.slopeW[0];
598
scan.w[1] += scan.slopeW[1];
602
scan.c[0] += scan.slopeC[0];
603
scan.c[1] += scan.slopeC[1];
607
scan.t[0][0] += scan.slopeT[0][0];
608
scan.t[0][1] += scan.slopeT[0][1];
612
scan.t[1][0] += scan.slopeT[1][0];
613
scan.t[1][1] += scan.slopeT[1][1];
622
} // end namespace video
623
} // end namespace irr
625
#endif // _IRR_COMPILE_WITH_BURNINGSVIDEO_
632
//! creates a flat triangle renderer
633
IBurningShader* createTRTextureGouraudNoZ2(CBurningVideoDriver* driver)
635
#ifdef _IRR_COMPILE_WITH_BURNINGSVIDEO_
636
return new CTRTextureGouraudNoZ2( driver );
639
#endif // _IRR_COMPILE_WITH_BURNINGSVIDEO_
643
} // end namespace video
644
} // end namespace irr