-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVisualizer3D.cpp
More file actions
104 lines (86 loc) · 2.61 KB
/
Visualizer3D.cpp
File metadata and controls
104 lines (86 loc) · 2.61 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
// Visualizer3D.cpp //
#include "Visualizer3D.h"
namespace dw
{
// Protected Constructor
Visualizer3D::Visualizer3D(const char* title) : title(title)
{
srand(time(NULL));
}
// Static private fields
Visualizer3D* Visualizer3D::instance = nullptr;
std::mutex Visualizer3D::mutex;
// Static method, create a new instance (if not yet created) and return it
Visualizer3D* Visualizer3D::getInstance(const char* title)
{
// Lock the mutex to stop data racing in multithread uses
std::lock_guard<std::mutex> lock(mutex);
if (instance == nullptr)
{
instance = new Visualizer3D(title);
}
return instance;
}
// Open a framework and a window from it, and store the window, framework, and camera in variables for use later
void Visualizer3D::init(int& argc, char**& argv)
{
// Open a new window framework
this->frame = new PandaFramework();
frame->open_framework(argc, argv);
// Set the window title and open the window
frame->set_window_title(title);
window = frame->open_window();
// Set the background black because the natural grey is gross
window->set_background_type(WindowFramework::BackgroundType::BT_black);
// Store camera in variable
camera = window->get_camera_group();
}
// Run the main loop of the framework
void Visualizer3D::run()
{
frame->main_loop();
}
// Close the framework
void Visualizer3D::shutdown()
{
frame->close_framework();
}
NodePath Visualizer3D::loadModel(const char* path, Vector3 scale, Vector3 pos, Vector3 color)
{
NodePath model = window->load_model(frame->get_models(), path);
model.reparent_to(window->get_render());
model.set_pos(pos.x, pos.y, pos.z);
model.set_scale(scale.x, scale.y, scale.z);
if (color.x < 0 || color.y < 0 || color.z < 0)
{
Vector3 randomColor = Vector3((float)(rand() % 6 + 3) / 10.0, (float)(rand() % 6 + 3) / 10.0, (float)(rand() % 6 + 3) / 10.0);
model.set_color(randomColor.x, randomColor.y, randomColor.z);
}
else
{
model.set_color(color.x, color.y, color.z);
}
return model;
}
void Visualizer3D::addTask(const char* name, GenericAsyncTask::TaskFunc* taskFunc, void* userData)
{
dw_visualizer_3d_task_manager->add(new GenericAsyncTask(name, taskFunc, userData));
}
void Visualizer3D::addKeyEvent(const char* key, const char* name, EventHandler::EventCallbackFunction* eventFunction, void* userData)
{
frame->define_key(key, name, eventFunction, userData);
}
// Getters: Unwrap the panda framework
NodePath* Visualizer3D::getCameraGroup()
{
return &camera;
}
PandaFramework* Visualizer3D::getFrame()
{
return frame;
}
WindowFramework* Visualizer3D::getWindow()
{
return window;
}
}