Merge pull request #11337 from super626/terrain

Terrain fix
This commit is contained in:
minggo 2015-04-09 11:44:26 +08:00
commit 69eac3b78f
14 changed files with 348 additions and 339 deletions

File diff suppressed because it is too large Load Diff

View File

@ -31,7 +31,8 @@ THE SOFTWARE.
#include "3d/CCAABB.h"
#include "3d/CCRay.h"
#include <vector>
#include "base/CCEventListenerCustom.h"
#include "base/CCEventDispatcher.h"
NS_CC_BEGIN
/**
@ -100,9 +101,9 @@ public:
DetailMap();
DetailMap(const char * detailMapSrc, float size = 35);
/*detail Image source file path*/
std::string detailMapSrc;
std::string _detailMapSrc;
/*detailMapSize determine how many tiles that Terrain represent*/
float detailMapSize;
float _detailMapSize;
};
/**
@ -122,40 +123,40 @@ public:
/**
*deterimine the chunk size,chunk is the minimal subdivision of the Terrain
*/
Size chunkSize;
Size _chunkSize;
/**height Map source path*/
std::string heightMapSrc;
std::string _heightMapSrc;
/**the source path of the alpha map*/
char* alphaMapSrc;
char* _alphaMapSrc;
/**detail maps*/
DetailMap detailMaps[4];
DetailMap _detailMaps[4];
/**terrain Maximum height*/
float mapHeight;
float _mapHeight;
/**terrain scale factor,you can combine setScale later.*/
float mapScale;
float _mapScale;
/**the amount of detailmap*/
int _detailMapAmount;
/**the skirt height ratio, only effect when terrain use skirt to fix crack*/
float skirtHeightRatio;
float _skirtHeightRatio;
};
private:
struct ChunkIndices
{
GLuint indices;
unsigned short size;
GLuint _indices;
unsigned short _size;
};
struct ChunkLODIndices
{
int relativeLod[5];
int _relativeLod[5];
ChunkIndices _chunkIndices;
};
struct ChunkLODIndicesSkirt
{
int selfLod;
int _selfLod;
ChunkIndices _chunkIndices;
};
/*
@ -167,13 +168,13 @@ private:
TerrainVertexData(){};
TerrainVertexData(Vec3 v1, Tex2F v2)
{
position = v1;
texcoord = v2;
_position = v1;
_texcoord = v2;
};
/*the vertex's attributes*/
cocos2d::Vec3 position;
cocos2d::Tex2F texcoord;
cocos2d::Vec3 normal;
cocos2d::Vec3 _position;
cocos2d::Tex2F _texcoord;
cocos2d::Vec3 _normal;
};
struct QuadTree;
@ -187,12 +188,12 @@ private:
/**destructor*/
~Chunk();
/*vertices*/
std::vector<TerrainVertexData> vertices;
std::vector<TerrainVertexData> _originalVertices;
/*LOD indices*/
struct LOD{
std::vector<GLushort> indices;
std::vector<GLushort> _indices;
};
GLuint vbo[2];
GLuint _vbo;
ChunkIndices _chunkIndices;
/**we now support four levels of detail*/
LOD _lod[4];
@ -230,16 +231,16 @@ private:
QuadTree * _parent;
/**the position X in terrain space*/
int pos_x;
int _posX;
/**the position Y in terrain space*/
int pos_y;
int _posY;
/**parent terrain*/
Terrain * _terrain;
/**chunk size*/
Size _size;
/**chunk's estimated slope*/
float _slope;
std::vector<TerrainVertexData> vertices_tmp;
std::vector<TerrainVertexData> _currentVertices;
};
/**
@ -272,7 +273,8 @@ private:
int _height;
int _width;
QuadTree * _parent;
AABB _aabb;
/**AABB's cache (in local space)*/
AABB _localAABB;
/**AABB's cache (in world space)*/
AABB _worldSpaceAABB;
Terrain * _terrain;
@ -381,15 +383,7 @@ public:
*/
QuadTree * getQuadTree();
//following methods are internal use only
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);
void reload();
protected:
Terrain();
@ -418,7 +412,16 @@ protected:
/**
* cache all unifrom loactions in GLSL.
**/
void cacheUniformLocation();
void cacheUniformAttribLocation();
//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);
protected:
std::vector <ChunkLODIndices> _chunkLodIndicesSet;
std::vector<ChunkLODIndicesSkirt> _chunkLodIndicesSkirtSet;
@ -444,6 +447,8 @@ protected:
Mat4 _oldCameraModelMatrix;
Mat4 _terrainModelMatrix;
GLuint _normalLocation;
GLuint _positionLocation;
GLuint _texcordLocation;
float _maxHeight;
float _minHeight;
CrackFixedType _crackFixedType;
@ -453,6 +458,9 @@ protected:
GLint _alphaMapLocation;
GLint _alphaIsHasAlphaMapLocation;
GLint _detailMapSizeLocation[4];
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WP8 || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
EventListenerCustom* _backToForegroundListener;
#endif
};
// end of actions group

