samb90
samb90

Reputation: 1073

Open GL ES Shader - Get Attributes

I am using the following code to try and retrieve the attributes from my shader.

This segment of code is retrieving the uniforms instead of the attributes.

/*********** 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]];        
}

Here is my shader:

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);
 }

Can anyone see anythng I am doing wrong?

Upvotes: 0

Views: 256

Answers (1)

Kimi
Kimi

Reputation: 14109

You should use glGetActiveAttrib instead of glGetActiveUniform and glGetAttribLocation to get attribute index instead of glGetUniformLocation.

Also get the maximum length of the attribute name to allocate a buffer needed by glGetActiveAttrib instead of hardcoding char attributesName[100];

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

Upvotes: 2

Related Questions