Moving vegetation test to the renderer

This commit is contained in:
Nigel Barink 2023-01-04 15:57:08 +01:00
parent 0f9be33bd6
commit d5a6ddb9d5
9 changed files with 185 additions and 34 deletions

View File

@ -1,7 +1,7 @@
#pragma once #pragma once
#include "../../YoggieEngine/src/YoggieEngine.h"
#include <filesystem> #include <filesystem>
#include <string> #include <string>
#include "../../YoggieEngine/src/YoggieEngine.h"
enum class ASSET_TYPE { enum class ASSET_TYPE {
Unknown = -1, Unknown = -1,

View File

@ -14,7 +14,6 @@
#include "SceneSerializer.h" #include "SceneSerializer.h"
#include "AssetManagement/AssetManager.h" #include "AssetManagement/AssetManager.h"
#include "UI/MainMenuBar.h" #include "UI/MainMenuBar.h"
const unsigned int MS_PER_UPDATE = 2;
void CreateTestProject(std::unique_ptr<Project>& project, Scene& scene); void CreateTestProject(std::unique_ptr<Project>& project, Scene& scene);
RendererConfig EditorSceneRendererConfig{ RendererConfig EditorSceneRendererConfig{
@ -25,7 +24,7 @@ RendererConfig EditorSceneRendererConfig{
}; };
glm::vec3 temp = glm::vec3(0); 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 { class Editor : public Application {
public: public:
Editor() Editor()
@ -120,7 +119,11 @@ public:
// Create the physics engine demo! // Create the physics engine demo!
Physics Physics; Physics Physics;
//Physics.Demo(); Physics.Demo();
/*Physics.EnableDebugVisuals();
Physics.Step(0);
Physics.SubmitMesh();
*/
double previous = glfwGetTime(); double previous = glfwGetTime();
double lag = 0.0; double lag = 0.0;
@ -134,21 +137,12 @@ public:
AppWindow.Poll(); 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(); RenderScene();
/*Physics.DebugRender(*framebuffer);*/
RenderEditorGUI(); RenderEditorGUI();
@ -173,7 +167,7 @@ private:
GUIRenderer EditorGUIRenderer; GUIRenderer EditorGUIRenderer;
// Editor State // Editor State
bool SimulatePhysics = false; bool SimulatePhysics = true;
entt::entity Selected; entt::entity Selected;
std::unique_ptr<Project> CurrentProject; std::unique_ptr<Project> CurrentProject;
@ -218,9 +212,10 @@ void CreateTestProject(std::unique_ptr<Project>& project, Scene& scene ) {
auto cube2 = scene.AddEntity("Cube2"); auto cube2 = scene.AddEntity("Cube2");
auto& rendercube2 = cube2.AddComponent<Render3DComponent>(); auto& rendercube2 = cube2.AddComponent<Render3DComponent>();
rendercube2.mesh = *(model->renderable->mesh); rendercube2.mesh = *(model->renderable->mesh);
auto relationcube = cube.AddComponent<RelationComponent>(cube2); auto relationcube = cube.AddComponent<RelationComponent>(cube2);
auto Grass = scene.AddEntity("Grass/Window-Pane");
//auto& renderGrass = Grass.AddComponent<Render3DComponent>();

View File

@ -1,6 +1,4 @@
#pragma once #pragma once
#define STB_IMAGE_IMPLEMENTATION
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "../Graphics/Primitives/Mesh.h" #include "../Graphics/Primitives/Mesh.h"
#include <assimp/Importer.hpp> #include <assimp/Importer.hpp>

View File

@ -9,13 +9,25 @@ namespace YoggieEngine {
Camera::Camera(glm::vec3 position, glm::vec3 rotation, float zoom) Camera::Camera(glm::vec3 position, glm::vec3 rotation, float zoom)
: Position(position), Rotation( rotation) : Position(position), Rotation( rotation)
{ {
glm::vec3 WorldUp = glm::vec3(0.0f, 1.0f, 0.0f);
Front = glm::vec3(-1.0f, 0.0f, 0.0f); Front = glm::vec3(0.0f, 0.0f, 1.0f);
Right = 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); Up = glm::vec3(0.0f, 1.0f, 0.0f);
Zoom = zoom; 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( ViewMatrix = glm::lookAt(
Position, Position,
Position + Front, Position + Front,

View File

@ -2,6 +2,7 @@
namespace YoggieEngine { namespace YoggieEngine {
class Texture { class Texture {
public: public:
Texture() = default;
Texture(const std::string texturePath, bool Transparency = false); Texture(const std::string texturePath, bool Transparency = false);
void Bind(); void Bind();

View File

@ -6,11 +6,20 @@
#include "../Graphics/Memory/VertexArray.h" #include "../Graphics/Memory/VertexArray.h"
#include "../Graphics/Primitives/DrawCommand.h" #include "../Graphics/Primitives/DrawCommand.h"
extern YoggieEngine::Camera cam; extern YoggieEngine::Camera cam;
namespace YoggieEngine { namespace YoggieEngine {
unsigned int quadVAO = 0; unsigned int quadVAO = 0;
unsigned int quadVBO = 0; unsigned int quadVBO = 0;
// vegetation test
std::vector<glm::vec3> 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[]{ float skyboxVertices[]{
@ -62,7 +71,8 @@ Renderer::Renderer(RendererConfig& config)
: m_framebuffer(Framebuffer(config.ScreenWidth, config.ScreenHeight)), : m_framebuffer(Framebuffer(config.ScreenWidth, config.ScreenHeight)),
gBufferShader("build/Debug/Shaders/deferred/geometry.vert", "build/Debug/Shaders/deferred/geometry.frag"), 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"), 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; width = config.ScreenWidth;
height = config.ScreenHeight; height = config.ScreenHeight;
@ -136,6 +146,12 @@ Renderer::Renderer(RendererConfig& config)
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)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; 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) { void Renderer::Submit( Render3DComponent& renderComponent, TransformComponent& transform) {
if (renderComponent.VAO == 0 || renderComponent.IBO == 0) if (renderComponent.VAO == 0 || renderComponent.IBO == 0)
{ {
@ -189,7 +236,6 @@ void Renderer::Submit( Render3DComponent& renderComponent, TransformComponent& t
} }
void Renderer::GeometryPass() { void Renderer::GeometryPass() {
// 1.0 Geometry pass // 1.0 Geometry pass
gBufferShader.Use(); gBufferShader.Use();
@ -213,7 +259,6 @@ void Renderer::GeometryPass() {
} }
void Renderer::SkyboxPass() { void Renderer::SkyboxPass() {
// Render skybox // Render skybox
glDepthMask(GL_FALSE); glDepthMask(GL_FALSE);
@ -295,9 +340,42 @@ void Renderer::lightingPass(Scene& scene){
glBindVertexArray(0); 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) void Renderer::Render(Scene& scene)
{ {
SubmitVegetationDemo();
glBindFramebuffer(GL_FRAMEBUFFER, gBuffer); glBindFramebuffer(GL_FRAMEBUFFER, gBuffer);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
@ -334,6 +412,7 @@ void Renderer::Render(Scene& scene)
glBindVertexArray(0); glBindVertexArray(0);
} }
BlendingPass();

View File

@ -35,6 +35,8 @@ namespace YoggieEngine {
void GeometryPass(); void GeometryPass();
void SkyboxPass(); void SkyboxPass();
void lightingPass(Scene& scene); void lightingPass(Scene& scene);
void BlendingPass();
private: private:
@ -54,5 +56,7 @@ namespace YoggieEngine {
Shader gBufferShader; Shader gBufferShader;
Shader SkyboxShader; Shader SkyboxShader;
// blending
Shader BlendingShader;
}; };
} }

View File

@ -1,4 +1,5 @@
#include <YoggieEngine.h> #include <YoggieEngine.h>
#include <glm/gtc/type_precision.hpp>
#include "Physics.h" #include "Physics.h"
namespace YoggieEngine { namespace YoggieEngine {
@ -58,21 +59,75 @@ namespace YoggieEngine {
void Physics::Step(float dt) void Physics::Step(float dt)
{ {
mScene->simulate(dt); mScene->simulate(1.0f/60.0f);
mScene->fetchResults(true); mScene->fetchResults(true);
} }
void Physics::Demo() void Physics::Demo()
{ {
createScene(); createScene();
SetupPvdDebug(); SetupPvdDebug();
createGroundPlane(); createGroundPlane();
createStack(PxTransform(PxVec3(0, 0, stackZ -= 10.0f)), 10, 2.0f); 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<PxVec3> lines= std::vector<PxVec3>();
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* Physics::createDynamic(const PxTransform& t, const PxGeometry& geometry, const PxVec3& velocity = PxVec3(0)) {
PxRigidDynamic* dynamic = PxCreateDynamic(*mPhysics, t, geometry, *gMaterial, 10.0f); PxRigidDynamic* dynamic = PxCreateDynamic(*mPhysics, t, geometry, *gMaterial, 10.0f);
dynamic->setAngularDamping(0.5f); dynamic->setAngularDamping(0.5f);

View File

@ -26,8 +26,14 @@ namespace YoggieEngine {
void Demo(); void Demo();
void EnableDebugVisuals();
void SubmitMesh();
void DebugRender(Framebuffer& framebuffer);
private: 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); PxRigidDynamic* createDynamic(const PxTransform& t, const PxGeometry& geometry, const PxVec3& velocity);
void createStack(const PxTransform& t, PxU32 size, PxReal halfextent); void createStack(const PxTransform& t, PxU32 size, PxReal halfextent);
@ -37,6 +43,7 @@ namespace YoggieEngine {
void SetupPvdDebug(); void SetupPvdDebug();
// Memory Management // Memory Management
bool recordMemoryAllocations = true; bool recordMemoryAllocations = true;
PxDefaultErrorCallback gDefaultErrorCallback; PxDefaultErrorCallback gDefaultErrorCallback;