aboutsummaryrefslogtreecommitdiff
path: root/source
diff options
context:
space:
mode:
authorrtk0c <[email protected]>2022-05-07 18:52:29 -0700
committerrtk0c <[email protected]>2022-05-07 18:52:29 -0700
commitffb58d99615beadc67028d52dfeb85358afae40d (patch)
treeb5c70cac17e1afd87f18aeafaf27beaf8abce90d /source
parent9fcdfe312fd9809a1cd52c08e7d8d7bd991a9fb3 (diff)
Changeset: 26 Make SimpleGeometryObject render a cube
Diffstat (limited to 'source')
-rw-r--r--source/Color.hpp4
-rw-r--r--source/EditorCorePrivate.cpp37
-rw-r--r--source/SceneThings.cpp107
-rw-r--r--source/SceneThings.hpp22
4 files changed, 136 insertions, 34 deletions
diff --git a/source/Color.hpp b/source/Color.hpp
index c7d633c..ef0c5a9 100644
--- a/source/Color.hpp
+++ b/source/Color.hpp
@@ -90,6 +90,10 @@ public:
friend constexpr bool operator==(const RgbaColor&, const RgbaColor&) noexcept = default;
};
+constexpr RgbaColor kXAxisColor(0xFF, 0x80, 0x80, 0xFF);
+constexpr RgbaColor kYAxisColor(0x80, 0xFF, 0x80, 0xFF);
+constexpr RgbaColor kZAxisColor(0x80, 0x80, 0xFF, 0xFF);
+
class HsvColor {
public:
float h;
diff --git a/source/EditorCorePrivate.cpp b/source/EditorCorePrivate.cpp
index 35378ce..e4fdc54 100644
--- a/source/EditorCorePrivate.cpp
+++ b/source/EditorCorePrivate.cpp
@@ -315,9 +315,10 @@ EditorInstance::EditorInstance(App* app)
: mApp{ app }
, mEdContentBrowser(&mEdInspector) {
mEditorCamera.name = "Editor Camera"s;
- mEditorCamera.Move(glm::vec3(0, 0, 1));
- mEditorCamera.LookAtAngle(glm::vec3(0, 0, -1));
- mEditorCamera.SetHasPerspective(false);
+ mEditorCamera.Move(glm::vec3(50, 40, 50));
+ mEditorCamera.LookAtPos(glm::vec3(0, 0, 0));
+ mEditorCamera.SetHasPerspective(true);
+ app->BindActiveCamera(&mEditorCamera);
}
EditorInstance::~EditorInstance() {
@@ -729,22 +730,46 @@ void EditorInstance::ShowInspector(GameObject* object) {
float bounds[6] = {
/*x1*/ -sg->GetSize().x / 2.0f,
/*y1*/ -sg->GetSize().y / 2.0f,
- /*z1*/ 0.0f,
+ /*z1*/ -sg->GetSize().z / 2.0f,
/*x2*/ +sg->GetSize().x / 2.0f,
/*y2*/ +sg->GetSize().y / 2.0f,
- /*z2*/ 0.0f,
+ /*z2*/ +sg->GetSize().z / 2.0f,
};
ShowInspector(bounds);
ShowGuizmo(bounds);
ImGui::Separator();
- sg->SetSize(glm::vec2(bounds[3] - bounds[0], bounds[4] - bounds[1]));
+ sg->SetSize(glm::vec3(bounds[3] - bounds[0], bounds[4] - bounds[1], bounds[5] - bounds[2]));
auto size = sg->GetSize();
if (ImGui::InputFloat2("Size", &size.x)) {
sg->SetSize(size);
}
+
+ auto xFaceColor = sg->GetXFaceColor();
+ if (ImGui::ColorEdit4("X color", &xFaceColor)) {
+ sg->SetXFaceColor(xFaceColor);
+ }
+ auto yFaceColor = sg->GetYFaceColor();
+ if (ImGui::ColorEdit4("Y color", &yFaceColor)) {
+ sg->SetYFaceColor(yFaceColor);
+ }
+ auto zFaceColor = sg->GetZFaceColor();
+ if (ImGui::ColorEdit4("Z color", &zFaceColor)) {
+ sg->SetZFaceColor(zFaceColor);
+ }
+
+ if (ImGui::Button("Sync from X color")) {
+ sg->SetYFaceColor(xFaceColor);
+ sg->SetZFaceColor(xFaceColor);
+ }
+ ImGui::SameLine();
+ if (ImGui::Button("Reset")) {
+ sg->SetXFaceColor(kXAxisColor);
+ sg->SetYFaceColor(kYAxisColor);
+ sg->SetZFaceColor(kZAxisColor);
+ }
} break;
case GameObject::KD_Building: {
diff --git a/source/SceneThings.cpp b/source/SceneThings.cpp
index 5dcec1a..6e4624b 100644
--- a/source/SceneThings.cpp
+++ b/source/SceneThings.cpp
@@ -8,40 +8,109 @@
SimpleGeometryObject::SimpleGeometryObject(GameWorld* world)
: GameObject(KD_SimpleGeometry, world)
, mRenderObject()
- , mSize{ 100.0f, 100.0f }
- , mColor(60, 60, 60) {
+ , mSize{ 50.0f, 50.0f, 50 }
+ , mXFaceColor(kXAxisColor)
+ , mYFaceColor(kYAxisColor)
+ , mZFaceColor(kZAxisColor)
+ , mNeedsRebuildMesh{ true } {
mRenderObject.SetMaterial(gDefaultMaterial.Get());
mRenderObject.SetFormat(gVformatStandard.Get(), Tags::IT_16Bit);
mRenderObject.RebuildIfNecessary();
- UpdateRenderObject();
}
-void SimpleGeometryObject::SetSize(glm::vec2 size) {
+void SimpleGeometryObject::SetSize(glm::vec3 size) {
mSize = size;
- UpdateRenderObject();
+ mNeedsRebuildMesh = true;
}
-void SimpleGeometryObject::SetColor(RgbaColor color) {
- mColor = color;
- UpdateRenderObject();
+void SimpleGeometryObject::SetXFaceColor(RgbaColor color) {
+ mXFaceColor = color;
+ mNeedsRebuildMesh = true;
}
-std::span<const RenderObject> SimpleGeometryObject::GetRenderObjects() const {
- return { &mRenderObject, 1 };
+void SimpleGeometryObject::SetYFaceColor(RgbaColor color) {
+ mYFaceColor = color;
+ mNeedsRebuildMesh = true;
+}
+
+void SimpleGeometryObject::SetZFaceColor(RgbaColor color) {
+ mZFaceColor = color;
+ mNeedsRebuildMesh = true;
}
-void SimpleGeometryObject::UpdateRenderObject() {
+std::span<const RenderObject> SimpleGeometryObject::GetRenderObjects() const {
using namespace Tags;
- uint16_t indices[6];
- Index_U16::Assign(indices, 0);
- mRenderObject.GetIndexBuffer()->Upload((const std::byte*)indices, IT_16Bit, std::size(indices));
+ if (mNeedsRebuildMesh) {
+ mNeedsRebuildMesh = false;
+
+ Vertex_PTC vertices[4 /*vertices per face*/ * 6 /*faces*/];
+ uint16_t indices[3 /*indices per triangle*/ * 2 /*triangles per face*/ * 6 /*faces*/];
+
+ auto pos = GetPos();
+ auto extents = mSize / 2.0f;
- Vertex_PTC vertices[4];
- Vertex_PTC::Assign(vertices, Rect<float>{ -mSize / 2.0f, mSize });
- Vertex_PTC::Assign(vertices, 0.0f);
- Vertex_PTC::Assign(vertices, mColor);
- mRenderObject.GetVertexBufferBindings().bindings[0]->Upload((const std::byte*)vertices, sizeof(vertices));
+ int faceGenVerticesIdx = 0;
+ int faceGenIndicesIdx = 0;
+ auto GenerateFace = [&](glm::vec3 faceCenter, glm::vec3 firstExtentVec, glm::vec3 secondExtentVec, RgbaColor color) {
+ // Generates (if viewing top down on the face): bottom left, top left, bottom right, top right
+ // (-1, -1) , (-1, 1) , (1, -1) , (1, 1)
+ // idx=0 , idx=1 , idx=2 , idx=3
+
+ // These are index offsets, see above comment
+ constexpr int kBottomLeft = 0;
+ constexpr int kTopLeft = 1;
+ constexpr int kBottomRight = 2;
+ constexpr int kTopRight = 3;
+
+ int startVertIdx = faceGenVerticesIdx;
+ for (float firstDir : { -1, 1 }) {
+ for (float secondDir : { -1, 1 }) {
+ auto vertPos = faceCenter + firstExtentVec * firstDir + secondExtentVec * secondDir;
+ auto& vert = vertices[faceGenVerticesIdx];
+ vert.x = vertPos.x;
+ vert.y = vertPos.y;
+ vert.z = vertPos.z;
+ vert.r = color.r;
+ vert.g = color.g;
+ vert.b = color.b;
+ vert.a = color.a;
+ faceGenVerticesIdx += 1;
+ }
+ }
+
+ // Triangle #1
+ indices[faceGenIndicesIdx++] = startVertIdx + kTopRight;
+ indices[faceGenIndicesIdx++] = startVertIdx + kTopLeft;
+ indices[faceGenIndicesIdx++] = startVertIdx + kBottomLeft;
+ // Triangle #2
+ indices[faceGenIndicesIdx++] = startVertIdx + kTopRight;
+ indices[faceGenIndicesIdx++] = startVertIdx + kBottomLeft;
+ indices[faceGenIndicesIdx++] = startVertIdx + kBottomRight;
+ };
+ for (int xDir : { -1, 1 }) {
+ float x = pos.x + xDir * extents.x;
+ GenerateFace(glm::vec3(x, pos.y, pos.z), glm::vec3(0, 0, extents.z), glm::vec3(0, extents.y, 0), mXFaceColor);
+ }
+ for (int yDir : { -1, 1 }) {
+ float y = pos.y + yDir * extents.y;
+ GenerateFace(glm::vec3(pos.x, y, pos.z), glm::vec3(extents.x, 0, 0), glm::vec3(0, 0, extents.z), mYFaceColor);
+ }
+ for (int zDir : { -1, 1 }) {
+ float z = pos.z + zDir * extents.z;
+ GenerateFace(glm::vec3(pos.x, pos.y, z), glm::vec3(extents.x, 0, 0), glm::vec3(0, extents.y, 0), mZFaceColor);
+ }
+
+ for (auto& vert : vertices) {
+ vert.u = 0.0f;
+ vert.v = 0.0f;
+ }
+
+ mRenderObject.GetVertexBufferBindings().bindings[0]->Upload((const std::byte*)vertices, sizeof(vertices));
+ mRenderObject.GetIndexBuffer()->Upload((const std::byte*)indices, IT_16Bit, std::size(indices));
+ }
+
+ return { &mRenderObject, 1 };
}
BuildingObject::BuildingObject(GameWorld* world)
diff --git a/source/SceneThings.hpp b/source/SceneThings.hpp
index da551c0..c261fbb 100644
--- a/source/SceneThings.hpp
+++ b/source/SceneThings.hpp
@@ -10,20 +10,24 @@
class SimpleGeometryObject : public GameObject {
private:
RenderObject mRenderObject;
- glm::vec2 mSize;
- RgbaColor mColor;
+ glm::vec3 mSize;
+ RgbaColor mXFaceColor;
+ RgbaColor mYFaceColor;
+ RgbaColor mZFaceColor;
+ mutable bool mNeedsRebuildMesh ;
public:
SimpleGeometryObject(GameWorld* world);
- glm::vec2 GetSize() const { return mSize; }
- void SetSize(glm::vec2 size);
- RgbaColor GetColor() const { return mColor; }
- void SetColor(RgbaColor color);
+ glm::vec3 GetSize() const { return mSize; }
+ void SetSize(glm::vec3 size);
+ RgbaColor GetXFaceColor() const { return mXFaceColor; }
+ void SetXFaceColor(RgbaColor color);
+ RgbaColor GetYFaceColor() const { return mYFaceColor; }
+ void SetYFaceColor(RgbaColor color);
+ RgbaColor GetZFaceColor() const { return mZFaceColor; }
+ void SetZFaceColor(RgbaColor color);
virtual std::span<const RenderObject> GetRenderObjects() const override;
-
-private:
- void UpdateRenderObject();
};
class BuildingObject : public GameObject {