-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmain.cpp
More file actions
162 lines (134 loc) · 5.35 KB
/
main.cpp
File metadata and controls
162 lines (134 loc) · 5.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//
// Created by AregevDev on 23/04/2020.
//
#include <imgui.h>
#include <fmt/core.h>
#include "rvpt.h"
#define TINYOBJLOADER_IMPLEMENTATION // define this in only *one* .cc
#include "tinyobjloader/tiny_obj_loader.h"
void load_model(RVPT& rvpt, std::string inputfile, int material_id)
{
rvpt.get_asset_path(inputfile);
tinyobj::attrib_t attrib;
std::vector<tinyobj::shape_t> shapes;
std::vector<tinyobj::material_t> materials;
std::string warn;
std::string err;
tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err, inputfile.c_str());
if (!warn.empty())
{
fmt::print("[{}: {}] {}\n", "WARNING", "MODEL-LOADING", warn);
}
if (!err.empty())
{
fmt::print("[{}: {}] {}\n", "ERROR", "MODEL-LOADING", err);
exit(-1);
}
// Loop over shapes
for (auto & shape : shapes) {
// Loop over faces(polygon)
size_t index_offset = 0;
for (size_t f = 0; f < shape.mesh.num_face_vertices.size(); f++) {
const auto fv = shape.mesh.num_face_vertices[f];
// Loop over vertices in the face.
if (fv != 3)
{
fmt::print("Shape had a face with more than 3 vertices, skipping");
continue;
}
glm::vec3 vertices[3];
for (size_t v = 0; v < fv; v++) {
tinyobj::index_t idx = shape.mesh.indices[index_offset + v];
vertices[v].x = attrib.vertices[3*idx.vertex_index+0];
vertices[v].y = attrib.vertices[3*idx.vertex_index+1];
vertices[v].z = attrib.vertices[3*idx.vertex_index+2];
}
index_offset += fv;
rvpt.add_triangle(Triangle(vertices[0], vertices[1], vertices[2], material_id));
// per-face material
shape.mesh.material_ids[f];
}
}
}
void update_camera(Window& window, RVPT& rvpt)
{
glm::vec3 movement{};
double frameDelta = rvpt.time.since_last_frame();
if (window.is_key_held(Window::KeyCode::KEY_LEFT_SHIFT)) frameDelta *= 5;
if (window.is_key_held(Window::KeyCode::SPACE)) movement.y += 3.0f;
if (window.is_key_held(Window::KeyCode::KEY_LEFT_CONTROL)) movement.y -= 3.0f;
if (window.is_key_held(Window::KeyCode::KEY_W)) movement.z += 3.0f;
if (window.is_key_held(Window::KeyCode::KEY_S)) movement.z -= 3.0f;
if (window.is_key_held(Window::KeyCode::KEY_D)) movement.x += 3.0f;
if (window.is_key_held(Window::KeyCode::KEY_A)) movement.x -= 3.0f;
rvpt.scene_camera.translate(static_cast<float>(frameDelta) * movement);
glm::vec3 rotation{};
float rot_speed = 0.3f;
if (window.is_key_down(Window::KeyCode::KEY_RIGHT)) rotation.x = rot_speed;
if (window.is_key_down(Window::KeyCode::KEY_LEFT)) rotation.x = -rot_speed;
if (window.is_key_down(Window::KeyCode::KEY_UP)) rotation.y = -rot_speed;
if (window.is_key_down(Window::KeyCode::KEY_DOWN)) rotation.y = rot_speed;
rvpt.scene_camera.rotate(rotation);
}
int main()
{
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
// WARNING: THIS CODE IS BROKEN AND SHOULD ONLY BE RAN IF YOU KNOW EXACTLY WHAT YOU'RE GETTING YOURSELF INTO //
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
Window::Settings settings;
settings.width = 3330;
settings.height = 1340;
Window window(settings);
RVPT rvpt(window);
load_model(rvpt, "models/tridel-interior-test.obj", 1);
// Setup Demo Scene
rvpt.add_material(
Material(glm::vec4(1, 1, 1, 0), glm::vec4(0.1, 0.4, 0.6, 0), Material::Type::LAMBERT));
rvpt.add_material(Material(glm::vec4(1.0, 1.0, 1.0, 0), glm::vec4(0), Material::Type::LAMBERT));
bool rvpt_init_ret = rvpt.initialize();
if (!rvpt_init_ret)
{
fmt::print("failed to initialize RVPT\n");
return 0;
}
window.setup_imgui();
window.add_mouse_move_callback([&window, &rvpt](double x, double y) {
if (window.is_mouse_locked_to_window())
{
rvpt.scene_camera.rotate(glm::vec3(x * 0.3f, -y * 0.3f, 0));
}
});
window.add_mouse_click_callback([&window](Window::Mouse button, Window::Action action) {
if (button == Window::Mouse::LEFT && action == Window::Action::RELEASE &&
window.is_mouse_locked_to_window())
{
window.set_mouse_window_lock(false);
}
else if (button == Window::Mouse::LEFT && action == Window::Action::RELEASE &&
!window.is_mouse_locked_to_window())
{
if (!ImGui::GetIO().WantCaptureMouse)
{
window.set_mouse_window_lock(true);
}
}
});
while (!window.should_close())
{
window.poll_events();
if (window.is_key_down(Window::KeyCode::KEY_ESCAPE)) window.set_close();
if (window.is_key_down(Window::KeyCode::KEY_R)) rvpt.reload_shaders();
if (window.is_key_down(Window::KeyCode::KEY_V)) rvpt.toggle_debug();
if (window.is_key_up(Window::KeyCode::KEY_ENTER))
{
window.set_mouse_window_lock(!window.is_mouse_locked_to_window());
}
update_camera(window, rvpt);
ImGui::NewFrame();
rvpt.update_imgui();
rvpt.update();
rvpt.draw();
}
rvpt.shutdown();
return 0;
}