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">
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -1,26 +1,26 @@
|
|||
/****************************************************************************
|
||||
Copyright (c) 2014 Chukong Technologies Inc.
|
||||
Copyright (c) 2015 Chukong Technologies Inc.
|
||||
|
||||
http://www.cocos2d-x.org
|
||||
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:
|
||||
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 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.
|
||||
****************************************************************************/
|
||||
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.
|
||||
****************************************************************************/
|
||||
#ifndef CC_TERRAIN_H
|
||||
#define CC_TERRAIN_H
|
||||
|
||||
|
@ -29,27 +29,61 @@
|
|||
#include "renderer/CCCustomCommand.h"
|
||||
#include "3d/CCAABB.h"
|
||||
#include "2d/CCCamera.h"
|
||||
#include "3d/CCRay.h"
|
||||
#include <vector>
|
||||
NS_CC_BEGIN
|
||||
/*
|
||||
*the maximum amount of the chunkes
|
||||
**/
|
||||
/*
|
||||
* the maximum amount of the chunkes
|
||||
**/
|
||||
#define MAX_CHUNKES 256
|
||||
|
||||
/*
|
||||
*Terrain
|
||||
*use to render outdoor or large scene via heightMap
|
||||
**/
|
||||
|
||||
/*
|
||||
* 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;
|
||||
* 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:
|
||||
|
||||
/*
|
||||
*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*/
|
||||
/*
|
||||
*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*/
|
||||
DetailMap();
|
||||
DetailMap(const char * detailMapSrc , float size = 35);
|
||||
/*detail Image source file path*/
|
||||
|
@ -58,20 +92,20 @@ public:
|
|||
float detailMapSize;
|
||||
};
|
||||
|
||||
/*
|
||||
*TerrainData
|
||||
*This TerrainData struct warp all parameter that Terrain need to create
|
||||
*/
|
||||
struct CC_DLL TerrainData
|
||||
/*
|
||||
*TerrainData
|
||||
*This TerrainData struct warp all parameter that Terrain need to create
|
||||
*/
|
||||
struct CC_DLL TerrainData
|
||||
{
|
||||
/*Constructors*/
|
||||
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
|
||||
*/
|
||||
*deterimine the chunk size,chunk is the minimal subdivision of the Terrain
|
||||
*/
|
||||
Size chunkSize;
|
||||
/*height Map source path*/
|
||||
std::string heightMapSrc;
|
||||
|
@ -83,12 +117,13 @@ public:
|
|||
float mapHeight;
|
||||
/*terrain scale factor*/
|
||||
float mapScale;
|
||||
int _detailMapAmount;
|
||||
};
|
||||
private:
|
||||
|
||||
/*
|
||||
*terrain vertices internal data format
|
||||
**/
|
||||
/*
|
||||
*terrain vertices internal data format
|
||||
**/
|
||||
struct TerrainVertexData
|
||||
{
|
||||
/*constructor*/
|
||||
|
@ -104,18 +139,19 @@ private:
|
|||
cocos2d::Vec3 normal;
|
||||
};
|
||||
|
||||
/*
|
||||
*the terminal node of quad, use to subdivision terrain mesh and LOD
|
||||
**/
|
||||
/*
|
||||
*the terminal node of quad, use to subdivision terrain mesh and LOD
|
||||
**/
|
||||
struct Chunk
|
||||
{
|
||||
/*Constructor*/
|
||||
Chunk();
|
||||
~Chunk();
|
||||
/*vertices*/
|
||||
std::vector<TerrainVertexData> vertices;
|
||||
/*LOD indices*/
|
||||
struct LOD{
|
||||
std::vector<GLushort> indices;
|
||||
std::vector<GLushort> indices;
|
||||
};
|
||||
GLuint vbo[2];
|
||||
/*we now have four levels of detail*/
|
||||
|
@ -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*/
|
||||
|
@ -157,10 +192,10 @@ private:
|
|||
std::vector<TerrainVertexData> vertices_tmp;
|
||||
};
|
||||
|
||||
/*
|
||||
*QuadTree
|
||||
*use to frustum culling and set LOD
|
||||
**/
|
||||
/*
|
||||
*QuadTree
|
||||
*use to frustum culling and set LOD
|
||||
**/
|
||||
struct QuadTree
|
||||
{
|
||||
QuadTree(int x,int y,int width,int height,Terrain * terrain);
|
||||
|
@ -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