Obj loader simple implementation
This commit is contained in:
parent
def845e741
commit
9e699b0e94
2
3rdParty/tiny_obj_loader.cpp
vendored
Normal file
2
3rdParty/tiny_obj_loader.cpp
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
#define TINYOBJLOADER_IMPLEMENTATION
|
||||||
|
#include "tiny_obj_loader.h"
|
3499
3rdParty/tiny_obj_loader.h
vendored
Normal file
3499
3rdParty/tiny_obj_loader.h
vendored
Normal file
File diff suppressed because it is too large
Load Diff
@ -33,6 +33,7 @@ include(CheckCSourceCompiles)
|
|||||||
# Find libraries
|
# Find libraries
|
||||||
find_package(SFML REQUIRED)
|
find_package(SFML REQUIRED)
|
||||||
include_directories(sfml::sfml)
|
include_directories(sfml::sfml)
|
||||||
|
include_directories("3rdParty")
|
||||||
|
|
||||||
# define src/headers files groups
|
# define src/headers files groups
|
||||||
include(srcs.list)
|
include(srcs.list)
|
||||||
@ -50,9 +51,11 @@ target_precompile_headers(${PROJECT_NAME} PRIVATE
|
|||||||
"$<$<COMPILE_LANGUAGE:CXX>:<stdexcept$<ANGLE-R>>"
|
"$<$<COMPILE_LANGUAGE:CXX>:<stdexcept$<ANGLE-R>>"
|
||||||
"$<$<COMPILE_LANGUAGE:CXX>:<vector$<ANGLE-R>>"
|
"$<$<COMPILE_LANGUAGE:CXX>:<vector$<ANGLE-R>>"
|
||||||
"$<$<COMPILE_LANGUAGE:CXX>:<memory$<ANGLE-R>>"
|
"$<$<COMPILE_LANGUAGE:CXX>:<memory$<ANGLE-R>>"
|
||||||
|
"$<$<COMPILE_LANGUAGE:CXX>:${CMAKE_CURRENT_SOURCE_DIR}/3rdParty/tiny_obj_loader.h>"
|
||||||
"$<$<COMPILE_LANGUAGE:CXX>:${CMAKE_CURRENT_SOURCE_DIR}/Engine/Utils/3DMaths.hpp>"
|
"$<$<COMPILE_LANGUAGE:CXX>:${CMAKE_CURRENT_SOURCE_DIR}/Engine/Utils/3DMaths.hpp>"
|
||||||
)
|
)
|
||||||
target_sources(${PROJECT_NAME} PUBLIC ${MAIN_SCRS} ${UTILS_SCRS} ${MISC_SCRS} ${GAME_SCRS} ${GRAPHS_SCRS} ${SOUNDS_SCRS})
|
target_sources(${PROJECT_NAME} PUBLIC ${MAIN_SCRS} ${UTILS_SCRS} ${MISC_SCRS} ${GAME_SCRS} ${GRAPHS_SCRS} ${SOUNDS_SCRS})
|
||||||
|
target_sources(${PROJECT_NAME} PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/3rdParty/tiny_obj_loader.cpp")
|
||||||
target_link_libraries(${PROJECT_NAME} sfml::sfml)
|
target_link_libraries(${PROJECT_NAME} sfml::sfml)
|
||||||
set_target_properties(${PROJECT_NAME} PROPERTIES
|
set_target_properties(${PROJECT_NAME} PROPERTIES
|
||||||
OUTPUT_NAME ${PROJECT_NAME}${BUILDNAME_SUFFIX}
|
OUTPUT_NAME ${PROJECT_NAME}${BUILDNAME_SUFFIX}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
#include "3DRenderer.hpp"
|
#include "3DRenderer.hpp"
|
||||||
|
|
||||||
#include "../World/DbgCube.hpp"
|
#include "../World/DbgCube.hpp"
|
||||||
|
#include "../World/Tank.hpp"
|
||||||
|
|
||||||
|
|
||||||
// Rendering pipeline:
|
// Rendering pipeline:
|
||||||
@ -35,6 +36,9 @@ Graphic3DRenderer::Graphic3DRenderer() {
|
|||||||
mRenderList.push_back(std::make_shared<ObjectDbgCube>());
|
mRenderList.push_back(std::make_shared<ObjectDbgCube>());
|
||||||
mRenderList.back()->SetPosition(-31.f, 16.f, 24.f);
|
mRenderList.back()->SetPosition(-31.f, 16.f, 24.f);
|
||||||
mRenderList.back()->SetScale(10.0f);
|
mRenderList.back()->SetScale(10.0f);
|
||||||
|
mRenderList.push_back(std::make_shared<Tank>());
|
||||||
|
mRenderList.back()->SetPosition(0.f, 0.f, 0.f);
|
||||||
|
mRenderList.back()->SetScale(25.0f);
|
||||||
}
|
}
|
||||||
|
|
||||||
Graphic3DRenderer::~Graphic3DRenderer() {}
|
Graphic3DRenderer::~Graphic3DRenderer() {}
|
||||||
@ -53,6 +57,7 @@ void Graphic3DRenderer::Draw(sf::RenderTexture& context) {
|
|||||||
mRenderList[0]->SetRotation(thetaAngle, 0.f, thetaAngle * 0.5f);
|
mRenderList[0]->SetRotation(thetaAngle, 0.f, thetaAngle * 0.5f);
|
||||||
mRenderList[1]->SetRotation(thetaAngle2, 0.f, thetaAngle2 * 0.5f);
|
mRenderList[1]->SetRotation(thetaAngle2, 0.f, thetaAngle2 * 0.5f);
|
||||||
mRenderList[2]->SetRotation(thetaAngle3, 0.f, thetaAngle3 * 0.5f);
|
mRenderList[2]->SetRotation(thetaAngle3, 0.f, thetaAngle3 * 0.5f);
|
||||||
|
mRenderList[3]->SetRotation(0.f, thetaAngle, 0.f);
|
||||||
|
|
||||||
M3D_MATRIX viewProjMat = mMainCamera->GetView() * mMainCamera->GetProj();
|
M3D_MATRIX viewProjMat = mMainCamera->GetView() * mMainCamera->GetProj();
|
||||||
sf::Vertex v_tri[4];
|
sf::Vertex v_tri[4];
|
||||||
|
@ -0,0 +1,6 @@
|
|||||||
|
#include "Tank.hpp"
|
||||||
|
|
||||||
|
|
||||||
|
Tank::Tank() {
|
||||||
|
LoadMeshFromObjFile("tank_sample.obj");
|
||||||
|
}
|
13
Engine/World/Tank.hpp
Normal file
13
Engine/World/Tank.hpp
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "WorldObject.hpp"
|
||||||
|
|
||||||
|
|
||||||
|
class Tank final : public WorldObjectAbstract<Tank> {
|
||||||
|
public:
|
||||||
|
Tank();
|
||||||
|
~Tank() {}
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
};
|
@ -44,10 +44,11 @@ public:
|
|||||||
|
|
||||||
const Mesh& GetObjectMesh() const noexcept override { return mMesh; }
|
const Mesh& GetObjectMesh() const noexcept override { return mMesh; }
|
||||||
|
|
||||||
|
void LoadMeshFromObjFile(std::string file);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
inline static Mesh mMesh;
|
inline static Mesh mMesh;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
template<class D>
|
#include "WorldObject.tpp"
|
||||||
inline WorldObjectAbstract<D>::~WorldObjectAbstract() {}
|
|
64
Engine/World/WorldObject.tpp
Normal file
64
Engine/World/WorldObject.tpp
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
#include "WorldObject.hpp"
|
||||||
|
|
||||||
|
|
||||||
|
template<class D>
|
||||||
|
inline WorldObjectAbstract<D>::~WorldObjectAbstract() {}
|
||||||
|
|
||||||
|
template<class D>
|
||||||
|
void WorldObjectAbstract<D>::LoadMeshFromObjFile(std::string file) {
|
||||||
|
tinyobj::ObjReader reader;
|
||||||
|
tinyobj::ObjReaderConfig reader_config;
|
||||||
|
reader_config.mtl_search_path = "./";
|
||||||
|
reader_config.triangulate = false;
|
||||||
|
reader_config.vertex_color = false;
|
||||||
|
|
||||||
|
if (!reader.ParseFromFile(file, reader_config))
|
||||||
|
throw new std::runtime_error("Obj loader failure");
|
||||||
|
|
||||||
|
const tinyobj::attrib_t& attrib = reader.GetAttrib();
|
||||||
|
const std::vector<tinyobj::shape_t>& shapes = reader.GetShapes();
|
||||||
|
|
||||||
|
try {
|
||||||
|
mMesh.vertices.reserve(attrib.vertices.size());
|
||||||
|
} catch (const std::length_error& ex) {
|
||||||
|
throw ex;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reinterprete vertices to our format
|
||||||
|
for (size_t i = 0; i < attrib.vertices.size(); i += 3)
|
||||||
|
mMesh.vertices.push_back(Vertex(attrib.vertices[i], attrib.vertices[i+1], attrib.vertices[i+2]));
|
||||||
|
|
||||||
|
// Loop over shapes/subparts
|
||||||
|
for (size_t s = 0; s < shapes.size(); s++) {
|
||||||
|
MeshPart subpart;
|
||||||
|
|
||||||
|
// Loop over faces(polygon)
|
||||||
|
size_t index_offset = 0;
|
||||||
|
for (size_t f = 0; f < shapes[s].mesh.num_face_vertices.size(); f++) {
|
||||||
|
size_t fv = size_t(shapes[s].mesh.num_face_vertices[f]);
|
||||||
|
|
||||||
|
try {
|
||||||
|
subpart.indices.reserve(shapes[s].mesh.indices.size());
|
||||||
|
} catch (const std::length_error& ex) {
|
||||||
|
throw ex;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Loop over indices in the face.
|
||||||
|
for (size_t v = 0; v < fv; v++) {
|
||||||
|
subpart.indices.push_back(shapes[s].mesh.indices[index_offset + v].vertex_index);
|
||||||
|
// Check if `normal_index` is zero or positive. negative = no normal data
|
||||||
|
//if (idx.normal_index >= 0) {}
|
||||||
|
|
||||||
|
// Check if `texcoord_index` is zero or positive. negative = no texcoord data
|
||||||
|
//if (idx.texcoord_index >= 0) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
index_offset += fv;
|
||||||
|
|
||||||
|
// per-face material
|
||||||
|
//shapes[s].mesh.material_ids[f];
|
||||||
|
}
|
||||||
|
|
||||||
|
mMesh.parts.push_back(subpart);
|
||||||
|
}
|
||||||
|
}
|
@ -7,6 +7,8 @@ set(MAIN_SCRS
|
|||||||
set(UTILS_SCRS
|
set(UTILS_SCRS
|
||||||
#Engine/Utils/3DMaths.inl
|
#Engine/Utils/3DMaths.inl
|
||||||
#Engine/Utils/3DMaths.hpp
|
#Engine/Utils/3DMaths.hpp
|
||||||
|
#3rdParty/tiny_obj_loader.cpp
|
||||||
|
#3rdParty/tiny_obj_loader.h
|
||||||
Engine/Utils/MeshHelper.hpp
|
Engine/Utils/MeshHelper.hpp
|
||||||
Engine/Utils/Timers.hpp
|
Engine/Utils/Timers.hpp
|
||||||
Engine/Utils/Perfs.cpp
|
Engine/Utils/Perfs.cpp
|
||||||
@ -20,12 +22,14 @@ set(MISC_SCRS
|
|||||||
Engine/Misc/Fonts.hpp
|
Engine/Misc/Fonts.hpp
|
||||||
)
|
)
|
||||||
set(GAME_SCRS
|
set(GAME_SCRS
|
||||||
|
Engine/World/WorldObject.tpp
|
||||||
Engine/World/WorldObject.hpp
|
Engine/World/WorldObject.hpp
|
||||||
Engine/World/DbgCube.cpp
|
Engine/World/DbgCube.cpp
|
||||||
Engine/World/DbgCube.hpp
|
Engine/World/DbgCube.hpp
|
||||||
Engine/World/Arena.cpp
|
Engine/World/Arena.cpp
|
||||||
Engine/World/Player.cpp
|
Engine/World/Player.cpp
|
||||||
Engine/World/Tank.cpp
|
Engine/World/Tank.cpp
|
||||||
|
Engine/World/Tank.hpp
|
||||||
)
|
)
|
||||||
set(GRAPHS_SCRS
|
set(GRAPHS_SCRS
|
||||||
Engine/Graphics/3DRenderer.cpp
|
Engine/Graphics/3DRenderer.cpp
|
||||||
|
Loading…
x
Reference in New Issue
Block a user