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