2014-04-21 95 views
0

我想知道如何使用多个对象相同的着色器,但让他们的对象有不同的颜色的OpenGL 2.0渲染多个对象以相同的着色器

我在屏幕上有很多的立方体,所有当前的负载相同的着色器,唯一的区别是当它被绘制时,我改变了立方体的颜色。如果我为它们设置了相同的_program,它们就变成了完全相同的颜色。

- (void)draw:(float)eyeOffset 
{ 
    // Calculate the per-eye model view matrix: 
    GLKMatrix4 temp = GLKMatrix4MakeTranslation(eyeOffset, 0.0f, 0.0f); 
    GLKMatrix4 eyeBaseModelViewMatrix = GLKMatrix4Multiply(temp, self.baseModelViewMatrix); 

    if (self.isTransparant) 
    { 
     glEnable (GL_BLEND); 
     glDisable(GL_CULL_FACE); 
     //glDisable(GL_DEPTH_TEST); 
     glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 
    } 

    if (self.textureInfo) 
    { 
     glBindTexture(self.textureInfo.target, self.textureInfo.name); 
    } 

    glBindVertexArrayOES(_vertexArray); 

//See if we are sharing a program shader 
    if (self.tprogram) 
    { 
     glUseProgram(self.tprogram); 
    } 
    else 
    { 
     glUseProgram(_program); 
    } 

    self.modelViewMatrix = GLKMatrix4MakeTranslation(self.position.x,self.position.y, self.position.z);//(float)x, (float)y, -1.5f) 
    self.modelViewMatrix = GLKMatrix4Scale(self.modelViewMatrix, self.scale.x, self.scale.y, self.scale.z); 

    //rotation +=0.01; 
    self.modelViewMatrix = GLKMatrix4Rotate(self.modelViewMatrix,self.spinRotation, 0.0 ,0.0 ,1.0); 

    self.modelViewMatrix = GLKMatrix4Multiply(eyeBaseModelViewMatrix, self.modelViewMatrix); 

    GLKMatrix3 normalMatrix = GLKMatrix3InvertAndTranspose(GLKMatrix4GetMatrix3(self.modelViewMatrix), NULL); 
    GLKMatrix4 modelViewProjectionMatrix = GLKMatrix4Multiply(self.projectionMatrix, self.modelViewMatrix); 

    glUniformMatrix4fv(uniforms[UNIFORM_MODELVIEWPROJECTION_MATRIX], 1, 0, modelViewProjectionMatrix.m); 
    glUniformMatrix3fv(uniforms[UNIFORM_NORMAL_MATRIX], 1, 0, normalMatrix.m); 

    _colorSlot = glGetUniformLocation(_program, "color"); 
    GLfloat color[] = { 
     self.color.x, self.color.y, self.color.z, self.color.a}; 
    glUniform4fv(_colorSlot, 1, color); 

    glDrawArrays(GL_TRIANGLES, 0, 36); 

    if (self.isTransparant) 
    { 
     glEnable(GL_CULL_FACE); 
     //glEnable(GL_DEPTH_TEST); 
     glDisable(GL_BLEND); 
    } 
} 

//设定每个立方体

- (void)setup; 
{ 

    glGenVertexArraysOES(1, &_vertexArray); 
    glBindVertexArrayOES(_vertexArray); 

    glGenBuffers(1, &_vertexBuffer); 
    glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer); 
    glBufferData(GL_ARRAY_BUFFER, sizeof(gCubeVertexData), gCubeVertexData, GL_STATIC_DRAW); 

    glEnableVertexAttribArray(GLKVertexAttribPosition); 
    glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, 32, BUFFER_OFFSET(0)); 
    glEnableVertexAttribArray(GLKVertexAttribNormal); 
    glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_FALSE, 32, BUFFER_OFFSET(12)); 
    glEnableVertexAttribArray(GLKVertexAttribTexCoord0); 
    glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, 32, BUFFER_OFFSET(24)); 

    glBindVertexArrayOES(0); 

} 

着色

attribute vec4 position; 
attribute vec3 normal; 
uniform vec4 color; 

varying lowp vec4 colorVarying; 

uniform mat4 modelViewProjectionMatrix; 
uniform mat3 normalMatrix; 

void main() 
{ 
    //vec4 diffuseColor = color; 

    vec3 eyeNormal = normalize(normalMatrix * normal); 
    vec3 lightPosition = vec3(0.0, 0.0, 1.0); 
    //diffuseColor = vec4(0.4, 0.4, 1.0, 1.0); 

    float nDotVP = max(0.7, dot(eyeNormal, normalize(lightPosition))); // 0.0 

    colorVarying = color * nDotVP; 

    gl_Position = modelViewProjectionMatrix * position; 
} 

我认为均匀vec4颜色;允许我随时更改颜色,如果每个对象都有着色器,它可以正常工作,我可以在飞行中更改对象颜色

+0

我正在绘图函数中运行该代码,所以它应该更新。 – Burf2000

回答

2

如何为每个多维数据集发送一个不同的统一数据(例如uniform vec4 cubeColor并在片段中使用它着色器)在调用glDrawArrays()之前呢?或者,您可以考虑在设置过程中为每个立方体上传顶点和顶点颜色,然后在绘制时绑定合适的顶点缓冲区(例如attribute vec3 a_vertex)和顶点颜色缓冲区(例如,您分配的attribute vec4 a_vertexColor)在你的顶点着色器中,到varying vec4 v_vertexColor,并在你的片段着色器中用作varying vec4 v_vertexColor)。

此外,作为一个侧面说明,如果你打算使用同样的程序,你可以调用glUseProgram()一次,在安装过程中(OpenGL的是基于状态机,就意味着它回忆一定参数(如状态,如当前程序),只要您不更改它们即可)。这可能会提高你的程序的性能一点点;-)

祝你好运。

+1

如果颜色与整个图元的颜色相同,则“属性”方法的变体通常应该更有效。使用诸如'glVertexAttrib4f'之类的调用,而不是将颜色添加到顶点缓冲区,而是在绘制调用之前设置color属性的值。 –

+0

我添加了实际的绘图代码,所以你得到一个更好的主意 – Burf2000

+0

我找不到一个iOS的例子glVertexAttrib4f – Burf2000