打开GL ES着色器 - 获取属性

时间:2013-03-04 15:16:32

标签: ios objective-c xcode opengl-es

我正在使用以下代码尝试从着色器中检索属性。

这段代码正在检索制服而不是属性。

/*********** Get attribute locations. ***************/
// Dictionary to store each active attribute
NSMutableDictionary *m_attributes = [[NSMutableDictionary alloc] init];
int m_nAttributes = -1;
glGetProgramiv( _program, GL_ACTIVE_ATTRIBUTES, &m_nAttributes );

for(GLuint i = 0; i < m_nAttributes; i++)  {
    int name_len=-1, num=-1;
    GLenum type = GL_ZERO;
    char attributesName[100];

    glGetActiveUniform( _program, i, sizeof(attributesName)-1, &name_len, &num, &type, attributesName );

    attributesName[name_len] = 0;

    GLuint attributes = glGetUniformLocation( _program, attributesName );

    [m_attributes setObject:[NSNumber numberWithUnsignedInt:attributes]
                     forKey:[NSString stringWithUTF8String:attributesName]];        
}

这是我的着色器:

attribute vec3 VertexPosition;
attribute vec3 VertexNormal;
attribute vec2 VertexTexCoord0;


uniform mat4 ModelViewMatrix;
uniform mat4 ModelViewProjMatrix;


varying vec3 Normal;
varying vec2 TexCoord0;


void main(void)
 {
    Normal = VertexNormal;
        TexCoord0 = VertexTexCoord0;

    gl_Position = ModelViewProjMatrix * vec4(VertexPosition, 1.0);
 }

任何人都可以看到我做错了吗?

1 个答案:

答案 0 :(得分:2)

您应该使用glGetActiveAttrib代替glGetActiveUniformglGetAttribLocation来获取属性索引而不是glGetUniformLocation

同时获取属性名称的最大长度以分配glGetActiveAttrib所需的缓冲区,而不是硬编码char attributesName[100];

GLint maxNameLength;
glGetProgramiv(program_handle, GL_ACTIVE_ATTRIBUTE_MAX_LENGTH, &maxNameLength);