Compare commits

..

3 Commits

Author SHA1 Message Date
3fa5455b43 Improving the editor, ImGuizmo is now rendering guizmo's
- Added Guzimo's to the sceneview
- Added new menu to the menubar
- Improved multiple widgets (ImGui windows)
- Added a new RuntimeControl widget (ImGui window)
- New Screenshots
2022-12-24 02:10:29 +01:00
ad79aa2865 Small rendering improvements
- Make view and projection matrix part of the camera
- Add a getter function for the camera in renderer
- Take shader uniform values by const ref
2022-12-24 02:04:51 +01:00
fd68c5dde3 Moving Physics to its own system
- Moved Physx to a singular Physics system
- Removed old Physx test code from application.cpp
2022-12-24 02:00:11 +01:00
19 changed files with 545 additions and 178 deletions

View File

@ -16,6 +16,8 @@ includedirs{
-- I'd prefer if didn't need these..
-- We'll figure that out some time later
"../libs/physx/physx/include",
"../libs/physx/pxshared/include",
incfolder["lua"],
incfolder["spdlog"],
incfolder["glm"],

View File

@ -5,7 +5,7 @@
class EditorWindow {
public:
EditorWindow(const std::string& name ) { ImGui::Begin(name.c_str()); }
EditorWindow(const std::string& name, ImGuiWindowFlags_ flags = ImGuiWindowFlags_None ) { ImGui::Begin(name.c_str(), false ,flags); }

View File

@ -22,6 +22,7 @@ public:
ImGui_ImplGlfw_InitForOpenGL(window.GetGLFWHandle(), true);
ImGui_ImplOpenGL3_Init("#version 450");
ImGuizmo::SetImGuiContext(ImGui::GetCurrentContext());
ImGuizmo::SetOrthographic(true);
}

View File

@ -138,6 +138,33 @@ public:
}
}
void DebugMenu()
{
if (ImGui::BeginMenu("Debug")) {
ImGui::EndMenu();
}
}
void SelectMenu() {
if (ImGui::BeginMenu("Select")) {
ImGui::EndMenu();
}
}
void WindowMenu() {
if (ImGui::BeginMenu("Window")) {
ImGui::EndMenu();
}
}
void Help() {
if (ImGui::BeginMenu("Help")) {
ImGui::EndMenu();
}
}
~MainMenuBar() { ImGui::EndMainMenuBar(); }
private:
char* path = nullptr;

View File

