Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

You can copy paste this into the default OpenGl template created in Xcode.

Why am I not seeing anything :-)

It is strange as the glDrawArrays(GL_TRIANGLES, 0, 3); is working fine, but with

glDrawElements(GL_TRIANGLE_STRIP, sizeof(indices)/sizeof(GLubyte), GL_UNSIGNED_BYTE, indices);

Is giving BAD_ACCESS?

Copy paste this into Xcode default OpenGl template: ViewController

#import "ViewController.h"

#define BUFFER_OFFSET(i) ((char *)NULL + (i))

// Uniform index.
enum
{
    UNIFORM_MODELVIEWPROJECTION_MATRIX,
    UNIFORM_NORMAL_MATRIX,
    NUM_UNIFORMS
};
GLint uniforms[NUM_UNIFORMS];

// Attribute index.
enum
{
    ATTRIB_VERTEX,
    ATTRIB_NORMAL,
    NUM_ATTRIBUTES
};


@interface ViewController () {

    GLKMatrix4 _modelViewProjectionMatrix;
    GLKMatrix3 _normalMatrix;
    float _rotation;

    GLuint _vertexArray;
    GLuint _vertexBuffer;

    NSArray* arrayOfVertex;
}
@property (strong, nonatomic) EAGLContext *context;
@property (strong, nonatomic) GLKBaseEffect *effect;

- (void)setupGL;
- (void)tearDownGL;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];

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

    [self setupGL];
}

- (void)dealloc
{
    [self tearDownGL];

    if ([EAGLContext currentContext] == self.context) {
        [EAGLContext setCurrentContext:nil];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];

    if ([self isViewLoaded] && ([[self view] window] == nil)) {
        self.view = nil;

        [self tearDownGL];

        if ([EAGLContext currentContext] == self.context) {
            [EAGLContext setCurrentContext:nil];
        }
        self.context = nil;
    }

    // Dispose of any resources that can be recreated.
}

GLuint vertexBufferID;
GLuint indexBufferID;

static const GLfloat vertices[9] = {
    -0.5, -0.5,  0.5,
    0.5, -0.5,  0.5,
    -0.5,  0.5,  0.5
};

static const GLubyte indices[3] = {
    0, 1, 2
};

- (void)setupGL
{
    [EAGLContext setCurrentContext:self.context];

    // [self loadShaders];

    self.effect = [[GLKBaseEffect alloc] init];
    self.effect.light0.enabled = GL_TRUE;
    self.effect.light0.diffuseColor = GLKVector4Make(1.0f, 0.4f, 0.4f, 1.0f);

    glEnable(GL_DEPTH_TEST);

    // glGenVertexArraysOES(1, &_vertexArray);
    // glBindVertexArrayOES(_vertexArray);

    glGenBuffers(1, &vertexBufferID);
    glBindBuffer(GL_ARRAY_BUFFER, vertexBufferID);
    glBufferData(GL_ARRAY_BUFFER,
                 sizeof(vertices),
                 vertices,
                 GL_STATIC_DRAW);



    glGenBuffers(1, &indexBufferID);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferID);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER,
                 sizeof(indices),
                 indices,
                 GL_STATIC_DRAW);

    glEnableVertexAttribArray(GLKVertexAttribPosition);
    glVertexAttribPointer(GLKVertexAttribPosition, // Specifies the index of the generic vertex attribute to be modified.
                          3, // Specifies the number of components per generic vertex attribute. Must be 1, 2, 3, 4.
                          GL_FLOAT, //
                          GL_FALSE, //
                          0, //
                          BUFFER_OFFSET(0)); //

    // glBindVertexArrayOES(0);
}

- (void)tearDownGL
{
    [EAGLContext setCurrentContext:self.context];

    glDeleteBuffers(1, &_vertexBuffer);
    glDeleteVertexArraysOES(1, &_vertexArray);

    self.effect = nil;
}

#pragma mark - GLKView and GLKViewController delegate methods

