Skip to content

Commit 06c1e41

Browse files
committed
- Dev: utils.h moved to a more general place
1 parent 18dc000 commit 06c1e41

File tree

11 files changed

+106
-31
lines changed

11 files changed

+106
-31
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ target_link_libraries(ImGuiFileDialog PUBLIC imgui )
5555

5656
# Setup sources
5757
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/add_module_files.cmake)
58-
set(ENGINE_SOURCES "")
58+
set(ENGINE_SOURCES "src/utils.cpp")
5959
set(ENGINE_HEADERS "")
6060
add_module_files(core ENGINE_SOURCES ENGINE_HEADERS)
6161
add_module_files(graphics ENGINE_SOURCES ENGINE_HEADERS)

include/engine/common.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@
3232
#include <backends/imgui_impl_vulkan.h>
3333
#include <cstdlib>
3434
#include <fstream>
35+
#define GLM_ENABLE_EXPERIMENTAL
3536
#include <glm/glm.hpp>
37+
#include <glm/gtc/matrix_transform.hpp>
38+
#include <glm/gtx/hash.hpp>
3639
#include <imgui.h>
3740
#include <iostream>
3841
#include <map>

include/engine/graphics/device.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#define CONTEXT_H
1111

1212
#include <engine/common.h>
13+
#include <engine/utils.h>
1314

1415
#include <engine/graphics/accel.h>
1516
#include <engine/graphics/command_buffer.h>
@@ -20,7 +21,6 @@
2021
#include <engine/graphics/swapchain.h>
2122
#include <engine/graphics/utilities/bootstrap.h>
2223
#include <engine/graphics/utilities/initializers.h>
23-
#include <engine/graphics/utilities/utils.h>
2424

2525
VULKAN_ENGINE_NAMESPACE_BEGIN
2626

include/engine/graphics/utilities/bootstrap.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#ifndef BOOTSTRAP_H
1010
#define BOOTSTRAP_H
1111

12-
#include <engine/graphics/utilities/utils.h>
12+
#include <engine/utils.h>
1313

1414
VULKAN_ENGINE_NAMESPACE_BEGIN
1515

include/engine/graphics/vao.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
#ifndef VAO_H
1010
#define VAO_H
1111

12+
#include <engine/utils.h>
1213
#include <engine/graphics/buffer.h>
13-
#include <engine/graphics/utilities/utils.h>
1414

1515
VULKAN_ENGINE_NAMESPACE_BEGIN
1616

include/engine/systems/renderers/renderer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class BaseRenderer
5757
std::vector<Core::BasePass*> m_passes;
5858

5959
/*Automatic deletion queue*/
60-
Graphics::Utils::DeletionQueue m_deletionQueue;
60+
Utils::DeletionQueue m_deletionQueue;
6161

6262
/*Query*/
6363
uint32_t m_currentFrame = 0;

include/engine/tools/loaders.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ void load_OBJ(Core::Mesh* const mesh,
3434
bool importMaterials = false,
3535
bool calculateTangents = false,
3636
bool overrideGeometry = false);
37+
void load_OBJ2(Core::Mesh* const mesh,
38+
const std::string fileName,
39+
bool importMaterials = false,
40+
bool calculateTangents = false,
41+
bool overrideGeometry = false);
3742

