Skip to content

Instantly share code, notes, and snippets.

@Borgleader
Created September 14, 2015 23:21
Show Gist options
  • Save Borgleader/0dbc38c093fb84ed5dea to your computer and use it in GitHub Desktop.
Save Borgleader/0dbc38c093fb84ed5dea to your computer and use it in GitHub Desktop.
model_t load_obj_mesh(std::string filepath)
{
Assimp::Importer importer;
auto importFlags = aiProcess_Triangulate | aiProcess_JoinIdenticalVertices | aiProcess_SortByPType;
const aiScene* scene = importer.ReadFile(filepath.c_str(), importFlags);
if (!scene)
throw std::exception("Obj import failed.");
model_t model;
std::vector<float> vertex_buffer_data;
std::vector<index_t> index_buffer_data;
prepare_mesh_data(scene, model, vertex_buffer_data, index_buffer_data);
model.vao.bind();
model.vbo.bind();
std::size_t stride = (3 + (scene->mMeshes[0]->HasNormals() > 0 ? 3 : 0) + (scene->mMeshes[0]->HasTextureCoords(0) > 0 ? 2 : 0)) * sizeof(float);
char* offset = nullptr;
gl::BufferData(gl::ARRAY_BUFFER, vertex_buffer_data.size() * sizeof(float), vertex_buffer_data.data(), gl::STATIC_DRAW);
gl::EnableVertexAttribArray(0);
gl::VertexAttribPointer(0, 3, gl::FLOAT, gl::FALSE_, stride, offset);
offset += 3 * sizeof(float);
if (scene->mMeshes[0]->HasNormals() > 0)
{
gl::EnableVertexAttribArray(1);
gl::VertexAttribPointer(1, 3, gl::FLOAT, gl::FALSE_, stride, offset);
offset += 3 * sizeof(float);
}
if (scene->mMeshes[0]->HasTextureCoords(0) > 0)
{
gl::EnableVertexAttribArray(2);
gl::VertexAttribPointer(2, 2, gl::FLOAT, gl::FALSE_, stride, offset);
}
model.ebo.bind();
gl::BufferData(gl::ELEMENT_ARRAY_BUFFER, index_buffer_data.size() * sizeof(index_t), index_buffer_data.data(), gl::STATIC_DRAW);
model.vao.unbind();
return model;
}
#version 440 core
#include "vs_common.h"
#include "basic_shader_common.glsl"
layout (location = 0) in vec3 position;
//layout (location = 1) in vec3 normal;
layout (location = 2) in vec2 texcoord;
out VertexOut
{
vec2 texcoord;
} Out;
void main()
{
gl_Position = modelViewProjMat * vec4(position, 1.0);
Out.texcoord = texcoord;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment