forked from AmigurumiShaders/DX11Starter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSDFEntity.cpp
More file actions
196 lines (152 loc) · 6.24 KB
/
SDFEntity.cpp
File metadata and controls
196 lines (152 loc) · 6.24 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
#include "SDFEntity.h"
#include <iostream>
#include "imgui/imgui.h"
std::unique_ptr<SDFEntity> SDFEntity::s_Entity = nullptr;
static bool create_sphere = true;
static bool create_cube = false;
static bool create_cone = false;
// TODO: more shapes
static int item_current_idx = 0;
static bool continuos_update = false;
SDFEntity::SDFEntity(unsigned int nth, RaymarchPSExternalData* data):
masterPSData(data)
{
AddSphere();
name = "SDF number " + std::to_string(nth);
}
bool SDFEntity::CanAddPrimitive(int count)
{
if (count >= MAX_PRIMITIVES)
{
std::cerr << "Max sdf primitives count reached\n";
return false;
}
return true;
}
void SDFEntity::AddSphere()
{
if (!CanAddPrimitive(sphereCount))
return;
std::string name = "sphere" + std::to_string(sphereCount);
PrimitiveData newPrim = {};
newPrim.name = name;
newPrim.type = SDFType::Sphere;
newPrim.idx = masterPSData->sphereCount;
masterPSData->spherePrims[newPrim.idx] = {}; //new struct with default values
masterPSData->sphereCount++;
sphereCount++;
primitives.push_back(newPrim);
}
void SDFEntity::AddBox()
{
if (!CanAddPrimitive(boxCount))
return;
std::string name = "box" + std::to_string(boxCount);
PrimitiveData newPrim = {};
newPrim.name = name;
newPrim.type = SDFType::Box;
newPrim.idx = masterPSData->boxCount;
masterPSData->boxPrims[newPrim.idx] = {}; //new struct with default values
masterPSData->boxCount++;
boxCount++;
primitives.push_back(newPrim);
}
void SDFEntity::AddTorus()
{
if (!CanAddPrimitive(torusCount))
return;
std::string name = "torus" + std::to_string(torusCount);
PrimitiveData newPrim = {};
newPrim.name = name;
newPrim.type = SDFType::Torus;
newPrim.idx = masterPSData->torusCount;
masterPSData->torusPrims[newPrim.idx] = {};
masterPSData->torusCount++;
torusCount++;
primitives.push_back(newPrim);
}
void SDFEntity::UpdateGUI()
{
if (ImGui::TreeNode("Primitives"))
{
if (ImGui::BeginListBox("Primitives"))
{
for (int i = 0; i < primitives.size(); i++)
{
const bool is_selected = (item_current_idx == i);
const char* name = primitives.at(i).name.c_str(); //idk why it didnt work with [i] but why would strings in c++ ever miss out on a chance to make me miserable and confused
if (ImGui::Selectable(name, is_selected))
item_current_idx = i;
// Set the initial focus when opening the combo (scrolling + keyboard navigation focus)
if (is_selected)
ImGui::SetItemDefaultFocus();
}
ImGui::EndListBox();
}
ImGui::TreePop();
}
if (primitives.size() > 0 && item_current_idx < primitives.size())
{
switch (primitives.at(item_current_idx).type)
{
case SDFType::Sphere:
ShowSphereSettings(item_current_idx);
break;
case SDFType::Box:
ShowBoxSettings(item_current_idx);
break;
case SDFType::Torus:
ShowTorusSettings(item_current_idx);
break;
default:
break;
}
}
ImGui::Separator();
if (ImGui::Button("CreateSphere"))
{
AddSphere();
}
if (ImGui::Button("CreateBox"))
{
AddBox();
}
if (ImGui::Button("CreateTorus"))
{
AddTorus();
}
}
void SDFEntity::ShowSphereSettings(int selectedIndex)
{
ImGui::SeparatorText("Sphere Settings");
ImGui::SliderFloat3("Position", (float*)&masterPSData->spherePrims[primitives[selectedIndex].idx].Position, -50.0, 50.0);
ImGui::SliderFloat("Radius", &masterPSData->spherePrims[primitives[selectedIndex].idx].Radius, 0, 100);
ImGui::SliderInt("Material type", &masterPSData->spherePrims[primitives[selectedIndex].idx].MaterialType, 0, masterPSData->materialCount);
ImGui::SeparatorText("looping Animation Settings");
ImGui::SliderFloat3("Delta position", (float*)&masterPSData->spherePrims[primitives[selectedIndex].idx].DeltaPosition, 0, 10);
ImGui::SliderFloat("speed", &masterPSData->spherePrims[primitives[selectedIndex].idx].speed, 0, 10);
ImGui::SliderFloat("smooth step", &masterPSData->spherePrims[primitives[selectedIndex].idx].smoothStep, 0, 1);
}
void SDFEntity::ShowBoxSettings(int selectedIndex)
{
ImGui::SeparatorText("Box Settings");
ImGui::SliderFloat3("Position", (float*)&masterPSData->boxPrims[primitives[selectedIndex].idx].Position, -50.0, 50.0);
ImGui::SliderFloat3("Dimensions", (float*)&masterPSData->boxPrims[primitives[selectedIndex].idx].Dimensions, -100.0, 100.0);
ImGui::SliderInt("Material type", &masterPSData->boxPrims[primitives[selectedIndex].idx].MaterialType, 0, masterPSData->materialCount);
ImGui::SeparatorText("looping Animation Settings");
ImGui::SliderFloat3("Delta position", (float*)&masterPSData->boxPrims[primitives[selectedIndex].idx].DeltaPosition, 0, 10);
ImGui::SliderFloat("rotation Radius", &masterPSData->boxPrims[primitives[selectedIndex].idx].RotationRadius, 0, 10);
ImGui::SliderFloat("smooth step", &masterPSData->boxPrims[primitives[selectedIndex].idx].smoothStep, 0, 1);
}
void SDFEntity::ShowTorusSettings(int selectedIndex)
{
ImGui::SeparatorText("Torus Settings");
ImGui::SliderFloat3("Position", (float*)&masterPSData->torusPrims[primitives[selectedIndex].idx].Position, -50.0, 50.0);
ImGui::SliderFloat("Large Radius", &masterPSData->torusPrims[primitives[selectedIndex].idx].Radius, 0, 100);
ImGui::SliderFloat("Small Radius", &masterPSData->torusPrims[primitives[selectedIndex].idx].SmallRadius, 0, 10);
ImGui::SliderInt("Material type", &masterPSData->torusPrims[primitives[selectedIndex].idx].MaterialType, 0, masterPSData->materialCount);
ImGui::SeparatorText("looping Animation Settings");
ImGui::SliderFloat3("Delta position", (float*)&masterPSData->torusPrims[primitives[selectedIndex].idx].DeltaPosition, 0, 10);
ImGui::SliderFloat("rotation Radius", &masterPSData->torusPrims[primitives[selectedIndex].idx].RotationRadius, 0, 10);
ImGui::SliderFloat("smooth step", &masterPSData->torusPrims[primitives[selectedIndex].idx].smoothStep, 0, 1);
}