Skip to content

Instantly share code, notes, and snippets.

@siliconprime-chinh
Created May 21, 2015 16:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save siliconprime-chinh/e0d1b560aec90dc81df0 to your computer and use it in GitHub Desktop.
Save siliconprime-chinh/e0d1b560aec90dc81df0 to your computer and use it in GitHub Desktop.
#import "ViewController.h"
#import "tiny_obj_loader.h"
@interface ViewController ()
@end
@implementation ViewController
std::vector<tinyobj::shape_t> shapes;
std::vector<tinyobj::material_t> materials;
- (void)viewDidLoad {
currentCalculatedMatrix = CATransform3DIdentity;
currentCalculatedMatrix = CATransform3DTranslate(currentCalculatedMatrix, 0.0, -0.2, 0.0);
currentCalculatedMatrix = CATransform3DScale(currentCalculatedMatrix, 4.5, 4.5 * (320.0/480.0), 4.5);
plainDisplayProgram = [[GLProgram alloc] initWithVertexShaderFilename:@"PlainDisplay" fragmentShaderFilename:@"PlainDisplay"];
[plainDisplayProgram addAttribute:@"position"];
plainDisplayPositionAttribute = [plainDisplayProgram attributeIndex:@"position"];
plainDisplayModelViewMatrix = [plainDisplayProgram uniformIndex:@"modelViewProjMatrix"];
[self initData];
[self renderByRotatingAroundX:0 rotatingAroundY:0];
[super viewDidLoad];
}
- (void)renderByRotatingAroundX:(float)xRotation rotatingAroundY:(float)yRotation{
glClearColor(0, 104.0/255.0, 55.0/255.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
GLfloat currentModelViewMatrix[16];
// Perform incremental rotation based on current angles in X and Y
if ((xRotation != 0.0) || (yRotation != 0.0))
{
GLfloat totalRotation = sqrt(xRotation*xRotation + yRotation*yRotation);
CATransform3D temporaryMatrix = CATransform3DRotate(currentCalculatedMatrix, totalRotation * M_PI / 180.0,
((xRotation/totalRotation) * currentCalculatedMatrix.m12 + (yRotation/totalRotation) * currentCalculatedMatrix.m11),
((xRotation/totalRotation) * currentCalculatedMatrix.m22 + (yRotation/totalRotation) * currentCalculatedMatrix.m21),
((xRotation/totalRotation) * currentCalculatedMatrix.m32 + (yRotation/totalRotation) * currentCalculatedMatrix.m31));
if ((temporaryMatrix.m11 >= -100.0) && (temporaryMatrix.m11 <= 100.0))
currentCalculatedMatrix = temporaryMatrix;
}
[self convert3DTransform:&currentCalculatedMatrix toMatrix:currentModelViewMatrix];
[plainDisplayProgram use];
glUniformMatrix4fv(plainDisplayModelViewMatrix, 1, 0, currentModelViewMatrix);
[self display];
[(GLView *)self.view presentFramebuffer];
}
- (void) initData{
NSString* filePath = [[NSBundle mainBundle] pathForResource:@"cube"
ofType:@"obj"];
std::string *inputfile = new std::string([filePath UTF8String]);
std::string err = tinyobj::LoadObj(shapes, materials, inputfile->c_str());
GLenum errorCode = 0;
// Copy data to GPU
// Vertex
size_t vertex_buffer_size = 0;
for (size_t i = 0; i < shapes.size(); i++) {
vertex_buffer_size += sizeof(float)* shapes[i].mesh.positions.size();
}
glGenBuffers(1, &vertex_buffer);
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
glBufferData(GL_ARRAY_BUFFER, vertex_buffer_size, NULL, GL_STATIC_DRAW);
vertex_buffer_size = 0;
for (size_t i = 0; i < shapes.size(); i++) {
glBufferSubData(GL_ARRAY_BUFFER, vertex_buffer_size, sizeof(float)* shapes[i].mesh.positions.size(), &shapes[i].mesh.positions[0]);
vertex_buffer_size += sizeof(float)* shapes[i].mesh.positions.size();
}
glBindBuffer(GL_ARRAY_BUFFER, 0);
// Index
size_t index_buffer_size = 0;
for (size_t i = 0; i < shapes.size(); i++) {
index_buffer_size += sizeof(unsigned int)* shapes[i].mesh.indices.size();
}
glGenBuffers(1, &index_buffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_buffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, index_buffer_size, NULL, GL_STATIC_DRAW);
index_buffer_size = 0;
for (size_t i = 0; i < shapes.size(); i++) {
glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, index_buffer_size, sizeof(unsigned int)* shapes[i].mesh.indices.size(), &shapes[i].mesh.indices[0]);
index_buffer_size += sizeof(unsigned int)* shapes[i].mesh.indices.size();
}
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
// draw multiple objects with one draw call
glGenVertexArraysOES(1, &vertex_array_object);
glBindVertexArrayOES(vertex_array_object);
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_buffer);
//glBindBuffer(GL_ARRAY_BUFFER, 0);
//glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindVertexArrayOES(0);
errorCode = glGetError();
if (errorCode != 0){
}
}
- (void)display{
GLenum errorCode = 0;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindVertexArrayOES(vertex_array_object);
glEnableVertexAttribArray(0);
size_t vertex_buffer_size = 0;
for (size_t i = 0; i < shapes.size(); i++) {
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)vertex_buffer_size);
glDrawElements(GL_TRIANGLES, (GLsizei)(sizeof(int)*shapes[i].mesh.indices.size()), GL_UNSIGNED_INT, (void*)0);
vertex_buffer_size += sizeof(float)* shapes[i].mesh.positions.size();
if (errorCode != 0){
}
}
glDisableVertexAttribArray(0);
glBindVertexArrayOES(0);
// glUseProgram(0);
}
- (void)convert3DTransform:(CATransform3D *)transform3D toMatrix:(GLfloat *)matrix;
{
// struct CATransform3D
// {
// CGFloat m11, m12, m13, m14;
// CGFloat m21, m22, m23, m24;
// CGFloat m31, m32, m33, m34;
// CGFloat m41, m42, m43, m44;
// };
matrix[0] = (GLfloat)transform3D->m11;
matrix[1] = (GLfloat)transform3D->m12;
matrix[2] = (GLfloat)transform3D->m13;
matrix[3] = (GLfloat)transform3D->m14;
matrix[4] = (GLfloat)transform3D->m21;
matrix[5] = (GLfloat)transform3D->m22;
matrix[6] = (GLfloat)transform3D->m23;
matrix[7] = (GLfloat)transform3D->m24;
matrix[8] = (GLfloat)transform3D->m31;
matrix[9] = (GLfloat)transform3D->m32;
matrix[10] = (GLfloat)transform3D->m33;
matrix[11] = (GLfloat)transform3D->m34;
matrix[12] = (GLfloat)transform3D->m41;
matrix[13] = (GLfloat)transform3D->m42;
matrix[14] = (GLfloat)transform3D->m43;
matrix[15] = (GLfloat)transform3D->m44;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment