cocos Uniforms值的赋值

Posted ZhYQ_note

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了cocos Uniforms值的赋值相关的知识,希望对你有一定的参考价值。

cocos Uniforms值的赋值
举个void Renderer::drawBatchedTriangles()的渲染:
//Start drawing verties in batch
for(const auto& cmd : _batchedCommands)
{
auto newMaterialID = cmd->getMaterialID();
if(_lastMaterialID != newMaterialID || newMaterialID == MATERIAL_ID_DO_NOT_BATCH)
{
//Draw quads
if(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();
}

2void TrianglesCommand::useMaterial() const
{
//Set texture
//设置纹理
GL::bindTexture2D(_textureID);

//set blend mode
GL::blendFunc(_blendType.src, _blendType.dst);

_glProgramState->apply(_mv);
}

3void 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值的赋值的主要内容,如果未能解决你的问题,请参考以下文章

为啥 glGetProgramiv GL_ACTIVE_UNIFORMS 偶尔会返回垃圾并使我的程序崩溃?

.self 在 Swift 中的结构类型之后

6. Uniforms in American's Eyes 美国人眼里的制服

Xcode 运行cocos2dx弹出内部错误对话框

Jsp获取Java的重定向赋值(String)

cocos2d-x 2.x 学习与应用总结11: 理解CCGLProgram