码迷,mamicode.com
首页 > 其他好文 > 详细

cocos Uniforms值的赋值

时间:2016-09-13 20:45:45      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:

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值的赋值

标签:

原文地址:http://www.cnblogs.com/ZhYQ-Note/p/5869639.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!