diff options
author | rtk0c <[email protected]> | 2022-04-30 13:55:20 -0700 |
---|---|---|
committer | rtk0c <[email protected]> | 2022-04-30 13:55:20 -0700 |
commit | 453e1df6fb7a5847c8a5b26bd8479451091fb69d (patch) | |
tree | e0753be4c78535a3d2697c8b7be4123b76d2ad0d | |
parent | ac153a0a9463e3877fb0066e3603b6bf15fe6706 (diff) |
Changeset: 20 Add ImGuizmo for GameObjects, start to make things actually render
-rw-r--r-- | source/App.cpp | 7 | ||||
-rw-r--r-- | source/CommonVertexIndex.hpp | 6 | ||||
-rw-r--r-- | source/EditorCore.cpp | 198 | ||||
-rw-r--r-- | source/EditorCore.hpp | 29 | ||||
-rw-r--r-- | source/EditorUtils.cpp | 2 | ||||
-rw-r--r-- | source/EditorUtils.hpp | 2 | ||||
-rw-r--r-- | source/GameObject.hpp | 2 | ||||
-rw-r--r-- | source/Player.cpp | 3 | ||||
-rw-r--r-- | source/Renderer.cpp | 39 | ||||
-rw-r--r-- | source/Renderer.hpp | 19 | ||||
-rw-r--r-- | source/SceneThings.cpp | 7 | ||||
-rw-r--r-- | source/Shader.cpp | 37 | ||||
-rw-r--r-- | source/Shader.hpp | 7 | ||||
-rw-r--r-- | source/Sprite.cpp | 1 | ||||
-rw-r--r-- | source/Sprite.hpp | 2 | ||||
-rw-r--r-- | source/VertexIndex.hpp | 4 | ||||
-rw-r--r-- | source/main.cpp | 58 |
17 files changed, 321 insertions, 102 deletions
diff --git a/source/App.cpp b/source/App.cpp index dc38342..9c18fea 100644 --- a/source/App.cpp +++ b/source/App.cpp @@ -25,7 +25,7 @@ App::App() { mGameCamera.SetHasPerspective(false); mEditorCamera.Move(glm::vec3(0, 0, 1)); mEditorCamera.LookAt(glm::vec3(0, 0, 0)); - mGameCamera.SetHasPerspective(false); + mEditorCamera.SetHasPerspective(false); } App::~App() { @@ -55,7 +55,7 @@ void App::SetEditorVisible(bool visible) { mEditorVisible = visible; if (visible) { if (mEditor == nullptr) { - mEditor = std::make_unique<EditorInstance>(this, &mWorld); + mEditor = std::make_unique<EditorInstance>(this); } } } @@ -70,6 +70,7 @@ void App::Show() { void App::Update() { if (IsGameRunning()) { mWorld.Update(); + mEditor->OnUpdate(); } } @@ -98,6 +99,8 @@ void App::Draw(float currentTime, float deltaTime) { } mRenderer.EndFrame(); + // TODO pass camera info to editor without strong coupling to Renderer + mEditor->OnDraw(mRenderer.GetLastFrameInfo()); } void App::HandleMouse(int button, int action) { diff --git a/source/CommonVertexIndex.hpp b/source/CommonVertexIndex.hpp index c15e658..adb81b6 100644 --- a/source/CommonVertexIndex.hpp +++ b/source/CommonVertexIndex.hpp @@ -1,11 +1,17 @@ #pragma once #include "Color.hpp" +#include "RcPtr.hpp" #include "Rect.hpp" #include "Texture.hpp" +#include "VertexIndex.hpp" #include <cstdint> +// Initialized in main() +inline RcPtr<VertexFormat> gVformatStandard{}; +inline RcPtr<VertexFormat> gVformatStandardSplit{}; + // Suffixes: // - _P_osition // - _T_exture coordiantes diff --git a/source/EditorCore.cpp b/source/EditorCore.cpp index 8288df3..bd5d78d 100644 --- a/source/EditorCore.cpp +++ b/source/EditorCore.cpp @@ -20,7 +20,6 @@ #define GLFW_INCLUDE_NONE #include <GLFW/glfw3.h> -#include <ImGuizmo.h> #include <imgui.h> #include <misc/cpp/imgui_stdlib.h> #include <cstddef> @@ -204,6 +203,117 @@ void EditorContentBrowser::Show(bool* open) { ImGui::End(); } +void EditorTransformEdit::ShowWorld(float* cameraView, float* cameraProjection) { + glm::mat4 identityMatrix(1.00f); + + ImGuiIO& io = ImGui::GetIO(); + float viewManipulateRight = io.DisplaySize.x; + float viewManipulateTop = 0; + static ImGuiWindowFlags gizmoWindowFlags = 0; + + ImGuizmo::SetRect(0, 0, io.DisplaySize.x, io.DisplaySize.y); + + if (showGrid) { + ImGuizmo::DrawGrid(cameraView, cameraProjection, &identityMatrix[0][0], 100.f); + } + + ImGuizmo::ViewManipulate(cameraView, camDistance, ImVec2(viewManipulateRight - 128, viewManipulateTop), ImVec2(128, 128), 0x10101010); +} + +bool EditorTransformEdit::ShowObjectInspector(float* cameraView, float* cameraProjection, glm::vec3* pos, glm::quat* rotation, glm::vec3* scale) { + glm::mat4 identityMatrix(1.00f); + + if (ImGui::IsKeyPressed(90)) + mCurrentGizmoOperation = ImGuizmo::TRANSLATE; + if (ImGui::IsKeyPressed(69)) + mCurrentGizmoOperation = ImGuizmo::ROTATE; + if (ImGui::IsKeyPressed(82)) // r Key + mCurrentGizmoOperation = ImGuizmo::SCALE; + if (ImGui::RadioButton("Translate", mCurrentGizmoOperation == ImGuizmo::TRANSLATE)) + mCurrentGizmoOperation = ImGuizmo::TRANSLATE; + ImGui::SameLine(); + if (ImGui::RadioButton("Rotate", mCurrentGizmoOperation == ImGuizmo::ROTATE)) + mCurrentGizmoOperation = ImGuizmo::ROTATE; + ImGui::SameLine(); + if (ImGui::RadioButton("Scale", mCurrentGizmoOperation == ImGuizmo::SCALE)) + mCurrentGizmoOperation = ImGuizmo::SCALE; + if (ImGui::RadioButton("Universal", mCurrentGizmoOperation == ImGuizmo::UNIVERSAL)) + mCurrentGizmoOperation = ImGuizmo::UNIVERSAL; + + bool edited = false; + + float* matTranslation = &pos->x; + edited |= ImGui::InputFloat3("Tr", matTranslation); + + glm::vec3 eulerAngleRotation = glm::eulerAngles(*rotation); + float* matRotation = &eulerAngleRotation.x; + edited |= ImGui::InputFloat3("Rt", matRotation); + + float* matScale = &scale->x; + edited |= ImGui::InputFloat3("Sc", matScale); + + if (mCurrentGizmoOperation != ImGuizmo::SCALE) { + if (ImGui::RadioButton("Local", mCurrentGizmoMode == ImGuizmo::LOCAL)) + mCurrentGizmoMode = ImGuizmo::LOCAL; + ImGui::SameLine(); + if (ImGui::RadioButton("World", mCurrentGizmoMode == ImGuizmo::WORLD)) + mCurrentGizmoMode = ImGuizmo::WORLD; + } + if (ImGui::IsKeyPressed(83)) + useSnap = !useSnap; + ImGui::Checkbox("##UseSnap", &useSnap); + ImGui::SameLine(); + + switch (mCurrentGizmoOperation) { + case ImGuizmo::TRANSLATE: + ImGui::InputFloat3("Snap", &snap[0]); + break; + case ImGuizmo::ROTATE: + ImGui::InputFloat("Angle Snap", &snap[0]); + break; + case ImGuizmo::SCALE: + ImGui::InputFloat("Scale Snap", &snap[0]); + break; + default: break; + } + ImGui::Checkbox("Bound Sizing", &boundSizing); + if (boundSizing) { + ImGui::PushID(3); + ImGui::Checkbox("##BoundSizing", &boundSizingSnap); + ImGui::SameLine(); + ImGui::InputFloat3("Snap", boundsSnap); + ImGui::PopID(); + } + + return edited; +} + +bool EditorTransformEdit::ShowObject(float* cameraView, float* cameraProjection, glm::vec3* pos, glm::quat* rotation, glm::vec3* scale) { + glm::mat4 identityMatrix(1.00f); + glm::vec3 eulerAngleRotation = glm::eulerAngles(*rotation); + + float matrix[16]; + ImGuizmo::RecomposeMatrixFromComponents(&pos->x, &eulerAngleRotation.x, &scale->x, matrix); + + bool edited = ImGuizmo::Manipulate( + cameraView, + cameraProjection, + mCurrentGizmoOperation, + mCurrentGizmoMode, + matrix, + nullptr, + useSnap ? &snap[0] : nullptr, + boundSizing ? bounds : nullptr, + boundSizingSnap ? boundsSnap : nullptr); + + if (edited) { + ImGuizmo::DecomposeMatrixToComponents(matrix, &pos->x, &eulerAngleRotation.x, &scale->x); + *rotation = glm::quat(eulerAngleRotation); + } + + return edited; +} + namespace ProjectBrussel_UNITY_ID { void PushKeyCodeRecorder(App* app, int* writeKey, bool* writeKeyStatus) { app->PushKeyCaptureCallback([=](int key, int action) { @@ -300,22 +410,32 @@ struct CreatableGameObject { #undef GAMEOBJECT_CONSTRUCTOR } // namespace ProjectBrussel_UNITY_ID -EditorInstance::EditorInstance(App* app, GameWorld* world) +EditorInstance::EditorInstance(App* app) : mApp{ app } - , mWorld{ world } + , mFallbackFrameInfo{ + .camera = nullptr, + .matrixCombined = {}, + .time = 0.0f, + .deltaTime = 0.0f, + } + , mCurrentFrameInfo{ &mFallbackFrameInfo } , mEdContentBrowser(&mEdInspector) {} EditorInstance::~EditorInstance() { } +void EditorInstance::OnUpdate() { +} + +void EditorInstance::OnDraw(const RendererFrameInfo& frameInfo) { + mCurrentFrameInfo = &frameInfo; +} + void EditorInstance::Show() { using namespace ProjectBrussel_UNITY_ID; using namespace Tags; - if (!mWorld) { - return; - } - + auto world = mApp->GetWorld(); auto& io = ImGui::GetIO(); ImGui::BeginMainMenuBar(); @@ -348,11 +468,34 @@ void EditorInstance::Show() { mEdContentBrowser.Show(&mWindowVisible_ContentBrowser); } + auto cameraViewMat = mCurrentFrameInfo->matrixView; + auto cameraView = &cameraViewMat[0][0]; + auto cameraProjMat = mCurrentFrameInfo->matrixProj; + auto cameraProj = &cameraProjMat[0][0]; + // auto cameraView = &mCurrentFrameInfo->matrixView[0][0]; + // auto cameraProj = &mCurrentFrameInfo->matrixProj[0][0]; + // TODO moving camera + // mEdTransformEdit.ShowWorld(cameraView, cameraProj); + ImGuizmo::SetRect(0, 0, io.DisplaySize.x, io.DisplaySize.y); + ImGuizmo::SetDrawlist(ImGui::GetForegroundDrawList()); + if (mWindowVisible_Inspector) { ImGui::Begin("Inspector"); switch (mEdInspector.selectedItt) { case EditorInspector::ITT_GameObject: { - ShowInspector(static_cast<GameObject*>(mEdInspector.selectedItPtr)); + auto object = static_cast<GameObject*>(mEdInspector.selectedItPtr); + + auto objectPos = object->GetPos(); + auto objectRot = object->GetRotation(); + glm::vec3 scale(1.0f); + if (mEdTransformEdit.ShowObjectInspector(cameraView, cameraProj, &objectPos, &objectRot, &scale) || + mEdTransformEdit.ShowObject(cameraView, cameraProj, &objectPos, &objectRot, &scale)) + { + object->SetPos(objectPos); + object->SetRotation(objectRot); + } + + ShowInspector(object); } break; case EditorInspector::ITT_Ires: { @@ -377,7 +520,7 @@ void EditorInstance::Show() { GobjTreeNodeShowInfo showInfo{ .in_editor = this, }; - GobjTreeNode(showInfo, &mWorld->GetRoot()); + GobjTreeNode(showInfo, &world->GetRoot()); if (showInfo.out_openPopup) { mPopupCurrent_GameObject = showInfo.out_openPopup; @@ -395,7 +538,7 @@ void EditorInstance::Show() { for (size_t i = 0; i < std::size(creatableGameObjects); ++i) { auto& info = creatableGameObjects[i]; if (ImGui::MenuItem(info.name)) { - auto object = info.factory(mWorld); + auto object = info.factory(world); mPopupCurrent_GameObject->AddChild(object); } } @@ -444,22 +587,14 @@ void EditorInstance::ShowInspector(GameObject* object) { using namespace Tags; using namespace ProjectBrussel_UNITY_ID; - auto ShowFields = [&]() { - auto pos = object->GetPos(); - if (ImGui::InputFloat3("Position", &pos.x)) { - object->SetPos(pos); - } - - auto quat = object->GetRotation(); - if (ImGui::InputFloat4("Rotation", &quat.x)) { - object->SetRotation(quat); - } + auto ShowBasics = [&]() { + // TODO }; auto type = object->GetKind(); switch (type) { case GameObject::KD_Player: { - ShowFields(); + ShowBasics(); ImGui::Separator(); auto player = static_cast<Player*>(object); @@ -475,7 +610,9 @@ void EditorInstance::ShowInspector(GameObject* object) { if (auto payload = ImGui::AcceptDragDropPayload(IresObject::ToString(IresObject::KD_Spritesheet).data())) { auto spritesheet = *static_cast<IresSpritesheet* const*>(payload->Data); ea->confSprite.Attach(spritesheet); - player->sprite.SetDefinition(spritesheet->GetInstance()); + auto def = spritesheet->GetInstance(); + player->sprite.SetDefinition(def); + player->renderObject.autofill_TextureAtlas.Attach(def->GetAtlas()); } ImGui::EndDragDropTarget(); } @@ -532,15 +669,18 @@ void EditorInstance::ShowInspector(GameObject* object) { } break; case GameObject::KD_SimpleGeometry: { - ShowFields(); - ImGui::Separator(); - auto sg = static_cast<SimpleGeometryObject*>(object); - // TODO + + ShowBasics(); + + auto size = sg->GetSize(); + if (ImGui::InputFloat2("Size", &size.x)) { + sg->SetSize(size); + } } break; case GameObject::KD_Building: { - ShowFields(); + ShowBasics(); ImGui::Separator(); auto b = static_cast<BuildingObject*>(object); @@ -548,7 +688,7 @@ void EditorInstance::ShowInspector(GameObject* object) { } break; case GameObject::KD_LevelWrapper: { - ShowFields(); + ShowBasics(); ImGui::Separator(); auto lwo = static_cast<LevelWrapperObject*>(object); @@ -556,7 +696,7 @@ void EditorInstance::ShowInspector(GameObject* object) { } break; default: { - ShowFields(); + ShowBasics(); } break; } } diff --git a/source/EditorCore.hpp b/source/EditorCore.hpp index af35616..785fc6b 100644 --- a/source/EditorCore.hpp +++ b/source/EditorCore.hpp @@ -2,6 +2,7 @@ #include "EditorAttachment.hpp" #include "EditorCommandPalette.hpp" +#include "EditorUtils.hpp" #include "GameObject.hpp" #include "Ires.hpp" #include "RcPtr.hpp" @@ -55,16 +56,38 @@ public: void Show(bool* open = nullptr); }; +class EditorTransformEdit { +private: + ImGuizmo::OPERATION mCurrentGizmoOperation = ImGuizmo::TRANSLATE; + ImGuizmo::MODE mCurrentGizmoMode = ImGuizmo::LOCAL; + glm::mat4 cubeMatrix; + float snap[3] = { 1.f, 1.f, 1.f }; + float bounds[6] = { -0.5f, -0.5f, -0.5f, 0.5f, 0.5f, 0.5f }; + float boundsSnap[3] = { 0.1f, 0.1f, 0.1f }; + float camDistance = 8.f; + bool useSnap = false; + bool boundSizing = false; + bool boundSizingSnap = false; + bool showGrid = false; + +public: + void ShowWorld(float* cameraView, float* cameraProjection); + bool ShowObjectInspector(float* cameraView, float* cameraProjection, glm::vec3* pos, glm::quat* rotation, glm::vec3* scale); + bool ShowObject(float* cameraView, float* cameraProjection, glm::vec3* pos, glm::quat* rotation, glm::vec3* scale); +}; + class App; class EditorInstance { private: App* mApp; - GameWorld* mWorld; + RendererFrameInfo mFallbackFrameInfo; + const RendererFrameInfo* mCurrentFrameInfo; GameObject* mPopupCurrent_GameObject = nullptr; RcPtr<SpriteDefinition> mSpriteView_Instance; EditorCommandPalette mEdCommandPalette; EditorInspector mEdInspector; EditorContentBrowser mEdContentBrowser; + EditorTransformEdit mEdTransformEdit; int mSpriteView_Frame; bool mSpriteView_OpenNextFrame = false; bool mWindowVisible_ImGuiDemo = false; @@ -75,9 +98,11 @@ private: bool mWindowVisible_WorldProperties = true; public: - EditorInstance(App* app, GameWorld* world); + EditorInstance(App* app); ~EditorInstance(); + void OnUpdate(); + void OnDraw(const RendererFrameInfo& frameInfo); void Show(); EditorInspector& GetInspector() { return mEdInspector; } diff --git a/source/EditorUtils.cpp b/source/EditorUtils.cpp index 488d345..39d66fb 100644 --- a/source/EditorUtils.cpp +++ b/source/EditorUtils.cpp @@ -4,6 +4,8 @@ #include <imgui_internal.h> #include <backends/imgui_impl_glfw.h> +#include <glm/gtc/quaternion.hpp> +#include <glm/gtx/quaternion.hpp> const char* ImGui::GetKeyNameGlfw(int key) { return GetKeyName(ImGui_ImplGlfw_KeyToImGuiKey(key)); diff --git a/source/EditorUtils.hpp b/source/EditorUtils.hpp index 67ffb0c..b079f13 100644 --- a/source/EditorUtils.hpp +++ b/source/EditorUtils.hpp @@ -5,6 +5,8 @@ #include <imgui.h> #include <string> +#include <ImGuizmo.h> + // To check whether a payload is of this type, use starts_with() #define BRUSSEL_TAG_PREFIX_GameObject "GameObject" #define BRUSSEL_TAG_PREFIX_Ires "Ires" diff --git a/source/GameObject.hpp b/source/GameObject.hpp index be53ca0..d0f94a5 100644 --- a/source/GameObject.hpp +++ b/source/GameObject.hpp @@ -33,7 +33,7 @@ private: Kind mKind; protected: - bool mStopFreePropagation : 1 = true; + bool mStopFreePropagation : 1 = false; public: static void FreeRecursive(GameObject* object); diff --git a/source/Player.cpp b/source/Player.cpp index f97793e..b37a232 100644 --- a/source/Player.cpp +++ b/source/Player.cpp @@ -1,6 +1,7 @@ #include "Player.hpp" #include "AppConfig.hpp" +#include "CommonVertexIndex.hpp" #include "ScopeGuard.hpp" #include "Utils.hpp" @@ -22,7 +23,7 @@ Player::Player(GameWorld* world, int id) : GameObject(KD_Player, world) , mId{ id } { renderObject.SetMaterial(gDefaultMaterial.Get()); - renderObject.SetFormat(gVformatStandardPacked.Get(), Tags::IT_16Bit); + renderObject.SetFormat(gVformatStandard.Get(), Tags::IT_16Bit); renderObject.RebuildIfNecessary(); } diff --git a/source/Renderer.cpp b/source/Renderer.cpp index f96c3a0..99fc84d 100644 --- a/source/Renderer.cpp +++ b/source/Renderer.cpp @@ -99,10 +99,12 @@ void RenderObject::DeleteGLObjects() { void Renderer::BeginFrame(Camera& camera, float currentTime, float deltaTime) { assert(mInsideFrame == false); mInsideFrame = true; - mFrame_Cam = &camera; - mFrame_Transform = camera.CalcProjectionMatrix() * camera.CalcViewMatrix(); - mFrame_Time = currentTime; - mFrame_DeltaTime = deltaTime; + mFrame.camera = &camera; + mFrame.matrixView = camera.CalcViewMatrix(); + mFrame.matrixProj = camera.CalcProjectionMatrix(); + mFrame.matrixCombined = mFrame.matrixProj * mFrame.matrixView; + mFrame.time = currentTime; + mFrame.deltaTime = deltaTime; } void Renderer::EndFrame() { @@ -125,19 +127,30 @@ void Renderer::Draw(const RenderObject* objects, size_t count) { glUseProgram(shader->GetProgram()); + // Material uniforms + mat->UseUniforms(); + + // Next available texture unit ID after all material textures + int texIdx = mat->GetTextures().size(); + // Autofill uniforms - if (shader->autofillLoc_Transform != kInvalidLocation) { - glUniformMatrix4fv(shader->autofillLoc_Transform, 1, GL_FALSE, &mFrame_Transform[0][0]); + if (shader->autofill_Transform != kInvalidLocation) { + glUniformMatrix4fv(shader->autofill_Transform, 1, GL_FALSE, &mFrame.matrixCombined[0][0]); } - if (shader->autofillLoc_Time != kInvalidLocation) { - glUniform1f(shader->autofillLoc_Time, mFrame_Time); + if (shader->autofill_Time != kInvalidLocation) { + glUniform1f(shader->autofill_Time, mFrame.time); } - if (shader->autofillLoc_DeltaTime != kInvalidLocation) { - glUniform1f(shader->autofillLoc_DeltaTime, mFrame_DeltaTime); + if (shader->autofill_DeltaTime != kInvalidLocation) { + glUniform1f(shader->autofill_DeltaTime, mFrame.deltaTime); + } + if (shader->autofill_TextureAtlas != kInvalidLocation && + object.autofill_TextureAtlas != nullptr) + { + glActiveTexture(GL_TEXTURE0 + texIdx); + glBindTexture(GL_TEXTURE_2D, object.autofill_TextureAtlas->GetHandle()); + glUniform1i(shader->autofill_TextureAtlas, texIdx); + ++texIdx; } - - // Material uniforms - mat->UseUniforms(); glBindVertexArray(object.GetGLVao()); glDrawElements(GL_TRIANGLES, indexBuffer->count, indexBuffer->GetIndexTypeGL(), 0); diff --git a/source/Renderer.hpp b/source/Renderer.hpp index b4ca735..e527476 100644 --- a/source/Renderer.hpp +++ b/source/Renderer.hpp @@ -11,11 +11,13 @@ // TODO add optional support for OpenGL separate attrib binding & only depend on vertex format -// By default, RenderObject sets a default material but not data buffers. class RenderObject { public: glm::mat4 worldMatrix; +public: + RcPtr<Texture> autofill_TextureAtlas; + private: RcPtr<Material> mMaterial; RcPtr<GpuIndexBuffer> mIndexBuf; @@ -46,16 +48,23 @@ private: void DeleteGLObjects(); }; +struct RendererFrameInfo { + Camera* camera; + glm::mat4 matrixView; + glm::mat4 matrixProj; + glm::mat4 matrixCombined; + float time; + float deltaTime; +}; + class Renderer { private: - Camera* mFrame_Cam; - glm::mat4 mFrame_Transform; - float mFrame_Time; - float mFrame_DeltaTime; + RendererFrameInfo mFrame; bool mInsideFrame = false; public: void BeginFrame(Camera& camera, float currentTime, float deltaTime); + const RendererFrameInfo& GetLastFrameInfo() const { return mFrame; } void Draw(const RenderObject* objects, size_t count); void EndFrame(); }; diff --git a/source/SceneThings.cpp b/source/SceneThings.cpp index 2a25fb6..0413725 100644 --- a/source/SceneThings.cpp +++ b/source/SceneThings.cpp @@ -11,16 +11,19 @@ SimpleGeometryObject::SimpleGeometryObject(GameWorld* world) , mSize{ 20.0f, 20.0f } , mColor(60, 60, 60) { mRenderObject.SetMaterial(gDefaultMaterial.Get()); - mRenderObject.SetFormat(gVformatStandardPacked.Get(), Tags::IT_16Bit); + mRenderObject.SetFormat(gVformatStandard.Get(), Tags::IT_16Bit); mRenderObject.RebuildIfNecessary(); + UpdateRenderObject(); } void SimpleGeometryObject::SetSize(glm::vec2 size) { mSize = size; + UpdateRenderObject(); } void SimpleGeometryObject::SetColor(RgbaColor color) { mColor = color; + UpdateRenderObject(); } std::span<const RenderObject> SimpleGeometryObject::GetRenderObjects() const { @@ -44,7 +47,7 @@ void SimpleGeometryObject::UpdateRenderObject() { BuildingObject::BuildingObject(GameWorld* world) : GameObject(KD_Building, world) { mRenderObject.SetMaterial(gDefaultMaterial.Get()); - mRenderObject.SetFormat(gVformatStandardPacked.Get(), Tags::IT_32Bit); + mRenderObject.SetFormat(gVformatStandard.Get(), Tags::IT_32Bit); mRenderObject.RebuildIfNecessary(); } diff --git a/source/Shader.cpp b/source/Shader.cpp index fd68a0c..2ab77af 100644 --- a/source/Shader.cpp +++ b/source/Shader.cpp @@ -45,6 +45,26 @@ GLuint FindLocation(const std::vector<ShaderMathVariable>& vars, Tags::VertexEle } return Tags::kInvalidLocation; } + +constexpr auto kAfnTransform = "transform"; +constexpr auto kAfnTime = "time"; +constexpr auto kAfnDeltaTime = "deltaTime"; +constexpr auto kAfnTextureAtlas = "textureAtlas"; + +void InitAutoFill(const char* name, GLuint program, GLuint& location) { + GLint result = glGetUniformLocation(program, name); + if (result != -1) { + location = result; + } +} + +void InitAutoFills(Shader& shader) { + GLuint pg = shader.GetProgram(); + InitAutoFill(kAfnTransform, pg, shader.autofill_Transform); + InitAutoFill(kAfnTime, pg, shader.autofill_Time); + InitAutoFill(kAfnDeltaTime, pg, shader.autofill_DeltaTime); + InitAutoFill(kAfnTextureAtlas, pg, shader.autofill_TextureAtlas); +} } // namespace ProjectBrussel_UNITY_ID GLuint ShaderInfo::FindInputLocation(Tags::VertexElementSemantic semantic) { @@ -178,6 +198,8 @@ Shader::ErrorCode Shader::InitFromSources(const ShaderSources& sources) { sg.Dismiss(); mProgram = program; + InitAutoFills(*this); + return EC_Success; } @@ -295,6 +317,8 @@ Shader::ErrorCode Shader::InitFromSource(std::string_view source) { sg.Dismiss(); mProgram = program; + InitAutoFills(*this); + return EC_Success; } @@ -573,6 +597,7 @@ void IresShader::InvalidateInstance() { void IresShader::ShowEditor(EditorInstance& editor) { using namespace Tags; + using namespace ProjectBrussel_UNITY_ID; IresObject::ShowEditor(editor); @@ -607,6 +632,18 @@ void IresShader::ShowEditor(EditorInstance& editor) { for (auto& uniform : info.uniforms) { uniform->ShowInfo(); } + if (auto loc = mInstance->autofill_Transform; loc != kInvalidLocation) { + ImGui::BulletText("(Autofill)\nLocation: %d\nName: %s", loc, kAfnTransform); + } + if (auto loc = mInstance->autofill_Time; loc != kInvalidLocation) { + ImGui::BulletText("(Autofill)\nLocation: %d\nName: %s", loc, kAfnTime); + } + if (auto loc = mInstance->autofill_DeltaTime; loc != kInvalidLocation) { + ImGui::BulletText("(Autofill)\nLocation: %d\nName: %s", loc, kAfnDeltaTime); + } + if (auto loc = mInstance->autofill_TextureAtlas; loc != kInvalidLocation) { + ImGui::BulletText("(Autofill)\nLocation: %d\nName: %s", loc, kAfnTextureAtlas); + } } } diff --git a/source/Shader.hpp b/source/Shader.hpp index 15290e2..602b0db 100644 --- a/source/Shader.hpp +++ b/source/Shader.hpp @@ -91,9 +91,10 @@ private: GLuint mProgram = 0; public: - GLuint autofillLoc_Transform = Tags::kInvalidLocation; - GLuint autofillLoc_Time = Tags::kInvalidLocation; - GLuint autofillLoc_DeltaTime = Tags::kInvalidLocation; + GLuint autofill_Transform = Tags::kInvalidLocation; + GLuint autofill_Time = Tags::kInvalidLocation; + GLuint autofill_DeltaTime = Tags::kInvalidLocation; + GLuint autofill_TextureAtlas = Tags::kInvalidLocation; public: Shader(); diff --git a/source/Sprite.cpp b/source/Sprite.cpp index 6cff1d3..f855999 100644 --- a/source/Sprite.cpp +++ b/source/Sprite.cpp @@ -1,6 +1,7 @@ #include "Sprite.hpp" #include "AppConfig.hpp" +#include "CommonVertexIndex.hpp" #include "EditorCore.hpp" #include "EditorUtils.hpp" #include "Image.hpp" diff --git a/source/Sprite.hpp b/source/Sprite.hpp index de2077f..064aa30 100644 --- a/source/Sprite.hpp +++ b/source/Sprite.hpp @@ -1,12 +1,10 @@ #pragma once -#include "CommonVertexIndex.hpp" #include "Ires.hpp" #include "PodVector.hpp" #include "RcPtr.hpp" #include "Renderer.hpp" #include "Texture.hpp" -#include "VertexIndex.hpp" #include <rapidjson/fwd.h> #include <glm/glm.hpp> diff --git a/source/VertexIndex.hpp b/source/VertexIndex.hpp index cc5aeca..2d65617 100644 --- a/source/VertexIndex.hpp +++ b/source/VertexIndex.hpp @@ -65,7 +65,3 @@ struct VertexFormat : public RefCounted { void AddElement(VertexElementFormat element); void RemoveElement(int index); }; - -// Initialized in main() -inline RcPtr<VertexFormat> gVformatStandard{}; -inline RcPtr<VertexFormat> gVformatStandardPacked{}; diff --git a/source/main.cpp b/source/main.cpp index fe50b94..f96a6a3 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -1,10 +1,10 @@ #include "App.hpp" #include "AppConfig.hpp" +#include "CommonVertexIndex.hpp" #include "Ires.hpp" #include "Material.hpp" #include "Shader.hpp" -#include "VertexIndex.hpp" #define GLFW_INCLUDE_NONE #include <GLFW/glfw3.h> @@ -187,16 +187,16 @@ int main(int argc, char* argv[]) { glfwSetErrorCallback(&GlfwErrorCallback); - // Decide GL+GLSL versions + glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_API); + glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); + glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE); + #if defined(__APPLE__) - // GL 3.2 + GLSL 150 const char* imguiGlslVersion = "#version 150"; glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); - glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2); - glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // 3.2+ only + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // Required on Mac #else - // GL 3.3 + GLSL 130 const char* imguiGlslVersion = "#version 130"; glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); @@ -232,7 +232,9 @@ int main(int argc, char* argv[]) { } IMGUI_CHECKVERSION(); - ImGui::CreateContext(); + auto ctx = ImGui::CreateContext(); + auto& io = ImGui::GetIO(); + ImGuizmo::SetImGuiContext(ctx); ImGui_ImplGlfw_InitForOpenGL(window, true); if (imguiUseOpenGL3) { @@ -241,9 +243,6 @@ int main(int argc, char* argv[]) { ImGui_ImplOpenGL2_Init(); } - auto& io = ImGui::GetIO(); - auto& ctx = *ImGui::GetCurrentContext(); - IresManager::instance = new IresManager(); IresManager::instance->DiscoverFilesDesignatedLocation(); @@ -254,41 +253,40 @@ int main(int argc, char* argv[]) { .semantic = VES_Position, }); gVformatStandard->AddElement(VertexElementFormat{ - .bindingIndex = 1, + .bindingIndex = 0, .type = VET_Float2, .semantic = VES_TexCoords1, }); gVformatStandard->AddElement(VertexElementFormat{ - .bindingIndex = 1, + .bindingIndex = 0, .type = VET_Ubyte4Norm, .semantic = VES_Color1, }); - gVformatStandardPacked.Attach(new VertexFormat()); - gVformatStandardPacked->AddElement(VertexElementFormat{ + gVformatStandardSplit.Attach(new VertexFormat()); + gVformatStandardSplit->AddElement(VertexElementFormat{ .bindingIndex = 0, .type = VET_Float3, .semantic = VES_Position, }); - gVformatStandardPacked->AddElement(VertexElementFormat{ - .bindingIndex = 0, + gVformatStandardSplit->AddElement(VertexElementFormat{ + .bindingIndex = 1, .type = VET_Float2, .semantic = VES_TexCoords1, }); - gVformatStandardPacked->AddElement(VertexElementFormat{ - .bindingIndex = 0, + gVformatStandardSplit->AddElement(VertexElementFormat{ + .bindingIndex = 1, .type = VET_Ubyte4Norm, .semantic = VES_Color1, }); - // Matches gVformatStandardPacked + // Matches gVformatStandard gDefaultShader.Attach(new Shader()); gDefaultShader->InitFromSources(Shader::ShaderSources{ .vertex = R"""( #version 330 core layout(location = 0) in vec3 pos; -layout(location = 1) in vec2 texcoord; -layout(location = 2) in vec4 color; +layout(location = 1) in vec4 color; out vec4 v2fColor; uniform mat4 transformation; void main() { @@ -321,22 +319,6 @@ void main() { .index = (int)gDefaultShader->GetInfo().inputs.size() - 1, }); } - { // in vec2 texcoord; - ShaderMathVariable var; - var.scalarType = GL_FLOAT; - var.width = 1; - var.height = 2; - var.arrayLength = 1; - var.semantic = VES_TexCoords1; - var.location = 1; - gDefaultShader->GetInfo().inputs.push_back(std::move(var)); - gDefaultShader->GetInfo().things.try_emplace( - "texcoord"s, - ShaderThingId{ - .kind = ShaderThingId::KD_Input, - .index = (int)gDefaultShader->GetInfo().inputs.size() - 1, - }); - } { // in vec4 color; ShaderMathVariable var; var.scalarType = GL_FLOAT; @@ -344,7 +326,7 @@ void main() { var.height = 4; var.arrayLength = 1; var.semantic = VES_Color1; - var.location = 2; + var.location = 1; gDefaultShader->GetInfo().inputs.push_back(std::move(var)); gDefaultShader->GetInfo().things.try_emplace( "color"s, |