#include "M_object.h" #include "M_camera.h" void M_createViewModel(M_ViewModel *model, int num_vertices, float *vert, int num_indices, unsigned int *indices) { model->num_indices = num_indices; glGenVertexArrays(1, &model->VAO); glBindVertexArray(model->VAO); glGenBuffers(1, &model->VBO); glBindBuffer(GL_ARRAY_BUFFER, model->VBO); glBufferData(GL_ARRAY_BUFFER, sizeof(float)*num_vertices*3, vert, GL_STATIC_DRAW); glGenBuffers(1, &model->EBO); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, model->EBO); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int)*num_indices, indices, GL_STATIC_DRAW); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0); glEnableVertexAttribArray(0); } void M_killViewModel(M_ViewModel *model) { glDeleteVertexArrays(1, &model->VAO); glDeleteBuffers(1, &model->VBO); glDeleteBuffers(1, &model->EBO); } M_ViewModel M_loadViewModel(const char* filename) { const struct aiScene* scene = aiImportFile(filename, aiProcess_CalcTangentSpace | aiProcess_Triangulate | aiProcess_JoinIdenticalVertices | aiProcess_SortByPType); struct aiMesh *mesh = scene->mMeshes[0]; unsigned int *indices = malloc(mesh->mNumFaces*3*sizeof(unsigned int)); int index = 0; for (unsigned int i=0;imNumFaces;i++) { memcpy(&indices[index], mesh->mFaces[i].mIndices, 3*sizeof(unsigned int)); index+=3; } M_ViewModel model; M_createViewModel(&model, mesh->mNumVertices, (float*)mesh->mVertices, mesh->mNumFaces*3, indices); free(indices); return model; } void M_bindViewModel(M_ViewModel *model) { glBindVertexArray(model->VAO); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, model->EBO); } void M_createObject(M_Object *obj, M_ObjectInfo object_info) { switch (object_info.type) { case M_OBJECT_CUSTOM: obj->model = object_info.model; break; } obj->type = object_info.type; glm_mat4_identity(obj->transform); glm_vec3_zero(obj->pos); glm_vec3_one(obj->size); obj->rotation.angle = 0.0f; glm_vec3_zero(obj->rotation.direction); } void M_moveObject(M_Object *obj, vec3 pos) { glm_vec3_copy( pos, obj->pos ); M_updateObjectTransform(obj); } void M_scaleObject(M_Object *obj, vec3 scale) { glm_vec3_copy( scale, obj->size ); M_updateObjectTransform(obj); } void M_rotateObject(M_Object *obj, float angle, vec3 direction) { obj->rotation.angle = angle; glm_vec3_copy( direction, obj->rotation.direction ); M_updateObjectTransform(obj); } inline void M_updateObjectTransform(M_Object *obj) { glm_mat4_identity(obj->transform); glm_translate(obj->transform, obj->pos); glm_rotate(obj->transform, obj->rotation.angle, obj->rotation.direction); glm_scale(obj->transform, obj->size); }