From d5a6ddb9d57c510883431a11115fe33ac59f119d Mon Sep 17 00:00:00 2001 From: Nigel Barink Date: Wed, 4 Jan 2023 15:57:08 +0100 Subject: [PATCH] Moving vegetation test to the renderer --- Editor/src/AssetManagement/Asset.h | 2 +- Editor/src/app.cpp | 33 +++---- YoggieEngine/src/AssetManager/ModelImporter.h | 2 - .../src/Graphics/Primitives/Camera.cpp | 18 +++- .../src/Graphics/Primitives/Texture.h | 1 + YoggieEngine/src/Graphics/Renderer.cpp | 89 +++++++++++++++++-- YoggieEngine/src/Graphics/Renderer.h | 4 + YoggieEngine/src/Physics/Physics.cpp | 63 ++++++++++++- YoggieEngine/src/Physics/Physics.h | 7 ++ 9 files changed, 185 insertions(+), 34 deletions(-) diff --git a/Editor/src/AssetManagement/Asset.h b/Editor/src/AssetManagement/Asset.h index b76bf5c..19881dd 100644 --- a/Editor/src/AssetManagement/Asset.h +++ b/Editor/src/AssetManagement/Asset.h @@ -1,7 +1,7 @@ #pragma once +#include "../../YoggieEngine/src/YoggieEngine.h" #include #include -#include "../../YoggieEngine/src/YoggieEngine.h" enum class ASSET_TYPE { Unknown = -1, diff --git a/Editor/src/app.cpp b/Editor/src/app.cpp index fd514d2..9259ca6 100644 --- a/Editor/src/app.cpp +++ b/Editor/src/app.cpp @@ -14,7 +14,6 @@ #include "SceneSerializer.h" #include "AssetManagement/AssetManager.h" #include "UI/MainMenuBar.h" -const unsigned int MS_PER_UPDATE = 2; void CreateTestProject(std::unique_ptr& project, Scene& scene); RendererConfig EditorSceneRendererConfig{ @@ -25,7 +24,7 @@ RendererConfig EditorSceneRendererConfig{ }; glm::vec3 temp = glm::vec3(0); -Camera cam = Camera(glm::vec3(12.0f, 1.0f, 0.0f), glm::vec3(45.0f, 0.0f, 0.0f), 90); +Camera cam = Camera(glm::vec3(14.0f, 1.0f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f), 90); class Editor : public Application { public: Editor() @@ -120,7 +119,11 @@ public: // Create the physics engine demo! Physics Physics; - //Physics.Demo(); + Physics.Demo(); + /*Physics.EnableDebugVisuals(); + Physics.Step(0); + Physics.SubmitMesh(); + */ double previous = glfwGetTime(); double lag = 0.0; @@ -134,21 +137,12 @@ public: AppWindow.Poll(); - if (SimulatePhysics) - { - Physics.Step(1.0f / 60.0f); - } - - - while (lag >= MS_PER_UPDATE) - { - ActiveScene.Update(); - lag -= MS_PER_UPDATE; - } - - + Physics.Step(elapsed); + ActiveScene.Update(); + RenderScene(); + /*Physics.DebugRender(*framebuffer);*/ RenderEditorGUI(); @@ -173,7 +167,7 @@ private: GUIRenderer EditorGUIRenderer; // Editor State - bool SimulatePhysics = false; + bool SimulatePhysics = true; entt::entity Selected; std::unique_ptr CurrentProject; @@ -218,10 +212,11 @@ void CreateTestProject(std::unique_ptr& project, Scene& scene ) { auto cube2 = scene.AddEntity("Cube2"); auto& rendercube2 = cube2.AddComponent(); rendercube2.mesh = *(model->renderable->mesh); - auto relationcube = cube.AddComponent(cube2); - + auto Grass = scene.AddEntity("Grass/Window-Pane"); + //auto& renderGrass = Grass.AddComponent(); + } diff --git a/YoggieEngine/src/AssetManager/ModelImporter.h b/YoggieEngine/src/AssetManager/ModelImporter.h index 79f7629..617a11a 100644 --- a/YoggieEngine/src/AssetManager/ModelImporter.h +++ b/YoggieEngine/src/AssetManager/ModelImporter.h @@ -1,6 +1,4 @@ #pragma once -#define STB_IMAGE_IMPLEMENTATION -#define STB_IMAGE_WRITE_IMPLEMENTATION #include "../Graphics/Primitives/Mesh.h" #include diff --git a/YoggieEngine/src/Graphics/Primitives/Camera.cpp b/YoggieEngine/src/Graphics/Primitives/Camera.cpp index 9f2d311..a599cba 100644 --- a/YoggieEngine/src/Graphics/Primitives/Camera.cpp +++ b/YoggieEngine/src/Graphics/Primitives/Camera.cpp @@ -9,13 +9,25 @@ namespace YoggieEngine { Camera::Camera(glm::vec3 position, glm::vec3 rotation, float zoom) : Position(position), Rotation( rotation) { - - Front = glm::vec3(-1.0f, 0.0f, 0.0f); - Right = glm::vec3(0.0f, 0.0f, 1.0f); + glm::vec3 WorldUp = glm::vec3(0.0f, 1.0f, 0.0f); + Front = glm::vec3(0.0f, 0.0f, 1.0f); + Right = glm::vec3(-1.0f, 0.0f, 0.0f); Up = glm::vec3(0.0f, 1.0f, 0.0f); Zoom = zoom; + glm::vec3(direction); + float yaw = 180; + float pitch = 0; + + direction.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch)); + direction.z = sin(glm::radians(yaw)); + direction.y = sin(glm::radians(pitch)) * cos(glm::radians(pitch)); + Front = glm::normalize(direction); + Right = glm::normalize(glm::cross(Front, WorldUp)); + Up = glm::normalize(glm::cross(Right, Front)); + + ViewMatrix = glm::lookAt( Position, Position + Front, diff --git a/YoggieEngine/src/Graphics/Primitives/Texture.h b/YoggieEngine/src/Graphics/Primitives/Texture.h index 332f94a..c19b0f1 100644 --- a/YoggieEngine/src/Graphics/Primitives/Texture.h +++ b/YoggieEngine/src/Graphics/Primitives/Texture.h @@ -2,6 +2,7 @@ namespace YoggieEngine { class Texture { public: + Texture() = default; Texture(const std::string texturePath, bool Transparency = false); void Bind(); diff --git a/YoggieEngine/src/Graphics/Renderer.cpp b/YoggieEngine/src/Graphics/Renderer.cpp index a0895e5..3e39516 100644 --- a/YoggieEngine/src/Graphics/Renderer.cpp +++ b/YoggieEngine/src/Graphics/Renderer.cpp @@ -6,11 +6,20 @@ #include "../Graphics/Memory/VertexArray.h" #include "../Graphics/Primitives/DrawCommand.h" - extern YoggieEngine::Camera cam; namespace YoggieEngine { unsigned int quadVAO = 0; unsigned int quadVBO = 0; +// vegetation test +std::vector vegetation = { + glm::vec3(-1.5f, 0.0f, -0.48f), + glm::vec3(1.5f, 0.0f, 0.51f), + glm::vec3(0.0f, 0.0f, 0.7f), + glm::vec3(-0.3f, 0.0f, -2.3f) +}; + +unsigned int transparentVAO, transparentVBO; +Texture grassTexture; float skyboxVertices[]{ @@ -62,7 +71,8 @@ Renderer::Renderer(RendererConfig& config) : m_framebuffer(Framebuffer(config.ScreenWidth, config.ScreenHeight)), gBufferShader("build/Debug/Shaders/deferred/geometry.vert", "build/Debug/Shaders/deferred/geometry.frag"), lightingPassShader("build/Debug/Shaders/deferred/lightPass.vert", "build/Debug/Shaders/deferred/lightPass.frag"), - SkyboxShader("build/Debug/Shaders/Cubemaps/Skybox.vert", "build/Debug/Shaders/Cubemaps/Skybox.frag") + SkyboxShader("build/Debug/Shaders/Cubemaps/Skybox.vert", "build/Debug/Shaders/Cubemaps/Skybox.frag"), + BlendingShader("build/Debug/Shaders/forward/Blending.vert", "build/Debug/Shaders/forward/Blending.frag") { width = config.ScreenWidth; height = config.ScreenHeight; @@ -136,6 +146,12 @@ Renderer::Renderer(RendererConfig& config) glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0); +#ifdef WINDOW + grassTexture = Texture("build/Debug/Texture/blending_transparent_window.png", true); +#else + grassTexture = Texture("build/Debug/Texture/grass.png", true); +#endif + @@ -147,6 +163,37 @@ Camera& Renderer::getCamera() { return cam; } +void SubmitVegetationDemo() { + + + if (transparentVAO == 0) { + float transparentVertices[] = { + // positions // texture Coords (swapped y coordinates because texture is flipped upside down) + 0.0f, 0.5f, 0.0f, 0.0f, 0.0f, + 0.0f, -0.5f, 0.0f, 0.0f, 1.0f, + 1.0f, -0.5f, 0.0f, 1.0f, 1.0f, + + 0.0f, 0.5f, 0.0f, 0.0f, 0.0f, + 1.0f, -0.5f, 0.0f, 1.0f, 1.0f, + 1.0f, 0.5f, 0.0f, 1.0f, 0.0f + }; + + glGenVertexArrays(1, &transparentVAO); + glGenBuffers(1, &transparentVBO); + glBindVertexArray(transparentVAO); + glBindBuffer(GL_ARRAY_BUFFER, transparentVBO); + glBufferData(GL_ARRAY_BUFFER, sizeof(transparentVertices), transparentVertices, GL_STATIC_DRAW); + glEnableVertexAttribArray(0); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0); + glEnableVertexAttribArray(1); + glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float))); + glBindVertexArray(0); + + } + + +} + void Renderer::Submit( Render3DComponent& renderComponent, TransformComponent& transform) { if (renderComponent.VAO == 0 || renderComponent.IBO == 0) { @@ -189,7 +236,6 @@ void Renderer::Submit( Render3DComponent& renderComponent, TransformComponent& t } - void Renderer::GeometryPass() { // 1.0 Geometry pass gBufferShader.Use(); @@ -213,7 +259,6 @@ void Renderer::GeometryPass() { } - void Renderer::SkyboxPass() { // Render skybox glDepthMask(GL_FALSE); @@ -295,9 +340,42 @@ void Renderer::lightingPass(Scene& scene){ glBindVertexArray(0); } +void Renderer::BlendingPass() { + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + + // Vegetation / blending test; + BlendingShader.Use(); + glBindVertexArray(transparentVAO); + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, grassTexture.GetID()); + + BlendingShader.setUniformMat4("V", cam.ViewMatrix); + BlendingShader.setUniformMat4("P", cam.ProjectionMatrix); + + + for (unsigned int i = 0; i < vegetation.size(); i++) { + auto rotation = glm::rotate(glm::mat4(1.0f), 45.0f, glm::vec3(0.0f, 1.0f, 0.0f)); + auto translation = glm::translate(glm::mat4(1.0f), vegetation[i]); + auto transform = translation * rotation; + BlendingShader.setUniformMat4("M", transform); + glDrawArrays(GL_TRIANGLES, 0, 6); + + } + + glDisable(GL_BLEND); + + + +} + + + + + void Renderer::Render(Scene& scene) { - + SubmitVegetationDemo(); glBindFramebuffer(GL_FRAMEBUFFER, gBuffer); glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); @@ -334,6 +412,7 @@ void Renderer::Render(Scene& scene) glBindVertexArray(0); } + BlendingPass(); diff --git a/YoggieEngine/src/Graphics/Renderer.h b/YoggieEngine/src/Graphics/Renderer.h index c1ced62..dd39036 100644 --- a/YoggieEngine/src/Graphics/Renderer.h +++ b/YoggieEngine/src/Graphics/Renderer.h @@ -35,6 +35,8 @@ namespace YoggieEngine { void GeometryPass(); void SkyboxPass(); void lightingPass(Scene& scene); + void BlendingPass(); + private: @@ -54,5 +56,7 @@ namespace YoggieEngine { Shader gBufferShader; Shader SkyboxShader; + // blending + Shader BlendingShader; }; } diff --git a/YoggieEngine/src/Physics/Physics.cpp b/YoggieEngine/src/Physics/Physics.cpp index fba135f..5176a1d 100644 --- a/YoggieEngine/src/Physics/Physics.cpp +++ b/YoggieEngine/src/Physics/Physics.cpp @@ -1,4 +1,5 @@ #include +#include #include "Physics.h" namespace YoggieEngine { @@ -58,21 +59,75 @@ namespace YoggieEngine { void Physics::Step(float dt) { - mScene->simulate(dt); + mScene->simulate(1.0f/60.0f); mScene->fetchResults(true); } void Physics::Demo() { createScene(); - SetupPvdDebug(); - createGroundPlane(); - createStack(PxTransform(PxVec3(0, 0, stackZ -= 10.0f)), 10, 2.0f); } + void Physics::EnableDebugVisuals() { + mScene->setVisualizationParameter(PxVisualizationParameter::eSCALE, 1.0f); + mScene->setVisualizationParameter(PxVisualizationParameter::eACTOR_AXES, 2.0f); + + } + + + static unsigned int VisualizationVAO, VBO; + static unsigned int numLines; + void Physics::SubmitMesh() { + const PxRenderBuffer& rb = mScene->getRenderBuffer(); + + + std::vector lines= std::vector(); + + numLines = rb.getNbLines(); + + for (unsigned int i = 0; i < numLines; i++) { + auto line = rb.getLines()[i]; + lines.push_back(line.pos0); + lines.push_back(line.pos1); + + } + + glGenVertexArrays(1, &VisualizationVAO); + glGenBuffers(1, &VBO); + + glBindVertexArray(VisualizationVAO); + glBindBuffer(GL_ARRAY_BUFFER, VBO); + glBufferData(GL_ARRAY_BUFFER, lines.size() * sizeof(PxVec3), &lines, GL_STATIC_DRAW); + + glEnableVertexAttribArray(0); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0); + + glBindVertexArray(0); + glBindBuffer(GL_ARRAY_BUFFER, 0); + + + } + + + void Physics::DebugRender(Framebuffer& framebuffer) { + glDepthMask(GL_FALSE); + glBindFramebuffer(GL_FRAMEBUFFER, framebuffer.GetId()); + + debugDraw.Use(); + glBindVertexArray(VisualizationVAO); + glDrawArrays(GL_LINES, 0, numLines ); + glBindVertexArray(0); + + glBindFramebuffer(GL_FRAMEBUFFER, 0); + glDepthMask(GL_TRUE); + + + } + + PxRigidDynamic* Physics::createDynamic(const PxTransform& t, const PxGeometry& geometry, const PxVec3& velocity = PxVec3(0)) { PxRigidDynamic* dynamic = PxCreateDynamic(*mPhysics, t, geometry, *gMaterial, 10.0f); dynamic->setAngularDamping(0.5f); diff --git a/YoggieEngine/src/Physics/Physics.h b/YoggieEngine/src/Physics/Physics.h index ab29857..c0cfadd 100644 --- a/YoggieEngine/src/Physics/Physics.h +++ b/YoggieEngine/src/Physics/Physics.h @@ -26,8 +26,14 @@ namespace YoggieEngine { void Demo(); + void EnableDebugVisuals(); + void SubmitMesh(); + + void DebugRender(Framebuffer& framebuffer); + private: + Shader debugDraw = Shader("build/Debug/Shaders/forward/debug.vert", "build/Debug/Shaders/forward/debug.frag"); PxRigidDynamic* createDynamic(const PxTransform& t, const PxGeometry& geometry, const PxVec3& velocity); void createStack(const PxTransform& t, PxU32 size, PxReal halfextent); @@ -36,6 +42,7 @@ namespace YoggieEngine { void SetupPvdDebug(); + // Memory Management bool recordMemoryAllocations = true;