0

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

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

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

谁能看到我做错了什么?

4

1 回答 1

2

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

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

GLint maxNameLength;
glGetProgramiv(program_handle, GL_ACTIVE_ATTRIBUTE_MAX_LENGTH, &maxNameLength);
于 2013-03-05T09:21:40.250 回答