I am trying to use nsarray of objective-c class called Primitive which stores primitive data such as vertices, texture coordinates, vertexBuffer, indexBuffer and texture buffer.
But it is not drawing the rectangle with texture. When I tried c struct that contains same info, it drew fine.
I am not sure where I am missing Can anyone give me some hint?
//Here is my primitive class.
//It also has Vertex class which has vertex positions and texture coordinates
// This represents one primitive, in this app, it is rectangle
@interface Primitive : NSObject
{
@public
GLuint vertexBuffer;
GLuint indexBuffer;
GLuint textureBuffer;
}
@property (nonatomic, copy) NSArray * vertices; //consist 4 vertices
- (id)initWithVertices:(NSArray *)vertices;
@end
@interface Vertex : NSObject
@property (nonatomic, copy) NSArray * position; //(x,y,z) per vertex
@property (nonatomic, copy) NSArray * textureCoordinate; //(s,t) per vertex
- (id)initWithPointX:(float)x Y:(float)y Z:(float)z;
- (void)printVal;
@end
@implementation Primitive
@synthesize vertices = m_vertices;
- (id)initWithVertices:(NSArray *)vertices
{
self = [super init];
if(self)
{
m_vertices = [[NSArray alloc] initWithArray:vertices];
}
return self;
}
@end
@implementation Vertex
@synthesize position = m_position;
@synthesize textureCoordinate = m_textureCoordinate;
- (id)initWithPointX:(float)x Y:(float)y Z:(float)z
{
self = [super init];
if(self)
{
m_position = [[NSArray alloc] initWithObjects:
[NSNumber numberWithFloat:x],
[NSNumber numberWithFloat:y],
[NSNumber numberWithFloat:z], nil];
}
return self;
}
// and here is how I generate vbo. it's in other file
- (void)setupVertexBufferObjects
{
for(Primitive * primitive in m_primitives)
{
NSLog(@"setting vbo");
glGenBuffers(1, &primitive->vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, primitive->vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(primitive.vertices),
(__bridge void *)primitive.vertices, GL_STATIC_DRAW);
glGenBuffers(1, &primitive->indexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, primitive->indexBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(Indices), Indices, GL_STATIC_DRAW);
}
}
// and this is how I render, this is also in other file than Primitive and above method
// this one worked fine when I had c struct to provide vertices and texture coordinate
glClearColor(0, 104.0/255, 55.0/255, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
GLuint positionSlot = glGetAttribLocation(m_programHandle, "Position");
glEnableVertexAttribArray(positionSlot);
glVertexAttribPointer(positionSlot, 3, GL_FLOAT, GL_FALSE,
[m_candleModel sizeOfVertex], 0);
GLuint texCoordSlot = glGetAttribLocation(m_programHandle, "TexCoordIn");
glEnableVertexAttribArray(texCoordSlot);
GLuint textureUniform = glGetUniformLocation(m_programHandle, "Texutre");
glVertexAttribPointer(texCoordSlot, 2, GL_FLOAT, GL_FALSE,
[m_candleModel sizeOfVertex], (GLvoid *)(sizeof(float) * 3));
GLuint floorTexture = [m_candleModel getTexture];
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, floorTexture);
glUniform1i(textureUniform, 0);
glDrawElements(GL_TRIANGLES, sizeof(Indices)/sizeof(Indices[0]), GL_UNSIGNED_BYTE, 0);
glDisableVertexAttribArray(positionSlot);
[m_context presentRenderbuffer:GL_RENDERBUFFER];