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
43
// apply global override
44
#ifndef SOFTWARE_DRIVER_2_PERSPECTIVE_CORRECT
48
#ifndef SOFTWARE_DRIVER_2_SUBTEXEL
52
#ifndef SOFTWARE_DRIVER_2_USE_VERTEX_COLOR
56
#if !defined ( SOFTWARE_DRIVER_2_USE_WBUFFER ) && defined ( USE_ZBUFFER )
57
#ifndef SOFTWARE_DRIVER_2_PERSPECTIVE_CORRECT
82
class CTRNormalMap : public IBurningShader
87
CTRNormalMap(CBurningVideoDriver* driver);
89
//! draws an indexed triangle list
90
virtual void drawTriangle ( const s4DVertex *a,const s4DVertex *b,const s4DVertex *c );
94
void scanline_bilinear ();
96
sScanConvertData scan;
102
CTRNormalMap::CTRNormalMap(CBurningVideoDriver* driver)
103
: IBurningShader(driver)
106
setDebugName("CTRNormalMap");
114
void CTRNormalMap::scanline_bilinear ()
138
sVec4 slopeC[MATERIAL_MAX_COLORS];
141
sVec2 slopeT[BURNING_MATERIAL_MAX_TEXTURES];
144
sVec3 slopeL[BURNING_MATERIAL_MAX_TANGENT];
147
// apply top-left fill-convention, left
148
xStart = core::ceil32( line.x[0] );
149
xEnd = core::ceil32( line.x[1] ) - 1;
157
const f32 invDeltaX = core::reciprocal_approxim ( line.x[1] - line.x[0] );
160
slopeZ = (line.z[1] - line.z[0]) * invDeltaX;
163
slopeW = (line.w[1] - line.w[0]) * invDeltaX;
166
slopeC[0] = (line.c[0][1] - line.c[0][0]) * invDeltaX;
169
slopeT[0] = (line.t[0][1] - line.t[0][0]) * invDeltaX;
172
slopeT[1] = (line.t[1][1] - line.t[1][0]) * invDeltaX;
175
slopeT[2] = (line.t[2][1] - line.t[2][0]) * invDeltaX;
178
slopeL[0] = (line.l[0][1] - line.l[0][0]) * invDeltaX;
182
subPixel = ( (f32) xStart ) - line.x[0];
184
line.z[0] += slopeZ * subPixel;
187
line.w[0] += slopeW * subPixel;
190
line.c[0][0] += slopeC[0] * subPixel;
193
line.t[0][0] += slopeT[0] * subPixel;
196
line.t[1][0] += slopeT[1] * subPixel;
199
line.t[2][0] += slopeT[2] * subPixel;
202
line.l[0][0] += slopeL[0] * subPixel;
206
dst = (tVideoSample*)RenderTarget->lock() + ( line.y * RenderTarget->getDimension().Width ) + xStart;
209
z = (fp24*) DepthBuffer->lock() + ( line.y * RenderTarget->getDimension().Width ) + xStart;
220
tFixPoint r0, g0, b0;
221
tFixPoint r1, g1, b1;
222
tFixPoint r2, g2, b2;
224
tFixPoint lx, ly, lz;
231
tFixPoint r3, g3, b3;
234
for ( s32 i = 0; i <= dx; i++ )
237
if ( line.z[0] < z[i] )
240
if ( line.w[0] >= z[i] )
244
inversew = fix_inverse32 ( line.w[0] );
246
tx0 = tofix ( line.t[0][0].x,inversew);
247
ty0 = tofix ( line.t[0][0].y,inversew);
248
tx1 = tofix ( line.t[1][0].x,inversew);
249
ty1 = tofix ( line.t[1][0].y,inversew);
253
r3 = tofix ( line.c[0][0].y ,inversew );
254
g3 = tofix ( line.c[0][0].z ,inversew );
255
b3 = tofix ( line.c[0][0].w ,inversew );
259
tx0 = tofix ( line.t[0][0].x );
260
ty0 = tofix ( line.t[0][0].y );
261
tx1 = tofix ( line.t[1][0].x );
262
ty1 = tofix ( line.t[1][0].y );
265
r3 = tofix ( line.c[0][0].y );
266
g3 = tofix ( line.c[0][0].z );
267
b3 = tofix ( line.c[0][0].w );
271
getSample_texture ( r0, g0, b0, &IT[0], tx0, ty0 );
274
getSample_texture ( r1, g1, b1, &IT[1], tx1, ty1 );
276
r1 = ( r1 - FIX_POINT_HALF_COLOR) >> (COLOR_MAX_LOG2-1);
277
g1 = ( g1 - FIX_POINT_HALF_COLOR) >> (COLOR_MAX_LOG2-1);
278
b1 = ( b1 - FIX_POINT_HALF_COLOR) >> (COLOR_MAX_LOG2-1);
281
sVec3 l = line.l[0][0] * inversew;
284
lx = tofix ( l.x - 0.5f );
285
ly = tofix ( l.y - 0.5f );
286
lz = tofix ( l.z - 0.5f );
289
lx = tofix ( line.l[0][0].x, inversew );
290
ly = tofix ( line.l[0][0].y, inversew );
291
lz = tofix ( line.l[0][0].z, inversew );
293
// DOT 3 Normal Map light in tangent space
294
ndotl = saturateFix ( FIX_POINT_HALF_COLOR + (( imulFix ( r1, lx ) + imulFix ( g1, ly ) + imulFix ( b1, lz ) ) << (COLOR_MAX_LOG2-1)) );
299
r2 = imulFix ( imulFix_tex1 ( r0, ndotl ), r3 );
300
g2 = imulFix ( imulFix_tex1 ( g0, ndotl ), g3 );
301
b2 = imulFix ( imulFix_tex1 ( b0, ndotl ), b3 );
304
// heightmap: (1 - neu ) + alt - 0.5, on_minus_srcalpha + add signed
307
r2 = clampfix_maxcolor ( clampfix_mincolor ( imulFix ( r0 + a4, r3 ) ) );
308
g2 = clampfix_maxcolor ( clampfix_mincolor ( imulFix ( g0 + a4, g3 ) ) );
309
b2 = clampfix_maxcolor ( clampfix_mincolor ( imulFix ( b0 + a4, b3 ) ) );
313
r2 = clampfix_maxcolor ( imulFix_tex1 ( r2, r1 ) );
314
g2 = clampfix_maxcolor ( imulFix_tex1 ( g2, g1 ) );
315
b2 = clampfix_maxcolor ( imulFix_tex1 ( b2, b1 ) );
318
r2 = clampfix_maxcolor ( imulFix_tex4 ( r0, r1 ) );
319
g2 = clampfix_maxcolor ( imulFix_tex4 ( g0, g1 ) );
320
b2 = clampfix_maxcolor ( imulFix_tex4 ( b0, b1 ) );
324
dst[i] = fix_to_color ( r2, g2, b2 );
341
line.c[0][0] += slopeC[0];
344
line.t[0][0] += slopeT[0];
347
line.t[1][0] += slopeT[1];
350
line.t[2][0] += slopeT[2];
353
line.l[0][0] += slopeL[0];
359
void CTRNormalMap::drawTriangle ( const s4DVertex *a,const s4DVertex *b,const s4DVertex *c )
362
if ( F32_A_GREATER_B ( a->Pos.y , b->Pos.y ) ) swapVertexPointer(&a, &b);
363
if ( F32_A_GREATER_B ( b->Pos.y , c->Pos.y ) ) swapVertexPointer(&b, &c);
364
if ( F32_A_GREATER_B ( a->Pos.y , b->Pos.y ) ) swapVertexPointer(&a, &b);
366
const f32 ca = c->Pos.y - a->Pos.y;
367
const f32 ba = b->Pos.y - a->Pos.y;
368
const f32 cb = c->Pos.y - b->Pos.y;
369
// calculate delta y of the edges
370
scan.invDeltaY[0] = core::reciprocal( ca );
371
scan.invDeltaY[1] = core::reciprocal( ba );
372
scan.invDeltaY[2] = core::reciprocal( cb );
374
if ( F32_LOWER_EQUAL_0 ( scan.invDeltaY[0] ) )
377
// find if the major edge is left or right aligned
380
temp[0] = a->Pos.x - c->Pos.x;
382
temp[2] = b->Pos.x - a->Pos.x;
385
scan.left = ( temp[0] * temp[3] - temp[1] * temp[2] ) > 0.f ? 0 : 1;
386
scan.right = 1 - scan.left;
388
// calculate slopes for the major edge
389
scan.slopeX[0] = (c->Pos.x - a->Pos.x) * scan.invDeltaY[0];
390
scan.x[0] = a->Pos.x;
393
scan.slopeZ[0] = (c->Pos.z - a->Pos.z) * scan.invDeltaY[0];
394
scan.z[0] = a->Pos.z;
398
scan.slopeW[0] = (c->Pos.w - a->Pos.w) * scan.invDeltaY[0];
399
scan.w[0] = a->Pos.w;
403
scan.slopeC[0][0] = (c->Color[0] - a->Color[0]) * scan.invDeltaY[0];
404
scan.c[0][0] = a->Color[0];
408
scan.slopeT[0][0] = (c->Tex[0] - a->Tex[0]) * scan.invDeltaY[0];
409
scan.t[0][0] = a->Tex[0];
413
scan.slopeT[1][0] = (c->Tex[1] - a->Tex[1]) * scan.invDeltaY[0];
414
scan.t[1][0] = a->Tex[1];
418
scan.slopeT[2][0] = (c->Tex[2] - a->Tex[2]) * scan.invDeltaY[0];
419
scan.t[2][0] = a->Tex[2];
423
scan.slopeL[0][0] = (c->LightTangent[0] - a->LightTangent[0]) * scan.invDeltaY[0];
424
scan.l[0][0] = a->LightTangent[0];
427
// top left fill convention y run
436
// rasterize upper sub-triangle
437
//if ( (f32) 0.0 != scan.invDeltaY[1] )
438
if ( F32_GREATER_0 ( scan.invDeltaY[1] ) )
440
// calculate slopes for top edge
441
scan.slopeX[1] = (b->Pos.x - a->Pos.x) * scan.invDeltaY[1];
442
scan.x[1] = a->Pos.x;
445
scan.slopeZ[1] = (b->Pos.z - a->Pos.z) * scan.invDeltaY[1];
446
scan.z[1] = a->Pos.z;
450
scan.slopeW[1] = (b->Pos.w - a->Pos.w) * scan.invDeltaY[1];
451
scan.w[1] = a->Pos.w;
455
scan.slopeC[0][1] = (b->Color[0] - a->Color[0]) * scan.invDeltaY[1];
456
scan.c[0][1] = a->Color[0];
460
scan.slopeT[0][1] = (b->Tex[0] - a->Tex[0]) * scan.invDeltaY[1];
461
scan.t[0][1] = a->Tex[0];
465
scan.slopeT[1][1] = (b->Tex[1] - a->Tex[1]) * scan.invDeltaY[1];
466
scan.t[1][1] = a->Tex[1];
470
scan.slopeT[2][1] = (b->Tex[2] - a->Tex[2]) * scan.invDeltaY[1];
471
scan.t[2][1] = a->Tex[2];
475
scan.slopeL[0][1] = (b->LightTangent[0] - a->LightTangent[0]) * scan.invDeltaY[1];
476
scan.l[0][1] = a->LightTangent[0];
479
// apply top-left fill convention, top part
480
yStart = core::ceil32( a->Pos.y );
481
yEnd = core::ceil32( b->Pos.y ) - 1;
484
subPixel = ( (f32) yStart ) - a->Pos.y;
486
// correct to pixel center
487
scan.x[0] += scan.slopeX[0] * subPixel;
488
scan.x[1] += scan.slopeX[1] * subPixel;
491
scan.z[0] += scan.slopeZ[0] * subPixel;
492
scan.z[1] += scan.slopeZ[1] * subPixel;
496
scan.w[0] += scan.slopeW[0] * subPixel;
497
scan.w[1] += scan.slopeW[1] * subPixel;
501
scan.c[0][0] += scan.slopeC[0][0] * subPixel;
502
scan.c[0][1] += scan.slopeC[0][1] * subPixel;
506
scan.t[0][0] += scan.slopeT[0][0] * subPixel;
507
scan.t[0][1] += scan.slopeT[0][1] * subPixel;
511
scan.t[1][0] += scan.slopeT[1][0] * subPixel;
512
scan.t[1][1] += scan.slopeT[1][1] * subPixel;
516
scan.t[2][0] += scan.slopeT[2][0] * subPixel;
517
scan.t[2][1] += scan.slopeT[2][1] * subPixel;
521
scan.l[0][0] += scan.slopeL[0][0] * subPixel;
522
scan.l[0][1] += scan.slopeL[0][1] * subPixel;
527
// rasterize the edge scanlines
528
for( line.y = yStart; line.y <= yEnd; ++line.y)
530
line.x[scan.left] = scan.x[0];
531
line.x[scan.right] = scan.x[1];
534
line.z[scan.left] = scan.z[0];
535
line.z[scan.right] = scan.z[1];
539
line.w[scan.left] = scan.w[0];
540
line.w[scan.right] = scan.w[1];
544
line.c[0][scan.left] = scan.c[0][0];
545
line.c[0][scan.right] = scan.c[0][1];
549
line.t[0][scan.left] = scan.t[0][0];
550
line.t[0][scan.right] = scan.t[0][1];
554
line.t[1][scan.left] = scan.t[1][0];
555
line.t[1][scan.right] = scan.t[1][1];
559
line.t[2][scan.left] = scan.t[2][0];
560
line.t[2][scan.right] = scan.t[2][1];
564
line.l[0][scan.left] = scan.l[0][0];
565
line.l[0][scan.right] = scan.l[0][1];
569
scanline_bilinear ();
571
scan.x[0] += scan.slopeX[0];
572
scan.x[1] += scan.slopeX[1];
575
scan.z[0] += scan.slopeZ[0];
576
scan.z[1] += scan.slopeZ[1];
580
scan.w[0] += scan.slopeW[0];
581
scan.w[1] += scan.slopeW[1];
585
scan.c[0][0] += scan.slopeC[0][0];
586
scan.c[0][1] += scan.slopeC[0][1];
590
scan.t[0][0] += scan.slopeT[0][0];
591
scan.t[0][1] += scan.slopeT[0][1];
595
scan.t[1][0] += scan.slopeT[1][0];
596
scan.t[1][1] += scan.slopeT[1][1];
600
scan.t[2][0] += scan.slopeT[2][0];
601
scan.t[2][1] += scan.slopeT[2][1];
605
scan.l[0][0] += scan.slopeL[0][0];
606
scan.l[0][1] += scan.slopeL[0][1];
612
// rasterize lower sub-triangle
613
//if ( (f32) 0.0 != scan.invDeltaY[2] )
614
if ( F32_GREATER_0 ( scan.invDeltaY[2] ) )
616
// advance to middle point
617
//if( (f32) 0.0 != scan.invDeltaY[1] )
618
if ( F32_GREATER_0 ( scan.invDeltaY[1] ) )
620
temp[0] = b->Pos.y - a->Pos.y; // dy
622
scan.x[0] = a->Pos.x + scan.slopeX[0] * temp[0];
624
scan.z[0] = a->Pos.z + scan.slopeZ[0] * temp[0];
627
scan.w[0] = a->Pos.w + scan.slopeW[0] * temp[0];
630
scan.c[0][0] = a->Color[0] + scan.slopeC[0][0] * temp[0];
633
scan.t[0][0] = a->Tex[0] + scan.slopeT[0][0] * temp[0];
636
scan.t[1][0] = a->Tex[1] + scan.slopeT[1][0] * temp[0];
639
scan.t[2][0] = a->Tex[2] + scan.slopeT[2][0] * temp[0];
642
scan.l[0][0] = a->LightTangent[0] + scan.slopeL[0][0] * temp[0];
647
// calculate slopes for bottom edge
648
scan.slopeX[1] = (c->Pos.x - b->Pos.x) * scan.invDeltaY[2];
649
scan.x[1] = b->Pos.x;
652
scan.slopeZ[1] = (c->Pos.z - b->Pos.z) * scan.invDeltaY[2];
653
scan.z[1] = b->Pos.z;
657
scan.slopeW[1] = (c->Pos.w - b->Pos.w) * scan.invDeltaY[2];
658
scan.w[1] = b->Pos.w;
662
scan.slopeC[0][1] = (c->Color[0] - b->Color[0]) * scan.invDeltaY[2];
663
scan.c[0][1] = b->Color[0];
667
scan.slopeT[0][1] = (c->Tex[0] - b->Tex[0]) * scan.invDeltaY[2];
668
scan.t[0][1] = b->Tex[0];
672
scan.slopeT[1][1] = (c->Tex[1] - b->Tex[1]) * scan.invDeltaY[2];
673
scan.t[1][1] = b->Tex[1];
677
scan.slopeT[2][1] = (c->Tex[2] - b->Tex[2]) * scan.invDeltaY[2];
678
scan.t[2][1] = b->Tex[2];
682
scan.slopeL[0][1] = (c->LightTangent[0] - b->LightTangent[0]) * scan.invDeltaY[2];
683
scan.l[0][1] = b->LightTangent[0];
686
// apply top-left fill convention, top part
687
yStart = core::ceil32( b->Pos.y );
688
yEnd = core::ceil32( c->Pos.y ) - 1;
692
subPixel = ( (f32) yStart ) - b->Pos.y;
694
// correct to pixel center
695
scan.x[0] += scan.slopeX[0] * subPixel;
696
scan.x[1] += scan.slopeX[1] * subPixel;
699
scan.z[0] += scan.slopeZ[0] * subPixel;
700
scan.z[1] += scan.slopeZ[1] * subPixel;
704
scan.w[0] += scan.slopeW[0] * subPixel;
705
scan.w[1] += scan.slopeW[1] * subPixel;
709
scan.c[0][0] += scan.slopeC[0][0] * subPixel;
710
scan.c[0][1] += scan.slopeC[0][1] * subPixel;
714
scan.t[0][0] += scan.slopeT[0][0] * subPixel;
715
scan.t[0][1] += scan.slopeT[0][1] * subPixel;
719
scan.t[1][0] += scan.slopeT[1][0] * subPixel;
720
scan.t[1][1] += scan.slopeT[1][1] * subPixel;
724
scan.t[2][0] += scan.slopeT[2][0] * subPixel;
725
scan.t[2][1] += scan.slopeT[2][1] * subPixel;
729
scan.l[0][0] += scan.slopeL[0][0] * subPixel;
730
scan.l[0][1] += scan.slopeL[0][1] * subPixel;
735
// rasterize the edge scanlines
736
for( line.y = yStart; line.y <= yEnd; ++line.y)
738
line.x[scan.left] = scan.x[0];
739
line.x[scan.right] = scan.x[1];
742
line.z[scan.left] = scan.z[0];
743
line.z[scan.right] = scan.z[1];
747
line.w[scan.left] = scan.w[0];
748
line.w[scan.right] = scan.w[1];
752
line.c[0][scan.left] = scan.c[0][0];
753
line.c[0][scan.right] = scan.c[0][1];
757
line.t[0][scan.left] = scan.t[0][0];
758
line.t[0][scan.right] = scan.t[0][1];
762
line.t[1][scan.left] = scan.t[1][0];
763
line.t[1][scan.right] = scan.t[1][1];
767
line.t[2][scan.left] = scan.t[2][0];
768
line.t[2][scan.right] = scan.t[2][1];
772
line.l[0][scan.left] = scan.l[0][0];
773
line.l[0][scan.right] = scan.l[0][1];
777
scanline_bilinear ();
779
scan.x[0] += scan.slopeX[0];
780
scan.x[1] += scan.slopeX[1];
783
scan.z[0] += scan.slopeZ[0];
784
scan.z[1] += scan.slopeZ[1];
788
scan.w[0] += scan.slopeW[0];
789
scan.w[1] += scan.slopeW[1];
793
scan.c[0][0] += scan.slopeC[0][0];
794
scan.c[0][1] += scan.slopeC[0][1];
798
scan.t[0][0] += scan.slopeT[0][0];
799
scan.t[0][1] += scan.slopeT[0][1];
803
scan.t[1][0] += scan.slopeT[1][0];
804
scan.t[1][1] += scan.slopeT[1][1];
807
scan.t[2][0] += scan.slopeT[2][0];
808
scan.t[2][1] += scan.slopeT[2][1];
812
scan.l[0][0] += scan.slopeL[0][0];
813
scan.l[0][1] += scan.slopeL[0][1];
822
} // end namespace video
823
} // end namespace irr
825
#endif // _IRR_COMPILE_WITH_BURNINGSVIDEO_
833
//! creates a triangle renderer
834
IBurningShader* createTRNormalMap(CBurningVideoDriver* driver)
836
#ifdef _IRR_COMPILE_WITH_BURNINGSVIDEO_
837
return new CTRNormalMap(driver);
840
#endif // _IRR_COMPILE_WITH_BURNINGSVIDEO_
844
} // end namespace video
845
} // end namespace irr