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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
|
/*
* Copyright (c) 2021, Jesse Buhagiar <jooster669@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibCore/ElapsedTimer.h>
#include <LibCore/File.h>
#include <LibGL/GL/gl.h>
#include <LibGL/GLContext.h>
#include <LibGUI/ActionGroup.h>
#include <LibGUI/Application.h>
#include <LibGUI/FilePicker.h>
#include <LibGUI/Icon.h>
#include <LibGUI/Label.h>
#include <LibGUI/Menu.h>
#include <LibGUI/Menubar.h>
#include <LibGUI/MessageBox.h>
#include <LibGUI/Painter.h>
#include <LibGUI/Widget.h>
#include <LibGUI/Window.h>
#include <LibGfx/Bitmap.h>
#include <LibGfx/Palette.h>
#include <unistd.h>
#include "Mesh.h"
#include "WavefrontOBJLoader.h"
static constexpr u16 RENDER_WIDTH = 640;
static constexpr u16 RENDER_HEIGHT = 480;
class GLContextWidget final : public GUI::Frame {
C_OBJECT(GLContextWidget);
public:
bool load(const String& fname);
void toggle_rotate_x() { m_rotate_x = !m_rotate_x; }
void toggle_rotate_y() { m_rotate_y = !m_rotate_y; }
void toggle_rotate_z() { m_rotate_z = !m_rotate_z; }
void set_rotation_speed(float speed) { m_rotation_speed = speed; }
void set_stat_label(RefPtr<GUI::Label> l) { m_stats = l; };
void toggle_show_frame_rate()
{
m_show_frame_rate = !m_show_frame_rate;
m_stats->set_visible(m_show_frame_rate);
}
private:
GLContextWidget()
: m_mesh_loader(adopt_own(*new WavefrontOBJLoader()))
{
m_bitmap = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRx8888, { RENDER_WIDTH, RENDER_HEIGHT });
m_context = GL::create_context(*m_bitmap);
start_timer(20);
GL::make_context_current(m_context);
glFrontFace(GL_CW);
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
// Set projection matrix
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-0.5, 0.5, -0.5, 0.5, 1, 1500);
m_init_list = glGenLists(1);
glNewList(m_init_list, GL_COMPILE);
{
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClearDepth(1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
glEndList();
}
virtual void paint_event(GUI::PaintEvent&) override;
virtual void timer_event(Core::TimerEvent&) override;
virtual void mousemove_event(GUI::MouseEvent&) override;
private:
RefPtr<Mesh> m_mesh;
RefPtr<Gfx::Bitmap> m_bitmap;
OwnPtr<GL::GLContext> m_context;
NonnullOwnPtr<WavefrontOBJLoader> m_mesh_loader;
GLuint m_init_list { 0 };
bool m_rotate_x = true;
bool m_rotate_y = false;
bool m_rotate_z = true;
float m_angle_x = 0.0;
float m_angle_y = 0.0;
float m_angle_z = 0.0;
Gfx::IntPoint m_last_mouse;
float m_rotation_speed = 1.f;
bool m_show_frame_rate = false;
int m_cycles = 0;
int m_accumulated_time = 0;
RefPtr<GUI::Label> m_stats;
};
void GLContextWidget::paint_event(GUI::PaintEvent& event)
{
GUI::Frame::paint_event(event);
GUI::Painter painter(*this);
painter.add_clip_rect(event.rect());
painter.draw_scaled_bitmap(frame_inner_rect(), *m_bitmap, m_bitmap->rect());
}
void GLContextWidget::mousemove_event(GUI::MouseEvent& event)
{
if (event.buttons() == GUI::MouseButton::Left) {
int delta_x = m_last_mouse.x() - event.x();
int delta_y = m_last_mouse.y() - event.y();
m_angle_x -= delta_y / 100.0f;
m_angle_y -= delta_x / 100.0f;
}
m_last_mouse = event.position();
}
void GLContextWidget::timer_event(Core::TimerEvent&)
{
Core::ElapsedTimer timer;
timer.start();
glCallList(m_init_list);
if (m_rotate_x)
m_angle_x -= m_rotation_speed * 0.01f;
if (m_rotate_y)
m_angle_y -= m_rotation_speed * 0.01f;
if (m_rotate_z)
m_angle_z -= m_rotation_speed * 0.01f;
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0, 0, -8.5);
glRotatef(m_angle_x, 1, 0, 0);
glRotatef(m_angle_y, 0, 1, 0);
glRotatef(m_angle_z, 0, 0, 1);
if (!m_mesh.is_null())
m_mesh->draw();
m_context->present();
if ((m_cycles % 30) == 0) {
int render_time = m_accumulated_time / 30;
int frame_rate = render_time > 0 ? 1000 / render_time : 0;
m_stats->set_text(String::formatted("{} fps, {} ms", frame_rate, render_time));
m_accumulated_time = 0;
}
update();
m_accumulated_time += timer.elapsed();
m_cycles++;
}
bool GLContextWidget::load(const String& filename)
{
auto file = Core::File::construct(filename);
if (!file->filename().ends_with(".obj")) {
GUI::MessageBox::show(window(), String::formatted("Opening \"{}\" failed: invalid file type", filename), "Error", GUI::MessageBox::Type::Error);
return false;
}
if (!file->open(Core::OpenMode::ReadOnly) && file->error() != ENOENT) {
GUI::MessageBox::show(window(), String::formatted("Opening \"{}\" failed: {}", filename, strerror(errno)), "Error", GUI::MessageBox::Type::Error);
return false;
}
if (file->is_device()) {
GUI::MessageBox::show(window(), String::formatted("Opening \"{}\" failed: Can't open device files", filename), "Error", GUI::MessageBox::Type::Error);
return false;
}
if (file->is_directory()) {
GUI::MessageBox::show(window(), String::formatted("Opening \"{}\" failed: Can't open directories", filename), "Error", GUI::MessageBox::Type::Error);
return false;
}
auto new_mesh = m_mesh_loader->load(file);
if (new_mesh.is_null()) {
GUI::MessageBox::show(window(), String::formatted("Reading \"{}\" failed.", filename), "Error", GUI::MessageBox::Type::Error);
return false;
}
// Determine whether or not a texture for this model resides within the same directory
StringBuilder builder;
builder.append(filename.split('.').at(0));
builder.append(".bmp");
// Attempt to open the texture file from disk
auto texture_image = Gfx::Bitmap::try_load_from_file(builder.string_view());
GLuint tex;
glGenTextures(1, &tex);
if (texture_image) {
// Upload texture data to the GL
glBindTexture(GL_TEXTURE_2D, tex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, texture_image->width(), texture_image->height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, texture_image->scanline(0));
} else {
dbgln("3DFileViewer: Couldn't load texture for {}", filename);
}
m_mesh = new_mesh;
dbgln("3DFileViewer: mesh has {} triangles.", m_mesh->triangle_count());
return true;
}
int main(int argc, char** argv)
{
auto app = GUI::Application::construct(argc, argv);
if (pledge("stdio thread recvfd sendfd rpath", nullptr) < 0) {
perror("pledge");
return 1;
}
// Construct the main window
auto window = GUI::Window::construct();
auto app_icon = GUI::Icon::default_icon("app-3d-file-viewer");
window->set_icon(app_icon.bitmap_for_size(16));
window->set_title("3D File Viewer");
window->resize(640 + 4, 480 + 4);
window->set_resizable(false);
window->set_double_buffering_enabled(true);
auto& widget = window->set_main_widget<GLContextWidget>();
auto& time = widget.add<GUI::Label>();
time.set_visible(false);
time.set_foreground_role(ColorRole::HoverHighlight);
time.set_relative_rect({ 0, 8, 86, 10 });
time.move_by({ window->width() - time.width(), 0 });
widget.set_stat_label(time);
auto& file_menu = window->add_menu("&File");
auto load_model = [&](StringView const& filename) {
if (widget.load(filename)) {
auto canonical_path = Core::File::real_path_for(filename);
window->set_title(String::formatted("{} - 3D File Viewer", canonical_path));
}
};
file_menu.add_action(GUI::CommonActions::make_open_action([&](auto&) {
Optional<String> open_path = GUI::FilePicker::get_open_filepath(window);
if (!open_path.has_value())
return;
load_model(open_path.value());
}));
file_menu.add_separator();
file_menu.add_action(GUI::CommonActions::make_quit_action([&](auto&) {
app->quit();
}));
auto& view_menu = window->add_menu("&View");
view_menu.add_action(GUI::CommonActions::make_fullscreen_action([&](auto&) {
window->set_fullscreen(!window->is_fullscreen());
}));
auto& rotation_axis_menu = view_menu.add_submenu("Rotation &Axis");
auto rotation_x_action = GUI::Action::create_checkable("&X", [&widget](auto&) {
widget.toggle_rotate_x();
});
auto rotation_y_action = GUI::Action::create_checkable("&Y", [&widget](auto&) {
widget.toggle_rotate_y();
});
auto rotation_z_action = GUI::Action::create_checkable("&Z", [&widget](auto&) {
widget.toggle_rotate_z();
});
rotation_axis_menu.add_action(*rotation_x_action);
rotation_axis_menu.add_action(*rotation_y_action);
rotation_axis_menu.add_action(*rotation_z_action);
rotation_x_action->set_checked(true);
rotation_z_action->set_checked(true);
auto& rotation_speed_menu = view_menu.add_submenu("Rotation &Speed");
GUI::ActionGroup rotation_speed_actions;
rotation_speed_actions.set_exclusive(true);
auto no_rotation_action = GUI::Action::create_checkable("N&o Rotation", [&widget](auto&) {
widget.set_rotation_speed(0.f);
});
auto slow_rotation_action = GUI::Action::create_checkable("&Slow", [&widget](auto&) {
widget.set_rotation_speed(0.5f);
});
auto normal_rotation_action = GUI::Action::create_checkable("&Normal", [&widget](auto&) {
widget.set_rotation_speed(1.f);
});
auto fast_rotation_action = GUI::Action::create_checkable("&Fast", [&widget](auto&) {
widget.set_rotation_speed(1.5f);
});
rotation_speed_actions.add_action(*no_rotation_action);
rotation_speed_actions.add_action(*slow_rotation_action);
rotation_speed_actions.add_action(*normal_rotation_action);
rotation_speed_actions.add_action(*fast_rotation_action);
rotation_speed_menu.add_action(*no_rotation_action);
rotation_speed_menu.add_action(*slow_rotation_action);
rotation_speed_menu.add_action(*normal_rotation_action);
rotation_speed_menu.add_action(*fast_rotation_action);
normal_rotation_action->set_checked(true);
auto show_frame_rate_action = GUI::Action::create_checkable("Show Frame &Rate", [&widget](auto&) {
widget.toggle_show_frame_rate();
});
view_menu.add_action(*show_frame_rate_action);
auto& help_menu = window->add_menu("&Help");
help_menu.add_action(GUI::CommonActions::make_about_action("3D File Viewer", app_icon, window));
window->show();
auto filename = argc > 1 ? argv[1] : "/home/anon/Documents/3D Models/teapot.obj";
load_model(filename);
return app->exec();
}
|