0

I'm pretty new to OpenGL ES, but all I'm trying to do is draw indexed vertices using glDrawElements in a Character class. I've gotten this to work before inside of my GLKViewController class, but when I tried creating a Character class which would perform its own rendering, I got nothing but BAD_ACCESS. Here is my Character class:

#import "Character.h"

@interface Character()
{
    GLuint _vertexBuffer;
    GLuint _indexBuffer;
    GLuint _vertexArray;
}

@property(nonatomic, weak) GLKBaseEffect *effect;

@end

typedef struct {
    float Position[3];
    float Color[4];
} Vertex;

const Vertex Vertices[] = {
    {{1, -1, 0}, {1, 0, 0, 1}},
    {{1, 1, 0}, {0, 1, 0, 1}},
    {{-1, 1, 0}, {0, 0, 1, 1}},
    {{-1, -1, 0}, {0, 0, 0, 1}}
};

const GLushort Indices[] = {
    0, 1, 2,
    2, 3, 0
};

@implementation Character

- (id)initWithEffect:(GLKBaseEffect *)effect
{
    if (self = [super init])
    {
        self.effect = effect;
        [self setupGL];
    }

    return self;
}

- (void)setupGL
{
    glGenVertexArraysOES(1, &_vertexArray);
    glBindVertexArrayOES(_vertexArray);

    glGenBuffers(1, &_vertexBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);

    glGenBuffers(1, &_indexBuffer);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(Indices), Indices, GL_STATIC_DRAW);


    glEnableVertexAttribArray(GLKVertexAttribPosition);
    glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid *) offsetof(Vertex, Position));
    glEnableVertexAttribArray(GLKVertexAttribColor);
    glVertexAttribPointer(GLKVertexAttribColor, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid *) offsetof(Vertex, Color));


    glBindVertexArrayOES(0);
}

- (void)teardownGL
{
    glDeleteBuffers(1, &_vertexBuffer);
    glDeleteBuffers(1, &_indexBuffer);
}

- (void)render
{
    self.effect.transform.modelviewMatrix = GLKMatrix4Translate(GLKMatrix4Identity, self.position.x, self.position.y, self.position.z);
    self.effect.transform.modelviewMatrix = GLKMatrix4Rotate(self.effect.transform.modelviewMatrix, self.rotation, 0.0f, 0.0f, 1.0f);

    [self.effect prepareToDraw];
    glBindVertexArrayOES(_vertexArray);
    glDrawElements(GL_TRIANGLES, sizeof(Indices) / sizeof(Indices[0]), GL_UNSIGNED_SHORT, 0);
}

@end

Then in ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];

    GLKView *view = (GLKView *)self.view;
    view.context = self.context;

    character = [[Character alloc] initWithEffect:self.effect];
    character.position = GLKVector3Make(self.view.bounds.size.width / 2, self.view.bounds.size.height / 2, 0.0f);
    [self setupGL];
}

rendered using:

- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
{
    glClearColor(0.65f, 0.65f, 0.65f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    [character render];
}

I know it isn't something as simple as a miscalculation of byte-size or something because I've been at this for a couple days now.

Connor Pearson
  • 63,902
  • 28
  • 145
  • 142
michaelsnowden
  • 6,031
  • 2
  • 38
  • 83

1 Answers1

0

Hard to say for sure on a quick read, but it looks like the problem is that you don't have a current GL context when you're setting up your VAO in -[Character setupGL].

Creating an EAGLContext object doesn't make it the current context. If the rest of the code in your view controller looks like that from the Xcode "OpenGL Game" template, ViewController doesn't set the current context until its own setupGL method, which you call only after calling into your Character class and its attempts to set up OpenGL ES resources.

rickster
  • 124,678
  • 26
  • 272
  • 326
  • Oddly enough, when I looked at this after reading your answer, I had already changed that, and my code still doesn't work. The full code for the ViewController and the Character classes are here (Headers for ViewController and Character, and implementations): https://gist.github.com/doctordoder/9480644 It isn't a long read, so I hope you'll take the time to help me out and follow up on your answer. Actually, I'm begging for your help because I've been at this for weeks, and there isn't a single answer on the internet. – michaelsnowden Mar 11 '14 at 06:36