View File

@ -233,22 +233,22 @@ bool luaval_to_terraindata(lua_State* L, int lo, cocos2d::Terrain::TerrainData*
lua_gettable(L,lo);
if (!lua_isnil(L, -1))
{
luaval_to_size(L, -1, &(outValue->chunkSize));
luaval_to_size(L, -1, &(outValue->_chunkSize));
}
else
{
outValue->chunkSize = cocos2d::Size(32, 32);
outValue->_chunkSize = cocos2d::Size(32, 32);
}
lua_pop(L, 1);
lua_pushstring(L, "heightMapSrc");
lua_gettable(L,lo);
outValue->heightMapSrc = tolua_tocppstring(L, -1, "");
outValue->_heightMapSrc = tolua_tocppstring(L, -1, "");
lua_pop(L,1);
lua_pushstring(L, "alphaMapSrc");
lua_gettable(L,lo);
outValue->alphaMapSrc = const_cast<char*>(tolua_tocppstring(L, -1, ""));
outValue->_alphaMapSrc = const_cast<char*>(tolua_tocppstring(L, -1, ""));
lua_pop(L,1);
lua_pushstring(L, "detailMaps");
@ -264,12 +264,12 @@ bool luaval_to_terraindata(lua_State* L, int lo, cocos2d::Terrain::TerrainData*
{
lua_pushstring(L, "detailMapSrc");
lua_gettable(L,-2);
outValue->detailMaps[i].detailMapSrc = tolua_tocppstring(L, -1, "");
outValue->_detailMaps[i]._detailMapSrc = tolua_tocppstring(L, -1, "");
lua_pop(L,1);
lua_pushstring(L, "detailMapSize");
lua_gettable(L,-2);
outValue->detailMaps[i].detailMapSize = lua_isnil(L,-1) ? 0.0f : (float)lua_tonumber(L,-1);
outValue->_detailMaps[i]._detailMapSize = lua_isnil(L,-1) ? 0.0f : (float)lua_tonumber(L,-1);
lua_pop(L,1);
}
lua_pop(L, 1);
@ -279,12 +279,12 @@ bool luaval_to_terraindata(lua_State* L, int lo, cocos2d::Terrain::TerrainData*
lua_pushstring(L, "mapHeight");
lua_gettable(L,lo);
outValue->mapHeight = lua_isnil(L,-1) ? 2.0f : (float)lua_tonumber(L,-1);
outValue->_mapHeight = lua_isnil(L,-1) ? 2.0f : (float)lua_tonumber(L,-1);
lua_pop(L,1);
lua_pushstring(L, "mapScale");
lua_gettable(L,lo);
outValue->mapScale = lua_isnil(L,-1) ? 0.1f : (float)lua_tonumber(L,-1);
outValue->_mapScale = lua_isnil(L,-1) ? 0.1f : (float)lua_tonumber(L,-1);
lua_pop(L,1);
lua_pushstring(L, "_detailMapAmount");
@ -294,7 +294,7 @@ bool luaval_to_terraindata(lua_State* L, int lo, cocos2d::Terrain::TerrainData*
lua_pushstring(L, "skirtHeightRatio");
lua_gettable(L,lo);
outValue->skirtHeightRatio = lua_isnil(L,-1) ? 1.0f : (float)lua_tonumber(L,-1);
outValue->_skirtHeightRatio = lua_isnil(L,-1) ? 1.0f : (float)lua_tonumber(L,-1);
lua_pop(L,1);
}
@ -308,20 +308,20 @@ void terraindata_to_luaval(lua_State* L,const cocos2d::Terrain::TerrainData& inV
lua_newtable(L);
lua_pushstring(L, "chunkSize");
size_to_luaval(L, inValue.chunkSize);
size_to_luaval(L, inValue._chunkSize);
lua_rawset(L, -3);
if (inValue.heightMapSrc.length() > 0)
if (inValue._heightMapSrc.length() > 0)
{
lua_pushstring(L, "heightMapSrc");
lua_pushstring(L, inValue.heightMapSrc.c_str());
lua_pushstring(L, inValue._heightMapSrc.c_str());
lua_rawset(L, -3);
}
if (nullptr != inValue.alphaMapSrc)
if (nullptr != inValue._alphaMapSrc)
{
lua_pushstring(L, "alphaMapSrc");
lua_pushstring(L, inValue.alphaMapSrc);
lua_pushstring(L, inValue._alphaMapSrc);
lua_rawset(L, -3);
}
@ -334,11 +334,11 @@ void terraindata_to_luaval(lua_State* L,const cocos2d::Terrain::TerrainData& inV
lua_newtable(L);
lua_pushstring(L, "detailMapSrc");
lua_pushstring(L, inValue.detailMaps[i].detailMapSrc.c_str());
lua_pushstring(L, inValue._detailMaps[i]._detailMapSrc.c_str());
lua_rawset(L, -3);
lua_pushstring(L, "detailMapSize");
lua_pushnumber(L, (lua_Number)inValue.detailMaps[i].detailMapSize);
lua_pushnumber(L, (lua_Number)inValue._detailMaps[i]._detailMapSize);
lua_rawset(L, -3);
lua_rawset(L, -3);
@ -347,11 +347,11 @@ void terraindata_to_luaval(lua_State* L,const cocos2d::Terrain::TerrainData& inV
lua_pushstring(L, "mapHeight");
lua_pushnumber(L, (lua_Number)inValue.mapHeight);
lua_pushnumber(L, (lua_Number)inValue._mapHeight);
lua_rawset(L, -3);
lua_pushstring(L, "mapScale");
lua_pushnumber(L, (lua_Number)inValue.mapScale);
lua_pushnumber(L, (lua_Number)inValue._mapScale);
lua_rawset(L, -3);
lua_pushstring(L, "_detailMapAmount");
@ -359,7 +359,7 @@ void terraindata_to_luaval(lua_State* L,const cocos2d::Terrain::TerrainData& inV
lua_rawset(L, -3);
lua_pushstring(L, "skirtHeightRatio");
lua_pushnumber(L, (lua_Number)inValue.skirtHeightRatio);
lua_pushnumber(L, (lua_Number)inValue._skirtHeightRatio);
lua_rawset(L, -3);
}

View File

@ -19,7 +19,7 @@ TerrainSimple::TerrainSimple()
_camera->setPosition3D(Vec3(-1,1.6,4));
addChild(_camera);
Terrain::DetailMap r("TerrainTest/dirt.dds"),g("TerrainTest/Grass2.dds"),b("TerrainTest/road.dds"),a("TerrainTest/GreenSkin.jpg");
Terrain::DetailMap r("TerrainTest/dirt.jpg"),g("TerrainTest/Grass2.jpg"),b("TerrainTest/road.jpg"),a("TerrainTest/GreenSkin.jpg");
Terrain::TerrainData data("TerrainTest/heightmap16.jpg","TerrainTest/alphamap.png",r,g,b,a);
@ -69,19 +69,18 @@ void TerrainSimple::onTouchesMoved(const std::vector<cocos2d::Touch*>& touches,
std::string TerrainWalkThru::title() const
{
return " ";
return "Player walk around in terrain";
}
std::string TerrainWalkThru::subtitle() const
{
return " ";
return "touch to move";
}
TerrainWalkThru::TerrainWalkThru()
{
auto listener = EventListenerTouchAllAtOnce::create();
listener->onTouchesBegan = CC_CALLBACK_2(TerrainWalkThru::onTouchesBegan, this);
listener->onTouchesMoved = CC_CALLBACK_2(TerrainWalkThru::onTouchesMoved, this);
listener->onTouchesEnded = CC_CALLBACK_2(TerrainWalkThru::onTouchesEnd, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
@ -92,7 +91,7 @@ TerrainWalkThru::TerrainWalkThru()
_camera->setCameraFlag(CameraFlag::USER1);
addChild(_camera);
Terrain::DetailMap r("TerrainTest/dirt.dds"),g("TerrainTest/Grass2.dds",10),b("TerrainTest/road.dds"),a("TerrainTest/GreenSkin.jpg",20);
Terrain::DetailMap r("TerrainTest/dirt.jpg"),g("TerrainTest/Grass2.jpg",10),b("TerrainTest/road.jpg"),a("TerrainTest/GreenSkin.jpg",20);
Terrain::TerrainData data("TerrainTest/heightmap16.jpg","TerrainTest/alphamap.png",r,g,b,a,Size(32,32),40.0f,2);
_terrain = Terrain::create(data,Terrain::CrackFixedType::SKIRT);
@ -117,46 +116,10 @@ TerrainWalkThru::TerrainWalkThru()
_camera->setPosition3D(_player->getPosition3D()+camera_offset);
_camera->setRotation3D(Vec3(-45,0,0));
forward = Label::createWithSystemFont("forward","arial",22);
forward->setPosition(0,200);
forward->setAnchorPoint(Vec2(0,0));
backward = Label::createWithSystemFont("backward","arial",22);
backward->setPosition(0,250);
backward->setAnchorPoint(Vec2(0,0));
left = Label::createWithSystemFont("turn Left","arial",22);
left->setPosition(0,100);
left->setAnchorPoint(Vec2(0,0));
right = Label::createWithSystemFont("turn right","arial",22);
right->setPosition(0,150);
right->setAnchorPoint(Vec2(0,0));
addChild(_player);
addChild(_terrain);
}
void TerrainWalkThru::onTouchesMoved(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event* event)
{
float delta = Director::getInstance()->getDeltaTime();
auto touch = touches[0];
auto location = touch->getLocation();
auto PreviousLocation = touch->getPreviousLocation();
Point newPos = PreviousLocation - location;
Vec3 cameraDir;
Vec3 cameraRightDir;
_camera->getNodeToWorldTransform().getForwardVector(&cameraDir);
cameraDir.normalize();
cameraDir.y=0;
_camera->getNodeToWorldTransform().getRightVector(&cameraRightDir);
cameraRightDir.normalize();
cameraRightDir.y=0;
Vec3 cameraPos= _camera->getPosition3D();
cameraPos+=cameraDir*newPos.y*0.5*delta;
cameraPos+=cameraRightDir*newPos.x*0.5*delta;
_camera->setPosition3D(cameraPos);
}
void TerrainWalkThru::onTouchesBegan(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event* event)
{
@ -274,7 +237,6 @@ void Player::update(float dt)
player->setRotationQuat(headingQ*q2);
auto vec_offset =Vec4(camera_offset.x,camera_offset.y,camera_offset.z,1);
vec_offset = player->getNodeToWorldTransform()*vec_offset;
// _cam->setRotation3D(player->getRotation3D());
_cam->setPosition3D(player->getPosition3D() + camera_offset);
updateState();
}

View File

@ -64,14 +64,9 @@ public:
TerrainWalkThru();
virtual std::string title() const override;
virtual std::string subtitle() const override;
void onTouchesMoved(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event* event);
void onTouchesBegan(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event* event);
void onTouchesEnd(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event* event);
protected:
Label * forward;
Label * backward;
Label * left;
Label * right;
Camera * _camera;
Terrain * _terrain;
Player * _player;

Binary file not shown.

After

Width:  |  Height:  |  Size: 93 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 95 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 83 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 66 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 132 KiB