-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModuleEditor.cpp
283 lines (221 loc) · 7.99 KB
/
ModuleEditor.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
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
#include "Application.h"
#include "ModuleEditor.h"
#include "ModuleWindow.h"
#include "ModuleOpenGL.h"
#include "ModuleCamera.h"
#include "ModuleRender.h"
#include "Mesh.h"
#include "MathBuildConfig.h"
#include "MathGeoLib.h"
#include "MathGeoLibFwd.h"
ModuleEditor::ModuleEditor()
{
}
// Destructor
ModuleEditor::~ModuleEditor()
{
}
ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
bool ModuleEditor::Init()
{
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO(); (void)io;
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // Enable Docking
io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; // Enable Multi-Viewport / Platform Windows
io.ConfigFlags |= ImGuiWindowFlags_MenuBar;
ImGui::StyleColorsDark();
SDL_GL_MakeCurrent(App->GetWindow()->window, App->GetOpenGL()->context);
ImGui_ImplSDL2_InitForOpenGL(App->GetWindow()->window, App->GetOpenGL()->context);
ImGui_ImplOpenGL3_Init("#version 460");
return true;
}
update_status ModuleEditor::PreUpdate()
{
return UPDATE_CONTINUE;
}
update_status ModuleEditor::Update()
{
NewFrame();
if (ImGui::DockSpaceOverViewport(0, ImGuiDockNodeFlags_PassthruCentralNode))
{
/*if (ImGui::BeginMenuBar()) {
if (ImGui::BeginMenu("Menu"))
{
ImGui::EndMenu();
}
if (ImGui::BeginMenu("Examples"))
{
}
}*/
}
// 1. Show the big demo window (Most of the sample code is in ImGui::ShowDemoWindow()! You can browse its code to learn more about Dear ImGui!).
if (show_demo_window)
ImGui::ShowDemoWindow(&show_demo_window);
// 2. Show a simple window that we create ourselves. We use a Begin/End pair to create a named window.
InitialWindow();
if (show_camera_window)
CameraWindow();
if (show_model_window)
ModelWindow();
RenderAndUpdate();
return UPDATE_CONTINUE;
}
update_status ModuleEditor::PostUpdate()
{
return UPDATE_CONTINUE;
}
bool ModuleEditor::CleanUp()
{
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplSDL2_Shutdown();
ImGui::DestroyContext();
return true;
}
void ModuleEditor::NewFrame()
{
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplSDL2_NewFrame(App->GetWindow()->window);
ImGui::NewFrame();
}
void ModuleEditor::RenderAndUpdate()
{
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
SDL_Window* backup_current_window = SDL_GL_GetCurrentWindow();
SDL_GLContext backup_current_context = SDL_GL_GetCurrentContext();
ImGui::UpdatePlatformWindows();
ImGui::RenderPlatformWindowsDefault();
SDL_GL_MakeCurrent(backup_current_window, backup_current_context);
}
//Pop-up Windows
void ModuleEditor::InitialWindow()
{
static float h_fov = 0.0f;
static int counter = 0;
ImGui::Begin("Initial Application Window"); // Create a window called "Hello, world!" and append into it.
ImGui::Checkbox("Demo Examples Window", &show_demo_window); // Edit bools storing our window open/close state
ImGui::Text(" ");
ImGui::Checkbox("Camera Properties Window", &show_camera_window);
ImGui::Text(" ");
ImGui::Checkbox("Model Properties Window", &show_model_window);
ImGui::Text(" ");
ImGuiIO& io = ImGui::GetIO(); (void)io;
ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate);
ImGui::End();
}
void ModuleEditor::CameraWindow()
{
//Target
static float target[3] = {App->GetCamera()->target.x, App->GetCamera()->target.y, App->GetCamera()->target.z};
//Position
static float position[3] = { App->GetCamera()->GetPosition().x, App->GetCamera()->GetPosition().y, App->GetCamera()->GetPosition().z};
//Field of view
static float h_fov = App->GetCamera()->GetFrustum()->horizontalFov;
static float h_fov_deg;
//Aspect
static float aspect = App->GetCamera()->GetAspect();
//Planes
static float nearPlane = App->GetCamera()->GetFrustum()->nearPlaneDistance;
static float farPlane = App->GetCamera()->GetFrustum()->farPlaneDistance;
//Speed
static float movementSpeed = App->GetCamera()->GetMovementSpeed();
static float rotationSpeed = App->GetCamera()->GetRotationSpeed();
ImGui::Begin("Camera Properties Editor", &show_camera_window); // Pass a pointer to our bool variable (the window will have a closing button that will clear the bool when clicked)
ImGui::Checkbox("Orbiting", &App->GetCamera()->orbiting);
if (App->GetCamera()->orbiting)
{
ImGui::InputFloat3("Target", target);
App->GetCamera()->target = float3(target[0], target[1], target[2]);
}
ImGui::SeparatorText("Transform");
position[0] = App->GetCamera()->GetPosition().x;
position[1] = App->GetCamera()->GetPosition().y;
position[2] = App->GetCamera()->GetPosition().z;
ImGui::InputFloat3("Position", position);
App->GetCamera()->SetPosition(float3(position[0], position[1], position[2]));
ImGui::SeparatorText("View");
h_fov_deg = h_fov * 180.0 / math::pi;
ImGui::SliderFloat("Field of View", &h_fov_deg, 0.0f, 180);
h_fov = h_fov_deg * math::pi / 180.0f;
ImGui::SliderFloat("Aspect Ratio", &aspect, 0.25f, 4.0f);
App->GetCamera()->ChangeFOV(aspect, h_fov, App->GetWindow()->width, App->GetWindow()->height);
ImGui::SeparatorText("Plane Distances");
ImGui::SliderFloat("Near plane", &nearPlane, 0.001f, farPlane);
ImGui::SliderFloat("Far plane", &farPlane, nearPlane, 200.0);
App->GetCamera()->GetFrustum()->SetPlaneDistances(nearPlane, farPlane);
ImGui::SeparatorText("Speed");
ImGui::SliderFloat("Movement", &movementSpeed, 1.0f, 20.0f);
ImGui::SliderFloat("Rotation", &rotationSpeed, 0.2f, 4.0f);
App->GetCamera()->SetSpeeds(movementSpeed, rotationSpeed);
ImGui::Text("");
if (ImGui::Button("Reset Camera Properties"))
{
//View
h_fov = math::pi / 4.0;
aspect = 1;
//Planes
nearPlane = 0.001f;
farPlane = 100.0f;
//Speed
movementSpeed = 5.0f;
rotationSpeed = 1.0f;
}
ImGui::End();
}
void ModuleEditor::ModelWindow()
{
//Properties
ImGui::Begin("Model Properties", &show_camera_window);
ImGui::SeparatorText("Properties");
ImGui::Text("Name:"); ImGui::SameLine();
ImGui::Text(App->GetExercise()->GetModel()->GetFileName().c_str());
ImGui::Text(" ");
//Vertices and indices
ImGui::Text("Number of verticies: "); ImGui::SameLine();
ImGui::Text(std::to_string(App->GetExercise()->GetModel()->GetVertexCount()).c_str());
ImGui::Text("Number of triangles: "); ImGui::SameLine();
ImGui::Text(std::to_string(App->GetExercise()->GetModel()->GetTriangleCount()).c_str());
//Meshes
ImGui::Text("Number of meshes: "); ImGui::SameLine();
const int numberMeshes = App->GetExercise()->GetModel()->GetMeshes().size();
ImGui::Text(std::to_string(numberMeshes).c_str());
if (numberMeshes != 1)
{
ImGui::Text(" ");
for (int i = 0; i < numberMeshes; i++)
{
ImGui::Text("Mesh"); ImGui::SameLine(); ImGui::Text(std::to_string(i+1).c_str()); ImGui::SameLine();
ImGui::Text(":"); ImGui::SameLine();
Mesh* mesh = App->GetExercise()->GetModel()->GetMeshes()[i];
ImGui::Text(mesh->GetFileName().c_str());
ImGui::Text("Number of verticies: "); ImGui::SameLine();
ImGui::Text(std::to_string(mesh->GetVertexCount()).c_str());
ImGui::Text("Number of triangles: "); ImGui::SameLine();
ImGui::Text(std::to_string(mesh->GetIndexCount()/3).c_str());
ImGui::Text(" ");
}
}
ImGui::Text(" ");
//Transform
ImGui::SeparatorText("Transform Editor");
//Position
ImGui::Text(" ");
static float3 pos_ = App->GetExercise()->GetModel()->GetPosition();
static float pos[3] = { pos_.x, pos_.y, pos_.z };
ImGui::InputFloat3("Position", pos);
pos_ = float3(pos[0], pos[1], pos[2]);
//Rotation
ImGui::Text(" ");
static float rot = App->GetExercise()->GetModel()->GetRotation();
ImGui::SliderFloat("Rotation", &rot, 0.0f, 2*math::pi);
//Scale
ImGui::Text(" ");
static float3 scale_ = App->GetExercise()->GetModel()->GetScale();
static float scale[3] = {scale_.x, scale_.y, scale_.z};
ImGui::InputFloat3("Scale", scale);
scale_ = float3(scale[0], scale[1], scale[2]);
App->GetExercise()->GetModel()->ChangeTransform(pos_, rot, scale_);
ImGui::End();
}