mirror of https://github.com/axmolengine/axmol.git
commit
6ba0e82e55
|
@ -239,6 +239,7 @@ xcopy /Y /Q "$(ProjectDir)..\..\external\chipmunk\prebuilt\win32\release-lib\*.*
|
|||
<ClCompile Include="..\3d\CCSkeleton3D.cpp" />
|
||||
<ClCompile Include="..\3d\CCSprite3D.cpp" />
|
||||
<ClCompile Include="..\3d\CCSprite3DMaterial.cpp" />
|
||||
<ClCompile Include="..\3d\CCTerrain.cpp" />
|
||||
<ClCompile Include="..\audio\AudioEngine.cpp" />
|
||||
<ClCompile Include="..\audio\win32\AudioCache.cpp" />
|
||||
<ClCompile Include="..\audio\win32\AudioEngine-win32.cpp" />
|
||||
|
@ -608,6 +609,7 @@ xcopy /Y /Q "$(ProjectDir)..\..\external\chipmunk\prebuilt\win32\release-lib\*.*
|
|||
<ClInclude Include="..\3d\CCSkeleton3D.h" />
|
||||
<ClInclude Include="..\3d\CCSprite3D.h" />
|
||||
<ClInclude Include="..\3d\CCSprite3DMaterial.h" />
|
||||
<ClInclude Include="..\3d\CCTerrain.h" />
|
||||
<ClInclude Include="..\3d\cocos3d.h" />
|
||||
<ClInclude Include="..\audio\include\AudioEngine.h" />
|
||||
<ClInclude Include="..\audio\include\Export.h" />
|
||||
|
|
|
@ -1303,6 +1303,9 @@
|
|||
<ClCompile Include="..\3d\CCAABB.cpp">
|
||||
<Filter>3d</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\3d\CCTerrain.cpp">
|
||||
<Filter>3d</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="..\physics\CCPhysicsBody.h">
|
||||
|
@ -2548,6 +2551,9 @@
|
|||
<ClInclude Include="..\physics\CCPhysicsHelper.h">
|
||||
<Filter>physics</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\3d\CCTerrain.h">
|
||||
<Filter>3d</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="..\math\Mat4.inl">
|
||||
|
|
|
@ -28,7 +28,7 @@ v_normal = a_normal;\
|
|||
}\
|
||||
";
|
||||
|
||||
static const char * fragment_shader ="\n#ifdef GL_ES\n\
|
||||
static const char * fragment_shader_RGB_4_DETAIL ="\n#ifdef GL_ES\n\
|
||||
precision lowp float;\
|
||||
\n#endif\n\
|
||||
uniform vec3 u_color;\
|
||||
|
@ -59,59 +59,60 @@ void main()\
|
|||
gl_FragColor = color*lightFactor;\
|
||||
}\
|
||||
}";
|
||||
Terrain * cocos2d::Terrain::create(TerrainData ¶meter)
|
||||
NS_CC_BEGIN
|
||||
Terrain * Terrain::create(TerrainData ¶meter)
|
||||
{
|
||||
Terrain * obj = new (std::nothrow)Terrain();
|
||||
obj->_terrainData = parameter;
|
||||
Terrain * terrain = new (std::nothrow)Terrain();
|
||||
terrain->_terrainData = parameter;
|
||||
//chunksize
|
||||
obj->_chunkSize = parameter.chunkSize;
|
||||
terrain->_chunkSize = parameter.chunkSize;
|
||||
//heightmap
|
||||
obj->initHeightMap(parameter.heightMapSrc.c_str());
|
||||
terrain->initHeightMap(parameter.heightMapSrc.c_str());
|
||||
if(!parameter.alphaMapSrc)
|
||||
{
|
||||
auto textImage = new (std::nothrow)Image();
|
||||
textImage->initWithImageFile(parameter.detailMaps[0].detailMapSrc);
|
||||
auto texture = new (std::nothrow)Texture2D();
|
||||
texture->initWithImage(textImage);
|
||||
obj->textures.push_back(texture);
|
||||
obj->init();
|
||||
obj->setAnchorPoint(Vec2(0,0));
|
||||
terrain->textures.push_back(texture);
|
||||
}else
|
||||
{
|
||||
//alpha map
|
||||
auto textImage = new (std::nothrow)Image();
|
||||
textImage->initWithImageFile(parameter.alphaMapSrc);
|
||||
obj->_alphaMap = new (std::nothrow)Texture2D();
|
||||
obj->_alphaMap->initWithImage(textImage);
|
||||
terrain->_alphaMap = new (std::nothrow)Texture2D();
|
||||
terrain->_alphaMap->initWithImage(textImage);
|
||||
for(int i =0;i<4;i++)
|
||||
{
|
||||
auto textImage = new (std::nothrow)Image();
|
||||
textImage->initWithImageFile(parameter.detailMaps[i].detailMapSrc);
|
||||
auto texture = new (std::nothrow)Texture2D();
|
||||
texture->initWithImage(textImage);
|
||||
obj->textures.push_back(texture);
|
||||
obj->detailSize[i] = parameter.detailMaps[i].detailMapSize;
|
||||
terrain->textures.push_back(texture);
|
||||
terrain->detailSize[i] = parameter.detailMaps[i].detailMapSize;
|
||||
}
|
||||
obj->init();
|
||||
obj->setAnchorPoint(Vec2(0,0));
|
||||
}
|
||||
return obj;
|
||||
terrain->init();
|
||||
terrain->setAnchorPoint(Vec2(0,0));
|
||||
terrain->autorelease();
|
||||
return terrain;
|
||||
}
|
||||
|
||||
bool cocos2d::Terrain::init()
|
||||
bool Terrain::init()
|
||||
{
|
||||
_lodDistance[0]=96;
|
||||
_lodDistance[1]=288;
|
||||
_lodDistance[2]=480;
|
||||
auto shader = GLProgram::createWithByteArrays(vertex_shader,fragment_shader);
|
||||
_lodDistance[0]=64;
|
||||
_lodDistance[1]=128;
|
||||
_lodDistance[2]=196;
|
||||
auto shader = GLProgram::createWithByteArrays(vertex_shader,fragment_shader_RGB_4_DETAIL);
|
||||
auto state = GLProgramState::create(shader);
|
||||
|
||||
setGLProgramState(state);
|
||||
setDrawWire(false);
|
||||
setIsEnableFrustumCull(true);
|
||||
return true;
|
||||
}
|
||||
|
||||
void cocos2d::Terrain::draw(cocos2d::Renderer *renderer, const cocos2d::Mat4 &transform, uint32_t flags)
|
||||
void Terrain::draw(cocos2d::Renderer *renderer, const cocos2d::Mat4 &transform, uint32_t flags)
|
||||
{
|
||||
_customCommand.init(getGlobalZOrder());
|
||||
_customCommand.func = CC_CALLBACK_0(Terrain::onDraw, this, transform, flags);
|
||||
|
@ -119,13 +120,24 @@ void cocos2d::Terrain::draw(cocos2d::Renderer *renderer, const cocos2d::Mat4 &tr
|
|||
renderer->addCommand(&_customCommand);
|
||||
}
|
||||
|
||||
void cocos2d::Terrain::onDraw(const Mat4 &transform, uint32_t flags)
|
||||
void Terrain::onDraw(const Mat4 &transform, uint32_t flags)
|
||||
{
|
||||
auto glProgram = getGLProgram();
|
||||
glProgram->use();
|
||||
glProgram->setUniformsForBuiltins(transform);
|
||||
glDepthMask(GL_TRUE);
|
||||
GLboolean depthTestCheck;
|
||||
glGetBooleanv(GL_DEPTH_TEST,&depthTestCheck);
|
||||
if(!depthTestCheck)
|
||||
{
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
}
|
||||
GLboolean blendCheck;
|
||||
glGetBooleanv(GL_BLEND,&blendCheck);
|
||||
if(blendCheck)
|
||||
{
|
||||
glDisable(GL_BLEND);
|
||||
}
|
||||
if(!_alphaMap)
|
||||
{
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
|
@ -136,7 +148,7 @@ void cocos2d::Terrain::onDraw(const Mat4 &transform, uint32_t flags)
|
|||
glUniform1i(alpha_location,0);
|
||||
}else
|
||||
{
|
||||
for(int i =0;i<4;i++)
|
||||
for(int i =0;i<_maxDetailMapValue;i++)
|
||||
{
|
||||
glActiveTexture(GL_TEXTURE0+i);
|
||||
glBindTexture(GL_TEXTURE_2D,textures[i]->getName());
|
||||
|
@ -153,6 +165,7 @@ void cocos2d::Terrain::onDraw(const Mat4 &transform, uint32_t flags)
|
|||
auto detailSizeLocation = glGetUniformLocation(glProgram->getProgram(),str);
|
||||
glUniform1f(detailSizeLocation,detailSize[i]);
|
||||
}
|
||||
|
||||
auto alpha_location = glGetUniformLocation(glProgram->getProgram(),"u_has_alpha");
|
||||
glUniform1i(alpha_location,1);
|
||||
|
||||
|
@ -171,35 +184,50 @@ void cocos2d::Terrain::onDraw(const Mat4 &transform, uint32_t flags)
|
|||
quad->draw();
|
||||
quad->resetNeedDraw(true);//reset it
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
if(depthTestCheck)
|
||||
{
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
}else
|
||||
{
|
||||
glDisable(GL_DEPTH_TEST);
|
||||
}
|
||||
if(blendCheck)
|
||||
{
|
||||
glEnable(GL_BLEND);
|
||||
}
|
||||
}
|
||||
|
||||
void cocos2d::Terrain::initHeightMap(const char * heightMap)
|
||||
void Terrain::initHeightMap(const char * heightMap)
|
||||
{
|
||||
auto image = new Image();
|
||||
image->initWithImageFile(heightMap);
|
||||
_data = image->getData();
|
||||
imageWidth =image->getWidth();
|
||||
imageHeight =image->getHeight();
|
||||
auto format = image->getRenderFormat();
|
||||
_heightMapImage = new Image();
|
||||
_heightMapImage->initWithImageFile(heightMap);
|
||||
_data = _heightMapImage->getData();
|
||||
imageWidth =_heightMapImage->getWidth();
|
||||
imageHeight =_heightMapImage->getHeight();
|
||||
auto format = _heightMapImage->getRenderFormat();
|
||||
int chunk_amount_y = imageHeight/_chunkSize.height;
|
||||
int chunk_amount_x = imageWidth/_chunkSize.width;
|
||||
loadVertices();
|
||||
calculateNormal();
|
||||
|
||||
memset(_chunkesArray, 0, sizeof(_chunkesArray));
|
||||
for(int m =0;m<chunk_amount_y;m++)
|
||||
{
|
||||
for(int n =0; n<chunk_amount_x;n++)
|
||||
{
|
||||
_chunkesArray[m][n] = new Chunk();
|
||||
_chunkesArray[m][n]->_terrain = this;
|
||||
if(n-1>=0) _chunkesArray[m][n]->left = _chunkesArray[m][n-1];
|
||||
if(n+1<chunk_amount_x) _chunkesArray[m][n]->right = _chunkesArray[m][n+1];
|
||||
if(m-1>=0) _chunkesArray[m][n]->back = _chunkesArray[m-1][n];
|
||||
if(m+1<chunk_amount_y) _chunkesArray[m][n]->front = _chunkesArray[m+1][n];
|
||||
_chunkesArray[m][n]->_size = _chunkSize;
|
||||
_chunkesArray[m][n]->generate(imageWidth,imageHeight,m,n,_data);
|
||||
}
|
||||
}
|
||||
|
||||
for(int m =0;m<chunk_amount_y;m++)
|
||||
{
|
||||
for(int n =0; n<chunk_amount_x;n++)
|
||||
{
|
||||
|
||||
_chunkesArray[m][n]->_terrain = this;
|
||||
if(n-1>=0) _chunkesArray[m][n]->left = _chunkesArray[m][n-1];
|
||||
if(n+1<chunk_amount_x) _chunkesArray[m][n]->right = _chunkesArray[m][n+1];
|
||||
if(m-1>=0) _chunkesArray[m][n]->back = _chunkesArray[m-1][n];
|
||||
|
@ -211,12 +239,12 @@ void cocos2d::Terrain::initHeightMap(const char * heightMap)
|
|||
quad = new QuadTree(0,0,imageWidth,imageHeight,this);
|
||||
}
|
||||
|
||||
cocos2d::Terrain::Terrain()
|
||||
Terrain::Terrain()
|
||||
{
|
||||
_alphaMap = nullptr;
|
||||
}
|
||||
|
||||
void cocos2d::Terrain::setChunksLOD(Vec3 cameraPos)
|
||||
void Terrain::setChunksLOD(Vec3 cameraPos)
|
||||
{
|
||||
int chunk_amount_y = imageHeight/_chunkSize.height;
|
||||
int chunk_amount_x = imageWidth/_chunkSize.width;
|
||||
|
@ -239,12 +267,12 @@ void cocos2d::Terrain::setChunksLOD(Vec3 cameraPos)
|
|||
}
|
||||
}
|
||||
|
||||
float cocos2d::Terrain::getHeight(float x, float y ,float z)
|
||||
float Terrain::getHeight(float x,float z,Vec3 * normal)
|
||||
{
|
||||
Vec2 pos = Vec2(x,z);
|
||||
|
||||
//top-left
|
||||
Vec2 tl = Vec2(-1*_terrainData.mapScale*imageWidth/2,-1*_terrainData.mapScale*imageWidth/2);
|
||||
Vec2 tl = Vec2(-1*_terrainData.mapScale*imageWidth/2,-1*_terrainData.mapScale*imageHeight/2);
|
||||
auto result = getNodeToWorldTransform()*Vec4(tl.x,0.0f,tl.y,1.0f);
|
||||
tl = Vec2(result.x,result.z);
|
||||
|
||||
|
@ -264,27 +292,56 @@ float cocos2d::Terrain::getHeight(float x, float y ,float z)
|
|||
float v =image_y - (int)image_y;
|
||||
float i = (int)image_x;
|
||||
float j = (int)image_y;
|
||||
|
||||
|
||||
if(image_x>=imageWidth-1 || image_y >=imageHeight-1 || image_x<0 || image_y<0)
|
||||
{
|
||||
return y;
|
||||
return 0;
|
||||
}else
|
||||
{
|
||||
float a = getImageHeight(i,j)*getScaleY();
|
||||
float b = getImageHeight(i,j+1)*getScaleY();
|
||||
float c = getImageHeight(i+1,j)*getScaleY();
|
||||
float d = getImageHeight(i+1,j+1)*getScaleY();
|
||||
if(normal)
|
||||
{
|
||||
normal->x = c - b;
|
||||
normal->y = 2;
|
||||
normal->z = d - a;
|
||||
normal->normalize();
|
||||
//(*normal) = (1-u)*(1-v)*getNormal(i,j)+ (1-u)*v*getNormal(i,j+1) + u*(1-v)*getNormal(i+1,j)+ u*v*getNormal(i+1,j+1);
|
||||
}
|
||||
float reuslt = (1-u)*(1-v)*getImageHeight(i,j)*getScaleY() + (1-u)*v*getImageHeight(i,j+1)*getScaleY() + u*(1-v)*getImageHeight(i+1,j)*getScaleY() + u*v*getImageHeight(i+1,j+1)*getScaleY();
|
||||
return reuslt;
|
||||
}
|
||||
}
|
||||
|
||||
float cocos2d::Terrain::getHeight(Vec3 pos)
|
||||
float Terrain::getHeight(Vec2 pos,Vec3*Normal)
|
||||
{
|
||||
return getHeight(pos.x,pos.y,pos.z);
|
||||
return getHeight(pos.x,pos.y,Normal);
|
||||
}
|
||||
|
||||
float cocos2d::Terrain::getImageHeight(int pixel_x,int pixel_y)
|
||||
float Terrain::getImageHeight(int pixel_x,int pixel_y)
|
||||
{
|
||||
return _data[(pixel_y*imageWidth+pixel_x)*3]*1.0/255*_terrainData.mapHeight -0.5*_terrainData.mapHeight;
|
||||
int byte_stride =1;
|
||||
switch (_heightMapImage->getRenderFormat())
|
||||
{
|
||||
case Texture2D::PixelFormat::BGRA8888:
|
||||
byte_stride = 4;
|
||||
break;
|
||||
case Texture2D::PixelFormat::RGB888:
|
||||
byte_stride =3;
|
||||
break;
|
||||
case Texture2D::PixelFormat::I8:
|
||||
byte_stride =1;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return _data[(pixel_y*imageWidth+pixel_x)*byte_stride]*1.0/255*_terrainData.mapHeight -0.5*_terrainData.mapHeight;
|
||||
}
|
||||
|
||||
void cocos2d::Terrain::loadVertices()
|
||||
void Terrain::loadVertices()
|
||||
{
|
||||
for(int i =0;i<imageHeight;i++)
|
||||
{
|
||||
|
@ -292,7 +349,7 @@ void cocos2d::Terrain::loadVertices()
|
|||
{
|
||||
TerrainVertexData v;
|
||||
v.position = Vec3(j*_terrainData.mapScale- imageWidth/2*_terrainData.mapScale, //x
|
||||
_data[(i*imageWidth+j)*3]*1.0/255*_terrainData.mapHeight -0.5*_terrainData.mapHeight, //y
|
||||
getImageHeight(j,i), //y
|
||||
i*_terrainData.mapScale - imageHeight/2*_terrainData.mapScale);//z
|
||||
v.texcoord = Tex2F(j*1.0/imageWidth,i*1.0/imageHeight);
|
||||
vertices.push_back (v);
|
||||
|
@ -300,7 +357,7 @@ void cocos2d::Terrain::loadVertices()
|
|||
}
|
||||
}
|
||||
|
||||
void cocos2d::Terrain::calculateNormal()
|
||||
void Terrain::calculateNormal()
|
||||
{
|
||||
indices.clear();
|
||||
//we generate whole terrain indices(global indices) for correct normal calculate
|
||||
|
@ -340,24 +397,93 @@ void cocos2d::Terrain::calculateNormal()
|
|||
indices.clear();
|
||||
}
|
||||
|
||||
void cocos2d::Terrain::setDrawWire(bool bool_value)
|
||||
void Terrain::setDrawWire(bool bool_value)
|
||||
{
|
||||
_isDrawWire = bool_value;
|
||||
}
|
||||
|
||||
void cocos2d::Terrain::setLODDistance(float lod_1,float lod_2,float lod_3)
|
||||
void Terrain::setLODDistance(float lod_1,float lod_2,float lod_3)
|
||||
{
|
||||
_lodDistance[0] = lod_1;
|
||||
_lodDistance[1] = lod_2;
|
||||
_lodDistance[2] = lod_3;
|
||||
}
|
||||
|
||||
void cocos2d::Terrain::setIsEnableFrustumCull(bool bool_value)
|
||||
void Terrain::setIsEnableFrustumCull(bool bool_value)
|
||||
{
|
||||
_isEnableFrustumCull = bool_value;
|
||||
}
|
||||
|
||||
void cocos2d::Terrain::Chunk::finish()
|
||||
Terrain::~Terrain()
|
||||
{
|
||||
free(_data);
|
||||
for(int i = 0;i<MAX_CHUNKES;i++)
|
||||
{
|
||||
for(int j = 0;j<MAX_CHUNKES;j++)
|
||||
{
|
||||
if(_chunkesArray[i][j])
|
||||
{
|
||||
delete _chunkesArray[i][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cocos2d::Vec3 Terrain::getNormal(int pixel_x,int pixel_y)
|
||||
{
|
||||
float a = getImageHeight(pixel_x,pixel_y)*getScaleY();
|
||||
float b = getImageHeight(pixel_x,pixel_y+1)*getScaleY();
|
||||
float c = getImageHeight(pixel_x+1,pixel_y)*getScaleY();
|
||||
float d = getImageHeight(pixel_x+1,pixel_y+1)*getScaleY();
|
||||
Vec3 normal;
|
||||
normal.x = c - b;
|
||||
normal.y = 2;
|
||||
normal.z = d - a;
|
||||
normal.normalize();
|
||||
return normal;
|
||||
}
|
||||
|
||||
cocos2d::Vec3 Terrain::getIntersectionPoint(const Ray & ray)
|
||||
{
|
||||
Vec3 dir = ray._direction;
|
||||
dir.normalize();
|
||||
Vec3 rayStep = _terrainData.chunkSize.width*0.25*dir;
|
||||
Vec3 rayPos = ray._origin;
|
||||
Vec3 rayStartPosition = ray._origin;
|
||||
Vec3 lastRayPosition =rayPos;
|
||||
rayPos += rayStep;
|
||||
// Linear search - Loop until find a point inside and outside the terrain Vector3
|
||||
float height = getHeight(rayPos.x,rayPos.z);
|
||||
|
||||
while (rayPos.y > height)
|
||||
{
|
||||
lastRayPosition = rayPos;
|
||||
rayPos += rayStep;
|
||||
height = getHeight(rayPos.x,rayPos.z);
|
||||
}
|
||||
|
||||
Vec3 startPosition = lastRayPosition;
|
||||
Vec3 endPosition = rayPos;
|
||||
|
||||
for (int i= 0; i< 32; i++)
|
||||
{
|
||||
// Binary search pass
|
||||
Vec3 middlePoint = (startPosition + endPosition) * 0.5f;
|
||||
if (middlePoint.y < height)
|
||||
endPosition = middlePoint;
|
||||
else
|
||||
startPosition = middlePoint;
|
||||
}
|
||||
Vec3 collisionPoint = (startPosition + endPosition) * 0.5f;
|
||||
return collisionPoint;
|
||||
}
|
||||
|
||||
void Terrain::setMaxDetailMapAmount(int max_value)
|
||||
{
|
||||
_maxDetailMapValue = max_value;
|
||||
}
|
||||
|
||||
void Terrain::Chunk::finish()
|
||||
{
|
||||
//genearate two VBO ,the first for vertices, we just setup datas once ,won't changed at all
|
||||
//the second vbo for the indices, because we use level of detail technique to each chunk, so we will modified frequently
|
||||
|
@ -380,7 +506,7 @@ void cocos2d::Terrain::Chunk::finish()
|
|||
}
|
||||
}
|
||||
|
||||
void cocos2d::Terrain::Chunk::bindAndDraw()
|
||||
void Terrain::Chunk::bindAndDraw()
|
||||
{
|
||||
#if (CC_TARGET_PLATFORM == CC_PLATFORM_MAC) || (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_LINUX)
|
||||
if(_terrain->_isDrawWire)
|
||||
|
@ -415,7 +541,7 @@ void cocos2d::Terrain::Chunk::bindAndDraw()
|
|||
#endif
|
||||
}
|
||||
|
||||
void cocos2d::Terrain::Chunk::generate(int imageWidth,int imageHeight,int m,int n,const unsigned char * data)
|
||||
void Terrain::Chunk::generate(int imageWidth,int imageHeight,int m,int n,const unsigned char * data)
|
||||
{
|
||||
pos_y = m;
|
||||
pos_x = n;
|
||||
|
@ -433,7 +559,7 @@ void cocos2d::Terrain::Chunk::generate(int imageWidth,int imageHeight,int m,int
|
|||
finish();
|
||||
}
|
||||
|
||||
cocos2d::Terrain::Chunk::Chunk()
|
||||
Terrain::Chunk::Chunk()
|
||||
{
|
||||
_currentLod = 0;
|
||||
left = nullptr;
|
||||
|
@ -442,7 +568,7 @@ cocos2d::Terrain::Chunk::Chunk()
|
|||
front = nullptr;
|
||||
}
|
||||
|
||||
void cocos2d::Terrain::Chunk::updateIndices()
|
||||
void Terrain::Chunk::updateIndices()
|
||||
{
|
||||
int gridY = _size.height;
|
||||
int gridX = _size.width;
|
||||
|
@ -620,7 +746,7 @@ FINISH_INNER_INDICES_SET:
|
|||
}
|
||||
}
|
||||
|
||||
void cocos2d::Terrain::Chunk::calculateAABB()
|
||||
void Terrain::Chunk::calculateAABB()
|
||||
{
|
||||
std::vector<Vec3>pos;
|
||||
for(int i =0;i<vertices.size();i++)
|
||||
|
@ -630,7 +756,7 @@ void cocos2d::Terrain::Chunk::calculateAABB()
|
|||
_aabb.updateMinMax(&pos[0],pos.size());
|
||||
}
|
||||
|
||||
void cocos2d::Terrain::Chunk::calculateSlope()
|
||||
void Terrain::Chunk::calculateSlope()
|
||||
{
|
||||
//find max slope
|
||||
auto lowest = vertices[0].position;
|
||||
|
@ -655,11 +781,12 @@ float dist = a.distance(b);
|
|||
slope = (highest.y - lowest.y)/dist;
|
||||
}
|
||||
|
||||
void cocos2d::Terrain::Chunk::updateVerticesForLOD()
|
||||
void Terrain::Chunk::updateVerticesForLOD()
|
||||
{
|
||||
vertices_tmp = vertices;
|
||||
int gridY = _size.height;
|
||||
int gridX = _size.width;
|
||||
|
||||
if(_currentLod>=2 && abs(slope)>1.2)
|
||||
{
|
||||
int step = int(powf(2.0f, float(_currentLod)));
|
||||
|
@ -686,7 +813,12 @@ glBufferData(GL_ARRAY_BUFFER, sizeof(TerrainVertexData)*vertices_tmp.size(), &ve
|
|||
|
||||
}
|
||||
|
||||
cocos2d::Terrain::QuadTree::QuadTree(int x,int y,int width,int height,Terrain * terrain)
|
||||
Terrain::Chunk::~Chunk()
|
||||
{
|
||||
// glDeleteBuffers(2,vbo);
|
||||
}
|
||||
|
||||
Terrain::QuadTree::QuadTree(int x,int y,int width,int height,Terrain * terrain)
|
||||
{
|
||||
_needDraw = true;
|
||||
parent = nullptr;
|
||||
|
@ -724,7 +856,7 @@ cocos2d::Terrain::QuadTree::QuadTree(int x,int y,int width,int height,Terrain *
|
|||
}
|
||||
}
|
||||
|
||||
void cocos2d::Terrain::QuadTree::draw()
|
||||
void Terrain::QuadTree::draw()
|
||||
{
|
||||
if(!_needDraw)return;
|
||||
if(_isTerminal){
|
||||
|
@ -738,7 +870,7 @@ void cocos2d::Terrain::QuadTree::draw()
|
|||
}
|
||||
}
|
||||
|
||||
void cocos2d::Terrain::QuadTree::resetNeedDraw(bool value)
|
||||
void Terrain::QuadTree::resetNeedDraw(bool value)
|
||||
{
|
||||
this->_needDraw = value;
|
||||
if(!_isTerminal)
|
||||
|
@ -750,7 +882,7 @@ void cocos2d::Terrain::QuadTree::resetNeedDraw(bool value)
|
|||
}
|
||||
}
|
||||
|
||||
void cocos2d::Terrain::QuadTree::cullByCamera(const Camera * camera,const Mat4 & worldTransform)
|
||||
void Terrain::QuadTree::cullByCamera(const Camera * camera,const Mat4 & worldTransform)
|
||||
{
|
||||
auto aabb = _aabb;
|
||||
aabb.transform(worldTransform);
|
||||
|
@ -768,7 +900,7 @@ void cocos2d::Terrain::QuadTree::cullByCamera(const Camera * camera,const Mat4 &
|
|||
}
|
||||
}
|
||||
|
||||
cocos2d::Terrain::TerrainData::TerrainData(const char * heightMapsrc ,const char * textureSrc,const Size & chunksize,float mapHeight,float mapScale)
|
||||
Terrain::TerrainData::TerrainData(const char * heightMapsrc ,const char * textureSrc,const Size & chunksize,float mapHeight,float mapScale)
|
||||
{
|
||||
this->heightMapSrc = heightMapsrc;
|
||||
this->detailMaps[0].detailMapSrc = textureSrc;
|
||||
|
@ -778,7 +910,7 @@ cocos2d::Terrain::TerrainData::TerrainData(const char * heightMapsrc ,const char
|
|||
this->mapScale = mapScale;
|
||||
}
|
||||
|
||||
cocos2d::Terrain::TerrainData::TerrainData(const char * heightMapsrc ,const char * alphamap,const DetailMap& detail1,const DetailMap& detail2,const DetailMap& detail3,const DetailMap& detail4,const Size & chunksize,float mapHeight,float mapScale)
|
||||
Terrain::TerrainData::TerrainData(const char * heightMapsrc ,const char * alphamap,const DetailMap& detail1,const DetailMap& detail2,const DetailMap& detail3,const DetailMap& detail4,const Size & chunksize,float mapHeight,float mapScale)
|
||||
{
|
||||
this->heightMapSrc = heightMapsrc;
|
||||
this->alphaMapSrc = const_cast<char *>(alphamap);
|
||||
|
@ -789,21 +921,37 @@ cocos2d::Terrain::TerrainData::TerrainData(const char * heightMapsrc ,const char
|
|||
this->chunkSize = chunksize;
|
||||
this->mapHeight = mapHeight;
|
||||
this->mapScale = mapScale;
|
||||
_detailMapAmount = 4;
|
||||
}
|
||||
|
||||
cocos2d::Terrain::TerrainData::TerrainData()
|
||||
Terrain::TerrainData::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*/)
|
||||
{
|
||||
this->heightMapSrc = heightMapsrc;
|
||||
this->alphaMapSrc = const_cast<char *>(alphamap);
|
||||
this->detailMaps[0] = detail1;
|
||||
this->detailMaps[1] = detail2;
|
||||
this->detailMaps[2] = detail3;
|
||||
this->detailMaps[3] = nullptr;
|
||||
this->chunkSize = chunksize;
|
||||
this->mapHeight = mapHeight;
|
||||
this->mapScale = mapScale;
|
||||
_detailMapAmount = 3;
|
||||
}
|
||||
|
||||
Terrain::TerrainData::TerrainData()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
cocos2d::Terrain::DetailMap::DetailMap(const char * detailMapSrc , float size /*= 35*/)
|
||||
Terrain::DetailMap::DetailMap(const char * detailMapSrc , float size /*= 35*/)
|
||||
{
|
||||
this->detailMapSrc = detailMapSrc;
|
||||
this->detailMapSize = size;
|
||||
}
|
||||
|
||||
cocos2d::Terrain::DetailMap::DetailMap()
|
||||
Terrain::DetailMap::DetailMap()
|
||||
{
|
||||
detailMapSrc = "";
|
||||
detailMapSize = 35;
|
||||
}
|
||||
NS_CC_END
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/****************************************************************************
|
||||
Copyright (c) 2014 Chukong Technologies Inc.
|
||||
Copyright (c) 2015 Chukong Technologies Inc.
|
||||
|
||||
http://www.cocos2d-x.org
|
||||
|
||||
|
@ -29,6 +29,7 @@
|
|||
#include "renderer/CCCustomCommand.h"
|
||||
#include "3d/CCAABB.h"
|
||||
#include "2d/CCCamera.h"
|
||||
#include "3d/CCRay.h"
|
||||
#include <vector>
|
||||
NS_CC_BEGIN
|
||||
/*
|
||||
|
@ -36,9 +37,42 @@ NS_CC_BEGIN
|
|||
**/
|
||||
#define MAX_CHUNKES 256
|
||||
|
||||
|
||||
/*
|
||||
* Terrain
|
||||
*use to render outdoor or large scene via heightMap
|
||||
* 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;
|
||||
* let us cull chunks efficientlly to reduce drawCall amount And reduce the VBOs'Size that pass to the GPU.
|
||||
*
|
||||
* 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
|
||||
* means only the base level is used),the maxium number of LOD levels is 4. Of course ,you can hack the value individually.
|
||||
*
|
||||
* 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 .
|
||||
**/
|
||||
class CC_DLL Terrain :public Node{
|
||||
public:
|
||||
|
@ -68,7 +102,7 @@ public:
|
|||
TerrainData();
|
||||
TerrainData(const char* heightMapsrc ,const char * textureSrc,const Size & chunksize = Size(32,32),float mapHeight = 2,float mapScale = 0.1);
|
||||
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);
|
||||
|
||||
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);
|
||||
/*
|
||||
*deterimine the chunk size,chunk is the minimal subdivision of the Terrain
|
||||
*/
|
||||
|
@ -83,6 +117,7 @@ public:
|
|||
float mapHeight;
|
||||
/*terrain scale factor*/
|
||||
float mapScale;
|
||||
int _detailMapAmount;
|
||||
};
|
||||
private:
|
||||
|
||||
|
@ -111,6 +146,7 @@ private:
|
|||
{
|
||||
/*Constructor*/
|
||||
Chunk();
|
||||
~Chunk();
|
||||
/*vertices*/
|
||||
std::vector<TerrainVertexData> vertices;
|
||||
/*LOD indices*/
|
||||
|
@ -132,11 +168,10 @@ private:
|
|||
void finish();
|
||||
/*use linear-sample vertices for LOD mesh*/
|
||||
void updateVerticesForLOD();
|
||||
/*calculate the average slope of the chunk*/
|
||||
void calculateSlope();
|
||||
/*updateIndices for every frame*/
|
||||
void updateIndices();
|
||||
|
||||
void Terrain::Chunk::calculateSlope();
|
||||
/*current LOD of the chunk*/
|
||||
int _currentLod;
|
||||
/*the left,right,front,back neighbors*/
|
||||
|
@ -182,7 +217,6 @@ private:
|
|||
bool _needDraw;
|
||||
};
|
||||
|
||||
|
||||
friend QuadTree;
|
||||
friend Chunk;
|
||||
public:
|
||||
|
@ -191,10 +225,11 @@ public:
|
|||
void initHeightMap(const char* heightMap);
|
||||
/*create entry*/
|
||||
static Terrain * create(TerrainData ¶meter);
|
||||
/*get specified position's height mapping to the terrain*/
|
||||
float getHeight(float x,float y, float z);
|
||||
float getHeight(Vec3 pos);
|
||||
|
||||
/*get specified position's height mapping to the terrain*/
|
||||
float getHeight(float x,float z,Vec3 * normal= nullptr);
|
||||
float getHeight(Vec2 pos,Vec3*Normal = nullptr);
|
||||
Vec3 getNormal(int pixel_x,int pixel_y);
|
||||
/*get height from the raw height map*/
|
||||
float getImageHeight(int pixel_x,int pixel_y);
|
||||
/*Debug Use only, show the wireline instead of the surface. only support desktop platform*/
|
||||
|
@ -205,8 +240,13 @@ public:
|
|||
void setIsEnableFrustumCull(bool bool_value);
|
||||
// Overrides, internal use only
|
||||
virtual void draw(cocos2d::Renderer* renderer, const cocos2d::Mat4 &transform, uint32_t flags) override;
|
||||
private:
|
||||
//Ray-Terrain intersection.
|
||||
Vec3 getIntersectionPoint(const Ray & ray);
|
||||
void setMaxDetailMapAmount(int max_value);
|
||||
protected:
|
||||
|
||||
Terrain();
|
||||
virtual ~Terrain();
|
||||
void onDraw(const Mat4 &transform, uint32_t flags);
|
||||
//set each chunk's LOD
|
||||
void setChunksLOD(Vec3 cameraPos);
|
||||
|
@ -214,7 +254,7 @@ private:
|
|||
void loadVertices();
|
||||
//calculate Normal Line for each Vertex
|
||||
void calculateNormal();
|
||||
private:
|
||||
protected:
|
||||
TerrainData _terrainData;
|
||||
bool _isDrawWire;
|
||||
unsigned char * _data;
|
||||
|
@ -225,13 +265,17 @@ private:
|
|||
GLuint vbo[2];
|
||||
QuadTree * quad;
|
||||
int detailSize[4];
|
||||
Chunk * _chunkesArray[256][256];
|
||||
Chunk * _chunkesArray[MAX_CHUNKES][MAX_CHUNKES];
|
||||
std::vector<TerrainVertexData> vertices;
|
||||
std::vector<GLushort > indices;
|
||||
int imageWidth;
|
||||
int imageHeight;
|
||||
Size _chunkSize;
|
||||
bool _isEnableFrustumCull;
|
||||
int _maxDetailMapValue;
|
||||
cocos2d::Image * _heightMapImage;
|
||||
Mat4 _oldCameraModelMatrix;
|
||||
Mat4 _oldTerrrainModelMatrix;
|
||||
};
|
||||
NS_CC_END
|
||||
#endif
|
||||
|
|
|
@ -12,4 +12,4 @@
|
|||
|
||||
android.library=true
|
||||
# Project target.
|
||||
target=android-10
|
||||
target=android-19
|
||||
|
|
|
@ -0,0 +1,487 @@
|
|||
#include "TerrainTest.h"
|
||||
|
||||
|
||||
|
||||
Vec3 camera_offset(0,45,60);
|
||||
#define PLAYER_HEIGHT 0
|
||||
static std::function<Layer*()> createFunctions[] =
|
||||
{
|
||||
CL(TerrainSimple),
|
||||
CL(TerrainWalkThru),
|
||||
};
|
||||
|
||||
static int sceneIdx = -1;
|
||||
#define MAX_LAYER (sizeof(createFunctions) / sizeof(createFunctions[0]))
|
||||
|
||||
static Layer* nextTerrainTestAction()
|
||||
{
|
||||
sceneIdx++;
|
||||
sceneIdx = sceneIdx % MAX_LAYER;
|
||||
|
||||
auto layer = (createFunctions[sceneIdx])();
|
||||
return layer;
|
||||
}
|
||||
|
||||
static Layer* backTerrainTestAction()
|
||||
{
|
||||
sceneIdx--;
|
||||
int total = MAX_LAYER;
|
||||
if( sceneIdx < 0 )
|
||||
sceneIdx += total;
|
||||
|
||||
auto layer = (createFunctions[sceneIdx])();
|
||||
return layer;
|
||||
}
|
||||
|
||||
static Layer* restartTerrainTestAction()
|
||||
{
|
||||
auto layer = (createFunctions[sceneIdx])();
|
||||
return layer;
|
||||
}
|
||||
|
||||
TerrainSimple::TerrainSimple()
|
||||
{
|
||||
Size visibleSize = Director::getInstance()->getVisibleSize();
|
||||
|
||||
//use custom camera
|
||||
_camera = Camera::createPerspective(60,visibleSize.width/visibleSize.height,0.1,800);
|
||||
_camera->setCameraFlag(CameraFlag::USER1);
|
||||
addChild(_camera);
|
||||
|
||||
Terrain::DetailMap r("TerrainTest/dirt.dds"),g("TerrainTest/Grass2.dds"),b("TerrainTest/road.dds"),a("TerrainTest/Grass1.dds");
|
||||
|
||||
Terrain::TerrainData data("TerrainTest/heightmap16.jpg","TerrainTest/alphamap.png",r,g,b,a);
|
||||
_terrain = Terrain::create(data);
|
||||
_terrain->setMaxDetailMapAmount(4);
|
||||
addChild(_terrain);
|
||||
_terrain->setCameraMask(2);
|
||||
_terrain->setDrawWire(true);
|
||||
auto listener = EventListenerTouchAllAtOnce::create();
|
||||
listener->onTouchesMoved = CC_CALLBACK_2(TerrainSimple::onTouchesMoved, this);
|
||||
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
|
||||
|
||||
|
||||
TTFConfig ttfConfig("fonts/arial.ttf", 8);
|
||||
auto label1 = Label::createWithTTF(ttfConfig,"129*129 1 DETAIL MAP 32*32 chunk Size");
|
||||
auto menuItem1 = MenuItemLabel::create(label1, CC_CALLBACK_1(TerrainSimple::on127x127_1DetailMap32x32Callback,this));
|
||||
auto label2 = Label::createWithTTF(ttfConfig,"257*257 1 DETAIL MAP 32*32 chunk Size");
|
||||
auto menuItem2 = MenuItemLabel::create(label2, CC_CALLBACK_1(TerrainSimple::on257x257_1DetailMap32x32Callback,this));
|
||||
|
||||
auto label3 = Label::createWithTTF(ttfConfig,"Enable LOD");
|
||||
auto menuItem3 = MenuItemLabel::create(label3, CC_CALLBACK_1(TerrainSimple::onEnableLODCallback,this));
|
||||
|
||||
auto label4 = Label::createWithTTF(ttfConfig,"disable LOD");
|
||||
auto menuItem4 = MenuItemLabel::create(label4, CC_CALLBACK_1(TerrainSimple::onDisableLODCallback,this));
|
||||
|
||||
auto menu = Menu::create(menuItem1, menuItem2,menuItem3,menuItem4, nullptr);
|
||||
menu->setPosition(Vec2::ZERO);
|
||||
menuItem1->setPosition( Vec2(0, VisibleRect::top().y-160) );
|
||||
menuItem1->setAnchorPoint(Vec2(0,0));
|
||||
menuItem2->setPosition( Vec2(0, VisibleRect::top().y-190) );
|
||||
menuItem2->setAnchorPoint(Vec2(0,0));
|
||||
menuItem3->setPosition( Vec2(0, VisibleRect::top().y-210) );
|
||||
menuItem3->setAnchorPoint(Vec2(0,0));
|
||||
menuItem4->setPosition( Vec2(0, VisibleRect::top().y-240) );
|
||||
menuItem4->setAnchorPoint(Vec2(0,0));
|
||||
addChild(menu, 0);
|
||||
}
|
||||
|
||||
std::string TerrainSimple::title() const
|
||||
{
|
||||
return "Simple Terrain";
|
||||
}
|
||||
|
||||
std::string TerrainSimple::subtitle() const
|
||||
{
|
||||
return "Drag to walkThru";
|
||||
}
|
||||
|
||||
void TerrainSimple::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 TerrainSimple::on127x127_1DetailMap32x32Callback(Ref* sender)
|
||||
{
|
||||
_terrain->removeFromParent();
|
||||
Terrain::TerrainData data("TerrainTest/heightMap129.jpg","TerrainTest/sand.jpg");
|
||||
_terrain = Terrain::create(data);
|
||||
_terrain->setMaxDetailMapAmount(4);
|
||||
addChild(_terrain);
|
||||
_terrain->setCameraMask(2);
|
||||
_terrain->setDrawWire(true);
|
||||
}
|
||||
|
||||
void TerrainSimple::on257x257_1DetailMap32x32Callback(Ref* sender)
|
||||
{
|
||||
_terrain->removeFromParent();
|
||||
Terrain::TerrainData data("TerrainTest/heightmap16.jpg","TerrainTest/sand.jpg");
|
||||
_terrain = Terrain::create(data);
|
||||
_terrain->setMaxDetailMapAmount(4);
|
||||
addChild(_terrain);
|
||||
_terrain->setCameraMask(2);
|
||||
_terrain->setDrawWire(true);
|
||||
}
|
||||
|
||||
void TerrainSimple::onEnableLODCallback(Ref* sender)
|
||||
{
|
||||
_terrain->setScale(20);
|
||||
}
|
||||
|
||||
void TerrainSimple::onDisableLODCallback(Ref* sender)
|
||||
{
|
||||
_terrain->setScale(1);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void TerrainTestScene::runThisTest()
|
||||
{
|
||||
auto layer = nextTerrainTestAction();
|
||||
addChild(layer);
|
||||
Director::getInstance()->replaceScene(this);
|
||||
}
|
||||
|
||||
|
||||
void TerrainTestDemo::restartCallback(Ref* sender)
|
||||
{
|
||||
auto s = new (std::nothrow) TerrainTestScene();
|
||||
s->addChild(restartTerrainTestAction());
|
||||
|
||||
Director::getInstance()->replaceScene(s);
|
||||
s->release();
|
||||
}
|
||||
|
||||
void TerrainTestDemo::nextCallback(Ref* sender)
|
||||
{
|
||||
auto s = new (std::nothrow) TerrainTestScene();
|
||||
s->addChild( nextTerrainTestAction());
|
||||
Director::getInstance()->replaceScene(s);
|
||||
s->release();
|
||||
}
|
||||
|
||||
void TerrainTestDemo::backCallback(Ref* sender)
|
||||
{
|
||||
auto s = new (std::nothrow) TerrainTestScene();
|
||||
s->addChild( backTerrainTestAction());
|
||||
Director::getInstance()->replaceScene(s);
|
||||
s->release();
|
||||
}
|
||||
|
||||
std::string TerrainTestDemo::title() const
|
||||
{
|
||||
return "No title";
|
||||
}
|
||||
|
||||
std::string TerrainTestDemo::subtitle() const
|
||||
{
|
||||
return "";
|
||||
}
|
||||
|
||||
void TerrainTestDemo::onEnter()
|
||||
{
|
||||
BaseTest::onEnter();
|
||||
}
|
||||
|
||||
TerrainTestDemo::TerrainTestDemo(void)
|
||||
:BaseTest()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
TerrainTestDemo::~TerrainTestDemo(void)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
std::string TerrainWalkThru::title() const
|
||||
{
|
||||
return " ";
|
||||
}
|
||||
|
||||
std::string TerrainWalkThru::subtitle() const
|
||||
{
|
||||
return " ";
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
Size visibleSize = Director::getInstance()->getVisibleSize();
|
||||
|
||||
//use custom camera
|
||||
_camera = Camera::createPerspective(60,visibleSize.width/visibleSize.height,0.1,200);
|
||||
_camera->setCameraFlag(CameraFlag::USER1);
|
||||
addChild(_camera);
|
||||
|
||||
Terrain::TerrainData a("TerrainTest/heightmap16.jpg","TerrainTest/sand.jpg",Size(32,32),3);
|
||||
_terrain = Terrain::create(a);
|
||||
_terrain->setScale(20);
|
||||
|
||||
_terrain->setCameraMask(2);
|
||||
_terrain->setDrawWire(false);
|
||||
|
||||
_player = Player::create("Sprite3DTest/girl.c3b",_camera,_terrain);
|
||||
_player->setCameraMask(2);
|
||||
_player->setScale(0.08);
|
||||
_player->setPositionY(_terrain->getHeight(_player->getPositionX(),_player->getPositionZ())+PLAYER_HEIGHT);
|
||||
|
||||
auto animation = Animation3D::create("Sprite3DTest/girl.c3b","Take 001");
|
||||
if (animation)
|
||||
{
|
||||
auto animate = Animate3D::create(animation);
|
||||
_player->runAction(RepeatForever::create(animate));
|
||||
}
|
||||
|
||||
_camera->setPosition3D(_player->getPosition3D()+camera_offset);
|
||||
_camera->setRotation3D(Vec3(-45,0,0));
|
||||
|
||||
forward = Label::create("forward","arial",22);
|
||||
forward->setPosition(0,200);
|
||||
forward->setAnchorPoint(Vec2(0,0));
|
||||
|
||||
backward = Label::create("backward","arial",22);
|
||||
backward->setPosition(0,250);
|
||||
backward->setAnchorPoint(Vec2(0,0));
|
||||
|
||||
left = Label::create("turn Left","arial",22);
|
||||
left->setPosition(0,100);
|
||||
left->setAnchorPoint(Vec2(0,0));
|
||||
|
||||
right = Label::create("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)
|
||||
{
|
||||
}
|
||||
|
||||
void TerrainWalkThru::onTouchesEnd(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event* event)
|
||||
{
|
||||
auto touch = touches[0];
|
||||
auto location = touch->getLocationInView();
|
||||
if(_camera)
|
||||
{
|
||||
if(_player)
|
||||
{
|
||||
Vec3 nearP(location.x, location.y, 0.0f), farP(location.x, location.y, 1.0f);
|
||||
|
||||
auto size = Director::getInstance()->getWinSize();
|
||||
_camera->unproject(size, &nearP, &nearP);
|
||||
_camera->unproject(size, &farP, &farP);
|
||||
Vec3 dir = farP - nearP;
|
||||
dir.normalize();
|
||||
Vec3 rayStep = 15*dir;
|
||||
Vec3 rayPos = nearP;
|
||||
Vec3 rayStartPosition = nearP;
|
||||
Vec3 lastRayPosition =rayPos;
|
||||
rayPos += rayStep;
|
||||
// Linear search - Loop until find a point inside and outside the terrain Vector3
|
||||
float height = _terrain->getHeight(rayPos.x,rayPos.z);
|
||||
|
||||
while (rayPos.y > height)
|
||||
{
|
||||
lastRayPosition = rayPos;
|
||||
rayPos += rayStep;
|
||||
height = _terrain->getHeight(rayPos.x,rayPos.z);
|
||||
}
|
||||
|
||||
Vec3 startPosition = lastRayPosition;
|
||||
Vec3 endPosition = rayPos;
|
||||
|
||||
for (int i= 0; i< 32; i++)
|
||||
{
|
||||
// Binary search pass
|
||||
Vec3 middlePoint = (startPosition + endPosition) * 0.5f;
|
||||
if (middlePoint.y < height)
|
||||
endPosition = middlePoint;
|
||||
else
|
||||
startPosition = middlePoint;
|
||||
}
|
||||
Vec3 collisionPoint = (startPosition + endPosition) * 0.5f;
|
||||
dir = collisionPoint - _player->getPosition3D();
|
||||
dir.y = 0;
|
||||
dir.normalize();
|
||||
_player->_headingAngle = -1*acos(dir.dot(Vec3(0,0,-1)));
|
||||
dir.cross(dir,Vec3(0,0,-1),&_player->_headingAxis);
|
||||
_player->_targetPos=collisionPoint;
|
||||
_player->forward();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool Player::isDone() const
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
void Player::update(float dt)
|
||||
{
|
||||
auto player = (Sprite3D *)this;
|
||||
switch (_playerState)
|
||||
{
|
||||
case PLAYER_STATE_IDLE:
|
||||
break;
|
||||
case PLAYER_STATE_FORWARD:
|
||||
{
|
||||
Vec3 curPos= player->getPosition3D();
|
||||
Vec3 newFaceDir = _targetPos - curPos;
|
||||
newFaceDir.y = 0.0f;
|
||||
newFaceDir.normalize();
|
||||
Vec3 offset = newFaceDir * 25.0f * dt;
|
||||
curPos+=offset;
|
||||
player->setPosition3D(curPos);
|
||||
}
|
||||
break;
|
||||
case PLAYER_STATE_BACKWARD:
|
||||
{
|
||||
Vec3 forward_vec;
|
||||
player->getNodeToWorldTransform().getForwardVector(&forward_vec);
|
||||
forward_vec.normalize();
|
||||
player->setPosition3D(player->getPosition3D()-forward_vec*15*dt);
|
||||
}
|
||||
break;
|
||||
case PLAYER_STATE_LEFT:
|
||||
{
|
||||
player->setRotation3D(player->getRotation3D()+Vec3(0,25*dt,0));
|
||||
}
|
||||
break;
|
||||
case PLAYER_STATE_RIGHT:
|
||||
{
|
||||
player->setRotation3D(player->getRotation3D()+Vec3(0,-25*dt,0));
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
Vec3 Normal;
|
||||
float player_h = _terrain->getHeight(player->getPositionX(),player->getPositionZ(),&Normal);
|
||||
float dot_product = Normal.dot(Vec3(0,1,0));
|
||||
|
||||
player->setPositionY(player_h+PLAYER_HEIGHT);
|
||||
Quaternion q2;
|
||||
q2.createFromAxisAngle(Vec3(0,1,0),-M_PI,&q2);
|
||||
|
||||
Quaternion headingQ;
|
||||
headingQ.createFromAxisAngle(_headingAxis,_headingAngle,&headingQ);
|
||||
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();
|
||||
}
|
||||
|
||||
void Player::turnLeft()
|
||||
{
|
||||
_playerState = PLAYER_STATE_LEFT;
|
||||
}
|
||||
|
||||
void Player::turnRight()
|
||||
{
|
||||
_playerState = PLAYER_STATE_RIGHT;
|
||||
}
|
||||
|
||||
void Player::idle()
|
||||
{
|
||||
_playerState = PLAYER_STATE_IDLE;
|
||||
}
|
||||
|
||||
|
||||
void Player::forward()
|
||||
{
|
||||
_playerState = PLAYER_STATE_FORWARD;
|
||||
}
|
||||
|
||||
void Player::backward()
|
||||
{
|
||||
_playerState = PLAYER_STATE_BACKWARD;
|
||||
}
|
||||
|
||||
void Player::updateState()
|
||||
{
|
||||
auto player = (Sprite3D * )this;
|
||||
switch (_playerState)
|
||||
{
|
||||
case PLAYER_STATE_FORWARD:
|
||||
{
|
||||
Vec2 player_pos =Vec2(player->getPositionX(),player->getPositionZ());
|
||||
Vec2 targetPos = Vec2(_targetPos.x,_targetPos.z);
|
||||
auto dist = player_pos.distance(targetPos);
|
||||
if(dist<1)
|
||||
{
|
||||
_playerState = PLAYER_STATE_IDLE;
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Player * Player::create(const char * file,Camera * cam,Terrain * terrain)
|
||||
{
|
||||
//
|
||||
auto sprite = new (std::nothrow) Player();
|
||||
if (sprite && sprite->initWithFile(file))
|
||||
{
|
||||
sprite->_headingAngle = 0;
|
||||
sprite->_playerState = PLAYER_STATE_IDLE;
|
||||
sprite->_cam = cam;
|
||||
sprite->_terrain = terrain;
|
||||
sprite->autorelease();
|
||||
sprite->scheduleUpdate();
|
||||
return sprite;
|
||||
}
|
||||
CC_SAFE_DELETE(sprite);
|
||||
return nullptr;
|
||||
}
|
|
@ -0,0 +1,99 @@
|
|||
#ifndef TERRAIN_TESH_H
|
||||
#include "../testBasic.h"
|
||||
#include "../BaseTest.h"
|
||||
|
||||
#include "3d/CCSprite3D.h"
|
||||
#include "3d/CCTerrain.h"
|
||||
#include "2d/CCCamera.h"
|
||||
#include "2d/CCAction.h"
|
||||
class TerrainTestScene : public TestScene
|
||||
{
|
||||
public:
|
||||
virtual void runThisTest();
|
||||
};
|
||||
|
||||
class TerrainTestDemo : public BaseTest
|
||||
{
|
||||
public:
|
||||
TerrainTestDemo(void);
|
||||
virtual ~TerrainTestDemo(void);
|
||||
|
||||
void restartCallback(Ref* sender);
|
||||
void nextCallback(Ref* sender);
|
||||
void backCallback(Ref* sender);
|
||||
|
||||
// overrides
|
||||
virtual std::string title() const override;
|
||||
virtual std::string subtitle() const override;
|
||||
virtual void onEnter() override;
|
||||
};
|
||||
|
||||
|
||||
class TerrainSimple : public TerrainTestDemo
|
||||
{
|
||||
public:
|
||||
CREATE_FUNC(TerrainSimple);
|
||||
TerrainSimple();
|
||||
virtual std::string title() const override;
|
||||
virtual std::string subtitle() const override;
|
||||
void onTouchesMoved(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event* event);
|
||||
|
||||
void on127x127_1DetailMap32x32Callback(Ref* sender);
|
||||
void on257x257_1DetailMap32x32Callback(Ref* sender);
|
||||
void onEnableLODCallback(Ref* sender);
|
||||
void onDisableLODCallback(Ref* sender);
|
||||
Terrain * _terrain;
|
||||
protected:
|
||||
Camera * _camera;
|
||||
};
|
||||
|
||||
#define PLAYER_STATE_LEFT 0
|
||||
#define PLAYER_STATE_RIGHT 1
|
||||
#define PLAYER_STATE_IDLE 2
|
||||
#define PLAYER_STATE_FORWARD 3
|
||||
#define PLAYER_STATE_BACKWARD 4
|
||||
|
||||
class Player : public Sprite3D
|
||||
{
|
||||
public:
|
||||
static Player * create(const char * file,Camera * cam,Terrain * terrain);
|
||||
virtual bool isDone() const;
|
||||
virtual void update(float dt);
|
||||
|
||||
void turnLeft();
|
||||
void turnRight();
|
||||
void forward();
|
||||
void backward();
|
||||
void idle();
|
||||
cocos2d::Vec3 _targetPos;
|
||||
void updateState();
|
||||
float _headingAngle;
|
||||
Vec3 _headingAxis;
|
||||
private:
|
||||
Terrain * _terrain;
|
||||
Camera * _cam;
|
||||
int _playerState;
|
||||
};
|
||||
|
||||
|
||||
class TerrainWalkThru : public TerrainTestDemo
|
||||
{
|
||||
public:
|
||||
CREATE_FUNC(TerrainWalkThru);
|
||||
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;
|
||||
};
|
||||
|
||||
#endif // !TERRAIN_TESH_H
|
|
@ -80,6 +80,7 @@ Controller g_aTestNames[] = {
|
|||
{ "Node: Spine", []() { return new SpineTestScene(); } },
|
||||
{ "Node: Sprite", [](){return new SpriteTestScene(); } },
|
||||
{ "Node: Sprite3D", [](){ return new Sprite3DTestScene(); }},
|
||||
{ "Terrain Test", [](){ return new TerrainTestScene(); }},
|
||||
{ "Node: TileMap", [](){return new TileMapTestScene(); } },
|
||||
{ "Node: FastTileMap", [](){return new TileMapTestSceneNew(); } },
|
||||
{ "Node: Text Input", [](){return new TextInputTestScene(); } },
|
||||
|
|
|
@ -74,6 +74,7 @@
|
|||
#include "ReleasePoolTest/ReleasePoolTest.h"
|
||||
|
||||
#include "Sprite3DTest/Sprite3DTest.h"
|
||||
#include "TerrainTest/TerrainTest.h"
|
||||
#include "Camera3DTest/Camera3DTest.h"
|
||||
#include "BillBoardTest/BillBoardTest.h"
|
||||
#include "LightTest/LightTest.h"
|
||||
|
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
After Width: | Height: | Size: 14 KiB |
Binary file not shown.
Binary file not shown.
After Width: | Height: | Size: 6.1 KiB |
Binary file not shown.
After Width: | Height: | Size: 13 KiB |
Binary file not shown.
Binary file not shown.
After Width: | Height: | Size: 22 KiB |
|
@ -194,7 +194,8 @@ LOCAL_SRC_FILES := main.cpp \
|
|||
../../Classes/UITest/UITest.cpp \
|
||||
../../Classes/UserDefaultTest/UserDefaultTest.cpp \
|
||||
../../Classes/OpenURLTest/OpenURLTest.cpp \
|
||||
../../Classes/ZwoptexTest/ZwoptexTest.cpp
|
||||
../../Classes/ZwoptexTest/ZwoptexTest.cpp \
|
||||
../../Classes/TerrainTest/TerrainTest.cpp
|
||||
|
||||
LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes \
|
||||
$(LOCAL_PATH)/../../../..
|
||||
|
|
|
@ -187,6 +187,7 @@
|
|||
<ClCompile Include="..\Classes\SpineTest\SpineTest.cpp" />
|
||||
<ClCompile Include="..\Classes\Sprite3DTest\DrawNode3D.cpp" />
|
||||
<ClCompile Include="..\Classes\Sprite3DTest\Sprite3DTest.cpp" />
|
||||
<ClCompile Include="..\Classes\TerrainTest\TerrainTest.cpp" />
|
||||
<ClCompile Include="..\Classes\TexturePackerEncryptionTest\TextureAtlasEncryptionTest.cpp" />
|
||||
<ClCompile Include="..\Classes\TileMapTest\TileMapTest2.cpp" />
|
||||
<ClCompile Include="..\Classes\UITest\CocoStudioGUITest\CocosGUIScene.cpp" />
|
||||
|
@ -384,6 +385,7 @@
|
|||
<ClInclude Include="..\Classes\SpineTest\SpineTest.h" />
|
||||
<ClInclude Include="..\Classes\Sprite3DTest\DrawNode3D.h" />
|
||||
<ClInclude Include="..\Classes\Sprite3DTest\Sprite3DTest.h" />
|
||||
<ClInclude Include="..\Classes\TerrainTest\TerrainTest.h" />
|
||||
<ClInclude Include="..\Classes\TexturePackerEncryptionTest\TextureAtlasEncryptionTest.h" />
|
||||
<ClInclude Include="..\Classes\TileMapTest\TileMapTest2.h" />
|
||||
<ClInclude Include="..\Classes\UITest\CocoStudioGUITest\CocosGUIScene.h" />
|
||||
|
|
|
@ -346,6 +346,9 @@
|
|||
<Filter Include="Classes\AllocatorTest">
|
||||
<UniqueIdentifier>{eed1887a-757e-4625-b21c-bbfcfaa5200f}</UniqueIdentifier>
|
||||
</Filter>
|
||||
<Filter Include="Classes\TerrainTest">
|
||||
<UniqueIdentifier>{3f5069e6-78e5-4388-b614-04c0ea943b4c}</UniqueIdentifier>
|
||||
</Filter>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="main.cpp">
|
||||
|
@ -909,6 +912,9 @@
|
|||
<ClCompile Include="..\Classes\AllocatorTest\AllocatorTest.cpp">
|
||||
<Filter>Classes\AllocatorTest</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\Classes\TerrainTest\TerrainTest.cpp">
|
||||
<Filter>Classes\TerrainTest</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="main.h">
|
||||
|
@ -1670,5 +1676,8 @@
|
|||
<ClInclude Include="..\Classes\AllocatorTest\AllocatorTest.h">
|
||||
<Filter>Classes\AllocatorTest</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\Classes\TerrainTest\TerrainTest.h">
|
||||
<Filter>Classes\TerrainTest</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</Project>
|
Loading…
Reference in New Issue