ios - Opengl ES 着色器 - 获取属性

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

谁能看出我做错了什么?

最佳答案

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

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

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

关于ios - Opengl ES 着色器 - 获取属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15204817/

相关文章:

iOS UITableView 如何禁用滚动到底部弹跳

objective-c - 将 iOS 应用程序移植到 OSX 并维护一组代码?

objective-c - 'instance method not found' 警告的 -Wsomething 标志是什么?

ios - 将捕获的图像添加到数组中并显示在 Collection View 中并上传到服务器 iOS

xcode - NSUserDefaults 不保存

iphone - 使用帐户框架在 iOS 5 Twitter 中转推、回复和收藏

ios - "Open In..."移动 safari 与邮件或其他 iOS 应用程序

ios - 如何在 iphone 中访问动态创建的 uilabel

iOS 6 和 iOS 7 UIButton 标签垂直对齐

ios - iOS slider 的默认缩略图大小?