glGetAttribLocation/glGetUniformLocation gibt 0 zurück, wodurch EXC_BAD_ACCESS auf glDrawArrays anrufen (iOS)

Erstelle ich ein einfaches OpenGL ES 2.0-Anwendung für iOS, und immer wenn ich call glDrawArrays. Ich habe festgestellt, dass dies Auftritt, wenn ich vorher genannt glEnableVertexAttribArray für meine zwei Attribute (Farbe und position), und dann festgestellt, dass glGetAttribLocation wurde wieder 1 für die position und 0 für die Farbe, und dann auch festgestellt, dass glGetUniformLocation wurde wieder 0 für meine MVP-matrix. Ich bin nicht sicher, ob 0 ein Gültiger Wert ist, oder warum glEnableVertexAttribArray zu sein scheint verursacht EXC_BAD_ACCESS, wenn glDrawArrays genannt wird.

Hier ist mein code:

compileShaders Funktion:

-(void)compileShaders {
GLuint vertShader = [self compileShader:@"Shader" ofType:GL_VERTEX_SHADER];
GLuint fragShader = [self compileShader:@"Shader" ofType:GL_FRAGMENT_SHADER];

GLuint program = glCreateProgram();
glAttachShader(program, vertShader);
glAttachShader(program, fragShader);
glLinkProgram(program);

GLint success;
glGetProgramiv(program, GL_LINK_STATUS, &success);
if (success == GL_FALSE) {
    GLchar messages[256];
    glGetProgramInfoLog(program, sizeof(messages), 0, &messages[0]);
    NSLog(@"%@", [NSString stringWithUTF8String:messages]);
    exit(1);
}

glUseProgram(program);

_positionSlot = glGetAttribLocation(program, "position");
_colorSlot = glGetAttribLocation(program, "color"); //Returns 0
_mvpSlot = glGetUniformLocation(program, "MVP"); //Returns 0

if (!_positionSlot || !_colorSlot || !_mvpSlot) {
    NSLog(@"Failed to retrieve the locations of the shader variables:\n Position:%i\n Color:%i\n MVP:%i", _positionSlot, _colorSlot, _mvpSlot); //Prints out the values of 1, 0, 0
}

glEnableVertexAttribArray(_positionSlot);
glEnableVertexAttribArray(_colorSlot);

Meine render Funktion:

-(void)render:(CADisplayLink *)displayLink {
glClearColor(0.5, 0.5, 0.5, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);

GLfloat mvp[16] = {
    1.0f, 0.0f, 0.0f, 0.0f,
    0.0f, 1.0f, 0.0f, 0.0f,
    0.0f, 0.0f, 1.0f, 0.0f,
    0.0f, 0.0f, 0.0f, 1.0f,
};

glUniformMatrix4fv(_mvpSlot, 1, 0, mvp);

glViewport(0, 0, width, height);

glVertexAttribPointer(_positionSlot, 3, GL_FLOAT, GL_FALSE, 0, vertices);
glVertexAttribPointer(_colorSlot, 4, GL_UNSIGNED_BYTE, GL_FALSE, 0, colors);

glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

[_context presentRenderbuffer:GL_RENDERBUFFER];
}

Und meine vertex-shader:

attribute vec4 position;
attribute vec4 color;

uniform mat4 MVP;

varying vec4 v_color;

void main(void) {
    gl_Position = MVP * position;
    v_color = color;
}

InformationsquelleAutor Daniel | 2011-07-22

Schreibe einen Kommentar