-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
129 lines (97 loc) · 2.79 KB
/
main.cpp
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
#define USE_GL 0
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#include <emscripten/html5.h>
#endif
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <sstream>
#include "myengine_setup.h"
#include "filament_setup.h"
#include "ResourceManager.h"
// Some javascript is used to set the current GL context
void register_fila_context()
{
EM_ASM_INT({
const handle = GL.registerContext(myengine.glContext, myengine.glOptions);
GL.makeContextCurrent(handle);
});
}
ResourceManager g_rm;
bool g_mesh_loaded = false;
void on_load_final(ResourceManager& rm);
/* -------------------------------------------- */
void loop()
{
loop_myengine();
loop_filament();
// Final step to async loading
if (g_mesh_loaded == false && g_rm.pending == 0)
{
g_mesh_loaded = true;
on_load_final(g_rm);
}
}
/* -------------------------------------------- */
EM_BOOL em_mouse_callback(int eventType, const EmscriptenMouseEvent *mouseEvent, void *userData)
{
static bool down = false;
static int x = 0;
static int y = 0;
if (eventType == EMSCRIPTEN_EVENT_MOUSEDOWN)
{
down = true;
x = mouseEvent->clientX;
y = mouseEvent->clientY;
}
if (eventType == EMSCRIPTEN_EVENT_MOUSEUP)
{
down = false;
}
if (eventType == EMSCRIPTEN_EVENT_MOUSEMOVE)
{
if (down)
{
int dx = x - mouseEvent->clientX;
int dy = y - mouseEvent->clientY;
x = mouseEvent->clientX;
y = mouseEvent->clientY;
myengine_mouse_event(dx, dy);
}
}
return 0;
}
int main(int argc, char* argv[]) {
enum
{
FILAMESH = 0,
FILAMAT,
TEXTURE_KTX,
};
std::function x = &on_mesh_load;
std::function y = &on_material_load;
std::function z = &on_ktx_load;
g_rm.addResourceType(FILAMESH, &x);
g_rm.addResourceType(FILAMAT, &y);
g_rm.addResourceType(TEXTURE_KTX, &z);
g_rm.addResource("sky", "default_env/default_env_skybox.ktx", TEXTURE_KTX);
g_rm.addResource("ibl", "default_env/default_env_ibl.ktx", TEXTURE_KTX);
g_rm.addResource("Heart ao", "model/ao_etc.ktx", TEXTURE_KTX);
g_rm.addResource("Default material", "materials/plastic.filamat", FILAMAT);
g_rm.addResource("Heart mesh", "model/heart.filamesh", FILAMESH);
init_myengine();
register_fila_context();
init_filament();
emscripten_set_mousedown_callback("canvas", nullptr, true, em_mouse_callback);
emscripten_set_mouseup_callback("canvas", nullptr, true, em_mouse_callback);
emscripten_set_mousemove_callback("canvas", nullptr, true, em_mouse_callback);
g_rm.start_async_load();
#ifdef __EMSCRIPTEN__
emscripten_set_main_loop(loop, 0, 1);
#endif
stop_filament();
stop_myengine();
return EXIT_SUCCESS;
}
/* -------------------------------------------- */