2015-01-15 15:54:12 +08:00
|
|
|
/****************************************************************************
|
2015-01-23 18:09:54 +08:00
|
|
|
Copyright (c) 2015 Chukong Technologies Inc.
|
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
2015-01-15 11:45:06 +08:00
|
|
|
#ifndef CC_TERRAIN_H
|
|
|
|
#define CC_TERRAIN_H
|
2015-01-15 15:54:12 +08:00
|
|
|
|
2015-05-20 08:18:44 +08:00
|
|
|
#include <vector>
|
|
|
|
|
2015-01-15 11:45:06 +08:00
|
|
|
#include "2d/CCNode.h"
|
2015-04-02 14:04:31 +08:00
|
|
|
#include "2d/CCCamera.h"
|
2015-01-15 11:45:06 +08:00
|
|
|
#include "renderer/CCTexture2D.h"
|
|
|
|
#include "renderer/CCCustomCommand.h"
|
2015-05-20 08:18:44 +08:00
|
|
|
#include "renderer/CCRenderState.h"
|
2015-01-15 11:45:06 +08:00
|
|
|
#include "3d/CCAABB.h"
|
2015-01-29 18:06:00 +08:00
|
|
|
#include "3d/CCRay.h"
|
2015-04-08 19:11:46 +08:00
|
|
|
#include "base/CCEventListenerCustom.h"
|
|
|
|
#include "base/CCEventDispatcher.h"
|
2015-05-20 08:18:44 +08:00
|
|
|
|
2015-01-15 11:45:06 +08:00
|
|
|
NS_CC_BEGIN
|
2015-03-30 18:25:22 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @addtogroup _3d
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2015-04-03 13:49:07 +08:00
|
|
|
/**
|
2015-09-09 11:37:41 +08:00
|
|
|
* the maximum amount of the chunks
|
2015-04-03 13:49:07 +08:00
|
|
|
**/
|
2015-01-15 11:45:06 +08:00
|
|
|
#define MAX_CHUNKES 256
|
|
|
|
|
2015-04-03 13:49:07 +08:00
|
|
|
/**
|
2015-01-29 18:06:00 +08:00
|
|
|
* Terrain
|
|
|
|
* Defines a Terrain that is capable of rendering large landscapes from 2D heightmap images.
|
|
|
|
* Terrains can be constructed from several different internal formats heightmap sources:
|
|
|
|
* 1. RGB888
|
|
|
|
* 2. RGBA8888
|
|
|
|
* 3. Luminance(gray-scale)8
|
|
|
|
*
|
|
|
|
* Terrain use TerrainData struct to initialize.the TerrainData struct warp
|
|
|
|
* all parameters that Terrain initialization need.
|
|
|
|
* TerrainData provide several handy constructor for users
|
|
|
|
*
|
|
|
|
* Surface detail is provided via texture splatting, where multiple Detail texture layers can be added
|
|
|
|
* along with alpha map to define how different Detail texture blend with each other. These DetailTexture
|
|
|
|
* can be defined in TerrainData. The number of supported Detail texture is Four. although typically 2-3 levels is
|
|
|
|
* sufficient. For simple usage ,surface detail also is provided via simple Texture.
|
|
|
|
*
|
|
|
|
* Internally, Terrain is divide into smaller, more manageable chunks, which can be culled
|
|
|
|
* separately for more efficient rendering. The size of the terrain chunks can be controlled
|
|
|
|
* via the chunkSize property in TerrainData.
|
|
|
|
*
|
|
|
|
* Chunks are managed under the QuadTree.As DE FACTO terminal Node of the QuadTree;
|
2015-09-09 11:37:41 +08:00
|
|
|
* let us cull chunks efficiently to reduce drawCall amount And reduce the VBOs'Size that pass to the GPU.
|
2015-01-29 18:06:00 +08:00
|
|
|
*
|
|
|
|
* Level of detail (LOD) is supported using a technique that is similar to texture mipmapping -- called GeoMapping.
|
|
|
|
* A distance-to-camera based test used to decide
|
|
|
|
* the appropriate LOD for a terrain chunk. The number of LOD levels is 0 by default (which
|
2015-09-09 11:37:41 +08:00
|
|
|
* means only the base level is used),the maximum number of LOD levels is 4. Of course ,you can hack the value individually.
|
2015-01-29 18:06:00 +08:00
|
|
|
*
|
|
|
|
* Finally, when LOD is enabled, cracks can begin to appear between terrain Chunks of
|
|
|
|
* different LOD levels. An acceptable solution might be to simply reduce the lower LOD(high detail,smooth) chunks border,
|
|
|
|
* And let the higher LOD(rough) chunks to seamlessly connect it.
|
|
|
|
*
|
|
|
|
* We can use ray-terrain intersection to pick a point of the terrain;
|
|
|
|
* Also we can get an arbitrary point of the terrain's height and normal vector for convenience .
|
2015-01-23 18:09:54 +08:00
|
|
|
**/
|
2015-05-20 08:18:44 +08:00
|
|
|
class CC_DLL Terrain : public Node
|
2015-03-30 18:25:22 +08:00
|
|
|
{
|
2015-01-15 11:45:06 +08:00
|
|
|
public:
|
2015-01-15 15:54:12 +08:00
|
|
|
|
2015-04-03 13:49:07 +08:00
|
|
|
/**the crack fix type. use to fix the gaps between different LOD chunks */
|
2015-03-30 18:25:22 +08:00
|
|
|
enum class CrackFixedType{
|
|
|
|
SKIRT,
|
|
|
|
INCREASE_LOWER,
|
|
|
|
};
|
|
|
|
|
2015-04-03 13:49:07 +08:00
|
|
|
/**
|
2015-01-23 18:09:54 +08:00
|
|
|
*DetailMap
|
|
|
|
*this struct maintain a detail map data ,including source file ,detail size.
|
|
|
|
*the DetailMap can use for terrain splatting
|
|
|
|
**/
|
|
|
|
struct CC_DLL DetailMap{
|
|
|
|
/*Constructors*/
|
2015-01-15 11:45:06 +08:00
|
|
|
DetailMap();
|
2015-03-30 18:25:22 +08:00
|
|
|
DetailMap(const char * detailMapSrc, float size = 35);
|
2015-01-15 15:54:12 +08:00
|
|
|
/*detail Image source file path*/
|
2015-04-08 15:17:05 +08:00
|
|
|
std::string _detailMapSrc;
|
2015-01-15 15:54:12 +08:00
|
|
|
/*detailMapSize determine how many tiles that Terrain represent*/
|
2015-04-08 15:17:05 +08:00
|
|
|
float _detailMapSize;
|
2015-01-15 11:45:06 +08:00
|
|
|
};
|
2015-01-15 15:54:12 +08:00
|
|
|
|
2015-06-23 14:27:19 +08:00
|
|
|
/**
|
|
|
|
* Triangle
|
|
|
|
*/
|
|
|
|
struct Triangle
|
|
|
|
{
|
|
|
|
Triangle(Vec3 p1, Vec3 p2, Vec3 p3);
|
|
|
|
bool getInsterctPoint(const Ray &ray, Vec3& interScetPoint) const;
|
|
|
|
void transform(Mat4 matrix);
|
|
|
|
Vec3 _p1, _p2, _p3;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-04-03 13:49:07 +08:00
|
|
|
/**
|
2015-01-23 18:09:54 +08:00
|
|
|
*TerrainData
|
|
|
|
*This TerrainData struct warp all parameter that Terrain need to create
|
|
|
|
*/
|
|
|
|
struct CC_DLL TerrainData
|
2015-01-15 11:45:06 +08:00
|
|
|
{
|
2015-04-03 13:49:07 +08:00
|
|
|
/**empty constructor*/
|
2015-01-15 11:45:06 +08:00
|
|
|
TerrainData();
|
2015-04-03 13:49:07 +08:00
|
|
|
/**constructor, this constructor construct a simple terrain which only have 1 detailmap*/
|
2015-03-30 18:25:22 +08:00
|
|
|
TerrainData(const char* heightMapsrc, const char * textureSrc, const Size & chunksize = Size(32,32), float mapHeight = 2, float mapScale = 0.1);
|
2015-04-03 13:49:07 +08:00
|
|
|
/**constructor, this constructor construct a terrain which have 4 detailmaps, 1 alpha map*/
|
2015-03-30 18:25:22 +08:00
|
|
|
TerrainData(const char* heightMapsrc, const char * 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);
|
2015-04-03 13:49:07 +08:00
|
|
|
/**constructor, this constructor construct a terrain which have 3 detailmaps, 1 alpha map*/
|
2015-03-30 18:25:22 +08:00
|
|
|
TerrainData(const char* heightMapsrc, const char * alphamap, const DetailMap& detail1,const DetailMap& detail2, const DetailMap& detail3, const Size & chunksize = Size(32,32), float mapHeight = 2, float mapScale = 0.1);
|
2015-04-03 13:49:07 +08:00
|
|
|
/**
|
2015-09-09 11:37:41 +08:00
|
|
|
*determine the chunk size,chunk is the minimal subdivision of the Terrain
|
2015-01-23 18:09:54 +08:00
|
|
|
*/
|
2015-04-08 15:17:05 +08:00
|
|
|
Size _chunkSize;
|
2015-04-03 13:49:07 +08:00
|
|
|
/**height Map source path*/
|
2015-04-08 15:17:05 +08:00
|
|
|
std::string _heightMapSrc;
|
2015-04-03 13:49:07 +08:00
|
|
|
/**the source path of the alpha map*/
|
2015-04-08 15:17:05 +08:00
|
|
|
char* _alphaMapSrc;
|
2015-04-03 13:49:07 +08:00
|
|
|
/**detail maps*/
|
2015-04-08 15:17:05 +08:00
|
|
|
DetailMap _detailMaps[4];
|
2015-04-03 13:49:07 +08:00
|
|
|
/**terrain Maximum height*/
|
2015-04-08 15:17:05 +08:00
|
|
|
float _mapHeight;
|
2015-04-03 13:49:07 +08:00
|
|
|
/**terrain scale factor,you can combine setScale later.*/
|
2015-04-08 15:17:05 +08:00
|
|
|
float _mapScale;
|
2015-04-03 13:49:07 +08:00
|
|
|
/**the amount of detailmap*/
|
2015-01-23 18:09:54 +08:00
|
|
|
int _detailMapAmount;
|
2015-04-03 13:49:07 +08:00
|
|
|
/**the skirt height ratio, only effect when terrain use skirt to fix crack*/
|
2015-04-08 15:17:05 +08:00
|
|
|
float _skirtHeightRatio;
|
2015-01-15 11:45:06 +08:00
|
|
|
};
|
|
|
|
private:
|
2015-01-15 15:54:12 +08:00
|
|
|
|
2015-03-30 18:25:22 +08:00
|
|
|
struct ChunkIndices
|
|
|
|
{
|
2015-04-08 15:17:05 +08:00
|
|
|
GLuint _indices;
|
|
|
|
unsigned short _size;
|
2015-03-30 18:25:22 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct ChunkLODIndices
|
|
|
|
{
|
2015-04-08 15:17:05 +08:00
|
|
|
int _relativeLod[5];
|
2015-03-30 18:25:22 +08:00
|
|
|
ChunkIndices _chunkIndices;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct ChunkLODIndicesSkirt
|
|
|
|
{
|
2015-04-08 15:17:05 +08:00
|
|
|
int _selfLod;
|
2015-03-30 18:25:22 +08:00
|
|
|
ChunkIndices _chunkIndices;
|
|
|
|
};
|
2015-01-23 18:09:54 +08:00
|
|
|
/*
|
|
|
|
*terrain vertices internal data format
|
|
|
|
**/
|
2015-01-15 11:45:06 +08:00
|
|
|
struct TerrainVertexData
|
|
|
|
{
|
2015-01-15 15:54:12 +08:00
|
|
|
/*constructor*/
|
2015-01-15 11:45:06 +08:00
|
|
|
TerrainVertexData(){};
|
2015-03-30 18:25:22 +08:00
|
|
|
TerrainVertexData(Vec3 v1, Tex2F v2)
|
2015-01-15 11:45:06 +08:00
|
|
|
{
|
2015-04-08 15:17:05 +08:00
|
|
|
_position = v1;
|
|
|
|
_texcoord = v2;
|
2015-01-15 11:45:06 +08:00
|
|
|
};
|
2015-01-15 15:54:12 +08:00
|
|
|
/*the vertex's attributes*/
|
2015-04-08 15:17:05 +08:00
|
|
|
cocos2d::Vec3 _position;
|
|
|
|
cocos2d::Tex2F _texcoord;
|
|
|
|
cocos2d::Vec3 _normal;
|
2015-01-15 11:45:06 +08:00
|
|
|
};
|
2015-01-15 15:54:12 +08:00
|
|
|
|
2015-03-03 14:14:50 +08:00
|
|
|
struct QuadTree;
|
2015-01-23 18:09:54 +08:00
|
|
|
/*
|
|
|
|
*the terminal node of quad, use to subdivision terrain mesh and LOD
|
|
|
|
**/
|
2015-01-15 11:45:06 +08:00
|
|
|
struct Chunk
|
|
|
|
{
|
2015-04-03 13:49:07 +08:00
|
|
|
/**Constructor*/
|
2015-01-15 11:45:06 +08:00
|
|
|
Chunk();
|
2015-04-03 13:49:07 +08:00
|
|
|
/**destructor*/
|
2015-01-29 18:06:00 +08:00
|
|
|
~Chunk();
|
2015-01-15 15:54:12 +08:00
|
|
|
/*vertices*/
|
2015-04-08 15:17:05 +08:00
|
|
|
std::vector<TerrainVertexData> _originalVertices;
|
2015-01-15 15:54:12 +08:00
|
|
|
/*LOD indices*/
|
2015-01-15 11:45:06 +08:00
|
|
|
struct LOD{
|
2015-04-08 15:17:05 +08:00
|
|
|
std::vector<GLushort> _indices;
|
2015-01-15 11:45:06 +08:00
|
|
|
};
|
2015-04-08 15:17:05 +08:00
|
|
|
GLuint _vbo;
|
2015-03-30 18:25:22 +08:00
|
|
|
ChunkIndices _chunkIndices;
|
2015-04-03 13:49:07 +08:00
|
|
|
/**we now support four levels of detail*/
|
2015-01-15 11:45:06 +08:00
|
|
|
LOD _lod[4];
|
2015-04-03 13:49:07 +08:00
|
|
|
/**AABB in local space*/
|
2015-01-15 11:45:06 +08:00
|
|
|
AABB _aabb;
|
2015-04-03 13:49:07 +08:00
|
|
|
/**setup Chunk data*/
|
2015-03-30 18:25:22 +08:00
|
|
|
void generate(int map_width, int map_height, int m, int n, const unsigned char * data);
|
2015-04-03 13:49:07 +08:00
|
|
|
/**calculateAABB*/
|
2015-01-15 11:45:06 +08:00
|
|
|
void calculateAABB();
|
2015-04-03 13:49:07 +08:00
|
|
|
/**internal use draw function*/
|
2015-01-15 11:45:06 +08:00
|
|
|
void bindAndDraw();
|
2015-04-03 13:49:07 +08:00
|
|
|
/**finish opengl setup*/
|
2015-01-15 11:45:06 +08:00
|
|
|
void finish();
|
2015-01-15 15:54:12 +08:00
|
|
|
/*use linear-sample vertices for LOD mesh*/
|
2015-01-15 11:45:06 +08:00
|
|
|
void updateVerticesForLOD();
|
2015-04-03 13:49:07 +08:00
|
|
|
/*updateIndices */
|
2015-03-30 18:25:22 +08:00
|
|
|
void updateIndicesLOD();
|
|
|
|
|
|
|
|
void updateIndicesLODSkirt();
|
2015-01-15 15:54:12 +08:00
|
|
|
|
2015-04-03 13:49:07 +08:00
|
|
|
/**calculate the average slop of chunk*/
|
2015-02-03 17:38:40 +08:00
|
|
|
void calculateSlope();
|
2015-06-23 14:27:19 +08:00
|
|
|
|
|
|
|
bool getInsterctPointWithRay(const Ray& ray, Vec3 &interscetPoint);
|
|
|
|
|
2015-04-03 13:49:07 +08:00
|
|
|
/**current LOD of the chunk*/
|
2015-01-15 15:54:12 +08:00
|
|
|
int _currentLod;
|
2015-03-03 14:14:50 +08:00
|
|
|
|
|
|
|
int _oldLod;
|
|
|
|
|
|
|
|
int _neighborOldLOD[4];
|
2015-01-15 15:54:12 +08:00
|
|
|
/*the left,right,front,back neighbors*/
|
2015-04-03 13:49:07 +08:00
|
|
|
Chunk * _left;
|
|
|
|
Chunk * _right;
|
|
|
|
Chunk * _front;
|
|
|
|
Chunk * _back;
|
2015-01-15 15:54:12 +08:00
|
|
|
|
2015-03-03 14:14:50 +08:00
|
|
|
QuadTree * _parent;
|
|
|
|
|
2015-04-03 13:49:07 +08:00
|
|
|
/**the position X in terrain space*/
|
2015-04-08 15:17:05 +08:00
|
|
|
int _posX;
|
2015-04-03 13:49:07 +08:00
|
|
|
/**the position Y in terrain space*/
|
2015-04-08 15:17:05 +08:00
|
|
|
int _posY;
|
2015-04-03 13:49:07 +08:00
|
|
|
/**parent terrain*/
|
2015-01-15 11:45:06 +08:00
|
|
|
Terrain * _terrain;
|
2015-04-03 13:49:07 +08:00
|
|
|
/**chunk size*/
|
2015-01-15 11:45:06 +08:00
|
|
|
Size _size;
|
2015-04-03 13:49:07 +08:00
|
|
|
/**chunk's estimated slope*/
|
|
|
|
float _slope;
|
2015-04-08 15:17:05 +08:00
|
|
|
std::vector<TerrainVertexData> _currentVertices;
|
2015-06-23 14:27:19 +08:00
|
|
|
|
|
|
|
std::vector<Triangle> _trianglesList;
|
2015-01-15 11:45:06 +08:00
|
|
|
};
|
2015-01-15 15:54:12 +08:00
|
|
|
|
2015-04-03 13:49:07 +08:00
|
|
|
/**
|
2015-01-23 18:09:54 +08:00
|
|
|
*QuadTree
|
2015-04-03 13:49:07 +08:00
|
|
|
* @breif use to hierarchically frustum culling and set LOD
|
2015-01-23 18:09:54 +08:00
|
|
|
**/
|
2015-01-15 11:45:06 +08:00
|
|
|
struct QuadTree
|
|
|
|
{
|
2015-04-03 13:49:07 +08:00
|
|
|
/**constructor*/
|
2015-03-30 18:25:22 +08:00
|
|
|
QuadTree(int x, int y, int width, int height, Terrain * terrain);
|
2015-04-03 13:49:07 +08:00
|
|
|
/**destructor*/
|
2015-04-01 18:24:05 +08:00
|
|
|
~QuadTree();
|
2015-04-03 13:49:07 +08:00
|
|
|
/**recursively draw*/
|
2015-01-15 11:45:06 +08:00
|
|
|
void draw();
|
2015-04-03 13:49:07 +08:00
|
|
|
/**recursively set itself and its children is need to draw*/
|
2015-01-15 11:45:06 +08:00
|
|
|
void resetNeedDraw(bool value);
|
2015-04-03 13:49:07 +08:00
|
|
|
/**recursively potential visible culling*/
|
2015-03-30 18:25:22 +08:00
|
|
|
void cullByCamera(const Camera * camera, const Mat4 & worldTransform);
|
2015-04-03 13:49:07 +08:00
|
|
|
/**precalculate the AABB(In world space) of each quad*/
|
2015-04-01 18:24:05 +08:00
|
|
|
void preCalculateAABB(const Mat4 & worldTransform);
|
2015-04-03 13:49:07 +08:00
|
|
|
QuadTree * _tl;
|
|
|
|
QuadTree * _tr;
|
|
|
|
QuadTree * _bl;
|
|
|
|
QuadTree * _br;
|
2015-09-09 11:37:41 +08:00
|
|
|
/**A flag present current quadTree node whether a terminal node,the terminal node is de facto the chunk*/
|
2015-01-15 11:45:06 +08:00
|
|
|
bool _isTerminal;
|
|
|
|
Chunk * _chunk;
|
2015-04-03 13:49:07 +08:00
|
|
|
int _posX;
|
|
|
|
int _posY;
|
|
|
|
int _height;
|
|
|
|
int _width;
|
|
|
|
QuadTree * _parent;
|
2015-04-08 15:17:05 +08:00
|
|
|
/**AABB's cache (in local space)*/
|
|
|
|
AABB _localAABB;
|
2015-04-03 13:49:07 +08:00
|
|
|
/**AABB's cache (in world space)*/
|
2015-03-03 14:14:50 +08:00
|
|
|
AABB _worldSpaceAABB;
|
|
|
|
Terrain * _terrain;
|
2015-04-03 13:49:07 +08:00
|
|
|
/** a flag determine whether a quadTree node need draw*/
|
2015-01-15 11:45:06 +08:00
|
|
|
bool _needDraw;
|
|
|
|
};
|
|
|
|
friend QuadTree;
|
|
|
|
friend Chunk;
|
|
|
|
public:
|
2015-08-03 13:30:30 +08:00
|
|
|
/** set light map texture */
|
2015-07-29 14:32:36 +08:00
|
|
|
void setLightMap(const std::string& fileName);
|
2015-07-29 13:24:12 +08:00
|
|
|
|
2015-08-03 13:30:30 +08:00
|
|
|
/**
|
|
|
|
set directional light for the terrain
|
|
|
|
@param lightDir The direction of directional light, Note that lightDir is in the terrain's local space. Most of the time terrain is placed at (0,0,0) and without rotation, so lightDir is also in the world space.
|
|
|
|
*/
|
2015-07-29 14:32:36 +08:00
|
|
|
void setLightDir(const Vec3& lightDir);
|
2015-01-15 15:54:12 +08:00
|
|
|
/*init function*/
|
2015-04-03 13:49:07 +08:00
|
|
|
/**initialize all Properties which terrain need */
|
|
|
|
bool initProperties();
|
|
|
|
/**initialize heightMap data */
|
|
|
|
bool initHeightMap(const char* heightMap);
|
|
|
|
/**initialize alphaMap ,detailMaps textures*/
|
|
|
|
bool initTextures();
|
|
|
|
/**create entry*/
|
2015-03-30 18:25:22 +08:00
|
|
|
static Terrain * create(TerrainData ¶meter, CrackFixedType fixedType = CrackFixedType::INCREASE_LOWER);
|
2015-04-03 13:49:07 +08:00
|
|
|
/**get specified position's height mapping to the terrain,use bi-linear interpolation method
|
|
|
|
* @param x the X position
|
|
|
|
* @param y the Z position
|
|
|
|
* @param normal the specified position's normal vector in terrain . if this argument is NULL or nullptr,Normal calculation shall be skip.
|
|
|
|
* @return the height value of the specified position of the terrain, if the (X,Z) position is out of the terrain bounds,it shall return 0;
|
|
|
|
**/
|
2015-05-27 18:19:44 +08:00
|
|
|
float getHeight(float x, float z, Vec3 * normal= nullptr) const;
|
2015-04-03 13:49:07 +08:00
|
|
|
|
|
|
|
/**get specified position's height mapping to the terrain,use bi-linear interpolation method
|
|
|
|
* @param pos the position (X,Z)
|
|
|
|
* @param normal the specified position's normal vector in terrain . if this argument is NULL or nullptr,Normal calculation shall be skip.
|
|
|
|
* @return the height value of the specified position of the terrain, if the (X,Z) position is out of the terrain bounds,it shall return 0;
|
|
|
|
**/
|
2015-05-27 18:19:44 +08:00
|
|
|
float getHeight(Vec2 pos, Vec3*Normal = nullptr) const;
|
2015-04-03 13:49:07 +08:00
|
|
|
|
2015-09-09 11:37:41 +08:00
|
|
|
/**get the normal of the specified position in terrain
|
2015-04-03 13:49:07 +08:00
|
|
|
* @return the normal vector of the specified position of the terrain.
|
|
|
|
* @note the fast normal calculation may not get precise normal vector.
|
|
|
|
**/
|
2015-05-27 18:19:44 +08:00
|
|
|
Vec3 getNormal(int pixelX, int pixelY) const;
|
2015-04-03 13:49:07 +08:00
|
|
|
/**get height from the raw height filed*/
|
2015-05-27 18:19:44 +08:00
|
|
|
float getImageHeight(int pixelX, int pixelY) const;
|
2015-04-03 13:49:07 +08:00
|
|
|
/**show the wireline instead of the surface,Debug Use only.
|
|
|
|
* @Note only support desktop platform
|
|
|
|
**/
|
|
|
|
void setDrawWire(bool boolValue);
|
|
|
|
/**
|
2015-09-09 11:37:41 +08:00
|
|
|
* Set threshold distance of each LOD level,must equal or greater than the chunk size
|
2015-04-03 13:49:07 +08:00
|
|
|
* @Note when invoke initHeightMap, the LOD distance will be automatic calculated.
|
|
|
|
*/
|
|
|
|
void setLODDistance(float lod1, float lod2, float lod3);
|
|
|
|
|
|
|
|
/**Switch frustum Culling Flag
|
|
|
|
* @Note frustum culling will remarkable improve your terrain rendering performance.
|
|
|
|
*/
|
|
|
|
void setIsEnableFrustumCull(bool boolValue);
|
2015-03-30 18:25:22 +08:00
|
|
|
|
|
|
|
/** set the alpha map*/
|
|
|
|
void setAlphaMap(cocos2d::Texture2D * newAlphaMapTexture);
|
|
|
|
/**set the Detail Map */
|
|
|
|
void setDetailMap(unsigned int index, DetailMap detailMap);
|
|
|
|
|
2015-01-15 15:54:12 +08:00
|
|
|
// Overrides, internal use only
|
|
|
|
virtual void draw(cocos2d::Renderer* renderer, const cocos2d::Mat4 &transform, uint32_t flags) override;
|
2015-04-03 13:49:07 +08:00
|
|
|
/**
|
|
|
|
* Ray-Terrain intersection.
|
|
|
|
* @return the intersection point
|
|
|
|
*/
|
2015-05-27 18:19:44 +08:00
|
|
|
Vec3 getIntersectionPoint(const Ray & ray) const;
|
2015-03-27 10:12:24 +08:00
|
|
|
|
2015-05-28 12:40:16 +08:00
|
|
|
/**
|
|
|
|
* Ray-Terrain intersection.
|
|
|
|
* @param ray to hit the terrain
|
2015-09-09 11:37:41 +08:00
|
|
|
* @param intersectionPoint hit point if hit
|
2015-05-28 12:40:16 +08:00
|
|
|
* @return true if hit, false otherwise
|
|
|
|
*/
|
2015-05-27 18:19:44 +08:00
|
|
|
bool getIntersectionPoint(const Ray & ray, Vec3 & intersectionPoint) const;
|
2015-03-27 10:12:24 +08:00
|
|
|
|
2015-04-03 13:49:07 +08:00
|
|
|
/**
|
|
|
|
* set the MaxDetailAmount.
|
|
|
|
*/
|
|
|
|
void setMaxDetailMapAmount(int maxValue);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert a world Space position (X,Z) to terrain space position (X,Z)
|
|
|
|
*/
|
2015-06-18 12:20:44 +08:00
|
|
|
Vec2 convertToTerrainSpace(Vec2 worldSpace) const;
|
2015-03-27 10:12:24 +08:00
|
|
|
|
2015-04-03 13:49:07 +08:00
|
|
|
/**
|
|
|
|
* reset the heightmap data.
|
|
|
|
*/
|
2015-03-27 10:12:24 +08:00
|
|
|
void resetHeightMap(const char * heightMap);
|
2015-03-27 11:46:02 +08:00
|
|
|
|
2015-04-03 13:49:07 +08:00
|
|
|
/**
|
2015-09-09 11:37:41 +08:00
|
|
|
* get the terrain's minimal height.
|
2015-04-03 13:49:07 +08:00
|
|
|
*/
|
2015-03-27 11:46:02 +08:00
|
|
|
float getMinHeight();
|
|
|
|
|
2015-04-03 13:49:07 +08:00
|
|
|
/**
|
2015-09-09 11:37:41 +08:00
|
|
|
* get the terrain's maximal height.
|
2015-04-03 13:49:07 +08:00
|
|
|
*/
|
2015-03-27 11:46:02 +08:00
|
|
|
float getMaxHeight();
|
|
|
|
|
2015-04-03 13:49:07 +08:00
|
|
|
/**
|
|
|
|
* get the terrain's AABB(in world space)
|
|
|
|
*/
|
2015-03-27 11:46:02 +08:00
|
|
|
AABB getAABB();
|
2015-03-27 13:43:09 +08:00
|
|
|
|
2015-04-03 13:49:07 +08:00
|
|
|
/**
|
|
|
|
* set the skirt height ratio
|
|
|
|
*/
|
|
|
|
void setSkirtHeightRatio(float ratio);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get the terrain's quad tree which is also the root node.
|
|
|
|
*/
|
2015-03-27 13:43:09 +08:00
|
|
|
QuadTree * getQuadTree();
|
2015-03-30 18:25:22 +08:00
|
|
|
|
2015-04-08 19:11:46 +08:00
|
|
|
void reload();
|
2015-05-08 15:49:33 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* get the terrain's size
|
|
|
|
*/
|
|
|
|
Size getTerrainSize() const { return Size(_imageWidth, _imageHeight); }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get the terrain's height data
|
|
|
|
*/
|
|
|
|
std::vector<float> getHeightData() const;
|
2015-07-30 17:17:57 +08:00
|
|
|
|
|
|
|
CC_CONSTRUCTOR_ACCESS:
|
2015-01-15 11:45:06 +08:00
|
|
|
Terrain();
|
2015-01-23 18:09:54 +08:00
|
|
|
virtual ~Terrain();
|
2015-07-31 18:35:47 +08:00
|
|
|
bool initWithTerrainData(TerrainData ¶meter, CrackFixedType fixedType);
|
2015-07-30 17:17:57 +08:00
|
|
|
protected:
|
2015-01-15 11:45:06 +08:00
|
|
|
void onDraw(const Mat4 &transform, uint32_t flags);
|
2015-04-03 13:49:07 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* recursively set each chunk's LOD
|
2015-09-09 11:37:41 +08:00
|
|
|
* @param cameraPos the camera position in world space
|
2015-04-03 13:49:07 +08:00
|
|
|
**/
|
2015-01-15 11:45:06 +08:00
|
|
|
void setChunksLOD(Vec3 cameraPos);
|
2015-04-03 13:49:07 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* load Vertices from height filed for the whole terrain.
|
|
|
|
**/
|
2015-01-15 11:45:06 +08:00
|
|
|
void loadVertices();
|
2015-04-03 13:49:07 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* calculate Normal Line for each Vertex
|
|
|
|
**/
|
2015-01-15 11:45:06 +08:00
|
|
|
void calculateNormal();
|
2015-04-03 13:49:07 +08:00
|
|
|
|
2015-04-01 18:24:05 +08:00
|
|
|
//override
|
|
|
|
virtual void onEnter() override;
|
|
|
|
|
2015-04-03 13:49:07 +08:00
|
|
|
/**
|
2015-09-09 11:37:41 +08:00
|
|
|
* cache all uniform locations in GLSL.
|
2015-04-03 13:49:07 +08:00
|
|
|
**/
|
2015-04-08 10:10:05 +08:00
|
|
|
void cacheUniformAttribLocation();
|
2015-04-08 15:17:05 +08:00
|
|
|
|
|
|
|
//IBO generate & cache
|
|
|
|
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);
|
2015-06-24 10:56:46 +08:00
|
|
|
|
|
|
|
Chunk * getChunkByIndex(int x,int y) const;
|
2015-05-20 08:18:44 +08:00
|
|
|
|
2015-01-30 15:46:39 +08:00
|
|
|
protected:
|
2015-03-30 18:25:22 +08:00
|
|
|
std::vector <ChunkLODIndices> _chunkLodIndicesSet;
|
|
|
|
std::vector<ChunkLODIndicesSkirt> _chunkLodIndicesSkirtSet;
|
2015-03-03 14:14:50 +08:00
|
|
|
Mat4 _CameraMatrix;
|
|
|
|
bool _isCameraViewChanged;
|
2015-01-15 11:45:06 +08:00
|
|
|
TerrainData _terrainData;
|
|
|
|
bool _isDrawWire;
|
|
|
|
unsigned char * _data;
|
|
|
|
float _lodDistance[3];
|
2015-03-30 18:25:22 +08:00
|
|
|
Texture2D * _detailMapTextures[4];
|
2015-01-15 11:45:06 +08:00
|
|
|
Texture2D * _alphaMap;
|
2015-07-29 13:24:12 +08:00
|
|
|
Texture2D * _lightMap;
|
|
|
|
Vec3 _lightDir;
|
2015-01-15 11:45:06 +08:00
|
|
|
CustomCommand _customCommand;
|
2015-03-27 13:43:09 +08:00
|
|
|
QuadTree * _quadRoot;
|
2015-01-23 18:14:44 +08:00
|
|
|
Chunk * _chunkesArray[MAX_CHUNKES][MAX_CHUNKES];
|
2015-04-02 14:04:31 +08:00
|
|
|
std::vector<TerrainVertexData> _vertices;
|
2015-07-29 13:24:12 +08:00
|
|
|
std::vector<unsigned int> _indices;
|
2015-04-02 14:04:31 +08:00
|
|
|
int _imageWidth;
|
|
|
|
int _imageHeight;
|
2015-01-15 11:45:06 +08:00
|
|
|
Size _chunkSize;
|
|
|
|
bool _isEnableFrustumCull;
|
2015-01-30 15:46:39 +08:00
|
|
|
int _maxDetailMapValue;
|
2015-01-23 18:09:54 +08:00
|
|
|
cocos2d::Image * _heightMapImage;
|
2015-01-30 15:46:39 +08:00
|
|
|
Mat4 _oldCameraModelMatrix;
|
2015-04-01 18:24:05 +08:00
|
|
|
Mat4 _terrainModelMatrix;
|
2015-03-03 14:14:50 +08:00
|
|
|
GLuint _normalLocation;
|
2015-04-07 18:47:19 +08:00
|
|
|
GLuint _positionLocation;
|
|
|
|
GLuint _texcordLocation;
|
2015-04-02 14:04:31 +08:00
|
|
|
float _maxHeight;
|
|
|
|
float _minHeight;
|
2015-03-30 18:25:22 +08:00
|
|
|
CrackFixedType _crackFixedType;
|
2015-04-01 18:24:05 +08:00
|
|
|
float _skirtRatio;
|
|
|
|
int _skirtVerticesOffset[4];
|
|
|
|
GLint _detailMapLocation[4];
|
|
|
|
GLint _alphaMapLocation;
|
|
|
|
GLint _alphaIsHasAlphaMapLocation;
|
2015-07-29 13:24:12 +08:00
|
|
|
GLint _lightMapCheckLocation;
|
|
|
|
GLint _lightMapLocation;
|
2015-04-01 18:24:05 +08:00
|
|
|
GLint _detailMapSizeLocation[4];
|
2015-07-29 13:24:12 +08:00
|
|
|
GLint _lightDirLocation;
|
2015-05-20 08:18:44 +08:00
|
|
|
RenderState::StateBlock* _stateBlock;
|
|
|
|
|
2015-05-09 00:19:13 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
|
2015-04-08 19:11:46 +08:00
|
|
|
EventListenerCustom* _backToForegroundListener;
|
|
|
|
#endif
|
2015-01-15 11:45:06 +08:00
|
|
|
};
|
2015-03-30 18:25:22 +08:00
|
|
|
|
|
|
|
// end of actions group
|
|
|
|
/// @}
|
|
|
|
|
2015-01-15 11:45:06 +08:00
|
|
|
NS_CC_END
|
|
|
|
#endif
|