mirror of https://github.com/axmolengine/axmol.git
Merge pull request #11903 from ricardoquesada/cleaner_code_properties
Terrain uses StateBlock
This commit is contained in:
commit
5d90a8725e
|
@ -33,6 +33,7 @@ USING_NS_CC;
|
||||||
#include "renderer/CCRenderer.h"
|
#include "renderer/CCRenderer.h"
|
||||||
#include "renderer/CCGLProgramStateCache.h"
|
#include "renderer/CCGLProgramStateCache.h"
|
||||||
#include "renderer/ccGLStateCache.h"
|
#include "renderer/ccGLStateCache.h"
|
||||||
|
#include "renderer/CCRenderState.h"
|
||||||
#include "base/CCDirector.h"
|
#include "base/CCDirector.h"
|
||||||
#include "2d/CCCamera.h"
|
#include "2d/CCCamera.h"
|
||||||
|
|
||||||
|
@ -79,6 +80,12 @@ bool Terrain::initProperties()
|
||||||
auto state = GLProgramState::create(shader);
|
auto state = GLProgramState::create(shader);
|
||||||
|
|
||||||
setGLProgramState(state);
|
setGLProgramState(state);
|
||||||
|
|
||||||
|
_stateBlock->setBlend(false);
|
||||||
|
_stateBlock->setDepthWrite(true);
|
||||||
|
_stateBlock->setDepthTest(true);
|
||||||
|
_stateBlock->setCullFace(true);
|
||||||
|
|
||||||
setDrawWire(false);
|
setDrawWire(false);
|
||||||
setIsEnableFrustumCull(true);
|
setIsEnableFrustumCull(true);
|
||||||
setAnchorPoint(Vec2(0,0));
|
setAnchorPoint(Vec2(0,0));
|
||||||
|
@ -104,34 +111,16 @@ void Terrain::onDraw(const Mat4 &transform, uint32_t flags)
|
||||||
glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
|
glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
GLboolean blendCheck = glIsEnabled(GL_BLEND);
|
|
||||||
if(blendCheck)
|
_stateBlock->bind();
|
||||||
{
|
|
||||||
glDisable(GL_BLEND);
|
|
||||||
}
|
|
||||||
GL::enableVertexAttribs(1<<_positionLocation | 1 << _texcordLocation | 1<<_normalLocation);
|
GL::enableVertexAttribs(1<<_positionLocation | 1 << _texcordLocation | 1<<_normalLocation);
|
||||||
glProgram->setUniformsForBuiltins(transform);
|
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)
|
if(!_alphaMap)
|
||||||
{
|
{
|
||||||
glActiveTexture(GL_TEXTURE0);
|
glActiveTexture(GL_TEXTURE0);
|
||||||
glBindTexture(GL_TEXTURE_2D,_detailMapTextures[0]->getName());
|
GL::bindTexture2D(_detailMapTextures[0]->getName());
|
||||||
glUniform1i(_detailMapLocation[0],0);
|
glUniform1i(_detailMapLocation[0],0);
|
||||||
glUniform1i(_alphaIsHasAlphaMapLocation,0);
|
glUniform1i(_alphaIsHasAlphaMapLocation,0);
|
||||||
}else
|
}else
|
||||||
|
@ -139,7 +128,7 @@ void Terrain::onDraw(const Mat4 &transform, uint32_t flags)
|
||||||
for(int i =0;i<_maxDetailMapValue;i++)
|
for(int i =0;i<_maxDetailMapValue;i++)
|
||||||
{
|
{
|
||||||
glActiveTexture(GL_TEXTURE0+i);
|
glActiveTexture(GL_TEXTURE0+i);
|
||||||
glBindTexture(GL_TEXTURE_2D,_detailMapTextures[i]->getName());
|
GL::bindTexture2D(_detailMapTextures[i]->getName());
|
||||||
glUniform1i(_detailMapLocation[i],i);
|
glUniform1i(_detailMapLocation[i],i);
|
||||||
|
|
||||||
glUniform1f(_detailMapSizeLocation[i],_terrainData._detailMaps[i]._detailMapSize);
|
glUniform1f(_detailMapSizeLocation[i],_terrainData._detailMaps[i]._detailMapSize);
|
||||||
|
@ -148,7 +137,7 @@ void Terrain::onDraw(const Mat4 &transform, uint32_t flags)
|
||||||
glUniform1i(_alphaIsHasAlphaMapLocation,1);
|
glUniform1i(_alphaIsHasAlphaMapLocation,1);
|
||||||
|
|
||||||
glActiveTexture(GL_TEXTURE4);
|
glActiveTexture(GL_TEXTURE4);
|
||||||
glBindTexture(GL_TEXTURE_2D,_alphaMap->getName());
|
GL::bindTexture2D(_alphaMap->getName());
|
||||||
glUniform1i(_alphaMapLocation,4);
|
glUniform1i(_alphaMapLocation,4);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -180,28 +169,7 @@ void Terrain::onDraw(const Mat4 &transform, uint32_t flags)
|
||||||
_isCameraViewChanged = false;
|
_isCameraViewChanged = false;
|
||||||
}
|
}
|
||||||
glActiveTexture(GL_TEXTURE0);
|
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 (CC_TARGET_PLATFORM == CC_PLATFORM_MAC) || (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_LINUX)
|
||||||
if(_isDrawWire)//reset state.
|
if(_isDrawWire)//reset state.
|
||||||
{
|
{
|
||||||
|
@ -260,8 +228,12 @@ bool Terrain::initHeightMap(const char * heightMap)
|
||||||
}
|
}
|
||||||
|
|
||||||
Terrain::Terrain()
|
Terrain::Terrain()
|
||||||
|
: _alphaMap(nullptr)
|
||||||
|
, _stateBlock(nullptr)
|
||||||
{
|
{
|
||||||
_alphaMap = nullptr;
|
_stateBlock = RenderState::StateBlock::create();
|
||||||
|
CC_SAFE_RETAIN(_stateBlock);
|
||||||
|
|
||||||
_customCommand.setTransparent(false);
|
_customCommand.setTransparent(false);
|
||||||
_customCommand.set3D(true);
|
_customCommand.set3D(true);
|
||||||
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
|
#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()
|
Terrain::~Terrain()
|
||||||
{
|
{
|
||||||
|
CC_SAFE_RELEASE(_stateBlock);
|
||||||
|
|
||||||
_alphaMap->release();
|
_alphaMap->release();
|
||||||
_heightMapImage->release();
|
_heightMapImage->release();
|
||||||
delete _quadRoot;
|
delete _quadRoot;
|
||||||
|
@ -832,7 +806,6 @@ void Terrain::reload()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
CCLOG("recreate");
|
|
||||||
initTextures();
|
initTextures();
|
||||||
_chunkLodIndicesSet.clear();
|
_chunkLodIndicesSet.clear();
|
||||||
_chunkLodIndicesSkirtSet.clear();
|
_chunkLodIndicesSkirtSet.clear();
|
||||||
|
|
|
@ -24,15 +24,18 @@ THE SOFTWARE.
|
||||||
#ifndef CC_TERRAIN_H
|
#ifndef CC_TERRAIN_H
|
||||||
#define CC_TERRAIN_H
|
#define CC_TERRAIN_H
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
#include "2d/CCNode.h"
|
#include "2d/CCNode.h"
|
||||||
#include "2d/CCCamera.h"
|
#include "2d/CCCamera.h"
|
||||||
#include "renderer/CCTexture2D.h"
|
#include "renderer/CCTexture2D.h"
|
||||||
#include "renderer/CCCustomCommand.h"
|
#include "renderer/CCCustomCommand.h"
|
||||||
|
#include "renderer/CCRenderState.h"
|
||||||
#include "3d/CCAABB.h"
|
#include "3d/CCAABB.h"
|
||||||
#include "3d/CCRay.h"
|
#include "3d/CCRay.h"
|
||||||
#include <vector>
|
|
||||||
#include "base/CCEventListenerCustom.h"
|
#include "base/CCEventListenerCustom.h"
|
||||||
#include "base/CCEventDispatcher.h"
|
#include "base/CCEventDispatcher.h"
|
||||||
|
|
||||||
NS_CC_BEGIN
|
NS_CC_BEGIN
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -81,7 +84,7 @@ NS_CC_BEGIN
|
||||||
* We can use ray-terrain intersection to pick a point of the terrain;
|
* 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 .
|
* 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:
|
public:
|
||||||
|
|
||||||
|
@ -433,6 +436,7 @@ protected:
|
||||||
ChunkIndices insertIndicesLOD(int neighborLod[4], int selfLod, GLushort * indices, int size);
|
ChunkIndices insertIndicesLOD(int neighborLod[4], int selfLod, GLushort * indices, int size);
|
||||||
|
|
||||||
ChunkIndices insertIndicesLODSkirt(int selfLod, GLushort * indices, int size);
|
ChunkIndices insertIndicesLODSkirt(int selfLod, GLushort * indices, int size);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
std::vector <ChunkLODIndices> _chunkLodIndicesSet;
|
std::vector <ChunkLODIndices> _chunkLodIndicesSet;
|
||||||
std::vector<ChunkLODIndicesSkirt> _chunkLodIndicesSkirtSet;
|
std::vector<ChunkLODIndicesSkirt> _chunkLodIndicesSkirtSet;
|
||||||
|
@ -469,6 +473,9 @@ protected:
|
||||||
GLint _alphaMapLocation;
|
GLint _alphaMapLocation;
|
||||||
GLint _alphaIsHasAlphaMapLocation;
|
GLint _alphaIsHasAlphaMapLocation;
|
||||||
GLint _detailMapSizeLocation[4];
|
GLint _detailMapSizeLocation[4];
|
||||||
|
|
||||||
|
RenderState::StateBlock* _stateBlock;
|
||||||
|
|
||||||
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
|
||||||
EventListenerCustom* _backToForegroundListener;
|
EventListenerCustom* _backToForegroundListener;
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue