0

I have recently started doing some things in my iOS app using OpenGL.

I found this tutorial which has been a tremendous help:

www.raywenderlich.com/3664/opengl-es-2-0-for-iphone-tutorial .

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

const Vertex Vertices[] = { ... };
const GLubyte Indices[] = { ... };

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

I need an array of variables/structs since the content is dependent on what happens at runtime and is not static.

How do I define and create a dynamic array when I don't know the number of elements in the array until runtime?

Do I need to use malloc or something like that? I haven't come across any examples of allocating memory for an iPhone app before so I'm a little wary. Any advice or direction would be appreciated.

1
  • Yes, you'll need malloc(). Alternatively you can rely on a GCC extension and write GLFloat vertices[someVariable]; Commented Jul 19, 2012 at 7:23

1 Answer 1

3

With malloc:

    Vertex* verts;
    void Load()
    {
        int SIZE=200;
        verts=(Vertex*)malloc(sizeof(Vertex)*SIZE);//in c you dont need (Vertex*)
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Can you show how you stock verts using this? I've tried assigning the verts to Vertices but it won't allow (expected expression).

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.