- (void)update
{
    float aspect = fabsf(self.view.bounds.size.width / self.view.bounds.size.height);
    GLKMatrix4 projectionMatrix = GLKMatrix4MakePerspective(GLKMathDegreesToRadians(65.0f), aspect, 0.1f, 100.0f);

    self.effect.transform.projectionMatrix = projectionMatrix;

    GLKMatrix4 baseModelViewMatrix = GLKMatrix4MakeTranslation(0.0f, 0.0f, -4.0f);
    baseModelViewMatrix = GLKMatrix4Rotate(baseModelViewMatrix, _rotation, 0.0f, 1.0f, 0.0f);

    // Compute the model view matrix for the object rendered with GLKit
    GLKMatrix4 modelViewMatrix = GLKMatrix4MakeTranslation(0.0f, 0.0f, -1.5f);
    modelViewMatrix = GLKMatrix4Rotate(modelViewMatrix, _rotation, 1.0f, 1.0f, 1.0f);
    modelViewMatrix = GLKMatrix4Multiply(baseModelViewMatrix, modelViewMatrix);

    self.effect.transform.modelviewMatrix = modelViewMatrix;

    // Compute the model view matrix for the object rendered with ES2
    modelViewMatrix = GLKMatrix4MakeTranslation(0.0f, 0.0f, 1.5f);
    modelViewMatrix = GLKMatrix4Rotate(modelViewMatrix, _rotation, 1.0f, 1.0f, 1.0f);
    modelViewMatrix = GLKMatrix4Multiply(baseModelViewMatrix, modelViewMatrix);

    _normalMatrix = GLKMatrix3InvertAndTranspose(GLKMatrix4GetMatrix3(modelViewMatrix), NULL);

    _modelViewProjectionMatrix = GLKMatrix4Multiply(projectionMatrix, modelViewMatrix);

    _rotation += self.timeSinceLastUpdate * 0.5f;
}

int i;
- (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);

    // glBindVertexArrayOES(_vertexArray);

    // Render the object with GLKit
    [self.effect prepareToDraw];

    //glDrawArrays(GL_TRIANGLES, 0, 3);

    // Render the object again with ES2

    // glDrawArrays(GL_TRIANGLES, 0, 3);
    glDrawElements(GL_TRIANGLE_STRIP, sizeof(indices)/sizeof(GLubyte), GL_UNSIGNED_BYTE, indices);
}

@end
share|improve this question

3 Answers 3

This is always caused by having a vertex array enabled for which you have either supplied no data or not enough data (e.g the buffer for which the glVertexAttribPointer call is made may not be big enough). During the draw itself you get a buffer overrun and it crashes, so check that part of your draw setup; you'll find something.

share|improve this answer

I finally found the solution, in case you are interested - "Specifies a pointer to the location where the indices are stored." That is what the documentation says, but ohh boy that is wrong - it has to be a fake pointer for the glDrawElements to do pointer arithmetic on - to be able to offset the indices. Well, that was 10 hours well spend :-)

share|improve this answer

you have

//glGenVertexArraysOES(1, &_vertexArray);
//glBindVertexArrayOES(_vertexArray);

commented out in setupGL

but in your drawInRect function you call

glBindVertexArrayOES(_vertexArray);

but the value in _vertexArray was never set (because you commented it out) and is surely garbage ...

maybe try getting the sample code to work before you edit it perhaps.

EDIT:

here

glDrawElements(GL_TRIANGLE_STRIP, sizeof(indices)/sizeof(GLubyte), GL_UNSIGNED_BYTE, indices);

you are doing sizeof(array). This will not work, this will return size of a pointer, probably 4 (bytes).

share|improve this answer
    
Thank you, but that is not it. And you dont need glGenVertexArraysOES, it is only for optimization copying between the Cpu and the Gpu if you change the state of the context. –  user699215 Nov 19 '12 at 22:42
    
see my update above –  Stephan van den Heuvel Nov 19 '12 at 23:02
    
Really, thank you Stephan, but it does return the right value: 3. Hmm, since it works with glDrawArrays, the glVertexAttribPointer should be allright, right ? –  user699215 Nov 19 '12 at 23:27
    
Nitpicking: sizeof(array) returns the size of the array, so that part is correct. An array does not decay into a pointer to its first element by doing sizeof. –  ccxvii Nov 19 '12 at 23:58
    
Agree with ccxvii, sizeof(indices) returns the entire memory used by the indices structure/array and not size of pointer given this isn't a pointer. This then get divided by sizeof(GLubytes) which gives it the number of elements. –  John Nov 20 '12 at 9:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.