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 namespace irr
72 {
73
74 namespace video
75 {
76
77 class CTRTextureLightMap2_M2 : public IBurningShader
78 {
79 public:
80
81 //! constructor
82 CTRTextureLightMap2_M2(CBurningVideoDriver* driver);
83
84 //! draws an indexed triangle list
85 virtual void drawTriangle ( const s4DVertex *a,const s4DVertex *b,const s4DVertex *c );
86
87
88 private:
89 void scanline_bilinear2 ();
90
91 sScanLineData line;
92
93 };
94
95 //! constructor
CTRTextureLightMap2_M2(CBurningVideoDriver * driver)96 CTRTextureLightMap2_M2::CTRTextureLightMap2_M2(CBurningVideoDriver* driver)
97 : IBurningShader(driver)
98 {
99 #ifdef _DEBUG
100 setDebugName("CTRTextureLightMap2_M2");
101 #endif
102 }
103
104 /*!
105 */
scanline_bilinear2()106 REALINLINE void CTRTextureLightMap2_M2::scanline_bilinear2 ()
107 {
108 tVideoSample *dst;
109 fp24 *z;
110
111 s32 xStart;
112 s32 xEnd;
113 s32 dx;
114 s32 i;
115
116
117 // apply top-left fill-convention, left
118 xStart = core::ceil32( line.x[0] );
119 xEnd = core::ceil32( line.x[1] ) - 1;
120
121 dx = xEnd - xStart;
122 if ( dx < 0 )
123 return;
124
125 // slopes
126 const f32 invDeltaX = core::reciprocal_approxim ( line.x[1] - line.x[0] );
127
128 // search z-buffer for first not occulled pixel
129 z = (fp24*) DepthBuffer->lock() + ( line.y * RenderTarget->getDimension().Width ) + xStart;
130
131 // subTexel
132 const f32 subPixel = ( (f32) xStart ) - line.x[0];
133
134 #ifdef IPOL_W
135 const f32 b = (line.w[1] - line.w[0]) * invDeltaX;
136 f32 a = line.w[0] + ( b * subPixel );
137
138 i = 0;
139
140 while ( a <= z[i] )
141 {
142 a += b;
143
144 i += 1;
145 if ( i > dx )
146 return;
147
148 }
149
150 // lazy setup rest of scanline
151
152 line.w[0] = a;
153 line.w[1] = b;
154 #else
155 const f32 b = (line.z[1] - line.z[0]) * invDeltaX;
156 f32 a = line.z[0] + ( b * subPixel );
157
158 i = 0;
159
160 while ( a > z[i] )
161 {
162 a += b;
163
164 i += 1;
165 if ( i > dx )
166 return;
167
168 }
169
170 // lazy setup rest of scanline
171
172 line.z[0] = a;
173 line.z[1] = b;
174 #endif
175 dst = (tVideoSample*)RenderTarget->lock() + ( line.y * RenderTarget->getDimension().Width ) + xStart;
176
177 a = (f32) i + subPixel;
178
179 line.t[0][1] = (line.t[0][1] - line.t[0][0]) * invDeltaX;
180 line.t[1][1] = (line.t[1][1] - line.t[1][0]) * invDeltaX;
181
182 line.t[0][0] += line.t[0][1] * a;
183 line.t[1][0] += line.t[1][1] * a;
184
185
186 #ifdef BURNINGVIDEO_RENDERER_FAST
187 u32 dIndex = ( line.y & 3 ) << 2;
188
189 tFixPoint r0, g0, b0;
190 tFixPoint r1, g1, b1;
191
192 #else
193 //
194 tFixPoint r0, g0, b0;
195 tFixPoint r1, g1, b1;
196 #endif
197
198
199 for ( ;i <= dx; i++ )
200 {
201 #ifdef IPOL_W
202 if ( line.w[0] >= z[i] )
203 {
204 z[i] = line.w[0];
205 #else
206 if ( line.z[0] < z[i] )
207 {
208 z[i] = line.z[0];
209 #endif
210
211 #ifdef SOFTWARE_DRIVER_2_PERSPECTIVE_CORRECT
212 f32 inversew = fix_inverse32 ( line.w[0] );
213 #else
214 f32 inversew = FIX_POINT_F32_MUL;
215 #endif
216
217
218
219 #ifdef BURNINGVIDEO_RENDERER_FAST
220
221 const tFixPointu d = dithermask [ dIndex | ( i ) & 3 ];
222
223 getSample_texture ( r0, g0, b0, &IT[0], d + tofix ( line.t[0][0].x,inversew), d + tofix ( line.t[0][0].y,inversew) );
224 getSample_texture ( r1, g1, b1, &IT[1], d + tofix ( line.t[1][0].x,inversew), d + tofix ( line.t[1][0].y,inversew) );
225 #else
226 getSample_texture ( r0, g0, b0, &IT[0], tofix ( line.t[0][0].x,inversew), tofix ( line.t[0][0].y,inversew) );
227 getSample_texture ( r1, g1, b1, &IT[1], tofix ( line.t[1][0].x,inversew), tofix ( line.t[1][0].y,inversew) );
228
229 #endif
230
231 dst[i] = fix_to_color ( clampfix_maxcolor ( imulFix_tex2 ( r0, r1 ) ),
232 clampfix_maxcolor ( imulFix_tex2 ( g0, g1 ) ),
233 clampfix_maxcolor ( imulFix_tex2 ( b0, b1 ) )
234 );
235 }
236
237 #ifdef IPOL_W
238 line.w[0] += line.w[1];
239 #else
240 line.z[0] += line.z[1];
241 #endif
242 line.t[0][0] += line.t[0][1];
243 line.t[1][0] += line.t[1][1];
244 }
245
246 }
247
248
249
250 void CTRTextureLightMap2_M2::drawTriangle ( const s4DVertex *a,const s4DVertex *b,const s4DVertex *c )
251 {
252 sScanConvertData scan;
253
254 // sort on height, y
255 if ( F32_A_GREATER_B ( a->Pos.y , b->Pos.y ) ) swapVertexPointer(&a, &b);
256 if ( F32_A_GREATER_B ( b->Pos.y , c->Pos.y ) ) swapVertexPointer(&b, &c);
257 if ( F32_A_GREATER_B ( a->Pos.y , b->Pos.y ) ) swapVertexPointer(&a, &b);
258
259 const f32 ca = c->Pos.y - a->Pos.y;
260 const f32 ba = b->Pos.y - a->Pos.y;
261 const f32 cb = c->Pos.y - b->Pos.y;
262 // calculate delta y of the edges
263 scan.invDeltaY[0] = core::reciprocal( ca );
264 scan.invDeltaY[1] = core::reciprocal( ba );
265 scan.invDeltaY[2] = core::reciprocal( cb );
266
267 if ( F32_LOWER_EQUAL_0 ( scan.invDeltaY[0] ) )
268 return;
269
270 // find if the major edge is left or right aligned
271 f32 temp[4];
272
273 temp[0] = a->Pos.x - c->Pos.x;
274 temp[1] = -ca;
275 temp[2] = b->Pos.x - a->Pos.x;
276 temp[3] = ba;
277
278 scan.left = ( temp[0] * temp[3] - temp[1] * temp[2] ) > 0.f ? 0 : 1;
279 scan.right = 1 - scan.left;
280
281 // calculate slopes for the major edge
282 scan.slopeX[0] = (c->Pos.x - a->Pos.x) * scan.invDeltaY[0];
283 scan.x[0] = a->Pos.x;
284
285 #ifdef IPOL_Z
286 scan.slopeZ[0] = (c->Pos.z - a->Pos.z) * scan.invDeltaY[0];
287 scan.z[0] = a->Pos.z;
288 #endif
289
290 #ifdef IPOL_W
291 scan.slopeW[0] = (c->Pos.w - a->Pos.w) * scan.invDeltaY[0];
292 scan.w[0] = a->Pos.w;
293 #endif
294
295 #ifdef IPOL_C0
296 scan.slopeC[0] = (c->Color[0] - a->Color[0]) * scan.invDeltaY[0];
297 scan.c[0] = a->Color[0];
298 #endif
299
300 #ifdef IPOL_T0
301 scan.slopeT[0][0] = (c->Tex[0] - a->Tex[0]) * scan.invDeltaY[0];
302 scan.t[0][0] = a->Tex[0];
303 #endif
304
305 #ifdef IPOL_T1
306 scan.slopeT[1][0] = (c->Tex[1] - a->Tex[1]) * scan.invDeltaY[0];
307 scan.t[1][0] = a->Tex[1];
308 #endif
309
310 // top left fill convention y run
311 s32 yStart;
312 s32 yEnd;
313
314 #ifdef SUBTEXEL
315 f32 subPixel;
316 #endif
317
318 // rasterize upper sub-triangle
319 if ( F32_GREATER_0 ( scan.invDeltaY[1] ) )
320 {
321 // calculate slopes for top edge
322 scan.slopeX[1] = (b->Pos.x - a->Pos.x) * scan.invDeltaY[1];
323 scan.x[1] = a->Pos.x;
324
325 #ifdef IPOL_Z
326 scan.slopeZ[1] = (b->Pos.z - a->Pos.z) * scan.invDeltaY[1];
327 scan.z[1] = a->Pos.z;
328 #endif
329
330 #ifdef IPOL_W
331 scan.slopeW[1] = (b->Pos.w - a->Pos.w) * scan.invDeltaY[1];
332 scan.w[1] = a->Pos.w;
333 #endif
334
335 #ifdef IPOL_C0
336 scan.slopeC[1] = (b->Color[0] - a->Color[0]) * scan.invDeltaY[1];
337 scan.c[1] = a->Color[0];
338 #endif
339
340 #ifdef IPOL_T0
341 scan.slopeT[0][1] = (b->Tex[0] - a->Tex[0]) * scan.invDeltaY[1];
342 scan.t[0][1] = a->Tex[0];
343 #endif
344
345 #ifdef IPOL_T1
346 scan.slopeT[1][1] = (b->Tex[1] - a->Tex[1]) * scan.invDeltaY[1];
347 scan.t[1][1] = a->Tex[1];
348 #endif
349
350 // apply top-left fill convention, top part
351 yStart = core::ceil32( a->Pos.y );
352 yEnd = core::ceil32( b->Pos.y ) - 1;
353
354 #ifdef SUBTEXEL
355 subPixel = ( (f32) yStart ) - a->Pos.y;
356
357 // correct to pixel center
358 scan.x[0] += scan.slopeX[0] * subPixel;
359 scan.x[1] += scan.slopeX[1] * subPixel;
360
361 #ifdef IPOL_Z
362 scan.z[0] += scan.slopeZ[0] * subPixel;
363 scan.z[1] += scan.slopeZ[1] * subPixel;
364 #endif
365
366 #ifdef IPOL_W
367 scan.w[0] += scan.slopeW[0] * subPixel;
368 scan.w[1] += scan.slopeW[1] * subPixel;
369 #endif
370
371 #ifdef IPOL_C0
372 scan.c[0] += scan.slopeC[0] * subPixel;
373 scan.c[1] += scan.slopeC[1] * subPixel;
374 #endif
375
376 #ifdef IPOL_T0
377 scan.t[0][0] += scan.slopeT[0][0] * subPixel;
378 scan.t[0][1] += scan.slopeT[0][1] * subPixel;
379 #endif
380
381 #ifdef IPOL_T1
382 scan.t[1][0] += scan.slopeT[1][0] * subPixel;
383 scan.t[1][1] += scan.slopeT[1][1] * subPixel;
384 #endif
385
386 #endif
387
388 // rasterize the edge scanlines
389 for( line.y = yStart; line.y <= yEnd; ++line.y)
390 {
391 line.x[scan.left] = scan.x[0];
392 line.x[scan.right] = scan.x[1];
393
394 #ifdef IPOL_Z
395 line.z[scan.left] = scan.z[0];
396 line.z[scan.right] = scan.z[1];
397 #endif
398
399 #ifdef IPOL_W
400 line.w[scan.left] = scan.w[0];
401 line.w[scan.right] = scan.w[1];
402 #endif
403
404 #ifdef IPOL_C0
405 line.c[scan.left] = scan.c[0];
406 line.c[scan.right] = scan.c[1];
407 #endif
408
409 #ifdef IPOL_T0
410 line.t[0][scan.left] = scan.t[0][0];
411 line.t[0][scan.right] = scan.t[0][1];
412 #endif
413
414 #ifdef IPOL_T1
415 line.t[1][scan.left] = scan.t[1][0];
416 line.t[1][scan.right] = scan.t[1][1];
417 #endif
418
419 // render a scanline
420 scanline_bilinear2 ();
421
422 scan.x[0] += scan.slopeX[0];
423 scan.x[1] += scan.slopeX[1];
424
425 #ifdef IPOL_Z
426 scan.z[0] += scan.slopeZ[0];
427 scan.z[1] += scan.slopeZ[1];
428 #endif
429
430 #ifdef IPOL_W
431 scan.w[0] += scan.slopeW[0];
432 scan.w[1] += scan.slopeW[1];
433 #endif
434
435 #ifdef IPOL_C0
436 scan.c[0] += scan.slopeC[0];
437 scan.c[1] += scan.slopeC[1];
438 #endif
439
440 #ifdef IPOL_T0
441 scan.t[0][0] += scan.slopeT[0][0];
442 scan.t[0][1] += scan.slopeT[0][1];
443 #endif
444
445 #ifdef IPOL_T1
446 scan.t[1][0] += scan.slopeT[1][0];
447 scan.t[1][1] += scan.slopeT[1][1];
448 #endif
449
450 }
451 }
452
453 // rasterize lower sub-triangle
454 //if ( (f32) 0.0 != scan.invDeltaY[2] )
455 if ( F32_GREATER_0 ( scan.invDeltaY[2] ) )
456 {
457 // advance to middle point
458 if ( F32_GREATER_0 ( scan.invDeltaY[1] ) )
459 {
460 temp[0] = b->Pos.y - a->Pos.y; // dy
461
462 scan.x[0] = a->Pos.x + scan.slopeX[0] * temp[0];
463 #ifdef IPOL_Z
464 scan.z[0] = a->Pos.z + scan.slopeZ[0] * temp[0];
465 #endif
466 #ifdef IPOL_W
467 scan.w[0] = a->Pos.w + scan.slopeW[0] * temp[0];
468 #endif
469 #ifdef IPOL_C0
470 scan.c[0] = a->Color[0] + scan.slopeC[0] * temp[0];
471 #endif
472 #ifdef IPOL_T0
473 scan.t[0][0] = a->Tex[0] + scan.slopeT[0][0] * temp[0];
474 #endif
475 #ifdef IPOL_T1
476 scan.t[1][0] = a->Tex[1] + scan.slopeT[1][0] * temp[0];
477 #endif
478
479 }
480
481 // calculate slopes for bottom edge
482 scan.slopeX[1] = (c->Pos.x - b->Pos.x) * scan.invDeltaY[2];
483 scan.x[1] = b->Pos.x;
484
485 #ifdef IPOL_Z
486 scan.slopeZ[1] = (c->Pos.z - b->Pos.z) * scan.invDeltaY[2];
487 scan.z[1] = b->Pos.z;
488 #endif
489
490 #ifdef IPOL_W
491 scan.slopeW[1] = (c->Pos.w - b->Pos.w) * scan.invDeltaY[2];
492 scan.w[1] = b->Pos.w;
493 #endif
494
495 #ifdef IPOL_C0
496 scan.slopeC[1] = (c->Color[0] - b->Color[0]) * scan.invDeltaY[2];
497 scan.c[1] = b->Color[0];
498 #endif
499
500 #ifdef IPOL_T0
501 scan.slopeT[0][1] = (c->Tex[0] - b->Tex[0]) * scan.invDeltaY[2];
502 scan.t[0][1] = b->Tex[0];
503 #endif
504
505 #ifdef IPOL_T1
506 scan.slopeT[1][1] = (c->Tex[1] - b->Tex[1]) * scan.invDeltaY[2];
507 scan.t[1][1] = b->Tex[1];
508 #endif
509
510 // apply top-left fill convention, top part
511 yStart = core::ceil32( b->Pos.y );
512 yEnd = core::ceil32( c->Pos.y ) - 1;
513
514 #ifdef SUBTEXEL
515
516 subPixel = ( (f32) yStart ) - b->Pos.y;
517
518 // correct to pixel center
519 scan.x[0] += scan.slopeX[0] * subPixel;
520 scan.x[1] += scan.slopeX[1] * subPixel;
521
522 #ifdef IPOL_Z
523 scan.z[0] += scan.slopeZ[0] * subPixel;
524 scan.z[1] += scan.slopeZ[1] * subPixel;
525 #endif
526
527 #ifdef IPOL_W
528 scan.w[0] += scan.slopeW[0] * subPixel;
529 scan.w[1] += scan.slopeW[1] * subPixel;
530 #endif
531
532 #ifdef IPOL_C0
533 scan.c[0] += scan.slopeC[0] * subPixel;
534 scan.c[1] += scan.slopeC[1] * subPixel;
535 #endif
536
537 #ifdef IPOL_T0
538 scan.t[0][0] += scan.slopeT[0][0] * subPixel;
539 scan.t[0][1] += scan.slopeT[0][1] * subPixel;
540 #endif
541
542 #ifdef IPOL_T1
543 scan.t[1][0] += scan.slopeT[1][0] * subPixel;
544 scan.t[1][1] += scan.slopeT[1][1] * subPixel;
545 #endif
546
547 #endif
548
549 // rasterize the edge scanlines
550 for( line.y = yStart; line.y <= yEnd; ++line.y)
551 {
552 line.x[scan.left] = scan.x[0];
553 line.x[scan.right] = scan.x[1];
554
555 #ifdef IPOL_Z
556 line.z[scan.left] = scan.z[0];
557 line.z[scan.right] = scan.z[1];
558 #endif
559
560 #ifdef IPOL_W
561 line.w[scan.left] = scan.w[0];
562 line.w[scan.right] = scan.w[1];
563 #endif
564
565 #ifdef IPOL_C0
566 line.c[scan.left] = scan.c[0];
567 line.c[scan.right] = scan.c[1];
568 #endif
569
570 #ifdef IPOL_T0
571 line.t[0][scan.left] = scan.t[0][0];
572 line.t[0][scan.right] = scan.t[0][1];
573 #endif
574
575 #ifdef IPOL_T1
576 line.t[1][scan.left] = scan.t[1][0];
577 line.t[1][scan.right] = scan.t[1][1];
578 #endif
579
580 // render a scanline
581 scanline_bilinear2 ();
582
583 scan.x[0] += scan.slopeX[0];
584 scan.x[1] += scan.slopeX[1];
585
586 #ifdef IPOL_Z
587 scan.z[0] += scan.slopeZ[0];
588 scan.z[1] += scan.slopeZ[1];
589 #endif
590
591 #ifdef IPOL_W
592 scan.w[0] += scan.slopeW[0];
593 scan.w[1] += scan.slopeW[1];
594 #endif
595
596 #ifdef IPOL_C0
597 scan.c[0] += scan.slopeC[0];
598 scan.c[1] += scan.slopeC[1];
599 #endif
600
601 #ifdef IPOL_T0
602 scan.t[0][0] += scan.slopeT[0][0];
603 scan.t[0][1] += scan.slopeT[0][1];
604 #endif
605
606 #ifdef IPOL_T1
607 scan.t[1][0] += scan.slopeT[1][0];
608 scan.t[1][1] += scan.slopeT[1][1];
609 #endif
610
611 }
612 }
613
614 }
615
616
617 } // end namespace video
618 } // end namespace irr
619
620 #endif // _IRR_COMPILE_WITH_BURNINGSVIDEO_
621
622 namespace irr
623 {
624 namespace video
625 {
626
627
628
629 //! creates a flat triangle renderer
createTriangleRendererTextureLightMap2_M2(CBurningVideoDriver * driver)630 IBurningShader* createTriangleRendererTextureLightMap2_M2(CBurningVideoDriver* driver)
631 {
632 #ifdef _IRR_COMPILE_WITH_BURNINGSVIDEO_
633 return new CTRTextureLightMap2_M2(driver);
634 #else
635 return 0;
636 #endif // _IRR_COMPILE_WITH_BURNINGSVIDEO_
637 }
638
639
640 } // end namespace video
641 } // end namespace irr
642
643
644
645