Reputation: 23
I have written a class that currently allows the loading of 3D Models via an array and a variable(specifying the amount of variables in the array). The code seems fine to me but doesn't draw anything on the screen.
This is the Vertex Struct.
typedef struct
{
float pos[3];
float texCoord[2];
float colour[4];
} Vertex;
This is the how the Model is initialised.
- (id)VModelWithArray:(Vertex[])Vertices count:(unsigned short)count
{
self.Vertices = Vertices;
self.count = count;
return self;
}
The two class variables are declared in the header like so.
@property (nonatomic, assign) Vertex *Vertices;
@property (nonatomic, assign) unsigned short count;
It is called in my view controller like so.
Vertex array[] = {
{{1, -1, 0}, {1, 0}, {1, 0, 0, 1}},
{{1, 1, 0}, {1, 1}, {0, 1, 0, 1}},
{{-1, 1, 0}, {0, 1}, {1, 1, 1, 1}}
};
unsigned short elements = sizeof(array)/sizeof(Vertex);
self.model = [[VModel alloc] VModelWithArray:array count:elements];
The Model class is declared in the header like so.
@property (strong, nonatomic) VModel *model;
The VBOs are created by this method.
- (void)CreateVBO
{
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex)*self.count, self.Vertices, GL_STATIC_DRAW);
}
It is called after the Model is initialised in the view controller like so.
[self.model CreateVBO];
The Model is rendered by this method.
- (void)Render
{
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid *) offsetof(Vertex, pos));
glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid *) offsetof(Vertex, texCoord));
glEnableVertexAttribArray(GLKVertexAttribColor);
glVertexAttribPointer(GLKVertexAttribColor, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid *) offsetof(Vertex, colour));
glDrawArrays(GL_TRIANGLES, 0, self.count);
glDisableVertexAttribArray(GLKVertexAttribPosition);
glDisableVertexAttribArray(GLKVertexAttribTexCoord0);
glDisableVertexAttribArray(GLKVertexAttribColor);
}
It is called in this method.
- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
{
glClearColor(0.5, 0.5, 0.5, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
[self.effect prepareToDraw];
[self.model Render];
}
Any help would be very much appreciated.
Upvotes: 1
Views: 158
Reputation: 23
I figured it out, I accidentally left out these lines when merging the code from a previous project.
[EAGLContext setCurrentContext:self.context];
self.effect = [[GLKBaseEffect alloc] init];
Upvotes: 1