#include #include "Mesh.h" #include "../lib/loaders/tinyobj.h" Mesh createMesh(const char* obj_file) { Scratch temp = scratchStart(0, 0); Mesh result = {0}; tinyobj::ObjReader reader = tinyobj::ObjReader(); bool success = reader.ParseFromFile(obj_file); log("%s\n", reader.Error().c_str()); const tinyobj::attrib_t attrib = reader.GetAttrib(); std::vector indices_t = reader.GetShapes().at(0).mesh.indices; list indices = PushFullList(temp.arena, uint32, indices_t.size()); list vertices = PushFullList(temp.arena, real32, 3*indices_t.size()); list normals = PushFullList(temp.arena, real32, 3*indices_t.size()); list texcoords = PushFullList(temp.arena, real32, 2*indices_t.size()); for (int i = 0; i < indices_t.size(); i++) { tinyobj::index_t vertex_data = indices_t[i]; vertices.data[3*i] = attrib.vertices[3*vertex_data.vertex_index]; vertices.data[3*i+1] = attrib.vertices[3*vertex_data.vertex_index + 1]; vertices.data[3*i+2] = attrib.vertices[3*vertex_data.vertex_index + 2]; normals.data[3*i] = attrib.normals[3*vertex_data.normal_index]; normals.data[3*i+1] = attrib.normals[3*vertex_data.normal_index + 1]; normals.data[3*i+2] = attrib.normals[3*vertex_data.normal_index + 2]; texcoords.data[2*i] = attrib.texcoords[2*vertex_data.texcoord_index]; texcoords.data[2*i+1] = attrib.texcoords[2*vertex_data.texcoord_index + 1]; indices.data[i] = i; } result.num_indices = indices_t.size(); glGenVertexArrays(1, &result.vao); glGenBuffers(1, &result.vbo_xyz); glGenBuffers(1, &result.vbo_uv); glGenBuffers(1, &result.vbo_norm); //glGenBuffers(1, &ebo); glBindVertexArray(result.vao); glBindBuffer(GL_ARRAY_BUFFER, result.vbo_xyz); glBufferData(GL_ARRAY_BUFFER, vertices.length * sizeof(float), vertices.data, GL_STATIC_DRAW); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0); glEnableVertexAttribArray(0); glBindBuffer(GL_ARRAY_BUFFER, result.vbo_uv); glBufferData(GL_ARRAY_BUFFER, texcoords.length * sizeof(float), texcoords.data, GL_STATIC_DRAW); glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)0); glEnableVertexAttribArray(1); glBindBuffer(GL_ARRAY_BUFFER, result.vbo_norm); glBufferData(GL_ARRAY_BUFFER, normals.length * sizeof(float), normals.data, GL_STATIC_DRAW); glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0); glEnableVertexAttribArray(2); //glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo); //glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), indices.data(), GL_STATIC_DRAW); scratchEnd(temp); return result; } Mesh createMesh(const Shape* shape) { Mesh result = {0}; result.num_indices = shape->indices_size; glGenVertexArrays(1, &result.vao); glGenBuffers(1, &result.vbo_xyz); glGenBuffers(1, &result.vbo_uv); glGenBuffers(1, &result.ebo); glBindVertexArray(result.vao); glBindBuffer(GL_ARRAY_BUFFER, result.vbo_xyz); glBufferData(GL_ARRAY_BUFFER, shape->xyz_size * sizeof(float), shape->xyz, GL_STATIC_DRAW); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0); glEnableVertexAttribArray(0); glBindBuffer(GL_ARRAY_BUFFER, result.vbo_uv); glBufferData(GL_ARRAY_BUFFER, shape->uv_size * sizeof(float), shape->uv, GL_STATIC_DRAW); glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)0); glEnableVertexAttribArray(1); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, result.ebo); glBufferData(GL_ELEMENT_ARRAY_BUFFER, shape->indices_size * sizeof(unsigned int), shape->indices, GL_STATIC_DRAW); return result; }