Your main problem here is that you can't just take the sizeof an array that is an instance variable because it's a pointer which will return a size of 8. Instead you're going to have to save the count of the array somewhere else as another instance variable (or use numOfVertices) and multiply it by the sizeof(int)
. So something like glBufferData(GL_ARRAY_BUFFER, numOfVariable*sizeof(int), iVertices, GL_STATIC_DRAW);
should work for your case.
Dynamic allocate array of struct for Open GL in Objective-C
-
29-06-2022 - |
Question
I'm beginner in Open GL but I already can draw simple triangle, rectangle etc.
My problem is:
I have the structure and static array of that structure
typedef struct {
GLKVector3 Position;
} Vertex;
const Vertex Vertices[] = {
{{0.0, 0.0, 0.0}},
{{0.5, 0.0, 0.0}},
{{0.5, 0.5, 0.0}},
{{0.0, 0.5, 0.0}},
{{0.0, 0.0, 0.0}}
};
...some code
but I need array of vertices create dynamically... :(
Example:
typedef struct {
GLKVector3 Position;
} Vertex;
instance variable - iVertices of type Vertex
- (void) viewDidLoad {
int numOfVertices = 0;
Vertex vertices[] = {{0.0, 0.0, 0.0}};
[self addVertex:vertices atIndex:numOfVertices];
numOfVertices ++;
Vertex vertices[] = {{0.5, 0.0, 0.0}};
[self addVertex:vertices atIndex:numOfVertices];
numOfVertices ++;
Vertex vertices[] = {{0.5, 0.5, 0.0}};
[self addVertex:vertices atIndex:numOfVertices];
}
- (void) addVertex:(Vertex) vertex atIndex:(int) num {
iVertices[num] = vertex;
}
...and somewhere
glBufferData(GL_ARRAY_BUFFER,
sizeof(iVertices),
iVertices,
GL_STATIC_DRAW);
and this is not allowed in Objective-C or I don't know how to do it :(
malloc nor callow doesn't help to me...
Thanks a lot!
La solution
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow