首页 > 代码库 > cocos Uniforms值的赋值
cocos Uniforms值的赋值
cocos Uniforms值的赋值举个void Renderer::drawBatchedTriangles()的渲染://Start drawing verties in batchfor(const auto& cmd : _batchedCommands){auto newMaterialID = cmd->getMaterialID();if(_lastMaterialID != newMaterialID || newMaterialID == MATERIAL_ID_DO_NOT_BATCH){//Draw quadsif(indexToDraw > 0){//使用前面绑定的定点数据,绘制图元glDrawElements(GL_TRIANGLES, (GLsizei) indexToDraw, GL_UNSIGNED_SHORT, (GLvoid*) (startIndex*sizeof(_indices[0])) );_drawnBatches++;_drawnVertices += indexToDraw;startIndex += indexToDraw;indexToDraw = 0;}//Use new material//这个函数内部就是对Uniforms赋值cmd->useMaterial();_lastMaterialID = newMaterialID;}indexToDraw += cmd->getIndexCount();}2、void TrianglesCommand::useMaterial() const{//Set texture//设置纹理GL::bindTexture2D(_textureID);//set blend modeGL::blendFunc(_blendType.src, _blendType.dst);_glProgramState->apply(_mv);}3、void GLProgramState::apply(const Mat4& modelView){//设置cocos内置的Uniforms值applyGLProgram(modelView);//一般用不到applyAttributes();//设置用户自定义的Uniforms值,当然也可以通过其他方式直接设置applyUniforms();}4、这个函数就是真正的设置Uniform的值void GLProgram::setUniformsForBuiltins(const Mat4 &matrixMV){auto& matrixP = _director->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);if (_flags.usesP)setUniformLocationWithMatrix4fv(_builtInUniforms[UNIFORM_P_MATRIX], matrixP.m, 1);if (_flags.usesMV)setUniformLocationWithMatrix4fv(_builtInUniforms[UNIFORM_MV_MATRIX], matrixMV.m, 1);if (_flags.usesMVP) {Mat4 matrixMVP = matrixP * matrixMV;setUniformLocationWithMatrix4fv(_builtInUniforms[UNIFORM_MVP_MATRIX], matrixMVP.m, 1);}if (_flags.usesNormal){Mat4 mvInverse = matrixMV;mvInverse.m[12] = mvInverse.m[13] = mvInverse.m[14] = 0.0f;mvInverse.inverse();mvInverse.transpose();GLfloat normalMat[9];normalMat[0] = mvInverse.m[0];normalMat[1] = mvInverse.m[1];normalMat[2] = mvInverse.m[2];normalMat[3] = mvInverse.m[4];normalMat[4] = mvInverse.m[5];normalMat[5] = mvInverse.m[6];normalMat[6] = mvInverse.m[8];normalMat[7] = mvInverse.m[9];normalMat[8] = mvInverse.m[10];setUniformLocationWithMatrix3fv(_builtInUniforms[UNIFORM_NORMAL_MATRIX], normalMat, 1);}if (_flags.usesTime) {// This doesn‘t give the most accurate global time value.// Cocos2D doesn‘t store a high precision time value, so this will have to do.// Getting Mach time per frame per shader using time could be extremely expensive.float time = _director->getTotalFrames() * _director->getAnimationInterval();setUniformLocationWith4f(_builtInUniforms[GLProgram::UNIFORM_TIME], time/10.0, time, time*2, time*4);setUniformLocationWith4f(_builtInUniforms[GLProgram::UNIFORM_SIN_TIME], time/8.0, time/4.0, time/2.0, sinf(time));setUniformLocationWith4f(_builtInUniforms[GLProgram::UNIFORM_COS_TIME], time/8.0, time/4.0, time/2.0, cosf(time));}if (_flags.usesRandom)setUniformLocationWith4f(_builtInUniforms[GLProgram::UNIFORM_RANDOM01], CCRANDOM_0_1(), CCRANDOM_0_1(), CCRANDOM_0_1(), CCRANDOM_0_1());}
cocos Uniforms值的赋值
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。