0

im currently working on some OpenGL Basics with OpenGL ES 2.0.

If i setup the vertices as constants in the top the triangle gets drawn; if i try to set them up in a method using a pointer, nothing is drawn. What am I missing?

#import "CometGLViewController.h"

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

//vertices store information for each "point" used to draw a triangle
Vertex* Vertices; // NOT DRAWING
//const Vertex Vertices[] = {
//    {{0, 0, 0},{0, 0, 0, 1}},
//    {{1, 1, 0}, {1, 0, 0, 1}},
//    {{0, 1, 0}, {1, 0, 0, 1}},
//}; // THIS WORKS

//Used to reuse vertices
const GLubyte Indices[] = {
    0, 1, 2,
};

... some other stuff

- (void)viewDidLoad
{
    [super viewDidLoad];

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

    if (!self.context) {
        NSLog(@"Failed to create ES context");
    }

    GLKView *view = (GLKView *)self.view;
    view.context = self.context;
    view.drawableMultisample = GLKViewDrawableMultisample4X;
    [self setupVertices];
    [self setupGL];
}

...

#pragma mark - GLKViewDelegate

- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
{
    glClearColor(_curRed, 0.5, 0.5, 1.0);
    glClear(GL_COLOR_BUFFER_BIT);

    [self.effect prepareToDraw];

    glBindVertexArrayOES(_vertexArray);
    glDrawElements(GL_TRIANGLES, sizeof(Indices)/sizeof(Indices[0]), GL_UNSIGNED_BYTE, 0);
}

...

#pragma mark - OpenGL stuff
- (void)setupGL {

    [EAGLContext setCurrentContext:self.context];
    glEnable(GL_CULL_FACE);

    glGenVertexArraysOES(1, &_vertexArray);
    glBindVertexArrayOES(_vertexArray);
// ----- create new buffer,  work with "vertexBuffer", glBufferData sends data for opengl usage
    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);

// ----- Setup vertices attributes
    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));
}

- (void)tearDownGL {

    [EAGLContext setCurrentContext:self.context];

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

    self.effect = nil;

}

- (void)setupVertices
{ // test
    int points = 3;
    Vertices = malloc(sizeof(Vertex) * points);
    Vertex v = {{0, 0, 0}, {0, 0, 0, 1}};
    Vertices[0] = v;
    Vertex v1 = {{1, 1, 0}, {0, 1, 0, 1}};
    Vertices[1] = v1;
    Vertex v2 = {{0, 1, 1}, {1, 0, 0, 1}};
    Vertices[2] = v2;
}

@end
3
  • Objective C is completely unreadable to me, but I could swear that there is nowhere in this code that you actually allocate or fill *Vertices with any data. If you could include that code, that would help. Commented Oct 11, 2013 at 18:42
  • 1
    He does in fact fill Vertices. It is defined in his header, and when the viewDidLoad method is called setUpOpenGL is called which in turn calls setUpVertices which allocates and fills it (Maybe he edited after you posted this, but I don't see an edit). Commented Oct 11, 2013 at 19:21
  • @BenPious: Wow, you are right. I upgraded to OS X 10.8 recently and it hides the scrollbar in code listings now, so I missed this entire function: - (void)setupVertices. I definitely have to figure out where that setting is and change it :) Commented Oct 11, 2013 at 20:53

1 Answer 1

2

Change

glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);

to

glBufferData(GL_ARRAY_BUFFER, sizeof(vertex) * 3, Vertices, GL_STATIC_DRAW);

Your problem is that sizeof(vertices) works differently for pointers than for arrays allocated on the stack.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.