3843
void load_PLY(Core::Mesh* const mesh,
3944
const std::string fileName,
Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,13 @@
1212
#include <chrono>
1313
#include <deque>
1414
#include <engine/common.h>
15-
#include <engine/graphics/utilities/initializers.h>
1615
#include <functional>
1716

18-
#define GLM_ENABLE_EXPERIMENTAL
19-
#include <glm/gtx/hash.hpp>
20-
2117
VULKAN_ENGINE_NAMESPACE_BEGIN
2218

23-
namespace Graphics {
24-
2519
namespace Utils {
2620

2721

28-
29-
3022
struct DeletionQueue {
3123
std::deque<std::function<void()>> deletors;
3224

@@ -61,12 +53,12 @@ class ManualTimer
6153
}
6254
};
6355

64-
struct memory_buffer : public std::streambuf {
56+
struct MemoryBuffer : public std::streambuf {
6557
char* p_start{nullptr};
6658
char* p_end{nullptr};
6759
size_t size;
6860

69-
memory_buffer(char const* first_elem, size_t size)
61+
MemoryBuffer(char const* first_elem, size_t size)
7062
: p_start(const_cast<char*>(first_elem))
7163
, p_end(p_start + size)
7264
, size(size) {
@@ -86,9 +78,9 @@ struct memory_buffer : public std::streambuf {
8678
}
8779
};
8880

89-
struct memory_stream : virtual memory_buffer, public std::istream {
90-
memory_stream(char const* first_elem, size_t size)
91-
: memory_buffer(first_elem, size)
81+
struct MemoryStream : virtual MemoryBuffer, public std::istream {
82+
MemoryStream(char const* first_elem, size_t size)
83+
: MemoryBuffer(first_elem, size)
9284
, std::istream(static_cast<std::streambuf*>(this)) {
9385
}
9486
};
@@ -115,11 +107,10 @@ inline std::vector<uint8_t> read_file_binary(const std::string& pathToFile) {
115107
return fileBufferBytes;
116108
}
117109

118-
119110
Vec3 get_tangent_gram_smidt(Vec3& p1, Vec3& p2, Vec3& p3, glm::vec2& uv1, glm::vec2& uv2, glm::vec2& uv3, Vec3 normal);
120111

121112
}; // namespace Utils
122-
} // namespace Graphics
113+
123114

124115
VULKAN_ENGINE_NAMESPACE_END
125116

src/core/geometries/geometry.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ void Geometry::compute_tangents_gram_smidt(std::vector<Graphics::Vertex>& vertic
145145
size_t i1 = indices[i + 1];
146146
size_t i2 = indices[i + 2];
147147

148-
Vec3 tangent = Graphics::Utils::get_tangent_gram_smidt(vertices[i0].pos,
148+
Vec3 tangent = Utils::get_tangent_gram_smidt(vertices[i0].pos,
149149
vertices[i1].pos,
150150
vertices[i2].pos,
151151
vertices[i0].texCoord,
@@ -160,7 +160,7 @@ void Geometry::compute_tangents_gram_smidt(std::vector<Graphics::Vertex>& vertic
160160
else
161161
for (size_t i = 0; i < vertices.size(); i += 3)
162162
{
163-
Vec3 tangent = Graphics::Utils::get_tangent_gram_smidt(vertices[i].pos,
163+
Vec3 tangent = Utils::get_tangent_gram_smidt(vertices[i].pos,
164164
vertices[i + 1].pos,
165165
vertices[i + 2].pos,
166166
vertices[i].texCoord,

src/tools/loaders.cpp

Lines changed: 84 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,87 @@ void VKFW::Tools::Loaders::load_OBJ(Core::Mesh* const mesh,
148148
return;
149149
}
150150

151+
void VKFW::Tools::Loaders::load_OBJ2(Core::Mesh* const mesh,
152+
const std::string fileName,
153+
bool importMaterials,
154+
bool calculateTangents,
155+
bool overrideGeometry) {
156+
157+
// Preparing output
158+
tinyobj::attrib_t attrib;
159+
std::vector<tinyobj::shape_t> shapes;
160+
std::vector<tinyobj::material_t> materials;
161+
std::string warn;
162+
std::string err;
163+
164+
tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err, fileName.c_str(), importMaterials ? nullptr : nullptr);
165+
166+
// Check for errors
167+
if (!warn.empty())
168+
{
169+
DEBUG_LOG("WARN: " + warn);
170+
}
171+
if (!err.empty())
172+
{
173+
ERR_LOG(err);
174+
DEBUG_LOG("ERROR: Couldn't load mesh");
175+
return;
176+
}
177+
178+
std::vector<Graphics::Vertex> vertices;
179+
std::vector<uint32_t> indices;
180+
std::unordered_map<glm::vec3, uint32_t> unique_vertices;
181+
182+
for (const auto& shape : shapes)
183+
{
184+
for (const auto& index : shape.mesh.indices)
185+
{
186+
Graphics::Vertex vertex;
187+
if (index.vertex_index >= 0)
188+
{
189+
190+
vertex.pos = {attrib.vertices[3 * index.vertex_index + 0],
191+
attrib.vertices[3 * index.vertex_index + 1],
192+
attrib.vertices[3 * index.vertex_index + 2]};
193+
vertex.color = {attrib.colors[3 * index.vertex_index + 0],
194+
attrib.colors[3 * index.vertex_index + 1],
195+
attrib.colors[3 * index.vertex_index + 2]};
196+
}
197+
if (index.normal_index >= 0)
198+
{
199+
vertex.normal = {attrib.normals[3 * index.normal_index + 0],
200+
attrib.normals[3 * index.normal_index + 1],
201+
attrib.normals[3 * index.normal_index + 2]};
202+
}
203+
204+
vertex.tangent = {0.0, 0.0, 0.0};
205+
206+
if (index.texcoord_index >= 0)
207+
{
208+
vertex.texCoord = {
209+
attrib.texcoords[2 * index.texcoord_index + 0], attrib.texcoords[2 * index.texcoord_index + 1]};
210+
}
211+
212+
213+
if (unique_vertices.count(vertex.pos) == 0)
214+
{
215+
unique_vertices[vertex.pos] = static_cast<uint32_t>(vertices.size());
216+
vertices.push_back(vertex);
217+
}
218+
219+
indices.push_back(unique_vertices[vertex.pos]);
220+
}
221+
}
222+
if (calculateTangents)
223+
{
224+
Core::Geometry::compute_tangents_gram_smidt(vertices, indices);
225+
}
226+
227+
Core::Geometry* g = new Core::Geometry();
228+
g->fill(vertices, indices);
229+
mesh->push_geometry(g);
230+
mesh->set_file_route(fileName);
231+
}
151232
void VKFW::Tools::Loaders::load_PLY(Core::Mesh* const mesh,
152233
const std::string fileName,
153234
bool preload,
@@ -163,8 +244,8 @@ void VKFW::Tools::Loaders::load_PLY(Core::Mesh* const mesh,
163244
// stream is a net win for parsing speed, about 40% faster.
164245
if (preload)
165246
{
166-
byte_buffer = Graphics::Utils::read_file_binary(fileName);
167-
file_stream.reset(new Graphics::Utils::memory_stream((char*)byte_buffer.data(), byte_buffer.size()));
247+
byte_buffer = Utils::read_file_binary(fileName);
248+
file_stream.reset(new Utils::MemoryStream((char*)byte_buffer.data(), byte_buffer.size()));
168249
} else
169250
{
170251
file_stream.reset(new std::ifstream(fileName, std::ios::binary));
@@ -271,7 +352,7 @@ void VKFW::Tools::Loaders::load_PLY(Core::Mesh* const mesh,
271352
if (verbose)
272353
std::cerr << "tinyply exception: " << e.what() << std::endl;
273354
}
274-
Graphics::Utils::ManualTimer readTimer;
355+
Utils::ManualTimer readTimer;
275356

276357
readTimer.start();
277358
file.read(*file_stream);

0 commit comments

Comments
 (0)