Skip to content
Snippets Groups Projects
Commit e8ee5281 authored by Martin Štourač's avatar Martin Štourač
Browse files

first attempt at .obj loading (bad normal indices)

parent 7396af5d
No related branches found
No related tags found
No related merge requests found
# Blender v2.90.1 OBJ File: ''
# www.blender.org
mtllib untitled.mtl
o Cube
v 1.000000 -1.000000 1.000000
v 1.000000 1.000000 1.000000
v 1.000000 -1.000000 -1.000000
v 1.000000 1.000000 -1.000000
v -1.000000 -1.000000 1.000000
v -1.000000 1.000000 1.000000
v -1.000000 -1.000000 -1.000000
v -1.000000 1.000000 -1.000000
vt 0.875000 0.500000
vt 0.625000 0.750000
vt 0.625000 0.500000
vt 0.375000 1.000000
vt 0.375000 0.750000
vt 0.625000 0.000000
vt 0.375000 0.250000
vt 0.375000 0.000000
vt 0.375000 0.500000
vt 0.125000 0.750000
vt 0.125000 0.500000
vt 0.625000 0.250000
vt 0.875000 0.750000
vt 0.625000 1.000000
vn 0.0000 -1.0000 0.0000
vn 0.0000 0.0000 -1.0000
vn -1.0000 0.0000 0.0000
vn 0.0000 1.0000 0.0000
vn 1.0000 0.0000 0.0000
vn 0.0000 0.0000 1.0000
usemtl Material
s off
f 5/1/1 3/2/1 1/3/1
f 3/2/2 8/4/2 4/5/2
f 7/6/3 6/7/3 8/8/3
f 2/9/4 8/10/4 6/11/4
f 1/3/5 4/5/5 2/9/5
f 5/12/6 2/9/6 6/7/6
f 5/1/1 7/13/1 3/2/1
f 3/2/2 7/14/2 8/4/2
f 7/6/3 5/12/3 6/7/3
f 2/9/4 4/5/4 8/10/4
f 1/3/5 3/2/5 4/5/5
f 5/12/6 1/3/6 2/9/6
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
......@@ -46,6 +46,9 @@ add_library(${THIS_TARGET_NAME}
./include/gfx/aabb.hpp
./src/aabb.cpp
./include/gfx/obj_loader.hpp
./src/obj_loader.cpp
# ./include/osi/opengl.hpp
# ./include/osi/gui.hpp
)
......
#ifndef OBJ_LOADER_INCLUDED
#define OBJ_LOADER_INCLUDED
#include <vector>
#include <string>
#include <stdexcept>
#include <memory>
#include <tiny_obj_loader.h>
#include <gfx/object.hpp>
using object_ptr = std::shared_ptr<Object>;
object_ptr load_object(std::string const& model_path);
#endif
\ No newline at end of file
#include <gfx/obj_loader.hpp>
object_ptr load_object(std::string const& model_path)
{
tinyobj::attrib_t attrib;
std::vector<tinyobj::shape_t> shapes;
std::vector<tinyobj::material_t> objmaterials;
std::string warn, err;
if (!tinyobj::LoadObj(&attrib, &shapes, &objmaterials, &warn, &err, model_path.c_str()))
throw(std::runtime_error(warn + err));
std::vector<unsigned int> indices;
for(auto shape = shapes.begin(); shape < shapes.end(); ++shape)
{
const std::vector<tinyobj::index_t> &obj_indices = shape->mesh.indices;
// const std::vector<int>& material_ids = shape->mesh.material_ids;
for (auto index = 0; index < obj_indices.size(); index += 1)
{
indices.emplace_back(obj_indices[index].vertex_index);
}
}
object_ptr result = std::make_shared<Object>(attrib.vertices, indices, attrib.normals);
return std::move(result);
}
\ No newline at end of file
......@@ -6,14 +6,15 @@
#include <glad/glad.h>
#include <gfx/shader.hpp>
#include <gfx/render.hpp>
#include <gfx/obj_loader.hpp>
#include <iostream>
#include <vector>
#include <filesystem>
#include <glm/gtc/constants.hpp>
#include <glm/gtx/intersect.hpp>
#include <limits>
#include <algorithm>
#include <tiny_obj_loader.h>
namespace studio {
std::vector<float> obj_vertices =
......@@ -291,6 +292,22 @@ void Simulator::createScene()
grid->setSelectability(false);
scene.back()->addObject(std::move(grid));
/* ADD DEER */
scene.emplace_back(std::make_shared<Node>(std::make_shared<Frame>(glm::vec3(0, 2, 0))));
object_ptr deer = load_object("./data/models/deer.obj");
scene.back()->getFrame()->setScale(glm::vec3(0.01f, 0.01f, 0.01f));
scene.back()->addObject(std::move(deer));
/* ADD .OBJ CUBE */
scene.emplace_back(std::make_shared<Node>(std::make_shared<Frame>(glm::vec3(-3, 2, 0))));
object_ptr file_cube = load_object("./data/models/cube.obj");
scene.back()->addObject(std::move(file_cube));
/* ADD .OBJ VASE */
scene.emplace_back(std::make_shared<Node>(std::make_shared<Frame>(glm::vec3(2, 1, -1))));
object_ptr vase = load_object("./data/models/smooth_vase.obj");
scene.back()->addObject(std::move(vase));
/* ADD CAMERA */
scene.emplace_back(std::make_shared<Node>(std::make_shared<Frame>(glm::vec3(0.0f, 1.0f, 6.0f))));
scene.back()->addObject(std::make_shared<Camera>(60.0f, scene.back()->getFrame()));
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment