-
Notifications
You must be signed in to change notification settings - Fork 0
/
indieGrass.cc
1572 lines (1292 loc) · 43.2 KB
/
indieGrass.cc
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
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
#include "platform/platform.h"
#include "core/resourceManager.h"
#include "core/stream/bitStream.h"
#include "console/consoleTypes.h"
#include "sceneGraph/sceneState.h"
#include "terrain/terrData.h"
#include "renderInstance/renderPassManager.h"
#include "gfx/gfxDrawUtil.h"
#include "gfx/primBuilder.h"
#include "T3D/gameConnection.h"
#include "gfx/gfxVertexBuffer.h"
#include "gfx/gfxStructs.h"
#include "sceneGraph/lightManager.h"
#include "sceneGraph/lightInfo.h"
#include "materials/shaderData.h"
#include "gfx/gfxTransformSaver.h"
#include "indieGrass.h"
#include "terrain/sky.h"
//#include "envioManager/enviornment.h"
extern bool gEditingMission;
IMPLEMENT_CO_NETOBJECT_V1(indieGrass);
//#define IG_DEBUG
#define IG_COLLISION_MASK ( TerrainObjectType )
static F32 mCosTable[720];
indieGrass::indieGrass()
{
// Setup NetObject.
mTypeMask |= StaticObjectType | StaticTSObjectType | StaticRenderedObjectType;
mNetFlags.set(Ghostable);
// Reset Last Render Time.
mLastRenderTime = 0;
//render params
mRender = false;
mRenderCells = true;
mRenderVectors = false;
mRenderTangets = false;
mLockFrustum = false;
mRenderFrustum = false;
mFoliageTexture = NULL;
mFoliageFile = NULL;
//grid stuffs
mCellSize = 4; //4 meter squared
mGridArea = 256;
mGrassPerCell = 4;
//blade stuffs
mBaseLength = 1.0f; //half a meter? :o
mBaseWidth = 0.1f;
mSteps = 4;
mVerticalOffset = 0.2f; //vertical offset of the cell level
//curve stuff
hOriginCoeff = 1.f; //controlls the origin point of the tip's curve
hEndCoeff = 2.f; //controlls the max endpoint of the tip's curve
//hDirTan = VectorF(0,0,0); //represents the directional vector for the tip's curve
//hEndTan = VectorF(-1, -1, -1); //represents the tip's vector for the tip's curve
hDirTan = 0.f; //represents the directional vector for the tip's curve
hEndTan = -1.f; //represents the tip's vector for the tip's curve
//lod
LODDistance[0] = 0; //past 0 meters out
LODDistance[1] = 100; //past 50 meters out
LODDistance[2] = 200; //past 80 meters out
mCullRadius = 300;
//F32 mFadeInGrad;
//F32 mFadeOutGrad;
//wiiiind
mWindStrength = 1.f; //0 is unaffected, 1 is completely bent from wind
swayMag = 0.1f;
swayPhase = 0.f;
swayTimeRatio = 0.f;
minSwayTime = 3.0f;
maxSwayTime = 10.f;
mCells = NULL;
mRenderDelegate.bind(this, &indieGrass::renderObject);
}
//------------------------------------------------------------------------------
indieGrass::~indieGrass()
{
mCells.clear();
}
//------------------------------------------------------------------------------
void indieGrass::initPersistFields()
{
// Initialise parents' persistent fields.
Parent::initPersistFields();
addGroup( "Render" );
addField( "renderGrass", TypeBool, Offset( mRender, indieGrass ) );
addField( "renderCells", TypeBool, Offset( mRenderCells, indieGrass ) );
addField( "lockFrustum", TypeBool, Offset( mLockFrustum, indieGrass ) );
addField( "bladeTexture", TypeFilename, Offset( mFoliageFile, indieGrass ) );
endGroup( "Render" );
addGroup( "Grid" );
addField( "cellSize", TypeF32, Offset( mCellSize, indieGrass ) );
addField( "grassPerCell", TypeS32, Offset( mGrassPerCell, indieGrass ) );
endGroup( "Grid" );
addGroup( "Curve" );
addField( "curveOriginCoefficient", TypeF32, Offset( hOriginCoeff, indieGrass ) );
addField( "curveEndCoefficient", TypeF32, Offset( hEndCoeff, indieGrass ) );
//addField( "curveDirectionTangent", TypeF32Vector, Offset( hDirTan, indieGrass ) );//controls bending resistance i think
//addField( "curveEndTangent", TypeF32Vector, Offset( hEndTan, indieGrass ) );
addField( "curveDirectionTangent", TypeF32, Offset( hDirTan, indieGrass ) );//controls bending resistance i think
addField( "curveEndTangent", TypeF32, Offset( hEndTan, indieGrass ) );
endGroup( "Curve" );
addGroup( "Blade" );
addField( "baseLength", TypeF32, Offset( mBaseLength, indieGrass ) );
addField( "baseWidth", TypeF32, Offset( mBaseWidth, indieGrass ) );
addField( "stepCount", TypeS32, Offset( mSteps, indieGrass ) );
endGroup( "Blade" );
addGroup( "LOD" );
addField( "LOD1MaxDistance", TypeF32, Offset( LODDistance[0], indieGrass ) );
addField( "LOD2MaxDistance", TypeF32, Offset( LODDistance[1], indieGrass ) );
addField( "LOD3MaxDistance", TypeF32, Offset( LODDistance[2], indieGrass ) );
endGroup( "LOD" );
}
//================================================================================
// generation code
//================================================================================
void indieGrass::_findTerrainCallback( SceneObject *obj, void *param )
{
Vector<TerrainBlock*> *terrains = reinterpret_cast<Vector<TerrainBlock*>*>( param );
TerrainBlock *terrain = dynamic_cast<TerrainBlock*>( obj );
if ( terrain )
terrains->push_back( terrain );
}
void indieGrass::updateCellRenderList()
{
const S32 cullerMasks = mCuller.ClipPlaneMask/* | FrustrumCuller::FarSphereMask*/;
mRenderedCells = 0;
for(U32 j=0; j < mCells.size(); j++)
{
// What will be the world placement bounds for this cell.
Box3F bounds = mCells[j]->mBounds;
S32 clipMask = mCuller.testBoxVisibility( bounds, cullerMasks, 0 );
if ( clipMask == -1 )
mCells[j]->mRender = false;
else
{
mRenderedCells++;
//if we're not already being rendered
if(!mCells[j]->mRender){
mCells[j]->mDirty = true;
mCells[j]->mRender = true;
}
}
}
//if(mRenderedCells)
//Con::printf("Rendered cells %i", mRenderedCells);
}
void indieGrass::generateCells()
{
mFoliageTexture.free();
//get us our terrain....
//TerrainBlock * terrain = dynamic_cast<TerrainBlock*>(Sim::findObject("Terrain"));
Vector<TerrainBlock*> terrainBlocks;
bool CollisionResult;
RayInfo RayEvent;
Point3F nodeStart, nodeEnd;
TerrainBlock *terrainBlock = NULL;
U32 steps = (U32)(mGridArea / mCellSize);
F32 height;
Point3F minPoint, maxPoint, normal, newPos, cellPos;
Box3F bounds;
F32 terrainMinHeight = -5000.0f,
terrainMaxHeight = 5000.0f;
Point2F start(-mGridArea / 2,-mGridArea / 2);
Point2F gStart = Point2F(mCeil(start.x / mCellSize) * mCellSize, //start of the whole grid
mCeil(start.y / mCellSize) * mCellSize);
//kill the old ones, teehee
mCells.clear();
// Reload the texture.
//since you be a 'tard >: (
//commented
//if ( mFoliageFile )
// mFoliageTexture.set( mFoliageFile, &GFXDefaultStaticDiffuseProfile, avar("%s() - mFoliageFile (line %d)", __FUNCTION__, __LINE__) );//&GFXMaterialStaticDXT5Profile );
for(U32 x = 0; x < steps; x++)
{
for(U32 y = 0; y < steps; y++)
{
newPos = Point3F(gStart.x, gStart.y,0);
newPos.x += mCellSize * x;
newPos.y += mCellSize * y;
bounds.minExtents = Point3F(newPos.x - (mCellSize/2), newPos.y - (mCellSize/2), terrainMinHeight);
bounds.maxExtents = Point3F(newPos.x + (mCellSize/2), newPos.y + (mCellSize/2), terrainMaxHeight);
//newPos = cellPos;
//terrain fetching
//------------------------------------------------------------
//getContainer()->findObjects( bounds, TerrainObjectType, _findTerrainCallback, &terrainBlocks );
gClientContainer.findObjects( bounds, TerrainObjectType, _findTerrainCallback, &terrainBlocks );
if ( terrainBlocks.empty() )
return;
// Which terrain do I place on?
if ( terrainBlocks.size() == 1 )
terrainBlock = terrainBlocks.first();
else
{
for ( U32 i = 0; i < terrainBlocks.size(); i++ )
{
TerrainBlock *terrain = terrainBlocks[i];
const Box3F &terrBounds = terrain->getWorldBox();
if ( newPos.x < terrBounds.minExtents.x || newPos.x > terrBounds.maxExtents.x ||
newPos.y < terrBounds.minExtents.y || newPos.y > terrBounds.maxExtents.y )
continue;
terrainBlock = terrain;
break;
}
}
//bail if we have nothing
if ( !terrainBlock )
continue;
//------------------------------------------------------------
//should have a terrain now, woo
//newPos.x += mCellSize * x;
//newPos.y += mCellSize * y;
Point3F p = getRenderTransform().getPosition();
if (terrainBlock->getNormalAndHeight(Point2F(newPos.x,
newPos.y), &normal, &height))
{
newPos.z = height;
//newPos.convolve(terrain->getScale());
//keep it in world
//terrain->getTransform().mulP(newPos);
}
/*Point3F shapePosWorld;
MatrixF objToWorld = getRenderTransform();
objToWorld.mulP(cellPos, &shapePosWorld);
nodeStart = nodeEnd = shapePosWorld;
nodeStart.z = 2000.f;
nodeEnd.z = -2000.f;
//do {
// Perform Ray Cast Collision on Client.
CollisionResult = gClientContainer.castRay(nodeStart, nodeEnd, IG_COLLISION_MASK, &RayEvent);
if (CollisionResult)
{
// For now, let's pretend we didn't get a collision.
CollisionResult = false;
// Yes, so get it's type.
U32 CollisionType = RayEvent.object->getTypeMask();
//Con::printf("%i returned collision mask. terrain is %i", CollisionType, TerrainObjectType);
// Check Illegal Placements, fail if we hit a disallowed type.
if ((CollisionType & TerrainObjectType))
{
CollisionResult = true;
}
else
{
continue;
}
}*/
//} while(!CollisionResult);
// create the node
cell *newCell = new cell;
//newPos = RayEvent.point;
newCell->mTransform.identity();//reset
newCell->mTransform.setColumn(3, newPos);
//this->getWorldTransform().mulP(newPos);
newCell->mPosition = newPos;
//newCell->mNormal = RayEvent.normal;
newCell->mNormal = normal;
newCell->mBounds.maxExtents = Point3F(newPos.x + (mCellSize/2),newPos.y + (mCellSize/2),newPos.z + (mCellSize));
newCell->mBounds.minExtents = Point3F(newPos.x - (mCellSize/2),newPos.y - (mCellSize/2),newPos.z/* - (mCellSize/2)*/);
minPoint.setMin( newCell->mBounds.minExtents - getRenderPosition() );
maxPoint.setMax( newCell->mBounds.maxExtents - getRenderPosition() );
//newCell->mSeed = RandomGen.randI();
mCells.push_back(newCell);
/*do{
//nodePosition = getPosition();
//nodePosition = Point3F(gStart.x + x*gridSize.x, gStart.y + y*gridSize.y, 2000); //give it a starting position
//Point3F gridPos = Point3F(start.x + (x * gridSize.x), start.y + (y * gridSize.y), 2000); //give it a starting position
// Transform into world space coordinates
Point3F shapePosWorld;
MatrixF objToWorld = getRenderTransform();
objToWorld.mulP(nodePos, &shapePosWorld);
nodePosition = shapePosWorld;
nodeStart = nodeEnd = nodePosition;
nodeStart.z = 2000.f;
nodeEnd.z = -2000.f;
// Perform Ray Cast Collision on Client.
CollisionResult = gClientContainer.castRay(nodeStart, nodeEnd, FG_COLL_MASK, &RayEvent);
if (CollisionResult)
{
// For now, let's pretend we didn't get a collision.
CollisionResult = false;
// Yes, so get it's type.
U32 CollisionType = RayEvent.object->getTypeMask();
Con::printf("%i returned collision mask. terrain is %i", CollisionType, TerrainObjectType);
// Check Illegal Placements, fail if we hit a disallowed type.
if ((CollisionType & TerrainObjectType))
{
CollisionResult = true;
}
else
{
continue;
}
}
} while(!CollisionResult);
// Check for Relocation Problem.
// if ( mRetries > 0 )
// {
nodePosition = RayEvent.point;
Con::printf("Node position %f, %f, %f", nodePosition.x, nodePosition.y, nodePosition.z);
//mNodes[y].Ci = x;
//mNodes[y].Cj = y;
//mNodes[y].Ni = x * y;
//mNodes[y].normal = RayEvent.normal;
nodeNormal = RayEvent.normal;
mNumNodes++;
Con::printf("Number of grid nodes %i", mNumNodes);
// }
// else
// {
// Warning.
//Clint too many warnings here.
//Con::warnf(ConsoleLogEntry::General, "fxFoliageReplicator - Could not find satisfactory position for Foliage!");
// Skip to next.
// Con::warnf("Ran out of retries! bad!");
// continue;
// }
//RandomGen.setSeed(mNodes[y].mSeed); //set the node's seed for the blades
//Box3F boundBox;
//boundBox.min = Point3F(-gridSize.x/2, -gridSize.y/2, gridSize.z);
//boundBox.max = Point3F(gridSize.x/2, gridSize.y/2, gridSize.z);
node *Node = new node; //clear it and ready it for use
Node->pos0 = nodePosition;
Node->normal = nodeNormal;
//Node->bBox = boundBox;
mNodes.push_back(Node);
mCurrentNumNodes++;*/
}
}
startTime = Platform::getRealMilliseconds();
F32 tIdx = 0.0f;
// No, so setup Tables.
for (U32 idx = 0; idx < 720; idx++, tIdx+=(F32)(M_2PI/720.0f))
{
mCosTable[idx] = mCos(tIdx);
}
// No, so choose a random Sway phase.
swayPhase = RandomGen.randF(0, 719.0f);
// Set to random Sway Time.
swayTimeRatio = 719.0f / RandomGen.randF(minSwayTime, maxSwayTime);
F32 memAllocated = mCells.size() * sizeof(cell);
memAllocated += mCells.size() * sizeof(cell*);
Con::printf("indieGrass - Approx. %0.2fMb allocated for %i cells.", memAllocated / 1048576.0f, mCells.size());
/*mObjBox.min.set(minPoint);
mObjBox.max.set(maxPoint);
setTransform(mObjToWorld);*/
}
void indieGrass::generateGrass()
{
S32 dirtyCells =0, clearedCells = 0, LOD1Cells = 0;
for(U32 j=0; j < mCells.size(); j++)
{
//is it even to be rendered?
if(mCells[j]->mRender)
{
//VectorF camVector = mCells[j]->mPosition - mCuller.mCamPos;
//F32 dist = getMax( camVector.len(), 0.01f );
F32 dist = mCuller.getBoxDistance(mCells[j]->mBounds);
if(dist > LODDistance[2]){
if(mCells[j]->LOD != 3){
mCells[j]->LOD = 3;
mCells[j]->mDirty = true;
}
}
else if(dist > LODDistance[1]){
if(mCells[j]->LOD != 2){
mCells[j]->LOD = 2;
mCells[j]->mDirty = true;
}
}
else if(dist > LODDistance[0]){
if(mCells[j]->LOD != 1){
mCells[j]->LOD = 1;
mCells[j]->mDirty = true;
}
}
//do we actually have to generate, or did we do that already?
if(mCells[j]->mDirty)
{
dirtyCells++;
switch(mCells[j]->LOD)
{
case(1):
LOD1Cells++;
generateGrassLOD1(mCells[j]);
break;
case(2):
generateGrassLOD2(mCells[j]);
break;
case(3):
default:
generateGrassLOD3(mCells[j]);
break;
}
}
}
else
{
//we aren't rendered, so make sure we generate the grass next time we are
if(mCells[j]->mBlades.size()){ //do we have some grass in here leftover?
emptyCell(mCells[j]);
clearedCells++;
}
}
}
//let us know if we changed anything
/*if(dirtyCells || clearedCells){
Con::printf("Dirty cells this update: %i", dirtyCells);
Con::printf("Cleared cells this update: %i", clearedCells);
F32 memBlades = mGrassPerCell * sizeof(cell::blade) * LOD1Cells;
memBlades += mGrassPerCell * sizeof(cell::blade*) * LOD1Cells;
Con::printf("indieGrass - Approx. %0.4fMb allocated for %i blades of grass per %i LOD 1 cells.", memBlades / 1048576.0f, mGrassPerCell, LOD1Cells);
F32 memFreed = clearedCells * sizeof(cell) * mGrassPerCell;
memFreed += clearedCells * sizeof(cell*) * mGrassPerCell;
Con::printf("indieGrass - Approx. %0.4fMb memory freed from %i cleared cells.", memFreed / 1048576.0f, clearedCells);
}*/
}
void indieGrass::generateGrassLOD1(cell *mCell)
{
VectorF wind = getWindDirection();
//for(U32 j=0; j < mCells.size(); j++)
//{
for(U32 x=0; x< mGrassPerCell; x++)
{
cell::blade* _blade = new cell::blade();
//setup the grass base curve
//we offset for spread later, but this is testing atm
//RandomGen.setSeed(mCell->mSeed);
_blade->pos0 = mCell->mPosition;
_blade->pos0.x += RandomGen.randRangeF(mCellSize/2);
_blade->pos0.y += RandomGen.randRangeF(mCellSize/2);
_blade->pos0.z += mVerticalOffset;
_blade->tan0.x = 0;
_blade->tan0.y = 0;
_blade->tan0.z = mBaseLength;//mGrid.mNodes[j].length;
_blade->tan1.x = wind.x;
_blade->tan1.y = wind.y;
_blade->tan1.z = 0;//it stands up initially
//---------------------------------------------------
//then set up the hermite curve, which has it's own pos0, tan0, pos1, and tan1
//---------------------------------------------------
//this was calculated empirically, so we just guess untill we get numbers that look good
_blade->H.coef1 = hOriginCoeff;
_blade->H.coef2 = hEndCoeff;
_blade->H.tan0 = VectorF(hDirTan,hDirTan,hDirTan);
_blade->H.tan1 = VectorF(hEndTan,hEndTan,hEndTan);
//---------------------------------------------------
_blade->H.pos0.x = _blade->pos0.x;
//_blade->H.pos0.y = _blade->pos0.y; not needed
_blade->H.pos0.z = mBaseLength;
_blade->H.pos1.x = _blade->H.coef1 * mBaseLength;
_blade->H.pos1.z = _blade->H.coef2 * mBaseLength;
//---------------------------------------------------
//and finally get the actual curve's pos1, using all the stuff we just setup.
_blade->pos1.x = _blade->pos0.x + VarX(_blade->H, mWindStrength);
_blade->pos1.y = _blade->pos0.y + VarY(_blade->H, mWindStrength);
_blade->pos1.z = VarZ(_blade->H, mWindStrength);
//probably not needed until we get to interaction
_blade->worldBox.minExtents = Point3F(_blade->pos0.x - mBaseWidth,_blade->pos0.y - mBaseWidth,_blade->pos0.z);
_blade->worldBox.maxExtents = Point3F(_blade->pos0.x + mBaseWidth,_blade->pos0.y + mBaseWidth,_blade->pos0.z + mBaseLength);
mCell->mBlades.push_back(_blade);
}
mCell->mDirty = false; //flag so we don't
//}
}
void indieGrass::generateGrassLOD2(cell *mCell)
{
VectorF wind = getWindDirection();
//for(U32 j=0; j < mCells.size(); j++)
//{
for(U32 x=0; x< 4; x++)
{
cell::blade* _blade = new cell::blade();
//setup the grass base curve
//we offset for spread later, but this is testing atm
_blade->pos0 = Point3F(mCell->mPosition.x-(mCellSize/2), mCell->mPosition.y-(mCellSize/2), mCell->mPosition.z);
//ugly, but it'll work for now
switch(x)
{
case(0):
break; //already in place
case(1):
_blade->pos0.x += mCellSize;
break;
case(2):
_blade->pos0.x += mCellSize;
_blade->pos0.y += mCellSize;
break;
case(3):
_blade->pos0.y += mCellSize;
break;
}
_blade->tan0.x = 0;
_blade->tan0.y = 0;
_blade->tan0.z = mBaseLength;//mGrid.mNodes[j].length;
_blade->tan1.x = wind.x;
_blade->tan1.y = wind.y;
_blade->tan1.z = 1 - wind.len();
//---------------------------------------------------
//then set up the hermite curve, which has it's own pos0, tan0, pos1, and tan1
//---------------------------------------------------
//this was calculated empirically, so we just guess untill we get numbers that look good
_blade->H.coef1 = hOriginCoeff;
_blade->H.coef2 = hEndCoeff;
_blade->H.tan0 = VectorF(hDirTan,hDirTan,hDirTan);
_blade->H.tan1 = VectorF(hEndTan,hEndTan,hEndTan);
//---------------------------------------------------
_blade->H.pos0.x = _blade->pos0.x;
//_blade->H.pos0.y = _blade->pos0.y; not needed
_blade->H.pos0.z = mBaseLength;
_blade->H.pos1.x = _blade->H.coef1 * mBaseLength;
_blade->H.pos1.z = _blade->H.coef2 * mBaseLength;
//---------------------------------------------------
//and finally get the actual curve's pos1, using all the stuff we just setup.
_blade->pos1.x = _blade->pos0.x + VarX(_blade->H, mWindStrength);
_blade->pos1.y = _blade->pos0.y + VarY(_blade->H, mWindStrength);
_blade->pos1.z = VarZ(_blade->H, mWindStrength);
_blade->worldBox.minExtents = _blade->worldBox.maxExtents = _blade->pos0;
mCell->mBlades.push_back(_blade);
}
mCell->mDirty = false; //flag so we don't
//}
}
void indieGrass::generateGrassLOD3(cell *mCell)
{
VectorF wind = getWindDirection();
cell::blade* _blade = new cell::blade();
_blade->pos0 = mCell->mPosition;
_blade->tan0.x = 0;
_blade->tan0.y = 0;
_blade->tan0.z = mBaseLength;
_blade->tan1.x = wind.x;
_blade->tan1.y = wind.y;
_blade->tan1.z = 1 - wind.len();
_blade->H.coef1 = hOriginCoeff;
_blade->H.coef2 = hEndCoeff;
_blade->H.tan0 = VectorF(hDirTan,hDirTan,hDirTan);
_blade->H.tan1 = VectorF(hEndTan,hEndTan,hEndTan);
_blade->H.pos0.x = _blade->pos0.x;
_blade->H.pos0.z = mBaseLength;
_blade->H.pos1.x = _blade->H.coef1 * mBaseLength;
_blade->H.pos1.z = _blade->H.coef2 * mBaseLength;
_blade->pos1.x = _blade->pos0.x + VarX(_blade->H, mWindStrength);
_blade->pos1.y = _blade->pos0.y + VarY(_blade->H, mWindStrength);
_blade->pos1.z = VarZ(_blade->H, mWindStrength);
_blade->worldBox.minExtents = _blade->worldBox.maxExtents = _blade->pos0;
mCell->mBlades.push_back(_blade);
mCell->mDirty = false; //flag so we don't
}
//================================================================================
// Hermite Curve code
//================================================================================
inline F32 indieGrass::H0( const F32 &t)
{
return (1-3*(t*t) + 2*(t*t*t));
}
inline F32 indieGrass::H1( const F32 &t)
{
return (3*(t*t) - 2*(t*t*t));
}
inline F32 indieGrass::H_0( const F32 &t)
{
return (t - 2*(t*t) + (t*t*t));
}
inline F32 indieGrass::H_1( const F32 &t)
{
return (-(t*t) + (t*t*t));
}
//all this crap is to calculate the 'interpolation curve' that the grass moves along to simulate the swaying
//or crushing animation.
F32 indieGrass::VarZ2D(Hermite H, F32 wind)
{
return H0(wind) * H.pos0.z + H1(wind) * H.pos1.z + H_0(wind) * H.tan0.z + H_1(wind) * H.tan1.z;
}
F32 indieGrass::VarX2D(Hermite H, F32 wind)
{
return H0(wind) * H.pos0.x + H1(wind) * H.pos1.x + H_0(wind) * H.tan0.x + H_1(wind) * H.tan1.x;
}
F32 indieGrass::VarX(Hermite H, F32 wind)
{
VectorF windDir = getWindDirection();
return VarX2D(H, wind) * (windDir.x / wind);
}
F32 indieGrass::VarY(Hermite H, F32 wind)
{
VectorF windDir = getWindDirection();
return VarX2D(H, wind) * (windDir.z / wind);
}
F32 indieGrass::VarZ(Hermite H, F32 wind)
{
return VarZ2D(H, wind);
}
//replace with direct feed from global wind
void indieGrass::processCell(cell *mCell)
{
VectorF wind = getWindDirection();
updateWindStrength();
for(U32 x=0; x< mCell->mBlades.size(); x++)
{
mCell->mBlades[x]->tan1.z = 1 - mWindStrength;
mCell->mBlades[x]->pos1.x = mCell->mBlades[x]->pos0.x + VarX(mCell->mBlades[x]->H, mWindStrength);
mCell->mBlades[x]->pos1.y = mCell->mBlades[x]->pos0.y + VarY(mCell->mBlades[x]->H, mWindStrength);
//mCell->mBlades[x]->pos1.z = mCell->mBlades[x]->pos0.z + VarZ(mCell->mBlades[x]->H, wind);
mCell->mBlades[x]->pos1.z = VarZ(mCell->mBlades[x]->H, mWindStrength);
}
}
//================================================================================
// Misc core code
//================================================================================
bool indieGrass::onAdd()
{
if(!Parent::onAdd()) return(false);
//dynamic_cast<SimSet*>(Sim::findObject("indieGrassSet"))->addObject(this);
// Set initial bounding box.
//
// NOTE:- Set this box to completely encapsulate your object.
// You must reset the world box and set the render transform
// after changing this.
//mObjBox.min.set(-0.5f,-0.5f,-0.5f);
//mObjBox.max.set(+0.5f,+0.5f,+0.5f);
// We don't use any bounds.
mObjBox.minExtents.set(-1e5, -1e5, -1e5);
mObjBox.maxExtents.set( 1e5, 1e5, 1e5);
// Reset the World Box.
resetWorldBox();
// Set the Render Transform.
setRenderTransform(mObjToWorld);
if ( isClientObject() )
{
generateCells();
}
// Add to Scene.
addToScene();
// Return OK.
return(true);
}
void indieGrass::onRemove()
{
// Remove from Scene.
removeFromScene();
//dynamic_cast<SimSet*>(Sim::findObject("indieGrassSet"))->removeObject(this);
// mGrid.onRemove();
// Do Parent.
Parent::onRemove();
}
void indieGrass::inspectPostApply()
{
// Set Parent.
Parent::inspectPostApply();
// Set fxPortal Mask.
setMaskBits(indieGrassMask);
}
void indieGrass::onEditorEnable()
{
}
void indieGrass::onEditorDisable()
{
}
//================================================================================
// Rendering code
//================================================================================
bool indieGrass::prepRenderImage( SceneState* state, const U32 stateKey, const U32 startZone,
const bool modifyBaseZoneState)
{
// Return if last state.
if (isLastState(state, stateKey)) return false;
// Set Last State.
setLastState(state, stateKey);
// Is Object Rendered?
if (!state->isObjectRendered(this))
return false;
//curse you world matrix! currrrseee youuuuu
GFXTransformSaver saver;
// Setup the frustum culler.
if (!mCuller.mSceneState || !mLockFrustum )
{
//mCuller.mFarDistance = mCullRadius;
mCuller.init( state );
}
ObjectRenderInst *ri = gRenderInstManager->allocInst<ObjectRenderInst>();
ri->mRenderDelegate = mRenderDelegate;
ri->state = state;
ri->type = RenderPassManager::RIT_Foliage;
gRenderInstManager->addInst( ri );
// Update the cells.
updateCellRenderList();
if(mRenderedCells)
generateGrass();
return true;
}
void indieGrass::renderObject(ObjectRenderInst *ri, BaseMatInstance* overrideMat)
{
//------------------------------------------------------------------
// Setup
//------------------------------------------------------------------
// Calculate Elapsed Time and take new Timestamp.
S32 Time = Platform::getVirtualMilliseconds();
F32 ElapsedTime = (Time - mLastRenderTime) * 0.001f;
mLastRenderTime = Time;
SceneState* state = ri->state;
// Return if we don't have a material
//if (!mFoliageTexture) return;
// Set up rendering state
GFX->disableShaders();
SceneGraphData sgData;
// Set up projection and world transform info.
MatrixF proj = GFX->getProjectionMatrix();
GFX->pushWorldMatrix();
GFX->multWorld(getRenderTransform());
MatrixF world = GFX->getWorldMatrix();
proj.mul(world);
proj.transpose();
//GFX->setVertexShaderConstF( 0, (float*)&proj, 4 );
//mConstBuffer->set( mModelViewProjectConst, proj );
// Store object and camera transform data
sgData.objTrans = getRenderTransform();
sgData.camPos = state->getCameraPosition();
//------------------------------------------------------------------
//if we're not acutally making grass, bail
if(mSteps <= 0)
return;
//const F32 MinViewDist = mViewClosest - mFieldData.mFadeOutRegion;
//const F32 MaxViewDist = ClippedViewDistance + mFieldData.mFadeInRegion;
if(mRender)
{
for(U32 j=0; j < mCells.size(); j++)
{
if(mCells[j]->mRender)
{
// Calculate Fog Alpha.
//FogAlpha = 1.0f - state->getHazeAndFog(Distance, pFoliageItem->Transform.getPosition().z - state->getCameraPosition().z);
// Trivially reject the billboard if it's totally transparent.
//if (FogAlpha < FXFOLIAGE_ALPHA_EPSILON) continue;
// Yes, so are we fading out?
/*if (Distance < mFieldData.mViewClosest)
{
// Yes, so set fade-out.
ItemAlpha = 1.0f - ((mFieldData.mViewClosest - Distance) * mFadeOutGradient);
}
// No, so are we fading in?
else if (Distance > ClippedViewDistance)
{
// Yes, so set fade-in
ItemAlpha = 1.0f - ((Distance - ClippedViewDistance) * mFadeInGradient);
}
// No, so set full.
else
{
ItemAlpha = 1.0f;
}
if (ItemAlpha > FogAlpha) ItemAlpha = FogAlpha;*/
//-------------------------------------------------
//update the grass sim
//processCell(mCells[j]);//do a check later against inreaction/wind updates and fresh rendering
//if we haven't changed from anything since last frame,
//don't bother processing
switch(mCells[j]->LOD)
{
//per-blade rendering
case(1):
renderLOD1(sgData, mCells[j]);
break;
//2.5d rendering
case(2):
//renderLOD2(mCells[j]);
break;
//billboard rendering
case(3):
default:
//renderLOD3(mCells[j]);
break;
}
}
}
}
// Used for debug drawing.
if(mRenderCells)
{
GFXDrawUtil* drawer = GFX->getDrawUtil();
drawer->clearBitmapModulation();
for(U32 j=0; j < mCells.size(); j++)
{
if(mCells[j]->mRender)
{
ColorI clr;
if(mCells[j]->LOD==1)
clr = ColorI(0,255,0);
else if(mCells[j]->LOD==2)
clr = ColorI(255,255,0);
else if(mCells[j]->LOD==3)
clr = ColorI(255,0,0);
Point3F size = Point3F(mCells[j]->mBounds.len_x()/2,
mCells[j]->mBounds.len_y()/2,mCells[j]->mBounds.len_z()/2);
drawer->drawWireCube( size, mCells[j]->mPosition, clr );
}
}
Point3F size = Point3F(mCells[1]->mBounds.len_x()/2,
mCells[1]->mBounds.len_y()/2,mCells[1]->mBounds.len_z()/2);
ColorI clr = ColorI(0,0,255);
drawer->drawWireCube( size, mCuller.mCamPos, clr );
}
/*if(mRenderVectors)
{
for(U32 j=0; j < mCells.size(); j++)
{
if(mCells[j]->mRender)
{
if(mCells[j]->LOD == 1)
{
F32 pas = 1.0f / mSteps;
Point3F p;
//-------------------------------------------------
//now that we're set, we generate the blade's curve and render
for(U32 g=0; g < mCells[j]->mBlades.size(); g++)
{
Point3F _pos0 = mCells[j]->mBlades[g]->pos0;
Point3F _pos1 = mCells[j]->mBlades[g]->pos1;
VectorF _tan0 = mCells[j]->mBlades[g]->tan0;
VectorF _tan1 = mCells[j]->mBlades[g]->tan1;
p.x = _pos0.x; //place the first
p.y = _pos0.y;
p.z = _pos0.z;
//render
glColor3f(0,0,255);