123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535 |
- #ifndef CC_TERRAIN_H
- #define CC_TERRAIN_H
- #include <vector>
- #include "2d/CCNode.h"
- #include "2d/CCCamera.h"
- #include "renderer/CCTexture2D.h"
- #include "renderer/CCCustomCommand.h"
- #include "renderer/CCRenderState.h"
- #include "3d/CCAABB.h"
- #include "3d/CCRay.h"
- #include "base/CCEventListenerCustom.h"
- #include "base/CCEventDispatcher.h"
- NS_CC_BEGIN
-
- #define MAX_CHUNKES 256
-
- class CC_DLL Terrain : public Node
- {
- public:
-
- enum class CrackFixedType{
- SKIRT,
- INCREASE_LOWER,
- };
-
- struct CC_DLL DetailMap{
-
- DetailMap();
- DetailMap(const std::string& detailMapSrc, float size = 35);
-
- std::string _detailMapSrc;
-
- float _detailMapSize;
- };
-
- struct Triangle
- {
- Triangle(const Vec3& p1, const Vec3& p2, const Vec3& p3);
- bool getIntersectPoint(const Ray& ray, Vec3& intersectPoint) const;
-
- CC_DEPRECATED_ATTRIBUTE bool getInsterctPoint(const Ray& ray, Vec3& interScetPoint) const;
- void transform(const Mat4& matrix);
- Vec3 _p1, _p2, _p3;
- };
-
- struct CC_DLL TerrainData
- {
-
- TerrainData();
-
- TerrainData(const std::string& heightMapsrc, const std::string& textureSrc, const Size & chunksize = Size(32,32), float mapHeight = 2, float mapScale = 0.1);
-
- TerrainData(const std::string& heightMapsrc, const std::string& alphamap, const DetailMap& detail1,const DetailMap& detail2, const DetailMap& detail3, const DetailMap& detail4, const Size & chunksize = Size(32,32), float mapHeight = 2, float mapScale = 0.1);
-
- TerrainData(const std::string& heightMapsrc, const std::string& alphamap, const DetailMap& detail1,const DetailMap& detail2, const DetailMap& detail3, const Size & chunksize = Size(32,32), float mapHeight = 2, float mapScale = 0.1);
-
- Size _chunkSize;
-
- std::string _heightMapSrc;
-
- std::string _alphaMapSrc;
-
- DetailMap _detailMaps[4];
-
- float _mapHeight;
-
- float _mapScale;
-
- int _detailMapAmount;
-
- float _skirtHeightRatio;
- };
- private:
- struct ChunkIndices
- {
- GLuint _indices;
- unsigned short _size;
- };
- struct ChunkLODIndices
- {
- int _relativeLod[5];
- ChunkIndices _chunkIndices;
- };
- struct ChunkLODIndicesSkirt
- {
- int _selfLod;
- ChunkIndices _chunkIndices;
- };
-
- struct CC_DLL TerrainVertexData
- {
-
- TerrainVertexData(){};
- TerrainVertexData(const Vec3& v1, const Tex2F& v2)
- {
- _position = v1;
- _texcoord = v2;
- }
-
- cocos2d::Vec3 _position;
- cocos2d::Tex2F _texcoord;
- cocos2d::Vec3 _normal;
- };
- struct CC_DLL QuadTree;
-
- struct Chunk
- {
-
- Chunk();
-
- ~Chunk();
-
- std::vector<TerrainVertexData> _originalVertices;
-
- struct LOD{
- std::vector<GLushort> _indices;
- };
- GLuint _vbo;
- ChunkIndices _chunkIndices;
-
- LOD _lod[4];
-
- AABB _aabb;
-
- void generate(int map_width, int map_height, int m, int n, const unsigned char * data);
-
- void calculateAABB();
-
- void bindAndDraw();
-
- void finish();
-
- void updateVerticesForLOD();
-
- void updateIndicesLOD();
- void updateIndicesLODSkirt();
-
- void calculateSlope();
- bool getIntersectPointWithRay(const Ray& ray, Vec3& intersectPoint);
-
- CC_DEPRECATED_ATTRIBUTE bool getInsterctPointWithRay(const Ray& ray, Vec3& intersectPoint);
-
- int _currentLod;
- int _oldLod;
- int _neighborOldLOD[4];
-
- Chunk * _left;
- Chunk * _right;
- Chunk * _front;
- Chunk * _back;
- QuadTree * _parent;
-
- int _posX;
-
- int _posY;
-
- Terrain * _terrain;
-
- Size _size;
-
- float _slope;
- std::vector<TerrainVertexData> _currentVertices;
- std::vector<Triangle> _trianglesList;
- };
-
- struct CC_DLL QuadTree
- {
-
- QuadTree(int x, int y, int width, int height, Terrain * terrain);
-
- ~QuadTree();
-
- void draw();
-
- void resetNeedDraw(bool value);
-
- void cullByCamera(const Camera * camera, const Mat4 & worldTransform);
-
- void preCalculateAABB(const Mat4 & worldTransform);
- QuadTree * _tl;
- QuadTree * _tr;
- QuadTree * _bl;
- QuadTree * _br;
-
- bool _isTerminal;
- Chunk * _chunk;
- int _posX;
- int _posY;
- int _height;
- int _width;
- QuadTree * _parent;
-
- AABB _localAABB;
-
- AABB _worldSpaceAABB;
- Terrain * _terrain;
-
- bool _needDraw;
- };
- friend QuadTree;
- friend Chunk;
- public:
-
- void setLightMap(const std::string& fileName);
-
- void setLightDir(const Vec3& lightDir);
-
-
- bool initProperties();
-
- bool initHeightMap(const std::string& heightMap);
-
- bool initTextures();
-
- static Terrain * create(TerrainData ¶meter, CrackFixedType fixedType = CrackFixedType::INCREASE_LOWER);
-
- float getHeight(float x, float z, Vec3 * normal= nullptr) const;
-
- float getHeight(const Vec2& pos, Vec3* normal = nullptr) const;
-
- Vec3 getNormal(int pixelX, int pixelY) const;
-
- float getImageHeight(int pixelX, int pixelY) const;
-
- void setDrawWire(bool boolValue);
-
- void setLODDistance(float lod1, float lod2, float lod3);
-
- void setIsEnableFrustumCull(bool boolValue);
-
- void setAlphaMap(cocos2d::Texture2D * newAlphaMapTexture);
-
- void setDetailMap(unsigned int index, DetailMap detailMap);
-
- virtual void draw(cocos2d::Renderer* renderer, const cocos2d::Mat4 &transform, uint32_t flags) override;
-
- Vec3 getIntersectionPoint(const Ray & ray) const;
-
- bool getIntersectionPoint(const Ray & ray, Vec3 & intersectionPoint) const;
-
- void setMaxDetailMapAmount(int maxValue);
-
- Vec2 convertToTerrainSpace(const Vec2& worldSpace) const;
-
- void resetHeightMap(const std::string& heightMap);
-
- float getMinHeight();
-
- float getMaxHeight();
-
- AABB getAABB();
-
- void setSkirtHeightRatio(float ratio);
-
- QuadTree * getQuadTree();
- void reload();
-
-
- Size getTerrainSize() const { return Size(static_cast<float>(_imageWidth), static_cast<float>(_imageHeight)); }
-
-
- std::vector<float> getHeightData() const;
- CC_CONSTRUCTOR_ACCESS:
- Terrain();
- virtual ~Terrain();
- bool initWithTerrainData(TerrainData ¶meter, CrackFixedType fixedType);
- protected:
- void onDraw(const Mat4 &transform, uint32_t flags);
-
- void setChunksLOD(const Vec3& cameraPos);
-
- void loadVertices();
-
- void calculateNormal();
-
- virtual void onEnter() override;
-
- void cacheUniformAttribLocation();
-
- ChunkIndices lookForIndicesLODSkrit(int selfLod, bool * result);
- ChunkIndices lookForIndicesLOD(int neighborLod[4], int selfLod, bool * result);
- ChunkIndices insertIndicesLOD(int neighborLod[4], int selfLod, GLushort * indices, int size);
- ChunkIndices insertIndicesLODSkirt(int selfLod, GLushort * indices, int size);
-
- Chunk * getChunkByIndex(int x,int y) const;
- protected:
- std::vector <ChunkLODIndices> _chunkLodIndicesSet;
- std::vector<ChunkLODIndicesSkirt> _chunkLodIndicesSkirtSet;
- Mat4 _CameraMatrix;
- bool _isCameraViewChanged;
- TerrainData _terrainData;
- bool _isDrawWire;
- unsigned char * _data;
- float _lodDistance[3];
- Texture2D * _detailMapTextures[4];
- Texture2D * _alphaMap;
- Texture2D * _lightMap;
- Vec3 _lightDir;
- CustomCommand _customCommand;
- QuadTree * _quadRoot;
- Chunk * _chunkesArray[MAX_CHUNKES][MAX_CHUNKES];
- std::vector<TerrainVertexData> _vertices;
- std::vector<unsigned int> _indices;
- int _imageWidth;
- int _imageHeight;
- Size _chunkSize;
- bool _isEnableFrustumCull;
- int _maxDetailMapValue;
- cocos2d::Image * _heightMapImage;
- Mat4 _oldCameraModelMatrix;
- Mat4 _terrainModelMatrix;
- GLuint _normalLocation;
- GLuint _positionLocation;
- GLuint _texcoordLocation;
- float _maxHeight;
- float _minHeight;
- CrackFixedType _crackFixedType;
- float _skirtRatio;
- int _skirtVerticesOffset[4];
- GLint _detailMapLocation[4];
- GLint _alphaMapLocation;
- GLint _alphaIsHasAlphaMapLocation;
- GLint _lightMapCheckLocation;
- GLint _lightMapLocation;
- GLint _detailMapSizeLocation[4];
- GLint _lightDirLocation;
- RenderState::StateBlock* _stateBlock;
- #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
- EventListenerCustom* _backToForegroundListener;
- #endif
- };
- NS_CC_END
- #endif
|