diff --git a/cocos/3d/CCTerrain.cpp b/cocos/3d/CCTerrain.cpp index d8c9b13a4f..ee8ac3e864 100644 --- a/cocos/3d/CCTerrain.cpp +++ b/cocos/3d/CCTerrain.cpp @@ -33,6 +33,7 @@ USING_NS_CC; #include "renderer/CCRenderer.h" #include "renderer/CCGLProgramStateCache.h" #include "renderer/ccGLStateCache.h" +#include "renderer/CCRenderState.h" #include "base/CCDirector.h" #include "2d/CCCamera.h" @@ -79,6 +80,12 @@ bool Terrain::initProperties() auto state = GLProgramState::create(shader); setGLProgramState(state); + + _stateBlock->setBlend(false); + _stateBlock->setDepthWrite(true); + _stateBlock->setDepthTest(true); + _stateBlock->setCullFace(true); + setDrawWire(false); setIsEnableFrustumCull(true); setAnchorPoint(Vec2(0,0)); @@ -104,34 +111,16 @@ void Terrain::onDraw(const Mat4 &transform, uint32_t flags) glPolygonMode(GL_FRONT_AND_BACK,GL_FILL); } #endif - GLboolean blendCheck = glIsEnabled(GL_BLEND); - if(blendCheck) - { - glDisable(GL_BLEND); - } + + _stateBlock->bind(); + GL::enableVertexAttribs(1<<_positionLocation | 1 << _texcordLocation | 1<<_normalLocation); glProgram->setUniformsForBuiltins(transform); - 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; - depthTestCheck = glIsEnabled(GL_DEPTH_TEST); - if(!depthTestCheck) - { - glEnable(GL_DEPTH_TEST); - } + if(!_alphaMap) { glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D,_detailMapTextures[0]->getName()); + GL::bindTexture2D(_detailMapTextures[0]->getName()); glUniform1i(_detailMapLocation[0],0); glUniform1i(_alphaIsHasAlphaMapLocation,0); }else @@ -139,7 +128,7 @@ void Terrain::onDraw(const Mat4 &transform, uint32_t flags) for(int i =0;i<_maxDetailMapValue;i++) { glActiveTexture(GL_TEXTURE0+i); - glBindTexture(GL_TEXTURE_2D,_detailMapTextures[i]->getName()); + GL::bindTexture2D(_detailMapTextures[i]->getName()); glUniform1i(_detailMapLocation[i],i); glUniform1f(_detailMapSizeLocation[i],_terrainData._detailMaps[i]._detailMapSize); @@ -148,7 +137,7 @@ void Terrain::onDraw(const Mat4 &transform, uint32_t flags) glUniform1i(_alphaIsHasAlphaMapLocation,1); glActiveTexture(GL_TEXTURE4); - glBindTexture(GL_TEXTURE_2D,_alphaMap->getName()); + GL::bindTexture2D(_alphaMap->getName()); glUniform1i(_alphaMapLocation,4); } @@ -180,28 +169,7 @@ void Terrain::onDraw(const Mat4 &transform, uint32_t flags) _isCameraViewChanged = false; } glActiveTexture(GL_TEXTURE0); - if(depthTestCheck) - { - }else - { - glDisable(GL_DEPTH_TEST); - } - if(depthMaskCheck) - { - }else - { - glDepthMask(GL_FALSE); - } - if(CullFaceCheck) - { - }else - { - glEnable(GL_CULL_FACE); - } - if(blendCheck) - { - glEnable(GL_BLEND); - } + #if (CC_TARGET_PLATFORM == CC_PLATFORM_MAC) || (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_LINUX) if(_isDrawWire)//reset state. { @@ -260,8 +228,12 @@ bool Terrain::initHeightMap(const char * heightMap) } Terrain::Terrain() +: _alphaMap(nullptr) +, _stateBlock(nullptr) { - _alphaMap = nullptr; + _stateBlock = RenderState::StateBlock::create(); + CC_SAFE_RETAIN(_stateBlock); + _customCommand.setTransparent(false); _customCommand.set3D(true); #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) @@ -453,6 +425,8 @@ void Terrain::setIsEnableFrustumCull(bool bool_value) Terrain::~Terrain() { + CC_SAFE_RELEASE(_stateBlock); + _alphaMap->release(); _heightMapImage->release(); delete _quadRoot; @@ -832,7 +806,6 @@ void Terrain::reload() } } - CCLOG("recreate"); initTextures(); _chunkLodIndicesSet.clear(); _chunkLodIndicesSkirtSet.clear(); diff --git a/cocos/3d/CCTerrain.h b/cocos/3d/CCTerrain.h index c348ecb22b..d06e1005d1 100644 --- a/cocos/3d/CCTerrain.h +++ b/cocos/3d/CCTerrain.h @@ -24,15 +24,18 @@ THE SOFTWARE. #ifndef CC_TERRAIN_H #define CC_TERRAIN_H +#include + #include "2d/CCNode.h" #include "2d/CCCamera.h" #include "renderer/CCTexture2D.h" #include "renderer/CCCustomCommand.h" +#include "renderer/CCRenderState.h" #include "3d/CCAABB.h" #include "3d/CCRay.h" -#include #include "base/CCEventListenerCustom.h" #include "base/CCEventDispatcher.h" + NS_CC_BEGIN /** @@ -81,7 +84,7 @@ NS_CC_BEGIN * 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 +class CC_DLL Terrain : public Node { public: @@ -433,6 +436,7 @@ protected: 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; @@ -469,6 +473,9 @@ protected: GLint _alphaMapLocation; GLint _alphaIsHasAlphaMapLocation; GLint _detailMapSizeLocation[4]; + + RenderState::StateBlock* _stateBlock; + #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) EventListenerCustom* _backToForegroundListener; #endif