@ -17,6 +17,11 @@
typedef void ( *voidFunction ) (void);
using namespace YoggieEngine;
auto matrix = glm::mat4(1.0f);
auto worldOrigin = glm::mat4(1.0f);
auto projection = glm::perspective(45.0f, 0.89f, 0.001f, 1.0f);
auto view = glm::mat4(1.0f);
inline void ComponentView(const std::string& componentName, voidFunction func)
{
ImGuiWindowFlags_ window_flags = ImGuiWindowFlags_None;
@ -123,28 +128,108 @@ public:
}
};
class Viewport : EditorWindow {
public:
Viewport (Framebuffer& fb) : EditorWindow("SceneView") {
ImGui::Image(
Viewport (Framebuffer& fb, Camera cam ) : EditorWindow("SceneView")
{
ImVec2 WinPos = ImGui::GetWindowPos();
ImVec2 ContentRegionMin = ImGui::GetWindowContentRegionMin();
ImVec2 ContentRegionMax = ImGui::GetWindowContentRegionMax();
ImVec2 ScreenSpaceMin = { ContentRegionMin.x + WinPos.x, ContentRegionMin.y + WinPos.y };
ImVec2 ScreenSpaceMax = { ContentRegionMax.x + WinPos.x,ContentRegionMax.y + WinPos.y };
ImGui::Image(
(void*)(intptr_t)fb.GetColourAttachment(),
ImVec2{ (float)ImGui::GetWindowWidth(),(float)ImGui::GetWindowHeight() }
);
//ImGuizmo::SetDrawlist();
//ImGuizmo::SetRect(ImGui::GetWindowPos().x, ImGui::GetWindowPos().y, ImGui::GetWindowWidth(), ImGui::GetWindowHeight());
//ImGuizmo::Enable(true);
//ImGuizmo::Manipulate(glm::value_ptr(view), glm::value_ptr(projection), ImGuizmo::TRANSLATE, ImGuizmo::WORLD, glm::value_ptr(trans));
ImGuizmo::Enable(true);
ImGuizmo::SetRect(ScreenSpaceMin.x, ScreenSpaceMin.y,ContentRegionMax.x, ContentRegionMax.y);
ImGuizmo::ViewManipulate(glm::value_ptr(cam.ViewMatrix), 1, { ScreenSpaceMin.x,ScreenSpaceMin.y }, { 90,90 }, 0x22CCCCCC);
ImGuizmo::DrawGrid(glm::value_ptr(cam.ViewMatrix), glm::value_ptr(cam.ProjectionMatrix), glm::value_ptr(worldOrigin), 100.0f);
// Matrix is the model matrix we would want to manipulate
ImGuizmo::Manipulate(glm::value_ptr(cam.ViewMatrix), glm::value_ptr(cam.ProjectionMatrix), ImGuizmo::TRANSLATE, ImGuizmo::WORLD, glm::value_ptr(matrix));
}
};
int selectedGfxAPI = 0;
int selectedPhysicsEngine = 0;
glm::vec3 Gravity = glm::vec3(0.0f, -9.81f, 0.0f);
bool ShowAdvancedOptions = false;
bool DebugEngine = false;
class Settings : EditorWindow {
public:
Settings() : EditorWindow("Settings") {
ImGui::LabelText("##title-settings", "Fine grain control over your engine... ");
Settings() : EditorWindow("Settings")
{
ImGui::LabelText("##title-settings", "Fine grain control over the engine!");
if (ImGui::BeginCombo("Graphics API", GraphicsAPI[selectedGfxAPI])) {
for (int i = 0; i < 3; i++) {
bool isSelected = i == selectedGfxAPI;
if (ImGui::Selectable(GraphicsAPI[i], isSelected)) {
selectedGfxAPI = i;
}
if(isSelected)
ImGui::SetItemDefaultFocus();
}
ImGui::EndCombo();
}
ImGui::NewLine();
if (ImGui::BeginCombo("Physics Engine", PhysicsEngine[selectedPhysicsEngine])) {
for (int i = 0; i < 2; i++) {
bool isSelected = i == selectedPhysicsEngine;
if (ImGui::Selectable(PhysicsEngine[i], isSelected)) {
selectedGfxAPI = i;
}
if (isSelected)
ImGui::SetItemDefaultFocus();
}
ImGui::EndCombo();
}
ImGui::InputFloat3("Gravity", glm::value_ptr(Gravity));
ImGui::NewLine();
if (ImGui::Button("Show Advanced options ")) {
ShowAdvancedOptions = !ShowAdvancedOptions;
}
if (ShowAdvancedOptions)
{
ImGui::Checkbox("Debug Engine", &DebugEngine);
}
}
private:
const char* PhysicsEngine[2] = {
"PhysX",
"Jolt Physics"
};
const char* GraphicsAPI[3] = {
"OpenGL",
"Vulkan",
"Metal (Apple)"
};
};
class ProjectInfo : EditorWindow {
@ -183,6 +268,9 @@ public:
ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImVec4(0.f, 0.f, 0.f, 0.f));
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4(1.f, 1.f, 1.f, 0.2f));
Texture folderIcon = Texture("rsc/folderIcon.png");
Texture assetIcon = Texture("rsc/assetIcon.png");
int row = 0;
int column = 0;
@ -197,14 +285,14 @@ public:
if (asset.isFolder) {
ImGui::ImageButton(
(ImTextureID)(Texture("rsc/folderIcon.png")).GetID(),
(ImTextureID)folderIcon.GetID(),
ImVec2{ (float)iconSize,(float)iconSize });
ImGui::Text(asset.GetName(), row);
}
else {
ImGui::ImageButton(
(ImTextureID)(Texture("rsc/assetIcon.png")).GetID(),
(ImTextureID)assetIcon.GetID(),
ImVec2{ (float)iconSize, (float)iconSize });
ImGui::Text(asset.GetName(), row);
@ -217,12 +305,58 @@ public:
ImGui::PopStyleColor(3);
ImGui::EndTable();
const GLuint textures[2]{ assetIcon.GetID(), folderIcon.GetID() };
glDeleteTextures(2, textures );
}
}
private:
static Texture folderIcon;
static Texture AssetIcon;
int iconSize = 60;
};
#define RuntimeControlWindowFlags (ImGuiWindowFlags_)(ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoCollapse)
struct ButtonInfo {
const char* Name;
ImVec4 Color;
};
class RuntimeControls : EditorWindow {
public:
RuntimeControls() : EditorWindow("RuntimeControls", RuntimeControlWindowFlags) {
ImGui::SameLine((ImGui::GetWindowContentRegionMax().x / 2) - ( numButtons * buttonSize.x ));
for (int i = 0; i < numButtons; i++) {
ImVec4 color = button[i].Color;
ImGui::PushStyleColor(ImGuiCol_Button, color);
const float strengthIncrease = 1.5f;
ImGui::PushStyleColor(
ImGuiCol_ButtonHovered,
ImVec4{
color.x * strengthIncrease,
color.y * strengthIncrease,
color.z * strengthIncrease,
color.w
}
);
if (ImGui::Button(button[i].Name, buttonSize)) {
}
ImGui::PopStyleColor();
ImGui::PopStyleColor();
ImGui::SameLine();
}
}
private:
ImVec2 buttonSize = ImVec2{ 90 ,25 };
unsigned int numButtons = 2;
ButtonInfo button[2] = {
{"Play" , ImVec4{ 0.001 * 12 , 0.001 * 201 , 0.001* 69, 1.0f}},
{"Simulate", ImVec4{ 0.001 * 14, 0.001 * 157, 0.001 * 201, 1.0f}}
};
};

