-
Notifications
You must be signed in to change notification settings - Fork 8
/
ScanLine.cpp
829 lines (719 loc) · 19.5 KB
/
ScanLine.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
#include "ScanLine.h"
#include <QImage>
#include <cmath>
#include <cassert>
#include "Point3D.h"
using std::fabs;
using std::max;
using std::min;
using std::tan;
using trimeshVec::normalize;
#define SATURATE(x) ( ((x)>255) ? 255 : (((x)<0) ? 0 : x) )
#define ROUND(x) int((x)+0.5)
#define DEFAULT_COLOR Color4u(255, 255, 255, 255)
#define DISABLE_STATE(state,pro) {(state) &= ~(pro);}
#define EABLE_STATE(state,pro) {(state) |= (pro);}
//////////////////////////////////////////////////////////////////////////
CScanLine::CScanLine()
: mType(SL_NONE), mMaxY(-1)
, mbInitialised(false)
, mbHasNormals(true)
{
_init();
}
CScanLine::CScanLine(int _w, int _h, QImage* _img)
: mType(SL_NONE), mMaxY(-1), mCurY(_h-1)
, mHeight(_h), mWidth(_w), mImg(_img)
, mZBuffer(_h*_w, 1.0)
, mbInitialised(true)
, mbHasNormals(true)
{
_init();
}
void CScanLine::_init()
{
mCurColor[0] = 1.0;
mCurColor[1] = 1.0;
mCurColor[2] = 1.0;
mCurColor[3] = 1.0;
mCurNormal[0] = 0;
mCurNormal[1] = 0;
mCurNormal[2] = 0;
mGlobalAmbient = Color4d(0.1, 0.1, 0.1, 1.0);
}
void CScanLine::setRenderTarget(int _w, int _h, QImage *_img)
{
mCurY = _h-1;
mWidth = _w;
mHeight = _h;
mImg = _img;
mZBuffer.assign(_w*_h, 1.0);
mbInitialised = true;
}
CScanLine::~CScanLine(void)
{
_clear();
}
void CScanLine::_clear()
{
ETableIterator it = mSortedET.begin();
ETableIterator it_end = mSortedET.end();
for (; it!=it_end; ++it)
{
EListIterator itl = it->second.begin();
EListIterator itl_end = it->second.end();
for (; itl!=itl_end; ++itl )
{
SAFE_DELETE(*itl);
}
}
TArrayItor itt = mTriArray.begin();
TArrayItor itt_end = mTriArray.end();
for (; itt!=itt_end; ++itt)
{
SAFE_DELETE(*itt);
}
VBufferItor itv = mVertexBuffer.begin();
VBufferItor itv_end = mVertexBuffer.end();
for (; itv!=itv_end; ++itv)
{
SAFE_DELETE(*itv);
}
mSortedET.clear();
mTriArray.clear();
mAEL.clear();
mVertexBuffer.clear();
}
void CScanLine::clear(int _target, const Color4u& _c /* = Color4u */, double _depth /* = 1.0 */)
{
if (_target & SL_COLOR_BUFFER)
{
mImg->fill(qRgba(_c[0], _c[1], _c[2], _c[3]));
}
if (_target & SL_DEPTH_BUFFER)
{
// z buffer reassignment
mZBuffer.assign(mWidth*mHeight, _depth);
}
}
void CScanLine::begin(TargetType _type)
{
if (!mbInitialised) return;
mType = _type;
_clear();
mMaxY = -1;
mCurY = mHeight-1;
}
//------------------------------------------------------------------------------
// Vertices
//------------------------------------------------------------------------------
void CScanLine::vertex3d(double _x, double _y, double _z)
{
Vertex *t_pv = new Vertex(_x, _y, _z, 1);
t_pv->color = mCurColor;
t_pv->normalWorld = mCurNormal;
mVertexBuffer.push_back(t_pv);
}
void CScanLine::vertex3dv(const Vec3d& v)
{
vertex3d(v[0], v[1], v[2]);
}
void CScanLine::vertex3fv(const CPoint3D& v)
{
vertex3d(v.x, v.y, v.z);
}
//------------------------------------------------------------------------------
// Colors
//------------------------------------------------------------------------------
void CScanLine::color3i(unsigned char _r, unsigned char _g, unsigned char _b)
{
mCurColor[0] = _r/255.0;
mCurColor[1] = _g/255.0;
mCurColor[2] = _b/255.0;
mCurColor[3] = 1.0;
}
void CScanLine::color4i(unsigned char _r, unsigned char _g, unsigned char _b, unsigned char _a)
{
mCurColor[0] = _r/255.0;
mCurColor[1] = _g/255.0;
mCurColor[2] = _b/255.0;
mCurColor[3] = _a/255.0;
}
void CScanLine::color3f(float _r, float _g, float _b)
{
mCurColor[0] = _r;
mCurColor[1] = _g;
mCurColor[2] = _b;
mCurColor[3] = 1.0;
}
void CScanLine::color4f(float _r, float _g, float _b, float _a)
{
mCurColor[0] = _r;
mCurColor[1] = _g;
mCurColor[2] = _b;
mCurColor[3] = _a;
}
//------------------------------------------------------------------------------
// Normals
//------------------------------------------------------------------------------
void CScanLine::normal3d(double _nx, double _ny, double _nz)
{
mCurNormal[0] = _nx;
mCurNormal[1] = _ny;
mCurNormal[2] = _nz;
}
void CScanLine::normal3fv(const CPoint3D& _n)
{
mCurNormal[0] = _n.x;
mCurNormal[1] = _n.y;
mCurNormal[2] = _n.z;
}
//void CScanLine::setLight(const Light& light)
//{
// if (mLights.empty())
// mLights.push_back(light);
// else
// mLights[0] = light;
//
// mLights.back().direction.norm();
//}
void CScanLine::end()
{
_modelViewProjectionTransform();
//_normalizeDeviceCoordinates();
_screenCoordinates();
switch (mType)
{
case SL_TRIANGLES:
_addTriangles();
break;
case SL_TRIANGLE_STRIP:
_addTriangleStrip();
break;
case SL_TRIANGLE_FAN:
_addTriangleFan();
break;
case SL_QUADS:
_addQuads();
break;
case SL_QUAD_STRIP:
_addQuadStrip();
break;
case SL_POLYGON:
_addPolygon();
return;
break;
case SL_NONE:
return;
break;
default :
return;
break;
}
_scanLine();
mType = SL_NONE;
}
void CScanLine::_addTriangles()
{
int v_num = mVertexBuffer.size()/3;
for (int i=0; i<v_num; ++i)
{
Vertex *t_v1 = mVertexBuffer[3*i];
Vertex *t_v2 = mVertexBuffer[3*i+1];
Vertex *t_v3 = mVertexBuffer[3*i+2];
_addATriangle(t_v1, t_v2, t_v3);
}
}
void CScanLine::_addATriangle(const Vertex *_v1, const Vertex *_v2, const Vertex *_v3)
{
// on x-z face, skip
if (_v1->posScreen[1] == _v2->posScreen[1] && _v1->posScreen[1] == _v3->posScreen[1])
return;
Normald t_nor = (_v2->posScreen - _v1->posScreen) CROSS (_v3->posScreen - _v1->posScreen);
if (t_nor[2]<0) // back cull, faster
return;
Triangle *tri = new Triangle;
tri->normal = t_nor;
tri->d = -(tri->normal DOT _v1->posScreen);
int t_minY = min(_v3->posScreen[1], min(_v1->posScreen[1], _v2->posScreen[1]));
int t_maxY = max(_v3->posScreen[1], max(_v1->posScreen[1], _v2->posScreen[1]));
tri->dy = t_maxY - t_minY;
int t_id = mTriArray.size();
_addEdge(_v1, _v2, t_id);
_addEdge(_v2, _v3, t_id);
_addEdge(_v3, _v1, t_id);
mTriArray.push_back(tri);
}
bool CScanLine::_addEdge(const Vertex* _v1, const Vertex* _v2, int _id)
{
assert(_id==mTriArray.size());
if (_v1->posScreen[1] == _v2->posScreen[1])
return false;
// clipping
// swap, so that y coordinates are sorted decreasingly
if (_v1->posScreen[1] < _v2->posScreen[1])
{
const Vertex *t_v = _v1;
_v1 = _v2;
_v2 = t_v;
}
// create edge
Edge *edge = new Edge;
edge->id = _id;
edge->dy = _v1->posScreen[1] - _v2->posScreen[1];
edge->x = _v2->posScreen[0];
edge->dx = (_v1->posScreen[0] - _v2->posScreen[0]) * 1.0 / edge->dy;
//color
const Color4d &c1 = _v1->color;
const Color4d &c2 = _v2->color;
edge->color = c2;
edge->dclr = (c1 - c2);
edge->dclr /= 1.0 * edge->dy;
// position in world space
const Vec4d &p1 = _v1->posWorld;
const Vec4d &p2 = _v2->posWorld;
edge->posW = p2;
edge->dPosW = (p1 - p2);
edge->dPosW /= 1.0 * edge->dy;
// normal in world space
if (mRenderState.isLighting())
{
const Normald &n1 = _v1->normalWorld;
const Normald &n2 = _v2->normalWorld;
edge->normalW = n2;
edge->dNorW = (n1 - n2);
edge->dNorW /= 1.0 * edge->dy;
}
// add new edge
mSortedET[_v2->posScreen[1]].push_back(edge);
if (_v2->posScreen[1] < mCurY)
mCurY = _v2->posScreen[1];
if (_v1->posScreen[1] > mMaxY) // && _v1->posScreen[1] < mHeight)
mMaxY = _v1->posScreen[1];
return true;
}
void CScanLine::_addTriangleStrip()
{
int v_num = mVertexBuffer.size();
for (int i=0; i<v_num-2; ++i)
{
Vertex *t_v1 = mVertexBuffer[i];
Vertex *t_v2 = mVertexBuffer[i+1];
Vertex *t_v3 = mVertexBuffer[i+2];
_addATriangle(t_v1, t_v2, t_v3);
}
}
void CScanLine::_addTriangleFan()
{
}
void CScanLine::_addQuads()
{
int v_num = mVertexBuffer.size();
for (int i=0; i<v_num/4; ++i)
{
for (int j=0; j<2; ++j)
{
Vertex *t_v1 = mVertexBuffer[4*i];
Vertex *t_v2 = mVertexBuffer[4*i+j+1];
Vertex *t_v3 = mVertexBuffer[4*i+j+2];
_addATriangle(t_v1, t_v2, t_v3);
}
}
}
void CScanLine::_addQuadStrip()
{
}
void CScanLine::_addPolygon()
{
}
bool CScanLine::_compare_edges(const Edge* e1, const Edge* e2)
{
return (e1->x == e2->x) ? (e1->dx < e2->dx) : (e1->x < e2->x);
}
void CScanLine::_scanLine()
{
if (mMaxY>=mHeight) mMaxY=mHeight-1;
int nNextY = mCurY; // the line which will be added next time
ETableIterator itset_next = mSortedET.begin(); // related iterator
while (mCurY<=mMaxY)
{
ActiveEdge *t_ae;
AEListItor itae, itae_end;
// Step 1: add edges at mCurY line to Active Edge List
//ETableIterator it_set = mSortedET.find(mCurY);
if (mCurY==nNextY && itset_next != mSortedET.end()
&& !(itset_next->second.empty()))
{
EListIterator itl = itset_next->second.begin();
EListIterator itl_end = itset_next->second.end();
for (; itl!=itl_end; ++itl)
{
int id = (*itl)->id;
itae = mAEL.find(id);
if (itae!=mAEL.end())
{
t_ae = &(itae->second);
if (t_ae->el->dy == 0)
t_ae->el = *itl;
if (t_ae->er->dy == 0)
t_ae->er = *itl;
}
else
{ // if it's a new triangle
//Edge *el = *itl;
//++itl;
//Edge *er = *itl;
//if (el->id!=er->id)
//{
// int i = 0;
//}
ActiveEdge *t_ae = &mAEL[id];
//t_ae->el = el;
//t_ae->er = er;
t_ae->el = *itl;
++itl;
assert(t_ae->el->id == (*itl)->id);
t_ae->er = *itl;
if (!_compare_edges(t_ae->el, t_ae->er))
{
Edge* t_e = t_ae->er;
t_ae->er = t_ae->el;
t_ae->el = t_e;
}
// calculate z depth at left
Triangle &tri = *(mTriArray[id]);
Edge *t_el = t_ae->el;
t_ae->zl = -(tri.normal[0]*t_el->x + tri.normal[1]*mCurY + tri.d) / (tri.normal[2]);
t_ae->dzx = -tri.normal[0] / tri.normal[2];
t_ae->dzy = -tri.normal[1] / tri.normal[2];
}
}
++itset_next;
if (itset_next != mSortedET.end())
nNextY = itset_next->first;
}// end if (mCurY==nNextY && it_set != mSortedET.end() && !(it_set->second.empty()))
if (mCurY>=0)
{
// Step 2: fill the region in pairs, horizontal operations
itae = mAEL.begin();
itae_end = mAEL.end();
for (; itae!=itae_end; ++itae )
{
t_ae = &(itae->second);
Edge *e1 = t_ae->el;
Edge *e2 = t_ae->er;
double t_xl = e1->x; // valid x on the left side
double t_xr = e2->x; // valid x on the right side
// skip the scan line which is out of region
if (t_xl>=mWidth || t_xr<0)
{// out of area
continue;
}
// calculate the interpolated color
double skip_x = e2->x - e1->x;
Color4d t_dclr(0, 0, 0, 0);
Vec4d t_dposW(0,0,0,0);
Normald t_dnorW(0, 0, 0);
if (skip_x>0)
{
t_dclr = (e2->color - e1->color) / skip_x;
if ( mRenderState.isSmoothShading() )
{
t_dposW = (e2->posW - e1->posW) / skip_x;
t_dnorW = (e2->normalW - e1->normalW) / skip_x;
}
}
Color4d t_color = e1->color;
Vec4d t_posW = e1->posW;
Normald t_norW = e1->normalW;
double t_zl = t_ae->zl; // z depth on the left side
// skip the region over the left
if (t_xl<0)
{
t_color += t_dclr*(-t_xl);
if (mRenderState.isSmoothShading())
{
t_posW += t_dposW*(-t_xl);
t_norW += t_dnorW*(-t_xl);
}
t_xl = 0;
}
Color4d t_final_clr;
//for (int pi = std::max(e1->x,0.0); pi<std::min(e2->x+1, (double)mImg->width()); ++pi)
for (int pi = int(t_xl); pi<std::min(int(t_xr+1), mWidth); ++pi)
{
if (t_zl<mZBuffer[mCurY*mWidth+pi])
{
t_final_clr = t_color;
if ( mRenderState.isSmoothShading() )
_calculateLight(t_posW, t_norW, t_final_clr);
_setFrameBuffer(mCurY, pi, t_final_clr);
mZBuffer[mCurY*mWidth+pi] = t_zl;
}
// update color, normal, zl
t_color += t_dclr;
t_zl += t_ae->dzx;
if ( mRenderState.isSmoothShading() )
{
t_posW += t_dposW;
t_norW += t_dnorW;
}
}
}
}
// Step 3: update the edges, vertical operations
itae = mAEL.begin();
itae_end = mAEL.end();
if (mCurY<0)
{
double nMinY = -mCurY; // NOTE
if (itset_next != mSortedET.end() && !(itset_next->second.empty())
&& (nNextY-mCurY < nMinY) )
{
nMinY = nNextY-mCurY;
}
if (nMinY)
for (; itae!=itae_end; ++itae)
{
t_ae = &(itae->second);
if (t_ae->el->dy < nMinY)
{
nMinY = t_ae->el->dy;
}
if (t_ae->er->dy < nMinY)
{
nMinY = t_ae->er->dy;
}
}
itae = mAEL.begin();
for (; itae!=itae_end;)
{
t_ae = &(itae->second);
// dy
t_ae->el->dy -= nMinY;
t_ae->er->dy -= nMinY;
if (t_ae->el->dy == 0 && t_ae->er->dy == 0)
{
itae = mAEL.erase(itae);
continue;
}
// x, zl
t_ae->el->x += t_ae->el->dx * nMinY;
t_ae->er->x += t_ae->er->dx * nMinY;
t_ae->zl += (t_ae->dzx * t_ae->el->dx + t_ae->dzy) * nMinY;
// color
t_ae->el->color += t_ae->el->dclr * nMinY;
t_ae->er->color += t_ae->er->dclr * nMinY;
if ( mRenderState.isSmoothShading() )
{
// normal in WS
t_ae->el->normalW += t_ae->el->dNorW * nMinY;
t_ae->er->normalW += t_ae->er->dNorW * nMinY;
// position in WS
t_ae->el->posW += t_ae->el->dPosW * nMinY;
t_ae->er->posW += t_ae->er->dPosW * nMinY;
}
++itae;
}
mCurY += int(nMinY);
}
else
{// mCurY>=0
for (; itae!=itae_end;)
{
// remove the edges whose nearby edge is at the other side of the scan line.
t_ae = &(itae->second);
if (t_ae->el->dy == 0 && t_ae->er->dy == 0)
{
itae = mAEL.erase(itae);
}
else
{
// dy
--t_ae->el->dy;
--t_ae->er->dy;
// x, zl
t_ae->el->x += t_ae->el->dx;
t_ae->er->x += t_ae->er->dx;
t_ae->zl += t_ae->dzx * t_ae->el->dx + t_ae->dzy;
// color
t_ae->el->color += t_ae->el->dclr;
t_ae->er->color += t_ae->er->dclr;
if ( mRenderState.isSmoothShading() )
{
// normal in WS
t_ae->el->normalW += t_ae->el->dNorW;
t_ae->er->normalW += t_ae->er->dNorW;
// position in WS
t_ae->el->posW += t_ae->el->dPosW;
t_ae->er->posW += t_ae->er->dPosW;
}
++itae;
}
}//end for update edges
++mCurY;
}
}
}
void CScanLine::_setFrameBuffer(int _y, int _x, Color4d& _clr)
{
if (mRenderState.isBlending())
{
QRgb oldclr = mImg->pixel(_x, mHeight-_y-1);
double t_a = _clr[3];
_clr[0] = _clr[0] * t_a + qRed(oldclr) * (1-t_a);
_clr[1] = _clr[1] * t_a + qGreen(oldclr) * (1-t_a);
_clr[2] = _clr[2] * t_a + qBlue(oldclr) * (1-t_a);
_clr[3] = 1.0;
}
_clr *= 255.0;
mImg->setPixel(_x, mHeight-_y-1,
qRgb(SATURATE(_clr[0]), SATURATE(_clr[1]), SATURATE(_clr[2])) );
}
void CScanLine::_calculateLight(const Vec4d& _pos, const Normald& _nor, Color4d& _clr)
{
if ( !mRenderState.isLighting())
return;
Normald normal = _nor.normed();
Color4d diffuse, ambient, specular, globalAmbient, finalColor;
double NdotL, NdotHV, dist, att;
Vec3d lightDir, halfVector;
Vec3d eyeDir = mCamera.pos() - _pos;
dist = eyeDir.length();
eyeDir.norm();
switch(mLight.type)
{
case SL_LIGHT_DIRECTIONAL:
lightDir = -mLight.direction;
lightDir.norm();
NdotL = std::max((normal DOT lightDir), 0.0);
//diffuse = mMaterial.diffuse * mLight.diffuse;
//ambient = mMaterial.ambient * mLight.ambient;
globalAmbient = mMaterial.ambient * mGlobalAmbient;
finalColor =globalAmbient;
// specular
if (NdotL>0)
{
diffuse = _clr * mLight.diffuse;
ambient = _clr * mLight.ambient;
halfVector = lightDir + eyeDir;
halfVector.norm();
NdotHV = std::max((normal DOT halfVector), 0.0);
specular = mMaterial.specular * mLight.specular * std::pow(NdotHV, mMaterial.shiness);
finalColor += NdotL * diffuse + specular + ambient;
}
_clr = finalColor;
_clr[3] = 1.0;
break;
case SL_LIGHT_POINT:
lightDir = mLight.position - _pos;
lightDir.norm();
NdotL = std::max((normal DOT lightDir), 0.0);
//diffuse = mMaterial.diffuse * mLight.diffuse;
//ambient = mMaterial.ambient * mLight.ambient;
globalAmbient = mMaterial.ambient * mGlobalAmbient;
finalColor = globalAmbient;
// specular
if (NdotL>0)
{
diffuse = _clr * mLight.diffuse;
ambient = _clr * mLight.ambient;
att = 1.0 / (mLight.attenuation0 + mLight.attenuation1 * dist +
mLight.attenuation2 * dist *dist);
halfVector = (lightDir + eyeDir).normed();
NdotHV = std::max((normal DOT halfVector), 0.0);
specular = mMaterial.specular * mLight.specular * std::pow(NdotHV, mMaterial.shiness);
// final color
finalColor += att * (NdotL * diffuse + ambient + specular);
}
_clr = finalColor;
_clr[3] = 1.0;
break;
case SL_LIGHT_SPOT:
break;
case SL_LIGHT_NONE:
break;
default:
break;
}
}
//------------------------------------------------------------------------------
// Camera Related
//------------------------------------------------------------------------------
void CScanLine::lookAt(const Vec3d& eye, const Vec3d& at, const Vec3d& up)
{
mCamera.lookAt(eye, at, up);
}
void CScanLine::perspective(double fovy, double aspect, double zNear, double zFar)
{
mCamera.perspective(fovy, aspect, zNear, zFar);
}
void CScanLine::frustum(double left, double right, double bottom, double top,
double near, double far)
{
mCamera.frustum(left, right, bottom, top, near, far);
}
void CScanLine::ortho(double left, double right, double bottom, double top,
double near, double far)
{
mCamera.ortho(left, right, bottom, top, near, far);
}
//------------------------------------------------------------------------------
// Transformations
//------------------------------------------------------------------------------
void CScanLine::_normalizeDeviceCoordinates()
{
VBufferItor it = mVertexBuffer.begin();
VBufferItor it_end = mVertexBuffer.end();
for (; it!=it_end; ++it)
{
Vertex *t_pv = *it;
t_pv->posWorld[0] /= t_pv->posWorld[3];
t_pv->posWorld[1] /= t_pv->posWorld[3];
t_pv->posWorld[2] /= t_pv->posWorld[3];
t_pv->posWorld[3] = 1;
}
}
void CScanLine::_screenCoordinates()
{
VBufferItor it = mVertexBuffer.begin();
VBufferItor it_end = mVertexBuffer.end();
for (; it!=it_end; ++it)
{
Vertex *t_pv = *it;
// round the coordinates
t_pv->posScreen[0] = ROUND((t_pv->posScreen[0]+1)*mWidth*0.5);
t_pv->posScreen[1] = ROUND((t_pv->posScreen[1]+1)*mHeight*0.5);
t_pv->posScreen[2] = 0.5 * t_pv->posScreen[2] + 0.5;
}
}
void CScanLine::_modelViewProjectionTransform()
{
Vertex *t_pv;
Vec4d t_pos;
VBufferItor it = mVertexBuffer.begin();
VBufferItor it_end = mVertexBuffer.end();
for (; it!=it_end; ++it)
{
t_pv = *it;
t_pos = t_pv->posWorld;
mCamera.transform(t_pos);
t_pv->posScreen[0] = t_pos[0] / t_pos[3];
t_pv->posScreen[1] = t_pos[1] / t_pos[3];
t_pv->posScreen[2] = t_pos[2] / t_pos[3];
// gouraud shading
if ( mRenderState.isFlatShading() )
_calculateLight(t_pv->posWorld, t_pv->normalWorld, t_pv->color);
}
}
void CScanLine::_vertexTransform()
{
_modelViewProjectionTransform();
}
//------------------------------------------------------------------------------
// Render States
//------------------------------------------------------------------------------
void CScanLine::setRenderState(int _state, int _val)
{
mRenderState.setState(_state, _val);
}