-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModuleImGui.cpp
More file actions
535 lines (440 loc) · 16.8 KB
/
Copy pathModuleImGui.cpp
File metadata and controls
535 lines (440 loc) · 16.8 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
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
#include "Globals.h"
#include "Application.h"
#include "ModuleD3D12.h"
#include "ModuleImGui.h"
#include "ModuleRenderer.h"
#include "imgui.h"
#include "imgui_internal.h"
#include "3rdParty/imgui-docking/backends/imgui_impl_win32.h"
#include "3rdParty/imgui-docking/backends/imgui_impl_dx12.h"
#include <d3d12.h>
#include <dxgi1_5.h>
#include <tchar.h>
#include <Windows.h>
extern IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WinProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
ModuleImGui::ModuleImGui()
{
transformData.scale = DirectX::SimpleMath::Vector3(0.025f, 0.025f, 0.025f);
transformData.translation = DirectX::SimpleMath::Vector3(0.0f, -1.0f, 0.0f);
//Initialize Materials
materialData.diffuseColor = DirectX::XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f);
materialData.specularColor = DirectX::XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f);
materialData.shininess = 32.0f;
materialData.hasTexture = 1.0f;
// Initialize light
lightData.direction = DirectX::SimpleMath::Vector3(-0.5f, -1.0f, -0.5f);
lightData.direction.Normalize();
lightData.color = DirectX::SimpleMath::Vector3(1.0f, 1.0f, 1.0f);
lightData.ambient = DirectX::SimpleMath::Vector3(0.2f, 0.2f, 0.2f);
lightData.viewPos = DirectX::SimpleMath::Vector3(0.0f, 0.0f, 5.0f);
}
ModuleImGui::~ModuleImGui()
{
}
bool ModuleImGui::init()
{
ModuleD3D12* d3d12 = app->getD3D12();
HWND hWnd = static_cast<HWND>(app->getWindow());
// Inicializamos tu clase pasando el dispositivo y la ventana
imguiPass = std::make_unique<ImGuiPass>(d3d12->getDevice(), hWnd);
return true;
}
void ModuleImGui::preRender()
{
imguiPass->startFrame();
}
void ModuleImGui::render()
{
drawMainMenuBar();
// Just draw windows directly, no dockspace
if (showConfigWindow) {
drawConfigurationWindow();
}
if (showAboutWindow)
{
ImGui::Begin("About this Engine", &showAboutWindow);
ImGui::Text("DirectX 12 Engine - Assignment 2");
ImGui::Separator();
ImGui::Text("Developed by: Meritxell Alguero");
ImGui::Separator();
ImGui::TextWrapped("This engine is part of an assigment for the master Advance Programming for AAA Games. ");
ImGui::TextWrapped("Right now it shows a duck on screen. The light values can be changed through the window 'Phong Shading Controls'");
if (ImGui::Button("Close"))
{
showAboutWindow = false;
}
ImGui::End();
}
if (showPhongControls) {
drawPhongControls();
}
if (showTransformControls) {
drawTransformControls();
}
auto cmdList = app->getD3D12()->getCommandList();
imguiPass->record(cmdList);
}
void ModuleImGui::postRender()
{
ModuleD3D12* d3d12 = app->getD3D12();
// imguiPass->record(d3d12->getCommandList());
}
void ModuleImGui::drawMainMenuBar()
{
if (ImGui::BeginMainMenuBar())
{
if (ImGui::BeginMenu("File"))
{
if (ImGui::MenuItem("Quit", "ESC"))
{
app->requestQuit();
}
ImGui::EndMenu();
}
if (ImGui::BeginMenu("View"))
{
if (ImGui::MenuItem("Configuration", NULL, showConfigWindow))
{
showConfigWindow = !showConfigWindow;
}
if (ImGui::MenuItem("Phong Controls", NULL, showPhongControls))
{
showPhongControls = !showPhongControls;
}
if (ImGui::MenuItem("Transform Controls", NULL, showTransformControls))
{
showTransformControls = !showTransformControls;
}
ImGui::EndMenu();
}
if (ImGui::BeginMenu("Help"))
{
if (ImGui::MenuItem("About")) { showAboutWindow = true; }
if (ImGui::MenuItem("ImGui Demo")) { ShellExecute(NULL, L"open", L"https://github.com/ocornut/imgui", NULL, NULL, SW_SHOWNORMAL); }
ImGui::EndMenu();
}
ImGui::EndMainMenuBar();
}
if (ImGui::IsKeyPressed(ImGuiKey_Escape))
{
app->requestQuit();
}
}
void ModuleImGui::drawConfigurationWindow()
{
if (ImGui::Begin("Configuration", &showConfigWindow))
{
ModuleD3D12* d3d12 = app->getD3D12();
ModuleRenderer* renderer = app->getRenderer();
float fps = ImGui::GetIO().Framerate;
float ms = 1000.0f / fps;
static int offset = 0;
fpsLog[offset] = fps;
offset = (offset + 1) % IM_ARRAYSIZE(fpsLog);
ImGui::Text("Application");
ImGui::PlotHistogram("##framerate", fpsLog, IM_ARRAYSIZE(fpsLog), 0, "Framerate", 0.0f, 100.0f, ImVec2(0, 80));
ImGui::Text("Framerate %.2f", fps);
ImGui::Text("Milliseconds %.2f", ms);
ImGui::Separator();
ImGui::Text("Debug Gizmos");
bool grid = renderer->isGridVisible();
bool axis = renderer->isAxisVisible();
if (ImGui::Checkbox("Show Grid", &grid)) {
renderer->setGridVisible(grid);
}
if (ImGui::Checkbox("Show Axis", &axis)) {
renderer->setAxisVisible(axis);
}
ImGui::Separator();
ImGui::Text("Texture Filtering");
const char* items[] = { "Wrap / Bilinear", "Clamp / Bilinear", "Wrap / Point", "Clamp / Point" };
static int item_current = 0;
if (ImGui::Combo("Sampler Mode", &item_current, items, IM_ARRAYSIZE(items)))
{
//app->getD3D12()->setSamplerMode(item_current);
}
}
ImGui::End();
}
void ModuleImGui::drawPhongControls()
{
ImGui::Begin("Phong Shading Controls", &showPhongControls);
// Light controls
ImGui::SeparatorText("Light Parameters");
// Store light direction as angles for 360° rotation
static float lightYaw = 0.0f;
static float lightPitch = -45.0f;
static bool anglesInitialized = false;
// Convert light direction to angles
if (!anglesInitialized)
{
float length = sqrt(
lightData.direction.x * lightData.direction.x +
lightData.direction.y * lightData.direction.y +
lightData.direction.z * lightData.direction.z);
if (length > 0.001f)
{
// Normalize
DirectX::SimpleMath::Vector3 dir = lightData.direction;
dir.Normalize();
// Calculate pitch (vertical angle)
lightPitch = asinf(dir.y) * (180.0f / XM_PI);
// Calculate yaw (horizontal angle)
lightYaw = atan2f(dir.x, dir.z) * (180.0f / XM_PI);
if (lightYaw < 0.0f) lightYaw += 360.0f; // Convert to 0-360
}
anglesInitialized = true;
}
ImGui::SliderFloat("Light Yaw", &lightYaw, 0.0f, 360.0f, "%.1f°");
ImGui::SliderFloat("Light Pitch", &lightPitch, -90.0f, 90.0f, "%.1f°");
float yawRad = DirectX::XMConvertToRadians(lightYaw);
float pitchRad = DirectX::XMConvertToRadians(lightPitch);
lightData.direction.x = cosf(pitchRad) * sinf(yawRad);
lightData.direction.y = sinf(pitchRad);
lightData.direction.z = cosf(pitchRad) * cosf(yawRad);
// Display current direction
ImGui::Text("Direction: (%.2f, %.2f, %.2f)",
lightData.direction.x, lightData.direction.y, lightData.direction.z);
//Light Colour
ImGui::ColorEdit3("Light Colour", &lightData.color.x);
//Ambient Colour
ImGui::ColorEdit3("Ambient Colour", &lightData.ambient.x);
// Light presets
if (ImGui::Button("Top Light"))
{
lightYaw = 0.0f;
lightPitch = -90.0f;
}
ImGui::SameLine();
if (ImGui::Button("Front Light"))
{
lightYaw = 0.0f;
lightPitch = -45.0f;
}
ImGui::SameLine();
if (ImGui::Button("Side Light"))
{
lightYaw = 90.0f;
lightPitch = -45.0f;
}
ImGui::SameLine();
if (ImGui::Button("Back Light"))
{
lightYaw = 180.0f;
lightPitch = -45.0f;
}
// Material controls - Keep as before
ImGui::SeparatorText("Material Parameters");
//Diffuse Colour
ImGui::ColorEdit4("Diffuse Colour", &materialData.diffuseColor.x);
//Specular Colour
ImGui::ColorEdit4("Specular Colour", &materialData.specularColor.x);
//Shininess
ImGui::DragFloat("Shininess", &materialData.shininess, 1.0f, 1.0f, 256.0f, "%.0f");
// Texture toggle
ImGui::Checkbox("Use Texture", reinterpret_cast<bool*>(&materialData.hasTexture));
ImGui::End();
}
void ModuleImGui::drawTransformControls()
{
if (!showTransformControls) return;
// Position window on the right side
ImGuiViewport* viewport = ImGui::GetMainViewport();
float windowWidth = 350.0f;
ImGui::SetNextWindowPos(ImVec2(viewport->Size.x - windowWidth - 10, 30), ImGuiCond_FirstUseEver);
ImGui::SetNextWindowSize(ImVec2(windowWidth, 500), ImGuiCond_FirstUseEver);
ImGui::Begin("Duck Transform Controls", &showTransformControls);
// Wrap angle between 0 and 360
auto wrapAngle360 = [](float angle) -> float {
angle = fmodf(angle, 360.0f);
if (angle < 0.0f) angle += 360.0f;
return angle;
};
// Use columns for better layout that scales
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(8, 4));
// Translation controls
ImGui::SeparatorText("Position");
//Labels on left, inputs on right
ImGui::Columns(2, "PositionColumns", false);
ImGui::SetColumnWidth(0, 100.0f); // Fixed width for labels
ImGui::Text("X:");
ImGui::NextColumn();
ImGui::PushItemWidth(-0.25); // Fill remaining width
ImGui::DragFloat("##PosX", &transformData.translation.x, 0.1f, 0.0f, 0.0f, "%.2f");
ImGui::PopItemWidth();
ImGui::NextColumn();
ImGui::Text("Y:");
ImGui::NextColumn();
ImGui::PushItemWidth(-0.25);
ImGui::DragFloat("##PosY", &transformData.translation.y, 0.1f, 0.0f, 0.0f, "%.2f");
ImGui::PopItemWidth();
ImGui::NextColumn();
ImGui::Text("Z:");
ImGui::NextColumn();
ImGui::PushItemWidth(-0.25);
ImGui::DragFloat("##PosZ", &transformData.translation.z, 0.1f, 0.0f, 0.0f, "%.2f");
ImGui::PopItemWidth();
ImGui::Columns(1); // Back to single column
// Current position display
ImGui::Text("Current: (%.2f, %.2f, %.2f)",
transformData.translation.x,
transformData.translation.y,
transformData.translation.z);
// Position buttons
ImGui::BeginGroup();
float buttonWidth = (ImGui::GetContentRegionAvail().x - ImGui::GetStyle().ItemSpacing.x) / 2.0f;
if (ImGui::Button("Center (0,0,0)", ImVec2(buttonWidth, 0))) {
transformData.translation = DirectX::SimpleMath::Vector3(0.0f, 0.0f, 0.0f);
}
ImGui::SameLine();
if (ImGui::Button("Reset Position", ImVec2(buttonWidth, 0))) {
transformData.translation = DirectX::SimpleMath::Vector3(0.0f, -1.0f, 0.0f);
}
ImGui::EndGroup();
// ROTATION CONTROLS
ImGui::SeparatorText("Rotation");
//2 columns for rotation
ImGui::Columns(2, "RotationColumns", false);
ImGui::SetColumnWidth(0, 100.0f);
ImGui::Text("X (Pitch):");
ImGui::NextColumn();
ImGui::PushItemWidth(-1);
ImGui::DragFloat("##RotX", &transformData.rotation.x, 1.0f, -360.0f, 360.0f, "%.1f°");
ImGui::PopItemWidth();
ImGui::NextColumn();
ImGui::Text("Y (Yaw):");
ImGui::NextColumn();
ImGui::PushItemWidth(-1);
ImGui::DragFloat("##RotY", &transformData.rotation.y, 1.0f, -360.0f, 360.0f, "%.1f°");
ImGui::PopItemWidth();
ImGui::NextColumn();
ImGui::Text("Z (Roll):");
ImGui::NextColumn();
ImGui::PushItemWidth(-1);
ImGui::DragFloat("##RotZ", &transformData.rotation.z, 1.0f, -360.0f, 360.0f, "%.1f°");
ImGui::PopItemWidth();
ImGui::Columns(1);
// Display angles
ImGui::Text("Display (0° to 360°):");
ImGui::Text("(%.1f°, %.1f°, %.1f°)",
wrapAngle360(transformData.rotation.x),
wrapAngle360(transformData.rotation.y),
wrapAngle360(transformData.rotation.z));
// Rotation reset button - full width
if (ImGui::Button("Reset Rotation (0,0,0)", ImVec2(-1, 0))) {
transformData.rotation = DirectX::SimpleMath::Vector3(0.0f, 0.0f, 0.0f);
}
// Scale controls
ImGui::SeparatorText("Scale");
static bool uniformScale = true;
// Uniform scale toggle
ImGui::Columns(2, "ScaleToggleColumns", false);
ImGui::SetColumnWidth(0, 100.0f);
ImGui::Text("Uniform:");
ImGui::NextColumn();
ImGui::Checkbox("##UniformScale", &uniformScale);
ImGui::Columns(1);
if (uniformScale) {
// Single slider for uniform scale
ImGui::Columns(2, "ScaleUniformColumns", false);
ImGui::SetColumnWidth(0, 100.0f);
ImGui::Text("Scale:");
ImGui::NextColumn();
ImGui::PushItemWidth(-1);
float uniformValue = transformData.scale.x;
if (ImGui::DragFloat("##ScaleUniform", &uniformValue, 0.01f, 0.0f, 0.0f, "%.2f")) {
transformData.scale = DirectX::SimpleMath::Vector3(uniformValue, uniformValue, uniformValue);
}
ImGui::PopItemWidth();
}
else {
// Separate sliders for XYZ scale
ImGui::Columns(2, "ScaleXYZColumns", false);
ImGui::SetColumnWidth(0, 100.0f);
ImGui::Text("X:");
ImGui::NextColumn();
ImGui::PushItemWidth(-1);
ImGui::DragFloat("##ScaleX", &transformData.scale.x, 0.01f);
ImGui::PopItemWidth();
ImGui::NextColumn();
ImGui::Text("Y:");
ImGui::NextColumn();
ImGui::PushItemWidth(-1);
ImGui::DragFloat("##ScaleY", &transformData.scale.y, 0.01f);
ImGui::PopItemWidth();
ImGui::NextColumn();
ImGui::Text("Z:");
ImGui::NextColumn();
ImGui::PushItemWidth(-1);
ImGui::DragFloat("##ScaleZ", &transformData.scale.z, 0.01f);
ImGui::PopItemWidth();
}
ImGui::Columns(1);
// Current scale display
ImGui::Text("Current: (%.3f, %.3f, %.3f)",
transformData.scale.x,
transformData.scale.y,
transformData.scale.z);
// Scale reset button - full width
if (ImGui::Button("Reset Scale (1,1,1)", ImVec2(-1, 0))) {
transformData.scale = DirectX::SimpleMath::Vector3(1.0f, 1.0f, 1.0f);
}
// Presets section
ImGui::SeparatorText("Presets");
// Calculate button sizes for presets
float presetButtonWidth = (ImGui::GetContentRegionAvail().x - ImGui::GetStyle().ItemSpacing.x * 2.0f) / 3.0f;
ImGui::BeginGroup();
if (ImGui::Button("Default Duck", ImVec2(presetButtonWidth, 0))) {
transformData.scale = DirectX::SimpleMath::Vector3(0.025f, 0.025f, 0.025f);
transformData.translation = DirectX::SimpleMath::Vector3(0.0f, -1.0f, 0.0f);
transformData.rotation = DirectX::SimpleMath::Vector3(0.0f, 0.0f, 0.0f);
}
ImGui::SameLine();
if (ImGui::Button("Giant (10x)", ImVec2(presetButtonWidth, 0))) {
transformData.scale = DirectX::SimpleMath::Vector3(0.25f, 0.25f, 0.25f);
}
ImGui::SameLine();
if (ImGui::Button("Tiny (0.1x)", ImVec2(presetButtonWidth, 0))) {
transformData.scale = DirectX::SimpleMath::Vector3(0.0025f, 0.0025f, 0.0025f);
}
ImGui::EndGroup();
// All reset button - full width with separator
ImGui::Separator();
if (ImGui::Button("Reset All Transform", ImVec2(-1, 0))) {
transformData.translation = DirectX::SimpleMath::Vector3(0.0f, -1.0f, 0.0f);
transformData.rotation = DirectX::SimpleMath::Vector3(0.0f, 0.0f, 0.0f);
transformData.scale = DirectX::SimpleMath::Vector3(0.025f, 0.025f, 0.025f);
}
// Advanced section (collapsed by default)
if (ImGui::TreeNode("Transformation Details"))
{
// Create transformation matrix using wrapped angles (0-360)
DirectX::SimpleMath::Matrix transform =
DirectX::SimpleMath::Matrix::CreateScale(transformData.scale) *
DirectX::SimpleMath::Matrix::CreateFromYawPitchRoll(
DirectX::XMConvertToRadians(wrapAngle360(transformData.rotation.y)),
DirectX::XMConvertToRadians(wrapAngle360(transformData.rotation.x)),
DirectX::XMConvertToRadians(wrapAngle360(transformData.rotation.z))
) *
DirectX::SimpleMath::Matrix::CreateTranslation(transformData.translation);
ImGui::Text("World Matrix (using 0-360° angles):");
for (int row = 0; row < 4; row++) {
ImGui::Text("%8.3f %8.3f %8.3f %8.3f",
transform.m[row][0], transform.m[row][1],
transform.m[row][2], transform.m[row][3]);
}
// Angle conversion info
ImGui::Text("Angle Conversions:");
ImGui::Text("Raw Input: (%.1f°, %.1f°, %.1f°)",
transformData.rotation.x,
transformData.rotation.y,
transformData.rotation.z);
ImGui::Text("Normalized 0-360°: (%.1f°, %.1f°, %.1f°)",
wrapAngle360(transformData.rotation.x),
wrapAngle360(transformData.rotation.y),
wrapAngle360(transformData.rotation.z));
ImGui::TreePop();
}
ImGui::PopStyleVar(); // ItemSpacing
ImGui::End();
}