void EffectNormalMapped::setLightPos(const Vec3& pos) { _lightPos = pos; auto glProgramState = getGLProgramState(); if(glProgramState) glProgramState->setUniformVec4("u_lightPosInLocalSpace", Vec4(_lightPos.x,_lightPos.y,_lightPos.z,1)); }
void Skybox::onDraw(const Mat4& transform, uint32_t flags) { auto camera = Camera::getVisitingCamera(); Mat4 cameraModelMat = camera->getNodeToWorldTransform(); auto state = getGLProgramState(); state->apply(transform); Vec4 color(_displayedColor.r / 255.f, _displayedColor.g / 255.f, _displayedColor.b / 255.f, 1.f); state->setUniformVec4("u_color", color); cameraModelMat.m[12] = cameraModelMat.m[13] = cameraModelMat.m[14] = 0; state->setUniformMat4("u_cameraRot", cameraModelMat); glEnable(GL_DEPTH_TEST); StateBlock::_defaultState->setDepthTest(true); glDepthFunc(GL_LEQUAL); StateBlock::_defaultState->setDepthFunction(DEPTH_LEQUAL); glEnable(GL_CULL_FACE); StateBlock::_defaultState->setCullFace(true); glCullFace(GL_BACK); StateBlock::_defaultState->setCullFaceSide(CULL_FACE_SIDE_BACK); glDisable(GL_BLEND); StateBlock::_defaultState->setBlend(false); if (Configuration::getInstance()->supportsShareableVAO()) { GL::bindVAO(_vao); } else { GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_POSITION); glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, sizeof(Vec3), nullptr); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer); } glDrawElements(GL_TRIANGLES, (GLsizei)36, GL_UNSIGNED_BYTE, nullptr); if (Configuration::getInstance()->supportsShareableVAO()) { GL::bindVAO(0); } else { glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); } CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, 8); CHECK_GL_ERROR_DEBUG(); }
//波光效果动画 void Tortoise::initUVAnimation() { // 将vsh与fsh装配成一个完整的Shader文件。 auto glprogram = GLProgram::createWithFilenames("3D/UVAnimation.vsh", "3D/UVAnimation.fsh"); // 由Shader文件创建这个Shader auto glprogramstate = GLProgramState::getOrCreateWithGLProgram(glprogram); // 给精灵设置所用的Shader _sprite->setGLProgramState(glprogramstate); //创建海龟所用的贴图。 auto textrue1 = Director::getInstance()->getTextureCache()->addImage("3D/tortoise.png"); //将贴图设置给Shader中的变量值u_texture1 glprogramstate->setUniformTexture("u_texture1", textrue1); //创建波光贴图。 auto textrue2 = Director::getInstance()->getTextureCache()->addImage("3D/caustics.png"); //将贴图设置给Shader中的变量值u_lightTexture glprogramstate->setUniformTexture("u_lightTexture", textrue2); //注意,对于波光贴图,我们希望它在进行UV动画时能产生四方连续效果,必须设置它的纹理UV寻址方式为GL_REPEAT。 Texture2D::TexParams tRepeatParams; tRepeatParams.magFilter = GL_LINEAR_MIPMAP_LINEAR; tRepeatParams.minFilter = GL_LINEAR; tRepeatParams.wrapS = GL_REPEAT; tRepeatParams.wrapT = GL_REPEAT; textrue2->setTexParameters(tRepeatParams); //在这里,我们设置一个波光的颜色,这里设置为白色。 Vec4 tLightColor(1.0,1.0,1.0,1.0); glprogramstate->setUniformVec4("v_LightColor",tLightColor); //下面这一段,是为了将我们自定义的Shader与我们的模型顶点组织方式进行匹配。模型的顶点数据一般包括位置,法线,色彩,纹理,以及骨骼绑定信息。而Shader需要将内部相应的顶点属性通道与模型相应的顶点属性数据进行绑定才能正确显示出顶点。 long offset = 0; auto attributeCount = _sprite->getMesh()->getMeshVertexAttribCount(); for (auto k = 0; k < attributeCount; k++) { auto meshattribute = _sprite->getMesh()->getMeshVertexAttribute(k); glprogramstate->setVertexAttribPointer(s_attributeNames[meshattribute.vertexAttrib], meshattribute.size, meshattribute.type, GL_FALSE, _sprite->getMesh()->getVertexSizeInBytes(), (GLvoid*)offset); offset += meshattribute.attribSizeBytes; } //uv滚动初始值设为0 _lightAni.x = _lightAni.y = 0; }//end initUVAnimation
void setCustomParams() { setUniformVec4("u_color", _color); }
bool FishLayer::init() { //加载模型文件 std::string fileName = "tortoise.c3b"; m_Sprite = Sprite3D::create(fileName); m_Sprite->setScale(0.1f); auto s = Director::getInstance()->getWinSize(); m_Sprite->setPosition(Vec2(s.width / 2.f, s.height / 2.f)); addChild(m_Sprite); //获取骨骼动画信息 m_Animation3D = Animation3D::create(fileName); if (m_Animation3D) { //从起始到1.933秒截取为游泳动作 m_Swim = Animate3D::create(m_Animation3D, 0.f, 1.933f); m_Swim->retain(); //让精灵循环播放游泳和的受伤动作 Sequence* pSequence = Sequence::create(m_Swim,NULL); m_Sprite->runAction(RepeatForever::create(pSequence)); } // 将vsh与fsh装配成一个完整的Shader文件。 auto glprogram = GLProgram::createWithFilenames("UVAnimation.vsh", "UVAnimation.fsh"); // 由Shader文件创建这个Shader auto glprogramstate = GLProgramState::getOrCreateWithGLProgram(glprogram); // 给精灵设置所用的Shader m_Sprite->setGLProgramState(glprogramstate); //创建海龟所用的贴图。 auto textrue1 = Director::getInstance()->getTextureCache()->addImage("tortoise.png"); //将贴图设置给Shader中的变量值u_texture1 glprogramstate->setUniformTexture("u_texture1", textrue1); //创建波光贴图。 auto textrue2 = Director::getInstance()->getTextureCache()->addImage("caustics.png"); //将贴图设置给Shader中的变量值u_lightTexture glprogramstate->setUniformTexture("u_lightTexture", textrue2); //注意,对于波光贴图,我们希望它在进行UV动画时能产生四方连续效果,必须设置它的纹理UV寻址方式为GL_REPEAT。 Texture2D::TexParams tRepeatParams; tRepeatParams.magFilter = GL_LINEAR_MIPMAP_LINEAR; tRepeatParams.minFilter = GL_LINEAR; tRepeatParams.wrapS = GL_REPEAT; tRepeatParams.wrapT = GL_REPEAT; textrue2->setTexParameters(tRepeatParams); //在这里,我们设置一个波光的颜色,这里设置为白色。 Vec4 tLightColor(1.0,1.0,1.0,1.0); glprogramstate->setUniformVec4("v_LightColor",tLightColor); //下面这一段,是为了将我们自定义的Shader与我们的模型顶点组织方式进行匹配。模型的顶点数据一般包括位置,法线,色彩,纹理,以及骨骼绑定信息。而Shader需要将内部相应的顶点属性通道与模型相应的顶点属性数据进行绑定才能正确显示出顶点。 long offset = 0; auto attributeCount = m_Sprite->getMesh()->getMeshVertexAttribCount(); for (auto k = 0; k < attributeCount; k++) { auto meshattribute = m_Sprite->getMesh()->getMeshVertexAttribute(k); glprogramstate->setVertexAttribPointer(s_attributeNames[meshattribute.vertexAttrib], meshattribute.size, meshattribute.type, GL_FALSE, m_Sprite->getMesh()->getVertexSizeInBytes(), (GLvoid*)offset); offset += meshattribute.attribSizeBytes; } //uv滚动初始值设为0 m_LightAni.x = m_LightAni.y = 0; return true; }