From dfa6137ce9ae7f20067635f5dde661a2ee1e11ac Mon Sep 17 00:00:00 2001 From: songmiao Date: Wed, 7 Jan 2015 17:58:10 +0800 Subject: [PATCH] modify arcball and fog shader --- .../Classes/Camera3DTest/Camera3DTest.cpp | 32 +++++++++---------- .../Classes/Camera3DTest/Camera3DTest.h | 1 - .../cpp-tests/Resources/Sprite3DTest/fog.frag | 6 ++-- .../cpp-tests/Resources/Sprite3DTest/fog.vert | 26 +++++++-------- 4 files changed, 31 insertions(+), 34 deletions(-) diff --git a/tests/cpp-tests/Classes/Camera3DTest/Camera3DTest.cpp b/tests/cpp-tests/Classes/Camera3DTest/Camera3DTest.cpp index f5b4ef4e31..800fb891c1 100644 --- a/tests/cpp-tests/Classes/Camera3DTest/Camera3DTest.cpp +++ b/tests/cpp-tests/Classes/Camera3DTest/Camera3DTest.cpp @@ -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 &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, ¢er); 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); diff --git a/tests/cpp-tests/Classes/Camera3DTest/Camera3DTest.h b/tests/cpp-tests/Classes/Camera3DTest/Camera3DTest.h index 992336718c..09d29633e2 100644 --- a/tests/cpp-tests/Classes/Camera3DTest/Camera3DTest.h +++ b/tests/cpp-tests/Classes/Camera3DTest/Camera3DTest.h @@ -188,7 +188,6 @@ protected: DrawNode3D* _drawGrid; Quaternion _rotationQuat; float _fRadius; - float _distanceX; float _distanceZ; OperateCamType _operate; Vec3 _center; diff --git a/tests/cpp-tests/Resources/Sprite3DTest/fog.frag b/tests/cpp-tests/Resources/Sprite3DTest/fog.frag index 1161338b9a..bdf3e369a6 100644 --- a/tests/cpp-tests/Resources/Sprite3DTest/fog.frag +++ b/tests/cpp-tests/Resources/Sprite3DTest/fog.frag @@ -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 } diff --git a/tests/cpp-tests/Resources/Sprite3DTest/fog.vert b/tests/cpp-tests/Resources/Sprite3DTest/fog.vert index a60a597049..4dd4f578e6 100644 --- a/tests/cpp-tests/Resources/Sprite3DTest/fog.vert +++ b/tests/cpp-tests/Resources/Sprite3DTest/fog.vert @@ -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 }