-------------------- -------------------- -------------------- -------

  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
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
/*-----------------------------------------------------------------------------*
| sourcefile ShTlTerrainSceneNode.cpp |
| |
| version 2.20 |
| date: (17.04.2008) |
| |
| author: Michal vantner |
| |
| Shifting Tiled Terrain Scene Node |
| |
| Writen for Irrlicht engine version 1.4 |
*-----------------------------------------------------------------------------*/
#include "ShTlTerrainSceneNode.h"
// constructor
ShTlTerrainSceneNode::ShTlTerrainSceneNode(scene::ISceneManager* smgr,
s32 width, s32 height, f32 tilesize, s32 visiblesize,
scene::ISceneNode* parent, s32 id)
: scene::ISceneNode(smgr->getRootSceneNode(), smgr, id)
{
Size.Width = width;
Size.Height = height;
TileSize = tilesize;
// make sure rendered terrain mesh is not larger than terrain itself
if(visiblesize > Size.Width) visiblesize = Size.Width;
if(visiblesize > Size.Height) visiblesize = Size.Height;
MeshSize.Width = visiblesize;
MeshSize.Height = visiblesize;
MeshPosition = core::vector2d<s32>(0,0);
Fnode = NULL;
ShStep = 1;
// create data array
Data.reset(Size.Width+1, Size.Height+1);
for(s32 j=0; j<Size.Height+1; j++)
for(s32 i=0; i<Size.Width+1; i++)
{
Data(i,j).Height = 0;
Data(i,j).Normal = core::vector3df(0,1,0);
Data(i,j).Color = video::SColor(255,255,255,255);
}
// create tile UV data array
UVdata.reset(Size.Width, Size.Height);
for(s32 j=0; j<Size.Height; j++)
for(s32 i=0; i<Size.Width; i++)
{
UVdata(i,j).Vertex[0] = core::vector2d<f32>(0,1);
UVdata(i,j).Vertex[1] = core::vector2d<f32>(0,0);
UVdata(i,j).Vertex[2] = core::vector2d<f32>(1,0);
UVdata(i,j).Vertex[3] = core::vector2d<f32>(1,1);
}
// calculate number of sectors
// terrain mesh will be split to 1 or 3 or 5 sectors on each axis depending on size
s32 w = 1;
s32 h = 1;
if(MeshSize.Width >= 30) w = 3;
if(MeshSize.Height >= 30) h = 3;
if(MeshSize.Width >= 50) w = 5;
if(MeshSize.Height >= 50) h = 5;
// create sectors
Sector.reset(w, h);
// find size of sectors in tiles
w = MeshSize.Width / Sector.width();
h = MeshSize.Height / Sector.height();
for(s32 j=0; j<Sector.height(); j++)
for(s32 i=0; i<Sector.width(); i++)
Sector(i,j).Size = core::dimension2d<s32>(w, h);
// find size of center sector in tiles
w = MeshSize.Width - Sector(0,0).Size.Width * (Sector.width()-1);
h = MeshSize.Height - Sector(0,0).Size.Height * (Sector.height()-1);
{s32 j=Sector.height()/2;
for(s32 i=0; i<Sector.width(); i++) Sector(i,j).Size.Height = h;}
{s32 i=Sector.width()/2;
for(s32 j=0; j<Sector.height(); j++) Sector(i,j).Size.Width = w;}
// find offset of sectors in tiles
for(s32 j=0; j<Sector.height(); j++)
for(s32 i=0; i<Sector.width(); i++)
{
if(i>0) Sector(i,j).Offset.X = Sector(i-1,j).Offset.X + Sector(i-1,j).Size.Width;
else Sector(i,j).Offset.X = 0;
if(j>0) Sector(i,j).Offset.Y = Sector(i,j-1).Offset.Y + Sector(i,j-1).Size.Height;
else Sector(i,j).Offset.Y = 0;
}
// fill sectors with tiles
for(s32 j=0; j<Sector.height(); j++)
for(s32 i=0; i<Sector.width(); i++)
{
Sector(i,j).Vertex.set_used( Sector(i,j).Size.Width * Sector(i,j).Size.Height * 4);
Sector(i,j).Index.set_used( Sector(i,j).Size.Width * Sector(i,j).Size.Height * 6);
u32 n = 0;
u32 m = 0;
while(n < Sector(i,j).Index.size())
{
Sector(i,j).Index[n] = m;
n++;
Sector(i,j).Index[n] = m+1;
n++;
Sector(i,j).Index[n] = m+2;
n++;
Sector(i,j).Index[n] = m;
n++;
Sector(i,j).Index[n] = m+2;
n++;
Sector(i,j).Index[n] = m+3;
n++;
m+=4;
}
}
// setup tiles
Tile.reset(MeshSize.Width, MeshSize.Height);
for(s32 j=0; j<Sector.height(); j++)
for(s32 i=0; i<Sector.width(); i++)
{
s32 k = 0;
for(s32 n=0; n<Sector(i,j).Size.Height; n++)
for(s32 m=0; m<Sector(i,j).Size.Width; m++)
{
s32 x = m + Sector(i,j).Offset.X;
s32 y = n + Sector(i,j).Offset.Y;
Tile(x,y).Vertex[0] = &Sector(i,j).Vertex[k];
k++;
Tile(x,y).Vertex[1] = &Sector(i,j).Vertex[k];
k++;
Tile(x,y).Vertex[2] = &Sector(i,j).Vertex[k];
k++;
Tile(x,y).Vertex[3] = &Sector(i,j).Vertex[k];
k++;
}
}
// set color of mesh vertices to white
// vertex color is not used, 2nd texture layer is used to render tile colors
for(s32 j=0; j<MeshSize.Height; j++)
for(s32 i=0; i<MeshSize.Width; i++)
{
Tile(i,j).Vertex[0]->Color = video::SColor(255,255,255,255);
Tile(i,j).Vertex[1]->Color = video::SColor(255,255,255,255);
Tile(i,j).Vertex[2]->Color = video::SColor(255,255,255,255);
Tile(i,j).Vertex[3]->Color = video::SColor(255,255,255,255);
}
// setup material
video::SMaterial material;
material.AmbientColor = video::SColor(255,255,255,255);
material.DiffuseColor = video::SColor(255,255,255,255);
material.EmissiveColor = video::SColor(255,0,0,0);
material.Shininess = 0.0f;
material.SpecularColor = video::SColor(255,0,0,0);
material.MaterialType = video::EMT_DETAIL_MAP;
Material.push_back(material);
// create 2nd texture layer
// find size of texture, must be power of two
s32 tw = 2;
while(tw < MeshSize.Width) tw = tw + tw;
s32 th = 2;
while(th < MeshSize.Height) th = th + th;
// create texture
// turn of mipmaps othervise they would need to be regenerated each time texture is updated
video::IVideoDriver* driver = SceneManager->getVideoDriver();
bool mmflag = driver->getTextureCreationFlag(video::ETCF_CREATE_MIP_MAPS);
driver->setTextureCreationFlag(video::ETCF_CREATE_MIP_MAPS, false);
CTexture = driver->addTexture(core::dimension2d<u32>(tw, th), "colortexture", video::ECF_A8R8G8B8);
driver->setTextureCreationFlag(video::ETCF_CREATE_MIP_MAPS, mmflag);
Material[0].TextureLayer[0].Texture = CTexture;
Material[0].TextureLayer[0].TextureWrapU = video::ETC_CLAMP_TO_EDGE;
Material[0].TextureLayer[0].TextureWrapV = video::ETC_CLAMP_TO_EDGE;
// setup UV coordinates of vertices on 2nd texture layer
f32 ax = (f32)MeshSize.Width / CTexture->getSize().Width / MeshSize.Width;
f32 ay = (f32)MeshSize.Height/ CTexture->getSize().Height / MeshSize.Height;
f32 ry = 1.0f - (f32)MeshSize.Height/ CTexture->getSize().Height;
s32 n = MeshSize.Height-1;
for(s32 j=0; j<MeshSize.Height; j++)
{
for(s32 i=0; i<MeshSize.Width; i++)
{
Tile(i,n).Vertex[0]->TCoords = core::vector2d<f32>(i*ax, ry+(j+1)*ay);
Tile(i,n).Vertex[1]->TCoords = core::vector2d<f32>(i*ax, ry+j*ay);
Tile(i,n).Vertex[3]->TCoords = core::vector2d<f32>((i+1)*ax, ry+(j+1)*ay);
Tile(i,n).Vertex[2]->TCoords = core::vector2d<f32>((i+1)*ax, ry+j*ay);
}
n--;
}
// set update vertices flag for sectors
for(s32 j=0; j<Sector.height(); j++)
for(s32 i=0; i<Sector.width(); i++) Sector(i,j).UpdateVertices = true;
// calculate bounding boxes
BoundingBox.MinEdge = core::vector3df(0,0,0);
BoundingBox.MaxEdge = core::vector3df(MeshSize.Width*TileSize,0,MeshSize.Height*TileSize);
for(s32 j=0; j<Sector.height(); j++)
for(s32 i=0; i<Sector.width(); i++)
{
Sector(i,j).BoundingBox.MinEdge =
core::vector3df(Sector(i,j).Offset.X*TileSize,0,Sector(i,j).Offset.Y*TileSize);
Sector(i,j).BoundingBox.MaxEdge = core::vector3df(
(Sector(i,j).Offset.X + Sector(i,j).Size.Width) * TileSize, 0,
(Sector(i,j).Offset.Y + Sector(i,j).Size.Height) * TileSize);
}
// set update texture flag for sectors
for(s32 j=0; j<Sector.height(); j++)
for(s32 i=0; i<Sector.width(); i++) Sector(i,j).UpdateTexture = true;
// turn off automatic culling
// culling will be done by terrain itself, sector by sector so that sectors
// which are not on screen will not be renderd to save time
setAutomaticCulling( scene::EAC_OFF );
}
// destructor
ShTlTerrainSceneNode::~ShTlTerrainSceneNode(){}
// frame
void ShTlTerrainSceneNode::OnRegisterSceneNode()
{
if (IsVisible)
SceneManager->registerNodeForRendering(this);
ISceneNode::OnRegisterSceneNode();
}
#include <iostream>
#include <fstream>
void ShTlTerrainSceneNode::CalculationLOD()
{
std::cout<<"Going to loop tr:"<<Sector(0,0).Index.size()<<" "<<Sector(0,0).Vertex.size()<<std::endl;
for(s32 j=0; j<Sector.height(); j++) {
for(s32 i=0; i<Sector.width(); i++) {
Sector(i,j).lodIndex.set_used( Sector(i,j).Index.size()/2);
int n =0;
for(s32 t=0; t<Sector(i,j).lodIndex.size(); t++) {
Sector(i,j).lodIndex[t] = 0;
}
//std::cout<<"make vertexes"<<std::endl;
for(s32 ii=0; ii<Sector(i,j).lodIndex.size()-4; ii++ ) {
Sector(i,j).lodIndex[ii] = Sector(i,j).Index[2*ii+1];
}
n=0;
}
}
}
// renders terrain
void ShTlTerrainSceneNode::render()
{
// update position if needed
//if(Fnode) centerAt(Fnode->getPosition());
double distance = Fnode->getAbsolutePosition().getDistanceFrom (this->getAbsolutePosition());
video::IVideoDriver* driver = SceneManager->getVideoDriver();
if (!driver) return;
driver->setTransform(video::ETS_WORLD, AbsoluteTransformation);
driver->setMaterial(Material[0]);
SectorsRendered = 0;
// test if sectors are vissible
for(s32 j=0; j<Sector.height(); j++)
for(s32 i=0; i<Sector.width(); i++)
if( isSectorOnScreen( &Sector(i,j) ) ) Sector(i,j).isVissible = true;
else Sector(i,j).isVissible = false;
// update texture if neede
u32* p = NULL;
for(s32 j=0; j<Sector.height(); j++)
for(s32 i=0; i<Sector.width(); i++)
if(Sector(i,j).isVissible)
if(Sector(i,j).UpdateTexture)
{
if(!p) p = (u32*)CTexture->lock();
updateTexture(p, Sector(i,j));
Sector(i,j).UpdateTexture = false;
}
if(p) CTexture->unlock();
if(distance < 80) {
// render geometry
for(s32 j=0; j<Sector.height(); j++)
for(s32 i=0; i<Sector.width(); i++)
if(Sector(i,j).isVissible)
{
SectorsRendered++;
// update vertices if needed
if(Sector(i,j).UpdateVertices)
{
updateVertices(Sector(i,j));
Sector(i,j).UpdateVertices = false;
}
driver->drawIndexedTriangleList
(&Sector(i,j).Vertex[0], Sector(i,j).Vertex.size(),
&Sector(i,j).Index[0], Sector(i,j).Index.size()/3);
}
}
else {
// render geometry
for(s32 j=0; j<Sector.height(); j++)
for(s32 i=0; i<Sector.width(); i++)
if(Sector(i,j).isVissible)
{
SectorsRendered++;
if(Sector(i,j).UpdateVertices)
{
updateVertices(Sector(i,j));
Sector(i,j).UpdateVertices = false;
}
driver->drawIndexedTriangleList
(&Sector(i,j).Vertex[0], Sector(i,j).Vertex.size(),
&Sector(i,j).lodIndex[0], Sector(i,j).lodIndex.size()/3);
}
}
// for debuging
if( DebugDataVisible == scene::EDS_OFF ) return;
// wireframe overlay
if (DebugDataVisible == scene::EDS_MESH_WIRE_OVERLAY || DebugDataVisible == scene::EDS_FULL)
{
video::SMaterial m;
m.Lighting = false;
m.Wireframe = true;
m.BackfaceCulling = false;
m.AmbientColor = video::SColor(255,0,255,0);
m.DiffuseColor = video::SColor(255,0,255,0);
driver->setMaterial(m);
for(s32 j=0; j<Sector.height(); j++)
for(s32 i=0; i<Sector.width(); i++)
{
if( Sector(i,j).isVissible )
{
driver->drawIndexedTriangleList
(&Sector(i,j).Vertex[0], Sector(i,j).Vertex.size(),
&Sector(i,j).Index[0], Sector(i,j).Index.size()/3);
}
}
}
}
// returns the axis aligned bounding box of terrain
const core::aabbox3d<f32>& ShTlTerrainSceneNode::getBoundingBox() const
{
return BoundingBox;
}
// recalculate terrain bounding box
void ShTlTerrainSceneNode::recalculateBoundingBox()
{
BoundingBox.MinEdge.X = TileSize * MeshPosition.X;
BoundingBox.MinEdge.Y = Data(0,0).Height;
BoundingBox.MinEdge.Z = TileSize * MeshPosition.Y;
BoundingBox.MaxEdge.X = TileSize * (MeshPosition.X + MeshSize.Width);
BoundingBox.MaxEdge.Y = Data(0,0).Height;
BoundingBox.MaxEdge.Z = TileSize * (MeshPosition.Y + MeshSize.Height);
for(s32 j=0; j<Size.Height+1; j++)
for(s32 i=0; i<Size.Width+1; i++)
{
if(BoundingBox.MinEdge.Y > Data(i,j).Height) BoundingBox.MinEdge.Y = Data(i,j).Height;
if(BoundingBox.MaxEdge.Y < Data(i,j).Height) BoundingBox.MaxEdge.Y = Data(i,j).Height;
}
for(s32 j=0; j<Sector.height(); j++)
for(s32 i=0; i<Sector.width(); i++)
{
Sector(i,j).BoundingBox.MinEdge = core::vector3df(
TileSize * Sector(i,j).Offset.X,
BoundingBox.MinEdge.Y ,
TileSize * Sector(i,j).Offset.Y);
Sector(i,j).BoundingBox.MaxEdge = core::vector3df(
TileSize * (Sector(i,j).Offset.X + Sector(i,j).Size.Width),
BoundingBox.MaxEdge.Y,
TileSize * (Sector(i,j).Offset.Y + Sector(i,j).Size.Height));
}
}
// returns amount of materials used by terrain
u32 ShTlTerrainSceneNode::getMaterialCount() const
{
return Material.size();
}
// returns the material of terrain
video::SMaterial& ShTlTerrainSceneNode::getMaterial(u32 i)
{
return Material[i];
}
// return number of tiles to skip before terrain mesh gets updated
s32 ShTlTerrainSceneNode::getStep()
{
return ShStep;
}
// set number of tiles to skip before terrain mesh gets updated
// default is 1
// updating slows down rendering and seting step higher will cause terrain to update less ofthen
void ShTlTerrainSceneNode::setStep(s32 newstep)
{
ShStep = newstep;
}
// return dimensions of whole terrain in tiles
core::dimension2d<s32> ShTlTerrainSceneNode::getSize()
{
return Size;
}
// return dimension of terrain tile
f32 ShTlTerrainSceneNode::getTileSize()
{
return TileSize;
}
// set new dimensions of terrain tiles
void ShTlTerrainSceneNode::setTileSize(f32 newsize)
{
if(newsize != TileSize)
{
TileSize = newsize;
update();
}
}
// returns dimension of rendered mesh in tiles
core::dimension2d<s32> ShTlTerrainSceneNode::getRenderedSize()
{
return MeshSize;
}
// return number of sectors
s32 ShTlTerrainSceneNode::getSectorCount()
{
return Sector.width() * Sector.height();
}
// returns numner of sectors rendered last frame
s32 ShTlTerrainSceneNode::getSectorsRendered()
{
return SectorsRendered;
}
// return height of terrain spot at terrain coordinates
f32 ShTlTerrainSceneNode::getHeight(s32 w, s32 h)
{
return Data(w,h).Height;
}
// return height of terrain at any position
f32 ShTlTerrainSceneNode::getHeight(core::vector3df pos)
{
// find position relative to terrain
pos = pos - getPosition();
if(pos.X>=0 && pos.X<(f32)Size.Width*TileSize && pos.Z>=0 && pos.Z<(f32)Size.Height*TileSize)
{
return Data(pos.X, pos.Z).Height;
}
else
return 0;
}
// set relative height of terrain spot at terrain coordinates
void ShTlTerrainSceneNode::setHeight(s32 w, s32 h, f32 newheight)
{
Data(w,h).Height = newheight;
// recalculate bounding boxes
if(newheight > BoundingBox.MaxEdge.Y)
{
BoundingBox.MaxEdge.Y = newheight;
for(s32 j=0; j<Sector.height(); j++)
for(s32 i=0; i<Sector.width(); i++)
Sector(i,j).BoundingBox.MaxEdge.Y = newheight;
}
if(newheight < BoundingBox.MinEdge.Y)
{
BoundingBox.MinEdge.Y = newheight;
for(s32 j=0; j<Sector.height(); j++)
for(s32 i=0; i<Sector.width(); i++)
Sector(i,j).BoundingBox.MinEdge.Y = newheight;
}
}
// return normal of terrain at terrain coordinates
core::vector3df ShTlTerrainSceneNode::getNormal(s32 w, s32 h)
{
return Data(w,h).Normal;
}
// set normal of terrain at terrain coordinates
void ShTlTerrainSceneNode::setNormal(s32 w, s32 h, core::vector3df newnormal)
{
Data(w,h).Normal = newnormal;
}
// recalculate normal at terrain coordinates
void ShTlTerrainSceneNode::recalculateNormal(s32 w, s32 h)
{
core::vector3df v0, v1;
core::vector3df n0, n1, n2, n3, n4, n5;
// calculate vector to point 0,-1
if(h > 0) // check if not out of array
v0 = core::vector3df(0, getHeight(w,h-1)-getHeight(w,h), -TileSize);
else
v0 = core::vector3df(0, 0, -TileSize);
// calculate vector to point -1,-1
if(w > 0 && h > 0) // check if not out of array
v1 = core::vector3df(-TileSize, getHeight(w-1,h-1)-getHeight(w,h), -TileSize);
else
v1 = core::vector3df(-TileSize, 0, -TileSize);
n0 = v0.crossProduct(v1);
// calculate vector to point -1,-1
v0 = v1;
// calculate vector to point -1,0
if(w > 0)
v1 = core::vector3df(-TileSize, getHeight(w-1,h)-getHeight(w,h), 0);
else
v1 = core::vector3df(-TileSize, 0, 0);
n1 = v0.crossProduct(v1);
// calculate vector to point -1,0
v0 = v1;
// calculate vector to point 0,1
if(h < Size.Height)
v1 = core::vector3df(0, getHeight(w,h+1)-getHeight(w,h), TileSize);
else
v1 = core::vector3df(0, 0, TileSize);
n2 = v0.crossProduct(v1);
// calculate vector to point 0,1
v0 = v1;
// calculate vector to point 1,1
if(w < Size.Width && h < Size.Height)
v1 = core::vector3df(TileSize, getHeight(w+1,h+1)-getHeight(w,h), TileSize);
else
v1 = core::vector3df(TileSize, 0, TileSize);
n3 = v0.crossProduct(v1);
// calculate vector to point 1,1
v0 = v1;
// calculate vector to point 1,0
if(w < Size.Width)
v1 = core::vector3df(TileSize, getHeight(w+1,h)-getHeight(w,h), 0);
else
v1 = core::vector3df(TileSize, 0, 0);
n4 = v0.crossProduct(v1);
// calculate vector to point 1,0
v0 = v1;
// calculate vector to point 0,-1
if(h > 0)
v1 = core::vector3df(0, getHeight(w,h-1)-getHeight(w,h), -TileSize);
else
v1 = core::vector3df(0, 0, -TileSize);
n5 = v0.crossProduct(v1);
// calculate normals of 4 tiles around point
core::vector3df m0, m1, m2, m3;
m0 = (n1 - n0) /2 + n0;
m1 = n2;
m2 = (n4 - n3) /2 + n3;
m3 = n5;
// calculate normals between oposing tiles
core::vector3df k0, k1;
k0 = (m2 - m0) /2 + m0;
k1 = (m3 - m1) /2 + m1;
// calculate normal of point
core::vector3df n = (k1 - k0) /2 + k0;
n.normalize();
setNormal(w,h,n);
}
// recalculare normals of whole terrain making it look smooth under light
void ShTlTerrainSceneNode::smoothNormals()
{
for(s32 j=0; j<Size.Height+1; j++)
for(s32 i=0; i<Size.Width+1; i++) recalculateNormal(i, j);
}
// get texture coordinates of tile corner
core::vector2d<f32> ShTlTerrainSceneNode::getTileUV(s32 w, s32 h, TILE_VERTEX corner)
{
return UVdata(w,h).Vertex[corner];
}
// set texture coordinates of tile
void ShTlTerrainSceneNode::setTileUV(s32 w, s32 h, core::vector2d<f32> UVlowerLeft,
core::vector2d<f32> UVupperLeft, core::vector2d<f32> UVupperRight,
core::vector2d<f32> UVlowerRight)
{
UVdata(w,h).Vertex[0] = UVlowerLeft;
UVdata(w,h).Vertex[1] = UVupperLeft;
UVdata(w,h).Vertex[2] = UVupperRight;
UVdata(w,h).Vertex[3] = UVlowerRight;
}
// stretch texture over whole terrain
void ShTlTerrainSceneNode::stretchTexture(core::vector2d<f32> scale)
{
f32 ax = scale.X / Size.Width;
f32 ay = scale.X / Size.Height;
s32 n = Size.Height-1;
for(s32 j=0; j<Size.Height; j++)
{
for(s32 i=0; i<Size.Width; i++)
{
UVdata(i,n).Vertex[1] = core::vector2d<f32>(i*ax, j*ay);
UVdata(i,n).Vertex[0] = core::vector2d<f32>(i*ax, j*ay+ay);
UVdata(i,n).Vertex[3] = core::vector2d<f32>(i*ax+ax, j*ay+ay);
UVdata(i,n).Vertex[2] = core::vector2d<f32>(i*ax+ax, j*ay);
}
n--;
}
}
// stretch texture over every tile individualy
void ShTlTerrainSceneNode::stretchTextureOverTile(core::vector2d<f32> scale)
{
for(s32 j=0; j<Size.Height; j++)
for(s32 i=0; i<Size.Width; i++)
{
UVdata(i,j).Vertex[1] = core::vector2d<f32>(0, 0);
UVdata(i,j).Vertex[0] = core::vector2d<f32>(0, scale.Y);
UVdata(i,j).Vertex[3] = core::vector2d<f32>(scale.X, scale.Y);
UVdata(i,j).Vertex[2] = core::vector2d<f32>(scale.X, 0);
}
}
// get color of tile at terrain coordinates
video::SColor ShTlTerrainSceneNode::getColor(s32 w, s32 h)
{
return Data(w,h).Color;
}
// set color of tile at terrain coordinates
void ShTlTerrainSceneNode::setColor(s32 w, s32 h, video::SColor newcolor)
{
Data(w,h).Color = newcolor;
}
// set rendered mesh position relative to terrain
void ShTlTerrainSceneNode::setMeshPosition(core::vector2d<s32> pos)
{
// correct if out of terrain bounds
if(pos.X < 0) pos.X = 0;
if(pos.Y < 0) pos.Y = 0;
if(pos.X > (s32)(Size.Width - MeshSize.Width)) pos.X = (s32)(Size.Width - MeshSize.Width);
if(pos.Y > (s32)(Size.Height - MeshSize.Height)) pos.Y = (s32)(Size.Height - MeshSize.Height);
// update
if(pos.X > MeshPosition.X + ShStep || pos.X < MeshPosition.X - ShStep ||
pos.Y > MeshPosition.Y + ShStep || pos.Y < MeshPosition.Y - ShStep)
{
MeshPosition = pos;
update();
}
}
// center rendered mesh at 3d coordinates
void ShTlTerrainSceneNode::centerAt(core::vector3d<f32> pos)
{
// find position relative to terrain
pos.X = pos.X - getPosition().X;
pos.Z = pos.Z - getPosition().Z;
// offset center
pos.X = pos.X - TileSize * MeshSize.Width/2;
pos.Z = pos.Z - TileSize * MeshSize.Height/2;
// find equivalent in tiles
s32 x = (s32)(pos.X / TileSize);
s32 y = (s32)(pos.Z / TileSize);
setMeshPosition(core::vector2d<s32>(x,y));
}
// update rendered mesh
void ShTlTerrainSceneNode::update()
{
// update position of bounding boxes
BoundingBox.MinEdge = core::vector3df(MeshPosition.X*TileSize, BoundingBox.MinEdge.Y, MeshPosition.Y*TileSize);
BoundingBox.MaxEdge = core::vector3df( (MeshPosition.X+MeshSize.Width)*TileSize, BoundingBox.MaxEdge.Y, (MeshPosition.Y+MeshSize.Height)*TileSize);
for(s32 j=0; j<Sector.height(); j++)
for(s32 i=0; i<Sector.width(); i++)
{
Sector(i,j).BoundingBox.MinEdge = core::vector3df(
(MeshPosition.X + Sector(i,j).Offset.X) * TileSize,
Sector(i,j).BoundingBox.MinEdge.Y,
(MeshPosition.Y + Sector(i,j).Offset.Y) * TileSize);
Sector(i,j).BoundingBox.MaxEdge = core::vector3df(
(MeshPosition.X + Sector(i,j).Offset.X + Sector(i,j).Size.Width) * TileSize,
Sector(i,j).BoundingBox.MaxEdge.Y,
(MeshPosition.Y + Sector(i,j).Offset.Y + Sector(i,j).Size.Height) * TileSize);
}
// set update texture flag for sectors
for(s32 j=0; j<Sector.height(); j++)
for(s32 i=0; i<Sector.width(); i++) Sector(i,j).UpdateTexture = true;
// set update vertices flag for sectors
for(s32 j=0; j<Sector.height(); j++)
for(s32 i=0; i<Sector.width(); i++) Sector(i,j).UpdateVertices = true;
}
// set scene node terrain should be automaticly rendered arround
// can be camera or player node for example
void ShTlTerrainSceneNode::follow(scene::ISceneNode* node)
{
Fnode = node;
}
// cancel pervious function
// stop following scene node if any
void ShTlTerrainSceneNode::stop()
{
Fnode = NULL;
}
// return true if sector is on screen
bool ShTlTerrainSceneNode::isSectorOnScreen(TlTSector* sctr)
{
// bounding box of sector
core::aabbox3d<f32> box = sctr->BoundingBox;
box.MinEdge = box.MinEdge + getPosition();
box.MaxEdge = box.MaxEdge + getPosition();
// get absolute position of bounding box
const scene::SViewFrustum* frustrum = SceneManager->getActiveCamera()->getViewFrustum();
for (s32 i=0; i<scene::SViewFrustum::VF_PLANE_COUNT; ++i)
if(box.classifyPlaneRelation(frustrum->planes[i]) == core::ISREL3D_FRONT)
return false;
return true;
}
// update vertices of sector
void ShTlTerrainSceneNode::updateVertices(TlTSector &sector)
{
for(s32 j=sector.Offset.Y; j<sector.Offset.Y+sector.Size.Height; j++)
for(s32 i=sector.Offset.X; i<sector.Offset.X+sector.Size.Width; i++)
{
// positon of tile relative to terrain
s32 x = i + MeshPosition.X;
s32 y = j + MeshPosition.Y;
// update position
Tile(i,j).Vertex[0]->Pos = core::vector3df(x*TileSize, Data(x,y).Height, y*TileSize);
Tile(i,j).Vertex[1]->Pos = core::vector3df(x*TileSize, Data(x,y+1).Height, (y+1)*TileSize);
Tile(i,j).Vertex[2]->Pos = core::vector3df( (x+1)*TileSize, Data(x+1,y+1).Height, (y+1)*TileSize);
Tile(i,j).Vertex[3]->Pos = core::vector3df( (x+1)*TileSize, Data(x+1,y).Height, y*TileSize);
// update normals
Tile(i,j).Vertex[0]->Normal = getNormal(x,y);
Tile(i,j).Vertex[1]->Normal = getNormal(x,y+1);
Tile(i,j).Vertex[2]->Normal = getNormal(x+1,y+1);
Tile(i,j).Vertex[3]->Normal = getNormal(x+1,y);
// update texture coordinates
Tile(i,j).Vertex[0]->TCoords2 = getTileUV(x,y,LOWER_LEFT);
Tile(i,j).Vertex[1]->TCoords2 = getTileUV(x,y,UPPER_LEFT);
Tile(i,j).Vertex[2]->TCoords2 = getTileUV(x,y,UPPER_RIGHT);
Tile(i,j).Vertex[3]->TCoords2 = getTileUV(x,y,LOWER_RIGHT);
}
}
// update 2nd texture layer
void ShTlTerrainSceneNode::updateTexture(u32* p, TlTSector &sector)
{
s32 x, y;
// in case created texure is larger than terrain mesh, update one more pixel
// on each axis to get rid of unused pixels at the border blended in to used ones
s32 w = 0;
if(MeshSize.Width < CTexture->getSize().Width) w = 1;
s32 h = 0;
if(MeshSize.Height < CTexture->getSize().Height) h = 1;
for(s32 j=sector.Offset.Y; j<sector.Offset.Y + sector.Size.Height + h; j++)
{
s32 n = CTexture->getSize().Height-1 - j;
for(s32 i=sector.Offset.X; i<sector.Offset.X + sector.Size.Width + w; i++)
{
x = i + MeshPosition.X;
y = j + MeshPosition.Y;
p[n*CTexture->getSize().Width + i] = getColor(x,y).color;
}
}
}