modify arcball and fog shader

This commit is contained in:
songmiao 2015-01-07 17:58:10 +08:00
parent 41cd946e27
commit dfa6137ce9
4 changed files with 31 additions and 34 deletions

View File

@ -975,7 +975,6 @@ CameraArcBallDemo::CameraArcBallDemo(void)
,_sprite3D1(nullptr)
,_sprite3D2(nullptr)
,_fRadius(1.0f)
,_distanceX(0.0f)
,_distanceZ(50.0f)
,_operate(OperateCamType::RotateCamera)
,_center(Vec3(0,0,0))
@ -1123,7 +1122,6 @@ void CameraArcBallDemo::onTouchsMoved( const std::vector<Touch*> &touchs, Event
{
Point newPos = touchs[0]->getPreviousLocation() - touchs[0]->getLocation();
_distanceZ -= newPos.y*0.1f;
_distanceZ += newPos.x*0.1f;
updateCameraTransform();
}
@ -1171,7 +1169,7 @@ float CameraArcBallDemo::projectToSphere( float r, float x, float y )
void CameraArcBallDemo::updateCameraTransform()
{
Mat4 trans, rot, center;
Mat4::createTranslation(Vec3(_distanceX, 0.0f, _distanceZ), &trans);
Mat4::createTranslation(Vec3(0.0f, 0.0f, _distanceZ), &trans);
Mat4::createRotation(_rotationQuat, &rot);
Mat4::createTranslation(_center, &center);
Mat4 result = center * rot * trans;
@ -1329,10 +1327,10 @@ void FogTestDemo::onEnter()
offset1 += meshattribute.attribSizeBytes;
}
_state->setUniformVec4("fogColor", Vec4(0.5,0.5,0.5,1.0));
_state->setUniformFloat("fStart",10);
_state->setUniformFloat("fEnd",60);
_state->setUniformInt("iEquation" ,0);
_state->setUniformVec4("u_fogColor", Vec4(0.5,0.5,0.5,1.0));
_state->setUniformFloat("u_fogStart",10);
_state->setUniformFloat("u_fogEnd",60);
_state->setUniformInt("u_fogEquation" ,0);
_layer3D->addChild(_sprite3D1);
_sprite3D1->setPosition3D( Vec3( 0, 0,0 ) );
@ -1361,28 +1359,28 @@ void FogTestDemo::switchTypeCallback(Ref* sender,int type)
{
if(type == 0)
{
_state->setUniformVec4("fogColor", Vec4(0.5,0.5,0.5,1.0));
_state->setUniformFloat("fStart",10);
_state->setUniformFloat("fEnd",60);
_state->setUniformInt("iEquation" ,0);
_state->setUniformVec4("u_fogColor", Vec4(0.5,0.5,0.5,1.0));
_state->setUniformFloat("u_fogStart",10);
_state->setUniformFloat("u_fogEnd",60);
_state->setUniformInt("u_fogEquation" ,0);
_sprite3D1->setGLProgramState(_state);
_sprite3D2->setGLProgramState(_state);
}
else if(type == 1)
{
_state->setUniformVec4("fogColor", Vec4(0.5,0.5,0.5,1.0));
_state->setUniformFloat("fogDensity",0.03);
_state->setUniformInt("iEquation" ,1);
_state->setUniformVec4("u_fogColor", Vec4(0.5,0.5,0.5,1.0));
_state->setUniformFloat("u_fogDensity",0.03);
_state->setUniformInt("u_fogEquation" ,1);
_sprite3D1->setGLProgramState(_state);
_sprite3D2->setGLProgramState(_state);
}
else if(type == 2)
{
_state->setUniformVec4("fogColor", Vec4(0.5,0.5,0.5,1.0));
_state->setUniformFloat("fogDensity",0.03);
_state->setUniformInt("iEquation" ,2);
_state->setUniformVec4("u_fogColor", Vec4(0.5,0.5,0.5,1.0));
_state->setUniformFloat("u_fogDensity",0.03);
_state->setUniformInt("u_fogEquation" ,2);
_sprite3D1->setGLProgramState(_state);
_sprite3D2->setGLProgramState(_state);

View File

@ -188,7 +188,6 @@ protected:
DrawNode3D* _drawGrid;
Quaternion _rotationQuat;
float _fRadius;
float _distanceX;
float _distanceZ;
OperateCamType _operate;
Vec3 _center;

View File

@ -3,12 +3,12 @@ varying mediump vec2 v_texture_coord;
#else
varying vec2 v_texture_coord;
#endif
varying float fogFactor;
varying float v_fogFactor;
uniform vec4 fogColor;
uniform vec4 u_fogColor;
void main (void)
{
vec4 finalColor = texture2D(CC_Texture0, v_texture_coord);
gl_FragColor = mix(fogColor, finalColor, fogFactor ); //out put finalColor
gl_FragColor = mix(u_fogColor, finalColor, v_fogFactor ); //out put finalColor
}

View File

@ -1,29 +1,29 @@
attribute vec4 a_position;
attribute vec2 a_texCoord;
varying float fogFactor; //weight for fog
varying float v_fogFactor; //weight for fog
varying vec2 v_texture_coord;
uniform float fogDensity;// For exp and exp2 equation
uniform float fStart; // This is only for linear fog
uniform float fEnd; // This is only for linear fog
uniform int iEquation; // 0 = linear, 1 = exp, 2 = exp2
uniform float u_fogDensity;// For exp and exp2 equation
uniform float u_fogStart; // This is only for linear fog
uniform float u_fogEnd; // This is only for linear fog
uniform int u_fogEquation; // 0 = linear, 1 = exp, 2 = exp2
void main(void)
{
gl_Position = CC_MVPMatrix * a_position;
v_texture_coord = a_texCoord;
float FogFragCoord = abs(gl_Position.z); //get fog distance
float fogFragCoord = abs(gl_Position.z); //get fog distance
if(iEquation == 0)
fogFactor = (fEnd-FogFragCoord )/(fEnd-fStart); //linear fog
else if(iEquation == 1)
fogFactor = exp(-fogDensity*FogFragCoord ); //exp fog
else if(iEquation == 2)
fogFactor = exp(-pow(fogDensity*FogFragCoord , 2.0)); //exp2 fog
if(u_fogEquation == 0)
v_fogFactor = (u_fogEnd-fogFragCoord )/(u_fogEnd-u_fogStart); //linear fog
else if(u_fogEquation == 1)
v_fogFactor = exp(-u_fogDensity*fogFragCoord ); //exp fog
else if(u_fogEquation == 2)
v_fogFactor = exp(-pow(u_fogDensity*fogFragCoord , 2.0)); //exp2 fog
fogFactor = clamp(fogFactor, 0.0, 1.0); //clamp 0 to 1
v_fogFactor = clamp(v_fogFactor, 0.0, 1.0); //clamp 0 to 1
}