00001
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifndef NL_LANDSCAPE_H
00027 #define NL_LANDSCAPE_H
00028
00029
00030 #include "nel/misc/types_nl.h"
00031 #include "nel/misc/class_id.h"
00032 #include "nel/misc/smart_ptr.h"
00033 #include "nel/misc/triangle.h"
00034 #include "3d/zone.h"
00035 #include "3d/tile_bank.h"
00036 #include "3d/patch_rdr_pass.h"
00037 #include "3d/vertex_buffer.h"
00038 #include "3d/primitive_block.h"
00039 #include "3d/material.h"
00040 #include "3d/tile_far_bank.h"
00041 #include "3d/texture_near.h"
00042 #include "3d/quad_grid.h"
00043 #include "nel/misc/block_memory.h"
00044 #include "3d/landscapevb_allocator.h"
00045 #include "3d/landscape_face_vector_manager.h"
00046 #include "3d/tess_face_priority_list.h"
00047 #include "nel/3d/point_light_influence.h"
00048
00049 #include <map>
00050
00051 #define NL_MAX_SIZE_OF_TEXTURE_EDGE_SHIFT (NL_MAX_TILES_BY_PATCH_EDGE_SHIFT+NL_NUM_PIXELS_ON_FAR_TILE_EDGE_SHIFT)
00052 #define NL_MAX_SIZE_OF_TEXTURE_EDGE (1<<NL_MAX_SIZE_OF_TEXTURE_EDGE_SHIFT) // Size max of a far texture edge in pixel
00053
00054
00055 namespace NL3D
00056 {
00057
00058
00059 class CHeightMap;
00060 class CTileNoiseMap;
00061 class CVegetableManager;
00062 class CVegetable;
00063 class CTileVegetableDesc;
00064 class CScene;
00065 class CTextureFar;
00066 class CTextureDLM;
00067 class CPatchDLMContextList;
00068
00069 using NLMISC::Exception;
00070 using NLMISC::CTriangle;
00071 using NLMISC::CBlockMemory;
00072
00073
00074
00075 const sint NbTilesMax= 65536;
00076
00077
00078 const sint TextureNearSize= 512;
00079 const sint NbTileLightMapByLine= TextureNearSize/NL_TILE_LIGHTMAP_SIZE;
00080 const sint NbTileLightMapByTexture= NbTileLightMapByLine*NbTileLightMapByLine;
00081
00082
00083
00084
00091 struct EBadBind : public Exception
00092 {
00093 private:
00094 mutable std::string _Output;
00095
00096 public:
00097 struct CBindError
00098 {
00099 CBindError(sint z, sint p) {ZoneId= z; PatchId= p;}
00100 sint ZoneId;
00101 sint PatchId;
00102 };
00103
00104
00105
00106 std::list<CBindError> BindErrors;
00107
00108 public:
00109 EBadBind() {}
00110 ~EBadBind() throw () {}
00111 virtual const char *what() const throw();
00112
00113 };
00114
00115
00116
00117
00138 class CLandscape : public NLMISC::CRefCount
00139 {
00140 public:
00141
00142 CTileBank TileBank;
00143 CTileFarBank TileFarBank;
00144
00145 public:
00146
00148 CLandscape();
00150 ~CLandscape();
00151
00152
00154
00156
00157
00163 bool addZone(const CZone &newZone);
00164
00170 bool removeZone(uint16 zoneId);
00171
00173 void clear();
00174
00176 void checkBinds() throw(EBadBind);
00178 void checkBinds(uint16 zoneId) throw(EBadBind);
00179
00185 bool initTileBanks ();
00186
00187
00188
00190
00192
00194
00196
00198
00199 void setRefineMode(bool enabled) {_RefineMode= enabled;}
00200 bool getRefineMode() const {return _RefineMode;}
00202 void setTileMaxSubdivision (uint tileDiv);
00204 uint getTileMaxSubdivision ();
00205
00207 void setNoiseMode(bool enabled);
00208 bool getNoiseMode() const;
00209
00210
00211
00212
00213
00214
00216
00222 void setDriver(IDriver *drv);
00226 void clip(const CVector &refineCenter, const std::vector<CPlane> &pyramid);
00229 void refine(const CVector &refineCenter);
00236 void render(const CVector &refineCenter, const CVector &frontVector, const CPlane pyramid[NL3D_TESSBLOCK_NUM_CLIP_PLANE], bool doTileAddPass=false);
00237
00239 void refineAll(const CVector &refineCenter);
00241 void excludePatchFromRefineAll(sint zoneId, uint patch, bool exclude);
00242
00243
00248 void averageTesselationVertices();
00249
00250
00251
00252
00253
00255
00263 void buildCollideFaces(const CAABBoxExt &bbox, std::vector<CTriangle> &faces, bool faceSplit);
00267 void buildCollideFaces(sint zoneId, sint patch, std::vector<CTriangle> &faces);
00268
00276 void buildTrianglesInBBox(const CAABBox &bbox, std::vector<CTrianglePatch> &triangles, uint8 tileTessLevel);
00277
00278
00285 void buildPatchBlocksInBBox(const CAABBox &bbox, std::vector<CPatchBlockIdent> &paBlockIds);
00289 void fillPatchQuadBlock(CPatchQuadBlock &quadBlock) const;
00290
00291
00295 CVector getTesselatedPos(const CPatchIdent &patchId, const CUV &uv) const;
00296
00297
00301 void getTessellationLeaves(std::vector<const CTessFace*> &leaves) const;
00302
00303
00304
00305
00306
00308
00314 CZone* getZone (sint zoneId);
00320 const CZone* getZone (sint zoneId) const;
00323 void getZoneList(std::vector<uint16> &zoneIds) const;
00325 static void buildZoneName(sint zoneId, std::string &zoneName);
00326
00327
00328
00330
00332
00334
00335
00337 NLMISC::CSmartPtr<ITexture> getTileTexture(uint16 tileId, CTile::TBitmap bitmapType, CVector &uvScaleBias);
00338
00340 CTileElement *getTileElement(const CPatchIdent &patchId, const CUV &uv);
00341
00342
00344
00352 void setupStaticLight (const CRGBA &diffuse, const CRGBA &ambiant, float multiply);
00358 const CRGBA* getStaticLight () const
00359 {
00360 return _LightValue;
00361 }
00362
00363
00368 void setPointLightDiffuseMaterial(CRGBA diffuse);
00372 CRGBA getPointLightDiffuseMaterial () const
00373 {
00374 return _PointLightDiffuseMaterial;
00375 }
00376
00377
00383 void enableAutomaticLighting(bool enable);
00389 void setupAutomaticLightDir(const CVector &lightDir);
00391 bool getAutomaticLighting() const {return _AutomaticLighting;}
00393 const CVector &getAutomaticLightDir() const {return _AutomaticLightDir;}
00394
00395
00398 void removeAllPointLights();
00399
00400
00406 void setPointLightFactor(const std::string &lightGroupName, NLMISC::CRGBA nFactor);
00407
00408
00409
00410
00411
00413
00415
00421 void setHeightField(const CHeightMap &hf);
00422
00423
00424
00426
00427
00431 void createVegetableBlendLayersModels(CScene *scene);
00432
00436 void setVegetableTime(double time);
00437
00441 void setVegetableUpdateLightingTime(double time);
00442
00443
00447 void enableVegetable(bool enable);
00448
00452 bool isVegetableActive() const;
00453
00456 void loadVegetableTexture(const std::string &textureFileName);
00457
00460 void setupVegetableLighting(const CRGBA &ambient, const CRGBA &diffuse, const CVector &directionalLight);
00461
00471 void setVegetableWind(const CVector &windDir, float windFreq, float windPower, float windBendMin);
00472
00473
00477 uint getNumVegetableFaceRendered() const;
00478
00479
00484 void setVegetableUpdateLightingFrequency(float freq);
00485
00486
00495 void setupColorsFromTileFlags(const NLMISC::CRGBA colors[4]);
00496
00497
00498
00499
00501
00502
00504 uint8 getLumel(const CPatchIdent &patchId, const CUV &uv) const;
00505
00507 void appendTileLightInfluences(const CPatchIdent &patchId, const CUV &uv,
00508 std::vector<CPointLightInfluence> &pointLightList) const;
00509
00510
00511
00512
00514
00515
00523 void setPZBModelPosition(const CVector &pos);
00524
00526 const CVector &getPZBModelPosition() const {return _PZBModelPosition;}
00527
00528
00529
00530
00532
00533
00537 void updateLighting(double time);
00538
00543 void setUpdateLightingFrequency(float freq);
00544
00548 void updateLightingAll();
00549
00550
00551
00552
00553
00555
00556
00560 void computeDynamicLighting(const std::vector<CPointLight*> &pls);
00561
00566 uint getDynamicLightingMemoryLoad() const;
00567
00571 void setDynamicLightingMaxAttEnd(float maxAttEnd);
00572
00575 float getDynamicLightingMaxAttEnd() const {return _DLMMaxAttEnd;}
00576
00580 void setDLMGlobalVegetableColor(CRGBA gvc);
00581
00584 CRGBA getDLMGlobalVegetableColor() const {return _DLMGlobalVegetableColor;}
00585
00586
00587
00588
00589
00590
00591 private:
00592
00593 friend class CTessFace;
00594 friend class CPatch;
00595 friend class CZone;
00596
00597
00599
00600
00601 CBlockMemory<CTessFace> TessFaceAllocator;
00602 CBlockMemory<CTessVertex> TessVertexAllocator;
00603 CBlockMemory<CTessNearVertex> TessNearVertexAllocator;
00604 CBlockMemory<CTessFarVertex> TessFarVertexAllocator;
00605 CBlockMemory<CTileMaterial> TileMaterialAllocator;
00606 CBlockMemory<CTileFace> TileFaceAllocator;
00607
00608 CTessFace *newTessFace();
00609 CTessVertex *newTessVertex();
00610 CTessNearVertex *newTessNearVertex();
00611 CTessFarVertex *newTessFarVertex();
00612 CTileMaterial *newTileMaterial();
00613 CTileFace *newTileFace();
00614
00615 void deleteTessFace(CTessFace *f);
00616 void deleteTessVertex(CTessVertex *v);
00617 void deleteTessNearVertex(CTessNearVertex *v);
00618 void deleteTessFarVertex(CTessFarVertex *v);
00619 void deleteTileMaterial(CTileMaterial *tm);
00620 void deleteTileFace(CTileFace *tf);
00621
00622
00623 CLandscapeFaceVectorManager _FaceVectorManager;
00624
00625
00626 CTessBlock _TessBlockModificationRoot;
00627
00628
00629
00630
00631 CPatchRdrPass *getFarRenderPass(CPatch* pPatch, uint farIndex, float& far1UScale, float& far1VScale, float& far1UBias, float& far1VBias, bool& bRot);
00632
00633 void freeFarRenderPass (CPatch* pPatch, CPatchRdrPass* pass, uint farIndex);
00634
00635 CPatchRdrPass *getTileRenderPass(uint16 tileId, bool additiveRdrPass);
00636
00637
00638 void getTileUvScaleBiasRot(uint16 tileId, CTile::TBitmap bitmapType, CVector &uvScaleBias, uint8 &rotAlpha);
00639
00640
00641 void resetRenderFarAndDeleteVBFV();
00643 void forceMergeAtTileLevel();
00644
00645
00646 void updateGlobalsAndLockBuffers (const CVector &refineCenter);
00647
00648 void lockBuffers ();
00649
00650 void unlockBuffers ();
00651
00652 void synchronizeATIVBHards();
00653
00654 void updateTessBlocksFaceVector();
00655
00656
00657
00658 void eraseFarRenderPassFromSet (CPatchRdrPass* pass);
00659
00660
00661
00662 private:
00663 TZoneMap Zones;
00664
00665
00666 float _TileDistNear;
00667 float _Threshold;
00668 bool _RefineMode;
00669 float _FarTransition;
00670 uint _TileMaxSubdivision;
00671
00672 float _VPThresholdChange;
00673
00675
00676 CRefPtr<IDriver> _Driver;
00677 CLandscapeVBAllocator _Far0VB;
00678 CLandscapeVBAllocator _Far1VB;
00679 CLandscapeVBAllocator _TileVB;
00680
00681 bool _VertexShaderOk;
00682
00683 bool _RenderMustRefillVB;
00684
00685
00686
00687
00688
00689
00690 typedef NLMISC::CRefPtr<ITexture> RPTexture;
00691 typedef std::map<std::string, RPTexture> TTileTextureMap;
00692 typedef TTileTextureMap::iterator ItTileTextureMap;
00693
00694 typedef std::set<CPatchRdrPass> TTileRdrPassSet;
00695 typedef TTileRdrPassSet::iterator ItTileRdrPassSet;
00696 typedef NLMISC::CSmartPtr<CPatchRdrPass> TSPRenderPass;
00697
00698
00699 struct CTileInfo
00700 {
00701
00702
00703
00704 CPatchRdrPass *DiffuseRdrPass;
00705
00706 CPatchRdrPass *AdditiveRdrPass;
00707
00708
00709
00710 CVector DiffuseUvScaleBias;
00711 CVector AlphaUvScaleBias;
00712 CVector AdditiveUvScaleBias;
00713
00714 uint8 RotAlpha;
00715 };
00716
00717
00718
00719
00720
00721 TTileTextureMap TileTextureMap;
00722
00723 TTileRdrPassSet TileRdrPassSet;
00724
00725 std::vector<CTileInfo*> TileInfos;
00726
00727
00728 std::vector<TSPRenderPass> _TextureNears;
00729 uint _NFreeLightMaps;
00730
00731
00732
00733 CMaterial TileMaterial;
00734
00735
00736 CMaterial FarMaterial;
00737
00738
00739
00740
00741
00742
00743
00744 typedef std::set<TSPRenderPass> TSPRenderPassSet;
00745 typedef TSPRenderPassSet::iterator ItSPRenderPassSet;
00746 typedef std::vector<TSPRenderPassSet> TSPRdrPassSetVector;
00747 TSPRenderPassSet _FarRdrPassSet;
00748 TSPRdrPassSetVector _FarRdrPassSetVectorFree;
00749 bool _FarInitialized;
00750
00751
00752 bool eraseTileFarIfNotGood (uint tileNumber, uint sizeOrder0, uint sizeOrder1, uint sizeOrder2);
00753
00754
00755 static uint getRdrPassIndexWithSize (uint width, uint height);
00756 void addPatch ();
00757 void removePatch ();
00758
00759
00760
00761 CRGBA _LightValue[256];
00762
00763 bool _AutomaticLighting;
00764 CVector _AutomaticLightDir;
00765
00766
00767 typedef std::map<std::string, NLMISC::CRGBA> TLightGroupColorMap;
00768 typedef TLightGroupColorMap::iterator ItLightGroupColorMap;
00769 TLightGroupColorMap _LightGroupColorMap;
00770
00771
00772 private:
00773
00774 void loadTile(uint16 tileId);
00775 void releaseTile(uint16 tileId);
00776 ITexture *findTileTexture(const std::string &textName);
00777 CPatchRdrPass *findTileRdrPass(const CPatchRdrPass &pass);
00778
00779
00780
00781
00782
00783 uint getTileLightMap(CRGBA map[NL_TILE_LIGHTMAP_SIZE*NL_TILE_LIGHTMAP_SIZE], CPatchRdrPass *&lightmapRdrPass);
00784
00785 void getTileLightMapUvInfo(uint tileLightMapId, CVector &uvScaleBias);
00786
00787 void releaseTileLightMap(uint tileLightMapId);
00788
00789
00790 void refillTileLightMap(uint tileLightMapId, CRGBA map[NL_TILE_LIGHTMAP_SIZE*NL_TILE_LIGHTMAP_SIZE]);
00791
00792
00793
00794
00795
00796 void checkZoneBinds(CZone &curZone, EBadBind &bindError);
00797
00798
00802 struct CBezierPatchZ
00803 {
00805 float Vertices[4];
00807 float Tangents[8];
00809 float Interiors[4];
00810
00812 void makeInteriors();
00814 float eval(float s, float t) const;
00815 };
00816
00817
00818
00819 struct CHeightField
00820 {
00821 std::vector<CBezierPatchZ> ZPatchs;
00822
00824 float OriginX, OriginY;
00826 float SizeX, SizeY;
00827 float OOSizeX, OOSizeY;
00829 uint Width, Height;
00830 };
00831 CHeightField _HeightField;
00832
00833
00835
00836
00837
00838 struct CPatchIdentEx : public CPatchIdent
00839 {
00840 const CPatch *Patch;
00841 };
00842
00844 CQuadGrid<CPatchIdentEx> _PatchQuadGrid;
00845 static const uint _PatchQuadGridSize;
00846 static const float _PatchQuadGridEltSize;
00847
00854 void addTrianglesInBBox(const CPatchIdentEx &paIdEx, const CAABBox &bbox, std::vector<CTrianglePatch> &triangles, uint8 tileTessLevel) const;
00855
00858 void addPatchBlocksInBBox(const CPatchIdentEx &paIdEx, const CAABBox &bbox, std::vector<CPatchBlockIdent> &paBlockIds);
00859
00860
00861
00863
00864
00865 bool _NoiseEnabled;
00866
00867
00868
00870
00872
00874
00876
00877 bool _OldRefineCenterSetuped;
00879 CTessFacePListNode _RootNewLeaves;
00880
00881
00882
00884
00886
00887
00889 bool _VegetableManagerEnabled;
00891 bool _DriverOkForVegetable;
00892
00894 CTessList<CLandscapeVegetableBlock> _VegetableBlockList;
00895
00899 const CTileVegetableDesc &getTileVegetableDesc(uint16 tileId);
00900
00901
00902
00903
00905
00907
00908
00909
00910
00912
00913
00914 double _ULPrecTime;
00915 bool _ULPrecTimeInit;
00916 double _ULTime;
00918 float _ULFrequency;
00919
00920
00922 sint _ULTotalFarPixels;
00924 float _ULFarPixelsToUpdate;
00926 CTextureFar *_ULRootTextureFar;
00928 uint _ULFarCurrentPatchId;
00929
00930
00932 sint _ULTotalNearPixels;
00934 float _ULNearPixelsToUpdate;
00936 CPatch *_ULRootNearPatch;
00938 uint _ULNearCurrentTessBlockId;
00939
00941 void linkPatchToNearUL(CPatch *patch);
00942 void unlinkPatchFromNearUL(CPatch *patch);
00943
00944
00946 void updateLightingTextureFar(float ratio);
00948 void updateLightingTextureNear(float ratio);
00949
00950
00951
00952
00953
00955
00957
00959
00961
00963
00965
00966
00968 CRGBA _DLMGlobalVegetableColor;
00969
00971 CRGBA _PointLightDiffuseMaterial;
00972
00973
00974
00975 };
00976
00977
00978
00979 }
00980
00981
00982 #endif // NL_LANDSCAPE_H
00983
00984