View File

@ -1,5 +1,6 @@
#include "../../YoggieEngine/src/EntryPoint.h"
#include "../../YoggieEngine/src/AssetManager/ModelImporter.h"
#include "../../YoggieEngine/src/Physics/Physics.h"
#include <nfd.h>
@ -18,99 +19,64 @@
const unsigned int MS_PER_UPDATE = 2;
void CreateTestProject(std::unique_ptr<Project>& project, Scene& scene);
RendererConfig EditorSceneRendererConfig{
1200, // Screen Width
700, // Screen Height
glm::vec3{0,0,0}, // Clear Color
true // Depth testing
};
class Editor : public Application {
public:
Editor() : Application("Editor"){
}
void Run() override
Editor()
: Application("Editor"),
AppWindow(1200,700),
framebuffer(new Framebuffer(1200,700)),
viewportRenderer(EditorSceneRendererConfig),
EditorGUIRenderer(AppWindow),
Selected((entt::entity)-1)
{
auto NativeEditorWindow = NativeWindow(1200, 700);
framebuffer = new Framebuffer(800, 600);
auto renderer = Renderer(RendererConfig{
1200, // Screen Width
700, // Screen Height
glm::vec3{0,0,0}, // Clear Color
true // Depth testing
});
auto GuiRenderer = GUIRenderer(NativeEditorWindow);
Selected = (entt::entity)-1;
CreateTestProject(CurrentProject, ActiveScene);
ActiveScene.Start();
renderer.setCurrentFrameBuffer(*framebuffer);
double previous = glfwGetTime();
double lag = 0.0;
while (!NativeEditorWindow.WindowShouldClose())
{
double current = glfwGetTime();
double elapsed = current - previous;
previous = current;
lag += elapsed;
NativeEditorWindow.Poll();
while (lag >= MS_PER_UPDATE)
{
ActiveScene.Update();
lag -= MS_PER_UPDATE;
}
// submit DrawCommands for all render3DComponents
auto group = ActiveScene.getReg().view<TransformComponent, Render3DComponent>();
group.each([&renderer](auto enity, TransformComponent& t, Render3DComponent& renderComponent) {
renderer.Submit(renderComponent, t);
});
renderer.Render();
GuiRenderer.Begin();
RenderGUI();
GuiRenderer.End();
NativeEditorWindow.SwapBuffers();
glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);
}
delete framebuffer;
ActiveScene.Stop();
viewportRenderer.setCurrentFrameBuffer(*framebuffer);
}
void RenderGUI() {
void RenderScene() {
// submit DrawCommands for all render3DComponents
auto group = ActiveScene.getReg().view<TransformComponent, Render3DComponent>();
group.each([&](auto enity, TransformComponent& t, Render3DComponent& renderComponent) {
viewportRenderer.Submit(renderComponent, t);
});
viewportRenderer.Render();
}
void RenderEditorGUI() {
EditorGUIRenderer.Begin();
ImGui::DockSpaceOverViewport(ImGui::GetMainViewport());
// Show a menu bar
{
MainMenuBar menuBar= MainMenuBar();
MainMenuBar menuBar = MainMenuBar();
menuBar.ApplicationMenu(CurrentProject);
menuBar.SceneMenu(CurrentProject, ActiveScene);
menuBar.SelectMenu();
menuBar.WindowMenu();
menuBar.DebugMenu();
menuBar.Help();
}
{
ProjectInfo projectInfo(*(CurrentProject.get()));
ProjectInfo projectInfo(*(CurrentProject.get()));
}
{
Viewport sceneview = Viewport(*framebuffer);
Viewport sceneview = Viewport(*framebuffer, viewportRenderer.getCamera());
}
{
RuntimeControls rc = RuntimeControls();
}
{
SceneExplorer explorer(Selected, ActiveScene);
}
@ -126,14 +92,14 @@ public:
}
{
Settings();
Settings settings = Settings();
}
{
AssetFinder assetsView = AssetFinder();
// AssetFinder assetsView = AssetFinder();
}
{
Console console = Console();
console.Show();
@ -141,15 +107,74 @@ public:
ImGui::ShowDemoWindow();
ImGui::ShowMetricsWindow();
EditorGUIRenderer.End();
}
void Run() override
{
CreateTestProject(CurrentProject, ActiveScene);
ActiveScene.Start();
// Create the physics engine demo!
Physics Physics;
//Physics.Demo();
double previous = glfwGetTime();
double lag = 0.0;
while (!AppWindow.WindowShouldClose())
{
double current = glfwGetTime();
double elapsed = current - previous;
previous = current;
lag += elapsed;
AppWindow.Poll();
if (SimulatePhysics)
{
Physics.Step(1.0f / 60.0f);
}
while (lag >= MS_PER_UPDATE)
{
ActiveScene.Update();
lag -= MS_PER_UPDATE;
}
RenderScene();
RenderEditorGUI();
AppWindow.SwapBuffers();
glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);
}
delete framebuffer;
ActiveScene.Stop();
}
private:
NativeWindow AppWindow;
Framebuffer* framebuffer;
Renderer viewportRenderer;
GUIRenderer EditorGUIRenderer;
// Editor State
bool SimulatePhysics = false;
entt::entity Selected;
std::unique_ptr<Project> CurrentProject;
Scene ActiveScene;
entt::entity Selected;
Framebuffer* framebuffer ;
};
@ -174,7 +199,7 @@ void CreateTestProject(std::unique_ptr<Project>& project, Scene& scene ) {
// Create a cube
auto model = importer.Import("build/Debug/Models/Cube.obj");
auto cube = scene.AddEntity("cube");
auto cube = scene.AddEntity("Cube");
auto& render3DComponent = cube.AddComponent<Render3DComponent>();
render3DComponent.mesh = *(model->renderable->mesh);

View File

@ -7,7 +7,9 @@
<img src="Screenshots/screen1.png" width="300"></img> \
<img src="Screenshots/screen2.png" width="300"></img> \
<img src="Screenshots/YoggieEditor.png" width="300"></img>
<img src="Screenshots/YoggieEditor.png" width="300"></img> \
<img src="Screenshots/YoggieEditor_workingGuizmo.png" width="300"></img> \
<img src="Screenshots/YoggieEditorV0.2.png" width="300"></img>
## Planning
see [TODO](docs/TODO.md) \
see [Features](Features.md)

BIN
Screenshots/YoggieEditorV0.2.png (Stored with Git LFS) Normal file

Binary file not shown.

BIN
Screenshots/YoggieEditor_workingGuizmo.png (Stored with Git LFS) Normal file

Binary file not shown.

View File

@ -1,45 +1,13 @@
#include <YoggieEngine.h>
#include "Application.h"
#include <PxPhysicsAPI.h>
#define PVD_HOST "127.0.0.1"
using namespace physx;
namespace YoggieEngine {
static PxDefaultErrorCallback gDefaultErrorCallback;
static PxDefaultAllocator gDefaultAllocatorCallback;
static PxFoundation* mFoundation;
static PxPvd* mPvd;
static PxPvdTransport* transport;
static PxPhysics* mPhysics;
bool recordMemoryAllocations = true;
Application::Application(const std::string& name )
Application::Application(const std::string& name)
: m_AppName(name)
{
EngineInstrumentation::PerfomanceSamplerInit();
// startup PhysX
mFoundation = PxCreateFoundation(PX_PHYSICS_VERSION, gDefaultAllocatorCallback, gDefaultErrorCallback);
if (!mFoundation) {
spdlog::critical("PxCreateFoundation failed!");
}
mPvd = physx::PxCreatePvd(*mFoundation);
if (!mPvd) {
spdlog::critical("pxCreatPvd failed!");
}
transport = PxDefaultPvdSocketTransportCreate(PVD_HOST, 5425, 10);
mPvd->connect(*transport, PxPvdInstrumentationFlag::eALL);
mPhysics = PxCreatePhysics(PX_PHYSICS_VERSION, *mFoundation, PxTolerancesScale(), recordMemoryAllocations, mPvd);
if (!mPhysics) {
spdlog::critical("pxCreatePhysics failed!");
}
}
@ -48,24 +16,5 @@ namespace YoggieEngine {
}
Application::~Application(){
if (mPhysics)
mPhysics->release();
if (mPvd) {
if (mPvd->isConnected()) {
mPvd->disconnect();
}
mPvd->release();
}
if (transport)
transport->release();
if (mFoundation)
mFoundation->release();
}
}
Application::~Application() {}
}

