diff options
author | rtk0c <[email protected]> | 2022-06-03 23:26:44 -0700 |
---|---|---|
committer | rtk0c <[email protected]> | 2022-06-03 23:26:44 -0700 |
commit | 60ccc62f4934e44ad5b905fdbcf458302b8d8a09 (patch) | |
tree | 02ec83cc8387abfd08bd5ee7ea4e8115f1bfb8d0 /source/Game/SceneThings.cpp | |
parent | c2ef7737536bf1f8c81fcfae95c0183b21c9753f (diff) |
Changeset: 63 [WIP] Rename directories
Diffstat (limited to 'source/Game/SceneThings.cpp')
-rw-r--r-- | source/Game/SceneThings.cpp | 142 |
1 files changed, 142 insertions, 0 deletions
diff --git a/source/Game/SceneThings.cpp b/source/Game/SceneThings.cpp new file mode 100644 index 0000000..3fa0436 --- /dev/null +++ b/source/Game/SceneThings.cpp @@ -0,0 +1,142 @@ +#include "SceneThings.hpp" + +#include "CommonVertexIndex.hpp" +#include "Rect.hpp" + +#include <utility> + +SimpleGeometryObject::SimpleGeometryObject(GameWorld* world) + : GameObject(KD_SimpleGeometry, world) + , mRenderObject() + , mSize{ 1.0f, 1.0f, 1.0f } + , mXFaceColor(kXAxisColor) + , mYFaceColor(kYAxisColor) + , mZFaceColor(kZAxisColor) + , mNeedsRebuildMesh{ true } { + mRenderObject.SetMaterial(gDefaultMaterial.Get()); + mRenderObject.SetFormat(gVformatStandard.Get(), Tags::IT_16Bit); + mRenderObject.RebuildIfNecessary(); +} + +void SimpleGeometryObject::SetSize(glm::vec3 size) { + mSize = size; + mNeedsRebuildMesh = true; +} + +void SimpleGeometryObject::SetXFaceColor(RgbaColor color) { + mXFaceColor = color; + mNeedsRebuildMesh = true; +} + +void SimpleGeometryObject::SetYFaceColor(RgbaColor color) { + mYFaceColor = color; + mNeedsRebuildMesh = true; +} + +void SimpleGeometryObject::SetZFaceColor(RgbaColor color) { + mZFaceColor = color; + mNeedsRebuildMesh = true; +} + +std::span<const RenderObject> SimpleGeometryObject::GetRenderObjects() const { + using namespace Tags; + + 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 extents = mSize / 2.0f; + + 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 = xDir * extents.x; + GenerateFace(glm::vec3(x, 0, 0), glm::vec3(0, 0, extents.z), glm::vec3(0, extents.y, 0), mXFaceColor); + } + for (int yDir : { -1, 1 }) { + float y = yDir * extents.y; + GenerateFace(glm::vec3(0, y, 0), glm::vec3(extents.x, 0, 0), glm::vec3(0, 0, extents.z), mYFaceColor); + } + for (int zDir : { -1, 1 }) { + float z = zDir * extents.z; + GenerateFace(glm::vec3(0, 0, 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) + : GameObject(KD_Building, world) { + mRenderObject.SetMaterial(gDefaultMaterial.Get()); + mRenderObject.SetFormat(gVformatStandard.Get(), Tags::IT_32Bit); + mRenderObject.RebuildIfNecessary(); +} + +// void BuildingObject::SetMeshMaterial(Material* material) { +// mMaterial.Attach(material); +// // TODO update render +// } + +// const Material* BuildingObject::GetMeshMaterial() const { +// return mMaterial.Get(); +// } + +// void BuildingObject::SetMesh(GpuMesh* mesh) { +// mMesh.Attach(mesh); +// // TODO update render +// } + +// const GpuMesh* BuildingObject::GetMesh() const { +// return mMesh.Get(); +// } + +std::span<const RenderObject> BuildingObject::GetRenderObjects() const { + return { &mRenderObject, 1 }; +} |