diff --git a/cocos/3d/CCTerrain.cpp b/cocos/3d/CCTerrain.cpp index 732cf8bcf8..7f124cb72e 100644 --- a/cocos/3d/CCTerrain.cpp +++ b/cocos/3d/CCTerrain.cpp @@ -36,6 +36,7 @@ USING_NS_CC; #include "base/CCDirector.h" #include "2d/CCCamera.h" +#include "base/CCEventType.h" NS_CC_BEGIN @@ -51,16 +52,16 @@ static bool isPOT(int number) Terrain * Terrain::create(TerrainData ¶meter, CrackFixedType fixedType) { Terrain * terrain = new (std::nothrow)Terrain(); - terrain->setSkirtHeightRatio(parameter.skirtHeightRatio); + terrain->setSkirtHeightRatio(parameter._skirtHeightRatio); terrain->_terrainData = parameter; terrain->_crackFixedType = fixedType; terrain->_isCameraViewChanged = true; //chunksize - terrain->_chunkSize = parameter.chunkSize; + terrain->_chunkSize = parameter._chunkSize; bool initResult =true; //init heightmap - initResult &= terrain->initHeightMap(parameter.heightMapSrc.c_str()); + initResult &= terrain->initHeightMap(parameter._heightMapSrc.c_str()); //init textures alpha map,detail Maps initResult &= terrain->initTextures(); initResult &= terrain->initProperties(); @@ -76,9 +77,8 @@ bool Terrain::initProperties() { auto shader = GLProgramCache::getInstance()->getGLProgram(GLProgram::SHADER_3D_TERRAIN); auto state = GLProgramState::create(shader); - + setGLProgramState(state); - _normalLocation = glGetAttribLocation(this->getGLProgram()->getProgram(),"a_normal"); setDrawWire(false); setIsEnableFrustumCull(true); setAnchorPoint(Vec2(0,0)); @@ -95,10 +95,21 @@ void Terrain::onDraw(const Mat4 &transform, uint32_t flags) { auto glProgram = getGLProgram(); glProgram->use(); + GL::enableVertexAttribs(1<<_positionLocation | 1 << _texcordLocation | 1<<_normalLocation); glProgram->setUniformsForBuiltins(transform); - glDepthMask(GL_TRUE); + GLboolean depthMaskCheck; + glGetBooleanv(GL_DEPTH_WRITEMASK, &depthMaskCheck); + if(!depthMaskCheck) + { + glDepthMask(GL_TRUE); + } + GLboolean CullFaceCheck =glIsEnabled(GL_CULL_FACE); + if(!CullFaceCheck) + { + glEnable(GL_CULL_FACE); + } GLboolean depthTestCheck; - glGetBooleanv(GL_DEPTH_TEST,&depthTestCheck); + depthTestCheck = glIsEnabled(GL_DEPTH_TEST); if(!depthTestCheck) { glEnable(GL_DEPTH_TEST); @@ -123,7 +134,7 @@ void Terrain::onDraw(const Mat4 &transform, uint32_t flags) glBindTexture(GL_TEXTURE_2D,_detailMapTextures[i]->getName()); glUniform1i(_detailMapLocation[i],i); - glUniform1f(_detailMapSizeLocation[i],_terrainData.detailMaps[i].detailMapSize); + glUniform1f(_detailMapSizeLocation[i],_terrainData._detailMaps[i]._detailMapSize); } glUniform1i(_alphaIsHasAlphaMapLocation,1); @@ -163,11 +174,22 @@ void Terrain::onDraw(const Mat4 &transform, uint32_t flags) glActiveTexture(GL_TEXTURE0); if(depthTestCheck) { - glEnable(GL_DEPTH_TEST); }else { glDisable(GL_DEPTH_TEST); } + if(depthMaskCheck) + { + }else + { + glDepthMask(GL_FALSE); + } + if(CullFaceCheck) + { + }else + { + glEnable(GL_CULL_FACE); + } if(blendCheck) { glEnable(GL_BLEND); @@ -226,6 +248,15 @@ bool Terrain::initHeightMap(const char * heightMap) Terrain::Terrain() { _alphaMap = nullptr; +//#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WP8 || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) + auto _backToForegroundListener = EventListenerCustom::create(EVENT_RENDERER_RECREATED, + [this](EventCustom*) + { + reload(); + } + ); + Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_backToForegroundListener, -1); +//#endif } void Terrain::setChunksLOD(Vec3 cameraPos) @@ -255,14 +286,14 @@ 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*_imageHeight/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); Vec2 to_tl = pos - tl; //real size - Vec2 size = Vec2(_imageWidth*_terrainData.mapScale,_imageHeight*_terrainData.mapScale); + Vec2 size = Vec2(_imageWidth*_terrainData._mapScale,_imageHeight*_terrainData._mapScale); result = getNodeToWorldTransform()*Vec4(size.x,0.0f,size.y,0.0f); size = Vec2(result.x,result.z); @@ -321,7 +352,7 @@ float Terrain::getImageHeight(int pixel_x,int pixel_y) default: break; } - return _data[(pixel_y*_imageWidth+pixel_x)*byte_stride]*1.0/255*_terrainData.mapHeight -0.5*_terrainData.mapHeight; + return _data[(pixel_y*_imageWidth+pixel_x)*byte_stride]*1.0/255*_terrainData._mapHeight -0.5*_terrainData._mapHeight; } void Terrain::loadVertices() @@ -334,10 +365,10 @@ void Terrain::loadVertices() { float height = getImageHeight(j,i); TerrainVertexData v; - v.position = Vec3(j*_terrainData.mapScale- _imageWidth/2*_terrainData.mapScale, //x + v._position = Vec3(j*_terrainData._mapScale- _imageWidth/2*_terrainData._mapScale, //x height, //y - i*_terrainData.mapScale - _imageHeight/2*_terrainData.mapScale);//z - v.texcoord = Tex2F(j*1.0/_imageWidth,i*1.0/_imageHeight); + i*_terrainData._mapScale - _imageHeight/2*_terrainData._mapScale);//z + v._texcoord = Tex2F(j*1.0/_imageWidth,i*1.0/_imageHeight); _vertices.push_back (v); //update the min & max height; @@ -370,18 +401,18 @@ void Terrain::calculateNormal() unsigned int Index0 = _indices[i]; unsigned int Index1 = _indices[i + 1]; unsigned int Index2 = _indices[i + 2]; - Vec3 v1 = _vertices[Index1].position - _vertices[Index0].position; - Vec3 v2 = _vertices[Index2].position - _vertices[Index0].position; + Vec3 v1 = _vertices[Index1]._position - _vertices[Index0]._position; + Vec3 v2 = _vertices[Index2]._position - _vertices[Index0]._position; Vec3 Normal; Vec3::cross(v1,v2,&Normal); Normal.normalize(); - _vertices[Index0].normal += Normal; - _vertices[Index1].normal += Normal; - _vertices[Index2].normal += Normal; + _vertices[Index0]._normal += Normal; + _vertices[Index1]._normal += Normal; + _vertices[Index2]._normal += Normal; } for (unsigned int i = 0 ; i < _vertices.size() ; i++) { - _vertices[i].normal.normalize(); + _vertices[i]._normal.normalize(); } //global indices no need at all _indices.clear(); @@ -429,14 +460,17 @@ Terrain::~Terrain() for(int i =0;i<_chunkLodIndicesSet.size();i++) { - glDeleteBuffers(1,&(_chunkLodIndicesSet[i]._chunkIndices.indices)); + glDeleteBuffers(1,&(_chunkLodIndicesSet[i]._chunkIndices._indices)); } for(int i =0;i<_chunkLodIndicesSkirtSet.size();i++) { - glDeleteBuffers(1,&(_chunkLodIndicesSkirtSet[i]._chunkIndices.indices)); + glDeleteBuffers(1,&(_chunkLodIndicesSkirtSet[i]._chunkIndices._indices)); } +#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WP8 || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) + Director::getInstance()->getEventDispatcher()->removeEventListener(_backToForegroundListener); +#endif } cocos2d::Vec3 Terrain::getNormal(int pixel_x, int pixel_y) @@ -457,7 +491,7 @@ cocos2d::Vec3 Terrain::getIntersectionPoint(const Ray & ray) { Vec3 dir = ray._direction; dir.normalize(); - Vec3 rayStep = _terrainData.chunkSize.width*0.25*dir; + Vec3 rayStep = _terrainData._chunkSize.width*0.25*dir; Vec3 rayPos = ray._origin; Vec3 rayStartPosition = ray._origin; Vec3 lastRayPosition =rayPos; @@ -498,14 +532,14 @@ cocos2d::Vec2 Terrain::convertToTerrainSpace(Vec2 worldSpaceXZ) Vec2 pos = Vec2(worldSpaceXZ.x,worldSpaceXZ.y); //top-left - Vec2 tl = Vec2(-1*_terrainData.mapScale*_imageWidth/2,-1*_terrainData.mapScale*_imageHeight/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); Vec2 to_tl = pos - tl; //real size - Vec2 size = Vec2(_imageWidth*_terrainData.mapScale,_imageHeight*_terrainData.mapScale); + Vec2 size = Vec2(_imageWidth*_terrainData._mapScale,_imageHeight*_terrainData._mapScale); result = getNodeToWorldTransform()*Vec4(size.x,0.0f,size.y,0.0f); size = Vec2(result.x,result.z); @@ -568,7 +602,7 @@ void Terrain::setDetailMap(unsigned int index, DetailMap detailMap) { CCLOG("invalid DetailMap index %d\n",index); } - _terrainData.detailMaps[index] = detailMap; + _terrainData._detailMaps[index] = detailMap; if(_detailMapTextures[index]) { @@ -576,16 +610,17 @@ void Terrain::setDetailMap(unsigned int index, DetailMap detailMap) } _detailMapTextures[index] = new (std::nothrow)Texture2D(); auto textImage = new (std::nothrow)Image(); - textImage->initWithImageFile(detailMap.detailMapSrc); + textImage->initWithImageFile(detailMap._detailMapSrc); _detailMapTextures[index]->initWithImage(textImage); + delete textImage; } Terrain::ChunkIndices Terrain::lookForIndicesLOD(int neighborLod[4], int selfLod, bool * result) { (* result) =false; ChunkIndices tmp; - tmp.indices = 0; - tmp.size = 0; + tmp._indices = 0; + tmp._size = 0; if(_chunkLodIndicesSet.empty()) { (* result) =false; @@ -597,7 +632,7 @@ Terrain::ChunkIndices Terrain::lookForIndicesLOD(int neighborLod[4], int selfLod test[4] = selfLod; for(int i =0;i<_chunkLodIndicesSet.size();i++) { - if(memcmp(test,_chunkLodIndicesSet[i].relativeLod,sizeof(test))==0) + if(memcmp(test,_chunkLodIndicesSet[i]._relativeLod,sizeof(test))==0) { (*result) = true; return _chunkLodIndicesSet[i]._chunkIndices; @@ -611,11 +646,11 @@ Terrain::ChunkIndices Terrain::lookForIndicesLOD(int neighborLod[4], int selfLod Terrain::ChunkIndices Terrain::insertIndicesLOD(int neighborLod[4], int selfLod, GLushort * indices,int size) { ChunkLODIndices lodIndices; - memcpy(lodIndices.relativeLod,neighborLod,sizeof(int [4])); - lodIndices.relativeLod[4] = selfLod; - lodIndices._chunkIndices.size = size; - glGenBuffers(1,&(lodIndices._chunkIndices.indices)); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, lodIndices._chunkIndices.indices); + memcpy(lodIndices._relativeLod,neighborLod,sizeof(int [4])); + lodIndices._relativeLod[4] = selfLod; + lodIndices._chunkIndices._size = size; + glGenBuffers(1,&(lodIndices._chunkIndices._indices)); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, lodIndices._chunkIndices._indices); glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(GLushort)*size,indices,GL_STATIC_DRAW); this->_chunkLodIndicesSet.push_back(lodIndices); return lodIndices._chunkIndices; @@ -624,8 +659,8 @@ Terrain::ChunkIndices Terrain::insertIndicesLOD(int neighborLod[4], int selfLod, Terrain::ChunkIndices Terrain::lookForIndicesLODSkrit(int selfLod, bool * result) { ChunkIndices badResult; - badResult.indices = 0; - badResult.size = 0; + badResult._indices = 0; + badResult._size = 0; if(this->_chunkLodIndicesSkirtSet.empty()) { (*result) = false; @@ -634,7 +669,7 @@ Terrain::ChunkIndices Terrain::lookForIndicesLODSkrit(int selfLod, bool * result for(int i =0;i<_chunkLodIndicesSkirtSet.size();i++) { - if(_chunkLodIndicesSkirtSet[i].selfLod == selfLod) + if(_chunkLodIndicesSkirtSet[i]._selfLod == selfLod) { (*result) = true; return _chunkLodIndicesSkirtSet[i]._chunkIndices; @@ -647,10 +682,10 @@ Terrain::ChunkIndices Terrain::lookForIndicesLODSkrit(int selfLod, bool * result Terrain::ChunkIndices Terrain::insertIndicesLODSkirt(int selfLod, GLushort * indices, int size) { ChunkLODIndicesSkirt skirtIndices; - skirtIndices.selfLod = selfLod; - skirtIndices._chunkIndices.size = size; - glGenBuffers(1,&(skirtIndices._chunkIndices.indices)); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, skirtIndices._chunkIndices.indices); + skirtIndices._selfLod = selfLod; + skirtIndices._chunkIndices._size = size; + glGenBuffers(1,&(skirtIndices._chunkIndices._indices)); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, skirtIndices._chunkIndices._indices); glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(GLushort)*size,indices,GL_STATIC_DRAW); this->_chunkLodIndicesSkirtSet.push_back(skirtIndices); return skirtIndices._chunkIndices; @@ -666,11 +701,15 @@ void Terrain::onEnter() Node::onEnter(); _terrainModelMatrix = getNodeToWorldTransform(); _quadRoot->preCalculateAABB(_terrainModelMatrix); - cacheUniformLocation(); + cacheUniformAttribLocation(); } -void Terrain::cacheUniformLocation() +void Terrain::cacheUniformAttribLocation() { + + _positionLocation = glGetAttribLocation(this->getGLProgram()->getProgram(),"a_position"); + _texcordLocation = glGetAttribLocation(this->getGLProgram()->getProgram(),"a_texCoord"); + _normalLocation = glGetAttribLocation(this->getGLProgram()->getProgram(),"a_normal"); _alphaMapLocation = -1; for(int i =0;i<4;i++) { @@ -703,10 +742,10 @@ bool Terrain::initTextures() Texture2D::TexParams texParam; texParam.wrapS = GL_REPEAT; texParam.wrapT = GL_REPEAT; - if(!_terrainData.alphaMapSrc) + if(!_terrainData._alphaMapSrc) { auto textImage = new (std::nothrow)Image(); - textImage->initWithImageFile(_terrainData.detailMaps[0].detailMapSrc); + textImage->initWithImageFile(_terrainData._detailMaps[0]._detailMapSrc); auto texture = new (std::nothrow)Texture2D(); texture->initWithImage(textImage); texture->generateMipmap(); @@ -719,7 +758,7 @@ bool Terrain::initTextures() { //alpha map auto image = new (std::nothrow)Image(); - image->initWithImageFile(_terrainData.alphaMapSrc); + image->initWithImageFile(_terrainData._alphaMapSrc); _alphaMap = new (std::nothrow)Texture2D(); _alphaMap->initWithImage(image); texParam.wrapS = GL_CLAMP_TO_EDGE; @@ -732,7 +771,7 @@ bool Terrain::initTextures() for(int i =0;i<_terrainData._detailMapAmount;i++) { auto textImage = new (std::nothrow)Image(); - textImage->initWithImageFile(_terrainData.detailMaps[i].detailMapSrc); + textImage->initWithImageFile(_terrainData._detailMaps[i]._detailMapSrc); auto texture = new (std::nothrow)Texture2D(); texture->initWithImage(textImage); delete textImage; @@ -750,15 +789,23 @@ bool Terrain::initTextures() return true; } +void Terrain::reload() +{ + CCLOG("recreate"); + initTextures(); + _chunkLodIndicesSet.clear(); + _chunkLodIndicesSkirtSet.clear(); +} + 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 - glGenBuffers(2,vbo); + glGenBuffers(1,&_vbo); //only set for vertices vbo - glBindBuffer(GL_ARRAY_BUFFER, vbo[0]); - glBufferData(GL_ARRAY_BUFFER, sizeof(TerrainVertexData)*vertices.size(), &vertices[0], GL_STREAM_DRAW); + glBindBuffer(GL_ARRAY_BUFFER, _vbo); + glBufferData(GL_ARRAY_BUFFER, sizeof(TerrainVertexData)*_originalVertices.size(), &_originalVertices[0], GL_STREAM_DRAW); glBindBuffer(GL_ARRAY_BUFFER,0); @@ -769,7 +816,7 @@ void Terrain::Chunk::finish() int step = int(powf(2.0f, float(_currentLod))); int indicesAmount =(_terrain->_chunkSize.width/step+1)*(_terrain->_chunkSize.height/step+1)*6+(_terrain->_chunkSize.height/step)*3*2 +(_terrain->_chunkSize.width/step)*3*2; - _lod[i].indices.reserve(indicesAmount); + _lod[i]._indices.reserve(indicesAmount); } } @@ -785,7 +832,7 @@ void Terrain::Chunk::bindAndDraw() } #endif - glBindBuffer(GL_ARRAY_BUFFER, vbo[0]); + glBindBuffer(GL_ARRAY_BUFFER, _vbo); if(_terrain->_isCameraViewChanged) { switch (_terrain->_crackFixedType) @@ -802,8 +849,7 @@ void Terrain::Chunk::bindAndDraw() break; } } - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,_chunkIndices.indices); - GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_POSITION | GL::VERTEX_ATTRIB_FLAG_TEX_COORD| GL::VERTEX_ATTRIB_FLAG_NORMAL); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,_chunkIndices._indices); unsigned long offset = 0; //position glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, sizeof(TerrainVertexData), (GLvoid *)offset); @@ -811,11 +857,10 @@ void Terrain::Chunk::bindAndDraw() //texcoord glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD,2,GL_FLOAT,GL_FALSE,sizeof(TerrainVertexData),(GLvoid *)offset); offset +=sizeof(Tex2F); - glEnableVertexAttribArray(_terrain->_normalLocation); //normal glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_NORMAL,3,GL_FLOAT,GL_FALSE,sizeof(TerrainVertexData),(GLvoid *)offset); - glDrawElements(GL_TRIANGLES, (GLsizei)_chunkIndices.size, GL_UNSIGNED_SHORT, 0); - CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, _chunkIndices.size); + glDrawElements(GL_TRIANGLES, (GLsizei)_chunkIndices._size, GL_UNSIGNED_SHORT, 0); + CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, _chunkIndices._size); #if (CC_TARGET_PLATFORM == CC_PLATFORM_MAC) || (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_LINUX) glPolygonMode(GL_FRONT_AND_BACK,GL_FILL); #endif @@ -823,8 +868,8 @@ void Terrain::Chunk::bindAndDraw() void Terrain::Chunk::generate(int imgWidth, int imageHei, int m, int n, const unsigned char * data) { - pos_y = m; - pos_x = n; + _posY = m; + _posX = n; switch (_terrain->_crackFixedType) { case CrackFixedType::SKIRT: @@ -836,47 +881,47 @@ void Terrain::Chunk::generate(int imgWidth, int imageHei, int m, int n, const un { if(j>=imgWidth)break; auto v =_terrain->_vertices[i*imgWidth + j]; - vertices.push_back (v); + _originalVertices.push_back (v); } } // add four skirts - float skirtHeight = _terrain->_skirtRatio *_terrain->_terrainData.mapScale*int(powf(2.0f, float(3))); + float skirtHeight = _terrain->_skirtRatio *_terrain->_terrainData._mapScale*int(powf(2.0f, float(3))); //#1 - _terrain->_skirtVerticesOffset[0] = (int)vertices.size(); + _terrain->_skirtVerticesOffset[0] = (int)_originalVertices.size(); for(int i =_size.height*m;i<=_size.height*(m+1);i++) { auto v = _terrain->_vertices[i*imgWidth +_size.width*(n+1)]; - v.position.y -= skirtHeight; - vertices.push_back (v); + v._position.y -= skirtHeight; + _originalVertices.push_back (v); } //#2 - _terrain->_skirtVerticesOffset[1] = (int)vertices.size(); + _terrain->_skirtVerticesOffset[1] = (int)_originalVertices.size(); for(int j =_size.width*n;j<=_size.width*(n+1);j++) { auto v = _terrain->_vertices[_size.height*(m+1)*imgWidth + j]; - v.position.y -=skirtHeight; - vertices.push_back (v); + v._position.y -=skirtHeight; + _originalVertices.push_back (v); } //#3 - _terrain->_skirtVerticesOffset[2] = (int)vertices.size(); + _terrain->_skirtVerticesOffset[2] = (int)_originalVertices.size(); for(int i =_size.height*m;i<=_size.height*(m+1);i++) { auto v = _terrain->_vertices[i*imgWidth + _size.width*n]; - v.position.y -= skirtHeight; - vertices.push_back (v); + v._position.y -= skirtHeight; + _originalVertices.push_back (v); } //#4 - _terrain->_skirtVerticesOffset[3] = (int)vertices.size(); + _terrain->_skirtVerticesOffset[3] = (int)_originalVertices.size(); for(int j =_size.width*n;j<=_size.width*(n+1);j++) { auto v = _terrain->_vertices[_size.height*m*imgWidth+j]; - v.position.y -= skirtHeight; + v._position.y -= skirtHeight; //v.position.y = -5; - vertices.push_back (v); + _originalVertices.push_back (v); } } break; @@ -889,7 +934,7 @@ void Terrain::Chunk::generate(int imgWidth, int imageHei, int m, int n, const un { if(j>=imgWidth)break; auto v =_terrain->_vertices[i*imgWidth + j]; - vertices.push_back (v); + _originalVertices.push_back (v); } } } @@ -955,19 +1000,19 @@ void Terrain::Chunk::updateIndicesLOD() //need update indices. { //t-junction inner - _lod[_currentLod].indices.clear(); + _lod[_currentLod]._indices.clear(); for(int i =step;i_currentLod > _currentLod) start +=step; for(int i =start;i_currentLod > _currentLod) start +=step; for(int i =start;i_currentLod > _currentLod)//front { for(int i =0;i_currentLod > _currentLod)//back { for(int i =0;iinsertIndicesLOD(currentNeighborLOD,_currentLod,&_lod[_currentLod].indices[0],(int)_lod[_currentLod].indices.size()); + _chunkIndices = _terrain->insertIndicesLOD(currentNeighborLOD,_currentLod,&_lod[_currentLod]._indices[0],(int)_lod[_currentLod]._indices.size()); }else{ //No lod difference, use simple method - _lod[_currentLod].indices.clear(); + _lod[_currentLod]._indices.clear(); for(int i =0;iinsertIndicesLOD(currentNeighborLOD,_currentLod,&_lod[_currentLod].indices[0],(int)_lod[_currentLod].indices.size()); + _chunkIndices = _terrain->insertIndicesLOD(currentNeighborLOD,_currentLod,&_lod[_currentLod]._indices[0],(int)_lod[_currentLod]._indices.size()); } } void Terrain::Chunk::calculateAABB() { std::vectorpos; - for(int i =0;i highest.y) + if(_originalVertices[i]._position.y> highest.y) { - highest = vertices[i].position; + highest = _originalVertices[i]._position; } } auto a = Vec2(lowest.x,lowest.z); @@ -1156,7 +1201,7 @@ void Terrain::Chunk::calculateSlope() void Terrain::Chunk::updateVerticesForLOD() { if(_oldLod == _currentLod){ return;} // no need to update vertices - vertices_tmp = vertices; + _currentVertices = _originalVertices; int gridY = _size.height; int gridX = _size.width; @@ -1174,21 +1219,21 @@ void Terrain::Chunk::updateVerticesForLOD() for(int m = j-step/2;m_skirtVerticesOffset[1] +j); - _lod[_currentLod].indices.push_back (nLocIndex + step); + _lod[_currentLod]._indices.push_back (nLocIndex); + _lod[_currentLod]._indices.push_back (_terrain->_skirtVerticesOffset[1] +j); + _lod[_currentLod]._indices.push_back (nLocIndex + step); - _lod[_currentLod].indices.push_back (nLocIndex + step); - _lod[_currentLod].indices.push_back (_terrain->_skirtVerticesOffset[1] +j); - _lod[_currentLod].indices.push_back (_terrain->_skirtVerticesOffset[1] +j + step); + _lod[_currentLod]._indices.push_back (nLocIndex + step); + _lod[_currentLod]._indices.push_back (_terrain->_skirtVerticesOffset[1] +j); + _lod[_currentLod]._indices.push_back (_terrain->_skirtVerticesOffset[1] +j + step); } //#3 for(int i =0;i_skirtVerticesOffset[2]+i); - _lod[_currentLod].indices.push_back ((i+step)*(gridX+1)); + _lod[_currentLod]._indices.push_back (nLocIndex); + _lod[_currentLod]._indices.push_back (_terrain->_skirtVerticesOffset[2]+i); + _lod[_currentLod]._indices.push_back ((i+step)*(gridX+1)); - _lod[_currentLod].indices.push_back ((i+step)*(gridX+1)); - _lod[_currentLod].indices.push_back (_terrain->_skirtVerticesOffset[2]+i); - _lod[_currentLod].indices.push_back (_terrain->_skirtVerticesOffset[2]+i +step); + _lod[_currentLod]._indices.push_back ((i+step)*(gridX+1)); + _lod[_currentLod]._indices.push_back (_terrain->_skirtVerticesOffset[2]+i); + _lod[_currentLod]._indices.push_back (_terrain->_skirtVerticesOffset[2]+i +step); } //#4 for(int j =0;j_skirtVerticesOffset[3]+j); - _lod[_currentLod].indices.push_back (nLocIndex); + _lod[_currentLod]._indices.push_back (nLocIndex + step); + _lod[_currentLod]._indices.push_back (_terrain->_skirtVerticesOffset[3]+j); + _lod[_currentLod]._indices.push_back (nLocIndex); - _lod[_currentLod].indices.push_back (_terrain->_skirtVerticesOffset[3] + j + step); - _lod[_currentLod].indices.push_back (_terrain->_skirtVerticesOffset[3] +j); - _lod[_currentLod].indices.push_back (nLocIndex + step); + _lod[_currentLod]._indices.push_back (_terrain->_skirtVerticesOffset[3] + j + step); + _lod[_currentLod]._indices.push_back (_terrain->_skirtVerticesOffset[3] +j); + _lod[_currentLod]._indices.push_back (nLocIndex + step); } - _chunkIndices = _terrain->insertIndicesLODSkirt(_currentLod,&_lod[_currentLod].indices[0], (int)_lod[_currentLod].indices.size()); + _chunkIndices = _terrain->insertIndicesLODSkirt(_currentLod,&_lod[_currentLod]._indices[0], (int)_lod[_currentLod]._indices.size()); } Terrain::QuadTree::QuadTree(int x, int y, int w, int h, Terrain * terrain) @@ -1300,20 +1344,20 @@ Terrain::QuadTree::QuadTree(int x, int y, int w, int h, Terrain * terrain) this->_br = new QuadTree(x+_width/2,y+_height/2,_width/2,_height/2,terrain); this->_br->_parent = this; - _aabb.merge(_tl->_aabb); - _aabb.merge(_tr->_aabb); - _aabb.merge(_bl->_aabb); - _aabb.merge(_br->_aabb); + _localAABB.merge(_tl->_localAABB); + _localAABB.merge(_tr->_localAABB); + _localAABB.merge(_bl->_localAABB); + _localAABB.merge(_br->_localAABB); }else // is terminal Node { int m = _posY/terrain->_chunkSize.height; int n = _posX/terrain->_chunkSize.width; _chunk = terrain->_chunkesArray[m][n]; _isTerminal = true; - _aabb = _chunk->_aabb; + _localAABB = _chunk->_aabb; _chunk->_parent = this; } - _worldSpaceAABB = _aabb; + _worldSpaceAABB = _localAABB; _worldSpaceAABB.transform(_terrain->getNodeToWorldTransform()); } @@ -1362,7 +1406,7 @@ void Terrain::QuadTree::cullByCamera(const Camera * camera, const Mat4 & worldTr void Terrain::QuadTree::preCalculateAABB(const Mat4 & worldTransform) { - _worldSpaceAABB = _aabb; + _worldSpaceAABB = _localAABB; _worldSpaceAABB.transform(worldTransform); if(!_isTerminal){ _tl->preCalculateAABB(worldTransform); @@ -1382,43 +1426,43 @@ Terrain::QuadTree::~QuadTree() Terrain::TerrainData::TerrainData(const char * heightMapsrc , const char * textureSrc, const Size & chunksize, float height, float scale) { - this->heightMapSrc = heightMapsrc; - this->detailMaps[0].detailMapSrc = textureSrc; - this->alphaMapSrc = nullptr; - this->chunkSize = chunksize; - this->mapHeight = height; - this->mapScale = scale; - skirtHeightRatio = 1; + this->_heightMapSrc = heightMapsrc; + this->_detailMaps[0]._detailMapSrc = textureSrc; + this->_alphaMapSrc = nullptr; + this->_chunkSize = chunksize; + this->_mapHeight = height; + this->_mapScale = scale; + _skirtHeightRatio = 1; } 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 height, float scale) { - this->heightMapSrc = heightMapsrc; - this->alphaMapSrc = const_cast(alphamap); - this->detailMaps[0] = detail1; - this->detailMaps[1] = detail2; - this->detailMaps[2] = detail3; - this->detailMaps[3] = detail4; - this->chunkSize = chunksize; - this->mapHeight = height; - this->mapScale = scale; + this->_heightMapSrc = heightMapsrc; + this->_alphaMapSrc = const_cast(alphamap); + this->_detailMaps[0] = detail1; + this->_detailMaps[1] = detail2; + this->_detailMaps[2] = detail3; + this->_detailMaps[3] = detail4; + this->_chunkSize = chunksize; + this->_mapHeight = height; + this->_mapScale = scale; _detailMapAmount = 4; - skirtHeightRatio = 1; + _skirtHeightRatio = 1; } 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 height /*= 2*/, float scale /*= 0.1*/) { - this->heightMapSrc = heightMapsrc; - this->alphaMapSrc = const_cast(alphamap); - this->detailMaps[0] = detail1; - this->detailMaps[1] = detail2; - this->detailMaps[2] = detail3; - this->detailMaps[3] = nullptr; - this->chunkSize = chunksize; - this->mapHeight = height; - this->mapScale = scale; + this->_heightMapSrc = heightMapsrc; + this->_alphaMapSrc = const_cast(alphamap); + this->_detailMaps[0] = detail1; + this->_detailMaps[1] = detail2; + this->_detailMaps[2] = detail3; + this->_detailMaps[3] = nullptr; + this->_chunkSize = chunksize; + this->_mapHeight = height; + this->_mapScale = scale; _detailMapAmount = 3; - skirtHeightRatio = 1; + _skirtHeightRatio = 1; } Terrain::TerrainData::TerrainData() @@ -1428,13 +1472,13 @@ Terrain::TerrainData::TerrainData() Terrain::DetailMap::DetailMap(const char * detailMapPath, float size /*= 35*/) { - this->detailMapSrc = detailMapPath; - this->detailMapSize = size; + this->_detailMapSrc = detailMapPath; + this->_detailMapSize = size; } Terrain::DetailMap::DetailMap() { - detailMapSrc = ""; - detailMapSize = 35; + _detailMapSrc = ""; + _detailMapSize = 35; } NS_CC_END diff --git a/cocos/3d/CCTerrain.h b/cocos/3d/CCTerrain.h index aacd6c761d..c19685dd21 100644 --- a/cocos/3d/CCTerrain.h +++ b/cocos/3d/CCTerrain.h @@ -31,7 +31,8 @@ THE SOFTWARE. #include "3d/CCAABB.h" #include "3d/CCRay.h" #include - +#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 vertices; + std::vector _originalVertices; /*LOD indices*/ struct LOD{ - std::vector indices; + std::vector _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 vertices_tmp; + std::vector _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 _chunkLodIndicesSet; std::vector _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 diff --git a/cocos/scripting/lua-bindings/manual/3d/lua_cocos2dx_3d_manual.cpp b/cocos/scripting/lua-bindings/manual/3d/lua_cocos2dx_3d_manual.cpp index 34d17b1a82..fc771dd3e0 100644 --- a/cocos/scripting/lua-bindings/manual/3d/lua_cocos2dx_3d_manual.cpp +++ b/cocos/scripting/lua-bindings/manual/3d/lua_cocos2dx_3d_manual.cpp @@ -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(tolua_tocppstring(L, -1, "")); + outValue->_alphaMapSrc = const_cast(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); } diff --git a/tests/cpp-tests/Classes/TerrainTest/TerrainTest.cpp b/tests/cpp-tests/Classes/TerrainTest/TerrainTest.cpp index 1f433cbb6d..d2dcbc4acc 100644 --- a/tests/cpp-tests/Classes/TerrainTest/TerrainTest.cpp +++ b/tests/cpp-tests/Classes/TerrainTest/TerrainTest.cpp @@ -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& 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& 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& 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(); } diff --git a/tests/cpp-tests/Classes/TerrainTest/TerrainTest.h b/tests/cpp-tests/Classes/TerrainTest/TerrainTest.h index b013677744..65f05c1530 100644 --- a/tests/cpp-tests/Classes/TerrainTest/TerrainTest.h +++ b/tests/cpp-tests/Classes/TerrainTest/TerrainTest.h @@ -64,14 +64,9 @@ public: TerrainWalkThru(); virtual std::string title() const override; virtual std::string subtitle() const override; - void onTouchesMoved(const std::vector& touches, cocos2d::Event* event); void onTouchesBegan(const std::vector& touches, cocos2d::Event* event); void onTouchesEnd(const std::vector& touches, cocos2d::Event* event); protected: - Label * forward; - Label * backward; - Label * left; - Label * right; Camera * _camera; Terrain * _terrain; Player * _player; diff --git a/tests/cpp-tests/Resources/TerrainTest/Grass1.dds b/tests/cpp-tests/Resources/TerrainTest/Grass1.dds deleted file mode 100644 index 3261fe863c..0000000000 Binary files a/tests/cpp-tests/Resources/TerrainTest/Grass1.dds and /dev/null differ diff --git a/tests/cpp-tests/Resources/TerrainTest/Grass1.jpg b/tests/cpp-tests/Resources/TerrainTest/Grass1.jpg new file mode 100644 index 0000000000..0104608da2 Binary files /dev/null and b/tests/cpp-tests/Resources/TerrainTest/Grass1.jpg differ diff --git a/tests/cpp-tests/Resources/TerrainTest/Grass2.dds b/tests/cpp-tests/Resources/TerrainTest/Grass2.dds deleted file mode 100644 index 5425a9f20a..0000000000 Binary files a/tests/cpp-tests/Resources/TerrainTest/Grass2.dds and /dev/null differ diff --git a/tests/cpp-tests/Resources/TerrainTest/Grass2.jpg b/tests/cpp-tests/Resources/TerrainTest/Grass2.jpg new file mode 100644 index 0000000000..9b78b1ea1d Binary files /dev/null and b/tests/cpp-tests/Resources/TerrainTest/Grass2.jpg differ diff --git a/tests/cpp-tests/Resources/TerrainTest/dirt.dds b/tests/cpp-tests/Resources/TerrainTest/dirt.dds deleted file mode 100644 index 5ff9dda1e3..0000000000 Binary files a/tests/cpp-tests/Resources/TerrainTest/dirt.dds and /dev/null differ diff --git a/tests/cpp-tests/Resources/TerrainTest/dirt.jpg b/tests/cpp-tests/Resources/TerrainTest/dirt.jpg new file mode 100644 index 0000000000..8cd11268b6 Binary files /dev/null and b/tests/cpp-tests/Resources/TerrainTest/dirt.jpg differ diff --git a/tests/cpp-tests/Resources/TerrainTest/road.dds b/tests/cpp-tests/Resources/TerrainTest/road.dds deleted file mode 100644 index e0de4a9b02..0000000000 Binary files a/tests/cpp-tests/Resources/TerrainTest/road.dds and /dev/null differ diff --git a/tests/cpp-tests/Resources/TerrainTest/road.jpg b/tests/cpp-tests/Resources/TerrainTest/road.jpg new file mode 100644 index 0000000000..46fd85d70a Binary files /dev/null and b/tests/cpp-tests/Resources/TerrainTest/road.jpg differ diff --git a/tests/cpp-tests/Resources/TerrainTest/road.png b/tests/cpp-tests/Resources/TerrainTest/road.png new file mode 100644 index 0000000000..be03098b43 Binary files /dev/null and b/tests/cpp-tests/Resources/TerrainTest/road.png differ