View File

@ -1,6 +1,7 @@
#include <YoggieEngine.h>
#include "Camera.h"
namespace YoggieEngine {
Camera::Camera(glm::vec3 position, glm::vec3 rotation, float zoom)
: Position(position), Rotation(rotation), Zoom(zoom) {
@ -8,17 +9,28 @@ namespace YoggieEngine {
Right = glm::vec3(0.0f, 0.0f, 1.0f);
Up = glm::vec3(0.0f, 1.0f, 0.0f);
auto rotated_position = glm::rotate(glm::mat4(1.0f), Rotation.x, glm::vec3(1.0f, 0.0f, 0.0f)) * glm::vec4(Position, 1.0f);
ViewMatrix = glm::lookAt(
Position,
glm::vec3{ rotated_position.x, rotated_position.y , rotated_position.z } + Front,
Up);
ProjectionMatrix = glm::perspective(glm::radians(Zoom), (800.0f / 600.0f), 0.001f, 100.0f);
}
Camera::~Camera() {
}
glm::mat4 Camera::GetViewMatrix() {
return glm::lookAt(
void Camera::Update() {
ViewMatrix = glm::lookAt(
Position,
Position + Front,
Up
);
Position + Front,
Up);
}
}

View File

@ -2,20 +2,24 @@
namespace YoggieEngine {
class Camera {
public:
Camera(glm::vec3 position, glm::vec3 rotation, float zoom);
~Camera();
void Update();
glm::vec3 Position;
glm::vec3 Rotation;
float Zoom;
Camera(glm::vec3 position, glm::vec3 rotation, float zoom);
~Camera();
glm::mat4 GetViewMatrix();
glm::mat4 ViewMatrix;
glm::mat4 ProjectionMatrix;
private:
glm::vec3 Front;
glm::vec3 Right;
glm::vec3 Up;
};
}

View File

@ -101,29 +101,29 @@ namespace YoggieEngine {
}
void Shader::setUniformMat4(std::string uniformName, glm::mat4 matrix4) const
void Shader::setUniformMat4(std::string uniformName, const glm::mat4& matrix4) const
{
glUniformMatrix4fv(glGetUniformLocation(id, uniformName.c_str()), 1, GL_FALSE, glm::value_ptr(matrix4));
}
void Shader::setUniformVec4(std::string uniformName, glm::vec4 vector4) const
void Shader::setUniformVec4(std::string uniformName, const glm::vec4& vector4) const
{
glUniform4fv(glGetUniformLocation(id, uniformName.c_str()), 1, glm::value_ptr(vector4));
}
void Shader::setUniformVec3(std::string uniformName, glm::vec3 vector3) const
void Shader::setUniformVec3(std::string uniformName, const glm::vec3& vector3) const
{
glUniform3fv(glGetUniformLocation(id, uniformName.c_str()), 1, glm::value_ptr(vector3));
}
void Shader::setUniformVec2(std::string uniformName, glm::vec2 vector2) const
void Shader::setUniformVec2(std::string uniformName,const glm::vec2& vector2) const
{
glUniform2fv(glGetUniformLocation(id, uniformName.c_str()), 1, glm::value_ptr(vector2));
}
void Shader::setUniformFloat(std::string uniformName, float value) const
void Shader::setUniformFloat(std::string uniformName, const float& value) const
{
glUniform1f(glGetUniformLocation(id, uniformName.c_str()), value);
}
void Shader::setUniformInt(std::string uniformName, int value) const
void Shader::setUniformInt(std::string uniformName, const int& value) const
{
glUniform1i(glGetUniformLocation(id, uniformName.c_str()), value);
}

View File

@ -8,12 +8,12 @@ namespace YoggieEngine {
public:
Shader(const std::string vertexShaderPath, const std::string fragmentShaderPath);
void Use() const;
void setUniformMat4(std::string uniformName, glm::mat4 matrix4)const;
void setUniformVec4(std::string uniformName, glm::vec4 vector4)const;
void setUniformVec3(std::string uniformName, glm::vec3 vector3)const;
void setUniformVec2(std::string uniformName, glm::vec2 vector2)const;
void setUniformFloat(std::string uniformName, float value)const;
void setUniformInt(std::string uniformName, int value) const;
void setUniformMat4(std::string uniformName, const glm::mat4& matrix4)const;
void setUniformVec4(std::string uniformName, const glm::vec4& vector4)const;
void setUniformVec3(std::string uniformName, const glm::vec3& vector3)const;
void setUniformVec2(std::string uniformName, const glm::vec2& vector2)const;
void setUniformFloat(std::string uniformName, const float& value)const;
void setUniformInt(std::string uniformName, const int& value) const;
int id;

View File

@ -7,9 +7,7 @@
#include "../Graphics/Primitives/DrawCommand.h"
namespace YoggieEngine {
float Angle = 0.0;
Camera cam = Camera(glm::vec3(12.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f), 90.0f);
glm::mat4 projection = glm::perspective(glm::radians(cam.Zoom), (800.0f / 600.0f), 0.001f, 100.0f);
Camera cam = Camera(glm::vec3(12.0f, 0.0f, 0.0f), glm::vec3(45.0f, 0.0f, 0.0f), 90.0f);
Renderer::Renderer(RendererConfig& config)
: m_framebuffer(Framebuffer(config.ScreenWidth, config.ScreenHeight))
@ -20,6 +18,10 @@ Renderer::Renderer(RendererConfig& config)
Renderer::~Renderer(){}
Camera& Renderer::getCamera() {
return cam;
}
void Renderer::Submit( Render3DComponent& renderComponent, TransformComponent& transform) {
if (renderComponent.VAO == 0 || renderComponent.IBO == 0)
@ -63,7 +65,7 @@ void Renderer::Submit( Render3DComponent& renderComponent, TransformComponent& t
}
void Renderer::Render()
void Renderer::Render()
{
glBindFramebuffer(GL_FRAMEBUFFER, m_framebuffer.GetId());
@ -89,10 +91,10 @@ void Renderer::Render()
glm::mat4 modelMatrix = glm::translate(glm::mat4(1.0f), command.transform.Position) * glm::scale(glm::mat4(1.0f), command.transform.Scale) * rotation;
command.shader.setUniformVec3("Color", glm::vec3(1.0f, 0.0f, 0.0f));
command.shader.setUniformVec3("Color", glm::vec3(0.3f, 0.3f, 0.3f));
command.shader.setUniformMat4("M", modelMatrix);
command.shader.setUniformMat4("V", cam.GetViewMatrix());
command.shader.setUniformMat4("P", projection);
command.shader.setUniformMat4("V", cam.ViewMatrix);
command.shader.setUniformMat4("P", cam.ProjectionMatrix);
glDrawElements(GL_TRIANGLES, static_cast<unsigned int>(command.num_elements),
GL_UNSIGNED_INT, NULL);

View File

@ -24,11 +24,13 @@ namespace YoggieEngine {
void Submit(Render3DComponent& renderComponent, TransformComponent& transform); // Collects DrawCommands
void Render(); // Draw to screen (usingthe drawCalls
void Render(); // Draw to screen (using drawCall structs)
void setCurrentFrameBuffer(const Framebuffer& fb);
void setClearColor(const glm::vec3& ClearColor);
Camera& getCamera();
private:
Framebuffer m_framebuffer;
glm::vec3 m_clearColor;

View File

@ -0,0 +1,132 @@
#include <YoggieEngine.h>
#include "Physics.h"
namespace YoggieEngine {
Physics::Physics()
{
// startup PhysX
mFoundation = PxCreateFoundation(PX_PHYSICS_VERSION, gDefaultAllocatorCallback, gDefaultErrorCallback);
if (!mFoundation) {
spdlog::critical("PxCreateFoundation failed!");
}
mPvd = physx::PxCreatePvd(*mFoundation);
if (!mPvd) {
spdlog::critical("pxCreatPvd failed!");
}
transport = PxDefaultPvdSocketTransportCreate(PVD_HOST, 5425, 10);
mPvd->connect(*transport, PxPvdInstrumentationFlag::eALL);
mPhysics = PxCreatePhysics(PX_PHYSICS_VERSION, *mFoundation, PxTolerancesScale(), recordMemoryAllocations, mPvd);
if (!mPhysics) {
spdlog::critical("pxCreatePhysics failed!");
}
gDispatcher = nullptr;
gMaterial = nullptr;
mScene = nullptr;
}
Physics::~Physics()
{
if (mScene)
mScene->release();
if (gDispatcher)
gDispatcher->release();
if (mPhysics)
mPhysics->release();
if (mPvd) {
if (mPvd->isConnected()) {
mPvd->disconnect();
}
mPvd->release();
}
if (transport)
transport->release();
if (mFoundation)
mFoundation->release();
}
void Physics::Step(float dt)
{
mScene->simulate(dt);
mScene->fetchResults(true);
}
void Physics::Demo()
{
createScene();
SetupPvdDebug();
createGroundPlane();
createStack(PxTransform(PxVec3(0, 0, stackZ -= 10.0f)), 10, 2.0f);
}
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);
dynamic->setLinearVelocity(velocity);
mScene->addActor(*dynamic);
return dynamic;
}
void Physics::createStack(const PxTransform& t, PxU32 size, PxReal halfextent)
{
PxShape* shape = mPhysics->createShape(PxBoxGeometry(halfextent, halfextent, halfextent), *gMaterial);
for (PxU32 i = 0; i < size; i++) {
for (PxU32 j = 0; j < size - i; j++) {
PxTransform localTm(PxVec3(PxReal(j * 2) - PxReal(size - i), PxReal(i * 2 + 1), 0) * halfextent);
PxRigidDynamic* body = mPhysics->createRigidDynamic(t.transform(localTm));
body->attachShape(*shape);
PxRigidBodyExt::updateMassAndInertia(*body, 10.0f);
mScene->addActor(*body);
}
}
shape->release();
}
void Physics::createScene()
{
PxSceneDesc sceneDesc(mPhysics->getTolerancesScale());
sceneDesc.gravity = PxVec3(0.0f, -9.81f, 0.0f);
gDispatcher = PxDefaultCpuDispatcherCreate(2);
sceneDesc.cpuDispatcher = gDispatcher;
sceneDesc.filterShader = PxDefaultSimulationFilterShader;
mScene = mPhysics->createScene(sceneDesc);
}
void Physics::createGroundPlane()
{
gMaterial = mPhysics->createMaterial(0.5f, 0.5f, 0.6f);
PxRigidStatic* groundPlane = PxCreatePlane(*mPhysics, PxPlane(0,1,0,0), *gMaterial);
mScene->addActor(*groundPlane);
}
void Physics::SetupPvdDebug()
{
PxPvdSceneClient* pvdClient = mScene->getScenePvdClient();
if (pvdClient) {
pvdClient->setScenePvdFlag(PxPvdSceneFlag::eTRANSMIT_CONSTRAINTS, true);
pvdClient->setScenePvdFlag(PxPvdSceneFlag::eTRANSMIT_CONTACTS, true);
pvdClient->setScenePvdFlag(PxPvdSceneFlag::eTRANSMIT_SCENEQUERIES, true);
}
}
}

View File

@ -0,0 +1,66 @@
#pragma once
#include <PxPhysicsAPI.h>
using namespace physx; // For now will just focus on getting Physx working in its most basic form
#define PVD_HOST "127.0.0.1"
#define MAX_IP_CHAR_COUNT 16
namespace YoggieEngine {
struct PhysicsConfig {
bool recordMemoryAllocations;
const char PvdHost[MAX_IP_CHAR_COUNT];
float StackZ;
};
class Physics {
public:
Physics();
~Physics();
void Step(float dt);
void Demo();
private:
PxRigidDynamic* createDynamic(const PxTransform& t, const PxGeometry& geometry, const PxVec3& velocity);
void createStack(const PxTransform& t, PxU32 size, PxReal halfextent);
void createScene();
void createGroundPlane();
void SetupPvdDebug();
// Memory Management
bool recordMemoryAllocations = true;
PxDefaultErrorCallback gDefaultErrorCallback;
PxDefaultAllocator gDefaultAllocatorCallback;
PxDefaultCpuDispatcher* gDispatcher;
// Basics
PxFoundation* mFoundation;
PxPhysics* mPhysics;
// Simulation
PxScene* mScene;
PxMaterial* gMaterial;
PxReal stackZ = 10.0f;
// DEBUG
PxPvd* mPvd;
PxPvdTransport* transport;
};
}

View File

@ -39,8 +39,11 @@ extern "C"
#include "Graphics/Primitives/Material.h"
#include "Graphics/Renderer.h"
#include "Physics/Physics.h"
#include "EventSystem/EventEmitter.h"
#include "EventSystem/EventListener.h"
#include "Scene/Scene.h"
#include "Application.h"