OpenGL 2.0使用相同的着色器渲染多个对象

时间:2014-04-21 13:43:20

标签: ios opengl-es-2.0 shader

我想知道如何为多个对象使用相同的着色器,但允许它们具有不同的颜色

我在屏幕上有很多立方体,它们当前都加载了相同的着色器,唯一的区别是当它是绘制时,我改变了立方体的颜色。如果我为它们设置了相同的_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颜色均匀;允许我随时更改颜色,如果每个对象都有一个着色器,它工作正常,我可以动态更改对象颜色

1 个答案:

答案 0 :(得分:2)

如何为每个多维数据集发送不同的制服(例如uniform vec4 cubeColor并在片段着色器中使用它),然后再在其上调用glDrawArrays()

或者,您可以考虑在设置期间为每个立方体上传顶点和顶点颜色,然后在绘制时,绑定适当的顶点缓冲区(例如attribute vec3 a_vertex)和顶点颜色缓冲区(例如{{1您在顶点着色器中指定的attribute vec4 a_vertexColor,并在片段着色器中用作varying vec4 v_vertexColor)。

另外,作为附注,如果您计划使用相同的程序,则可以在设置期间调用varying vec4 v_vertexColor一次(OpenGL基于状态机,这意味着回忆某些参数(又名。状态,例如当前程序),只要你不改变它们。这可能会稍微提高程序的性能; - )

祝你好运。

相关问题