From f422d1d1380b48deb417fbb0121166d9d8f4a8e8 Mon Sep 17 00:00:00 2001 From: "Grayson Riffe (Laptop)" Date: Fri, 1 Oct 2021 14:16:47 -0500 Subject: [PATCH] Added instanced drawing to the GBuffer --- Game/Game.vcxproj | 2 + Game/src/MainState.cpp | 2 +- .../base/shaders/gBufferFragment.shader | 2 +- .../base/shaders/gBufferVertex.shader | 6 +- .../shadow/directionalShadowVertex.shader | 4 +- .../shaders/shadow/pointShadowVertex.shader | 4 +- NothinFancy/NothinFancy.vcxproj | 8 +- NothinFancy/src/NFObject/Entity.cpp | 9 +- NothinFancy/src/Renderer/GBuffer.cpp | 26 ++- NothinFancy/src/Renderer/Model.cpp | 4 +- NothinFancy/src/include/Entity.h | 10 +- NothinFancy/src/include/GBuffer.h | 7 +- NothinFancy/src/include/Model.h | 2 +- NothinFancy/src/include/NothinFancy.h | 156 +----------------- 14 files changed, 63 insertions(+), 179 deletions(-) diff --git a/Game/Game.vcxproj b/Game/Game.vcxproj index 3ee775f..3b206e1 100644 --- a/Game/Game.vcxproj +++ b/Game/Game.vcxproj @@ -129,6 +129,7 @@ mainCRTStartup libcmt.lib /ignore:4099 %(AdditionalOptions) + true cd "$(SolutionDir)NFPackCreator\AssetBuild" && "$(SolutionDir)NFPackCreator\bin\Win32$(Configuration)\NFPackCreator.exe" && del "$(OutDir)assets\" /Q /S && move "$(SolutionDir)NFPackCreator\AssetBuild\*.nfpack" "$(OutDir)assets\" @@ -173,6 +174,7 @@ mainCRTStartup libcmt.lib /ignore:4099 %(AdditionalOptions) + true cd "$(SolutionDir)NFPackCreator\AssetBuild" && "$(SolutionDir)NFPackCreator\bin\Win32$(Configuration)\NFPackCreator.exe" && del "$(OutDir)assets\" /Q /S && move "$(SolutionDir)NFPackCreator\AssetBuild\*.nfpack" "$(OutDir)assets\" diff --git a/Game/src/MainState.cpp b/Game/src/MainState.cpp index 05b836a..48ba288 100644 --- a/Game/src/MainState.cpp +++ b/Game/src/MainState.cpp @@ -24,7 +24,7 @@ void MainState::onEnter() { for (int x = 0; x < 10; x++) { for (int y = 0; y < 10; y++) { entities.push_back(new nf::Entity); - entities.back()->create(ap["spec.obj"]); + entities.back()->create(ap["2mats.obj"]); entities.back()->setPosition(5.0 + x * 2.1, 0.05, -5.0 + y * 2.1); } } diff --git a/NFPackCreator/AssetBuild/base/shaders/gBufferFragment.shader b/NFPackCreator/AssetBuild/base/shaders/gBufferFragment.shader index 998ccd5..17e9524 100644 --- a/NFPackCreator/AssetBuild/base/shaders/gBufferFragment.shader +++ b/NFPackCreator/AssetBuild/base/shaders/gBufferFragment.shader @@ -25,6 +25,6 @@ void main() { normals = hasNormTex[matNum] ? normalize(tbn * (texture(normalTexture[matNum], texCoord).xyz * 2.0 - 1.0)) : normal; diffuse = hasDiffuseTex[matNum] ? texture(diffuseTexture[matNum], texCoord).rgb : diffuseColor[matNum]; specular.r = specPower[matNum]; - specular.g = hasSpecTex[matNum] ? texture(specularTexture[matNum], texCoord).r : 1.0; + specular.g = hasSpecTex[matNum] ? texture(specularTexture[matNum], texCoord).r : 0.2; specular.b = 1.0; } \ No newline at end of file diff --git a/NFPackCreator/AssetBuild/base/shaders/gBufferVertex.shader b/NFPackCreator/AssetBuild/base/shaders/gBufferVertex.shader index b402354..3269b4a 100644 --- a/NFPackCreator/AssetBuild/base/shaders/gBufferVertex.shader +++ b/NFPackCreator/AssetBuild/base/shaders/gBufferVertex.shader @@ -6,7 +6,7 @@ layout(location = 2) in vec3 normals; layout(location = 3) in vec3 tangent; layout(location = 4) in int matN; -uniform mat4 model; +uniform mat4 model[60]; uniform mat4 view; uniform mat4 proj; @@ -17,10 +17,10 @@ out mat3 tbn; flat out int matNum; void main() { - vec4 world = model * vec4(pos, 1.0); + vec4 world = model[gl_InstanceID] * vec4(pos, 1.0); fragPos = world.xyz; texCoord = texCoords; - mat3 normalMat = transpose(inverse(mat3(model))); + mat3 normalMat = transpose(inverse(mat3(model[gl_InstanceID]))); vec3 t = normalize(normalMat * tangent); vec3 n = normalize(normalMat * normals); normal = n; diff --git a/NFPackCreator/AssetBuild/base/shaders/shadow/directionalShadowVertex.shader b/NFPackCreator/AssetBuild/base/shaders/shadow/directionalShadowVertex.shader index d22deb7..635f12d 100644 --- a/NFPackCreator/AssetBuild/base/shaders/shadow/directionalShadowVertex.shader +++ b/NFPackCreator/AssetBuild/base/shaders/shadow/directionalShadowVertex.shader @@ -3,10 +3,10 @@ layout(location = 0) in vec3 pos; uniform mat4 lightSpace; -uniform mat4 model; +uniform mat4 model[60]; out vec3 texCoord; void main() { - gl_Position = lightSpace * model * vec4(pos, 1.0); + gl_Position = lightSpace * model[gl_InstanceID] * vec4(pos, 1.0); } \ No newline at end of file diff --git a/NFPackCreator/AssetBuild/base/shaders/shadow/pointShadowVertex.shader b/NFPackCreator/AssetBuild/base/shaders/shadow/pointShadowVertex.shader index 415ff55..0544077 100644 --- a/NFPackCreator/AssetBuild/base/shaders/shadow/pointShadowVertex.shader +++ b/NFPackCreator/AssetBuild/base/shaders/shadow/pointShadowVertex.shader @@ -2,8 +2,8 @@ layout(location = 0) in vec3 pos; -uniform mat4 model; +uniform mat4 model[60]; void main() { - gl_Position = model * vec4(pos, 1.0); + gl_Position = model[gl_InstanceID] * vec4(pos, 1.0); } \ No newline at end of file diff --git a/NothinFancy/NothinFancy.vcxproj b/NothinFancy/NothinFancy.vcxproj index 4fab881..e710a5e 100644 --- a/NothinFancy/NothinFancy.vcxproj +++ b/NothinFancy/NothinFancy.vcxproj @@ -94,7 +94,7 @@ Level3 true - GLEW_STATIC;WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + NFENGINE; GLEW_STATIC;WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) true $(ProjectDir)src\include\;$(ProjectDir)dep\include\ $(IntDir)obj\ @@ -120,7 +120,7 @@ true true true - GLEW_STATIC;WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + NFENGINE; GLEW_STATIC;WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) true $(ProjectDir)src\include\;$(ProjectDir)dep\include\ $(IntDir)obj\ @@ -146,7 +146,7 @@ Level3 true - GLEW_STATIC;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + NFENGINE; GLEW_STATIC;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) true $(ProjectDir)src\include\;$(ProjectDir)dep\include\ $(IntDir)obj\ @@ -172,7 +172,7 @@ true true true - GLEW_STATIC;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + NFENGINE; GLEW_STATIC;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) true $(ProjectDir)src\include\;$(ProjectDir)dep\include\ $(IntDir)obj\ diff --git a/NothinFancy/src/NFObject/Entity.cpp b/NothinFancy/src/NFObject/Entity.cpp index 3305ddd..2d4c16c 100644 --- a/NothinFancy/src/NFObject/Entity.cpp +++ b/NothinFancy/src/NFObject/Entity.cpp @@ -71,22 +71,23 @@ namespace nf { void Entity::render(Shader* shader, bool onlyDepth) { shader->bind(); - setModelMatrix(shader); - m_model->render(shader, onlyDepth); + glm::mat4 model = getModelMatrix(); + shader->setUniform("model[0]", model); + m_model->render(shader, onlyDepth, 1); } Model* Entity::getModel() const { return m_model; } - void Entity::setModelMatrix(Shader* shader) { + const glm::mat4 Entity::getModelMatrix() { glm::mat4 model(1.0f); model = glm::translate(model, glm::vec3(m_position.x, m_position.y, m_position.z)); model = glm::rotate(model, glm::radians((float)m_rotation.x), glm::vec3(1.0, 0.0, 0.0)); model = glm::rotate(model, glm::radians((float)m_rotation.y), glm::vec3(0.0, 1.0, 0.0)); model = glm::rotate(model, glm::radians((float)m_rotation.z), glm::vec3(0.0, 0.0, 1.0)); model = glm::scale(model, glm::vec3(m_scale.x, m_scale.y, m_scale.z)); - shader->setUniform("model", model); + return model; } void Entity::destroy() { diff --git a/NothinFancy/src/Renderer/GBuffer.cpp b/NothinFancy/src/Renderer/GBuffer.cpp index 75d0c8c..98e6002 100644 --- a/NothinFancy/src/Renderer/GBuffer.cpp +++ b/NothinFancy/src/Renderer/GBuffer.cpp @@ -4,6 +4,7 @@ #include "Application.h" #include "Entity.h" +#include "Model.h" #include "Shader.h" namespace nf { @@ -44,8 +45,29 @@ namespace nf { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glDisable(GL_BLEND); - for (Entity* curr : entites) - curr->render(shader); + for (Entity* curr : entites) { + m_modelsToDraw[curr->getModel()].push_back(curr->getModelMatrix()); + } + for (auto& curr : m_modelsToDraw) { + std::vector& mats = curr.second; + std::string pos; + unsigned int modelsRemaining = mats.size(); + while (modelsRemaining != 0) { + unsigned int modelCount; + if (modelsRemaining > 60) + modelCount = 60; + else + modelCount = modelsRemaining; + modelsRemaining -= modelCount; + for (unsigned int i = 0; i < modelCount; i++) { + pos = std::to_string(i) + "]"; + shader->setUniform(m_modelString + pos, mats[i]); + } + curr.first->render(shader, false, modelCount); + mats.erase(mats.begin(), mats.begin() + modelCount); + } + } + m_modelsToDraw.clear(); glEnable(GL_BLEND); //TODO: Blit depth buffer for transparent objects later diff --git a/NothinFancy/src/Renderer/Model.cpp b/NothinFancy/src/Renderer/Model.cpp index 7597412..348207c 100644 --- a/NothinFancy/src/Renderer/Model.cpp +++ b/NothinFancy/src/Renderer/Model.cpp @@ -310,12 +310,12 @@ namespace nf { m_ib = new IndexBuffer(&vboIndices[0], vboIndices.size()); } - void Model::render(Shader* shader, bool onlyDepth) { + void Model::render(Shader* shader, bool onlyDepth, unsigned int count) { m_vao->bind(); m_ib->bind(); if (!onlyDepth) bindMaterials(shader); - glDrawElements(GL_TRIANGLES, m_ib->getCount(), GL_UNSIGNED_INT, nullptr); + glDrawElementsInstanced(GL_TRIANGLES, m_ib->getCount(), GL_UNSIGNED_INT, nullptr, count); } void Model::bindMaterials(Shader* shader) { diff --git a/NothinFancy/src/include/Entity.h b/NothinFancy/src/include/Entity.h index 901b22f..81a62cd 100644 --- a/NothinFancy/src/include/Entity.h +++ b/NothinFancy/src/include/Entity.h @@ -2,6 +2,9 @@ #include "Assets.h" #include "NFObject.h" #include "Utility.h" +#ifdef NFENGINE +#include "glm/glm.hpp" +#endif namespace nf { class Shader; @@ -22,13 +25,14 @@ namespace nf { void setScale(double x, double y, double z); void setScale(const Vec3& scale); - void render(Shader* shader, bool onlyDepth = false); + void render(Shader* shader, bool onlyDepth); Model* getModel() const; - +#ifdef NFENGINE + const glm::mat4 getModelMatrix(); +#endif void destroy() override; ~Entity(); private: - void setModelMatrix(Shader* shader); bool m_constructed; Model* m_model; diff --git a/NothinFancy/src/include/GBuffer.h b/NothinFancy/src/include/GBuffer.h index b796395..cb534b7 100644 --- a/NothinFancy/src/include/GBuffer.h +++ b/NothinFancy/src/include/GBuffer.h @@ -1,9 +1,12 @@ #pragma once #include #include +#include +#include "glm\glm.hpp" namespace nf { class Entity; + class Model; class Shader; class GBuffer { @@ -19,7 +22,9 @@ namespace nf { unsigned int m_FBO; std::array m_textures; unsigned int m_depth; - unsigned int m_width, m_height; + + std::unordered_map> m_modelsToDraw; + const std::string m_modelString = "model["; }; } \ No newline at end of file diff --git a/NothinFancy/src/include/Model.h b/NothinFancy/src/include/Model.h index 34465bb..e55cab1 100644 --- a/NothinFancy/src/include/Model.h +++ b/NothinFancy/src/include/Model.h @@ -11,7 +11,7 @@ namespace nf { public: Model(AModel* model); - void render(Shader* shader, bool onlyDepth); + void render(Shader* shader, bool onlyDepth, unsigned int count); void bindMaterials(Shader* shader); bool isBaseAsset(); diff --git a/NothinFancy/src/include/NothinFancy.h b/NothinFancy/src/include/NothinFancy.h index a7ea0ff..96b1729 100644 --- a/NothinFancy/src/include/NothinFancy.h +++ b/NothinFancy/src/include/NothinFancy.h @@ -6,6 +6,8 @@ #include #include +#include "Application.h" +#include "Renderer.h" #include "Config.h" #include "Utility.h" #include "IntroGamestate.h" @@ -17,156 +19,4 @@ #include "UITexture.h" #include "Button.h" #include "Sound.h" -#include "Input.h" - -namespace nf { - class Drawable; - class Shader; - class Model; - class GBuffer; - class AudioEngine; - - class Renderer { - public: - Renderer(Application* app); - - void setFade(bool in, bool out, bool noText); - bool isFadeOutComplete(); - - void render(Entity& in); - void render(UIElement& in); - void render(Light& in); - void render(Cubemap& in); - - void doFrame(Camera* camera, double dT); - - ~Renderer(); - private: - void renderShadowMaps(unsigned int count); - - void loadBaseAssets(); - void createShadowMaps(); - - Application* m_app; - - HDC m_hdc; - HGLRC m_hglrc; - - AssetPack m_baseAP; - - GBuffer* m_gBuffer; - - unsigned int m_shadowMapFBO; - int m_directionalDepthTexSize; - int m_pointDepthTexSize; - std::vector m_directionalShadowMaps; - std::vector m_pointShadowMaps; - unsigned int m_texSlots; - - std::vector m_lights; - std::vector m_lGame; - Cubemap* m_cubemap; - std::vector m_lUI; - Shader* m_gBufferShader; - Shader* m_lightingShader; - Shader* m_textShader; - Shader* m_uiTextureShader; - Shader* m_cubemapShader; - Shader* m_fadeShader; - Shader* m_directionalShadowShader; - Shader* m_pointShadowShader; - - bool m_fadeIn, m_fadeOut; - bool m_fadeNoText; - bool m_fadeOutComplete; - Text m_loadingText; - - VertexArray* m_quadVAO; - IndexBuffer* m_quadIB; - }; - - class Application { - public: - Application(Config& conf); - Application() = delete; - Application(Application& other) = delete; - - void setWindowIcon(HANDLE hIcon); - void setWindowCursor(HCURSOR hCursor); - AudioEngine* getAudioEngine() const; - void addState(Gamestate* state, const std::string& stateName); - void addDefaultState(const std::string& stateName); - const std::string& getDefaultState(); - void run(); - bool isCustomWindowIcon(); - void changeState(const std::string& stateName); - Gamestate* getCurrentState(); - void showWindow(bool show); - const HWND& getWindow(); - void changeConfig(const Config& in); - const Config& getConfig() const; - int getFPS() const; - bool isKeyHeld(unsigned int code); - bool isKeyPressed(unsigned int code); - bool isMouse(unsigned int code); - void trackMouse(bool track); - void getMouseDiff(int& x, int& y); - Vec2 getMousePos(); - static Application* getApp(); - - void quit(); - ~Application(); - private: - void registerWindowClass(); - RECT getWindowRect() const; - void calculateNewWindowPos(int& x, int& y); - void toggleFullscreen(); - void updateMouse(); - void runMainGameThread(); - void doStateChange(); - static void setApp(Application* app); - - static LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam); - - static Application* currentApp; - Config m_currentConfig; - bool m_running; - bool m_quit; - HINSTANCE m_hInst; - LPCWSTR m_wclassName; - HWND m_window; - bool m_customWindowIconSet; - LONG m_defaultWindowStyle; - unsigned int m_altWidth, m_altHeight; - - std::chrono::duration m_fpsDuration; - double m_deltaTime; - std::chrono::steady_clock::time_point m_fpsClock1 = std::chrono::steady_clock::now(); - std::chrono::steady_clock::time_point m_fpsClock2 = m_fpsClock1; - const int m_targetFPS = 60; - const double m_minFrametime = 1.0 / m_targetFPS; - int m_FPS; - - //Inactive states to potentially be active during the Application's lifetime - //Mapped to const char* to be referenced later in the frontend - std::unordered_map m_states; - Gamestate* m_sIntro; - std::string m_defaultState; - bool m_defaultStateAdded; - Gamestate* m_currentState; - bool m_stateChange; - std::string m_nextState; - - //Array of booleans that represent keyboard and mouse input minus the scrollwheel - std::array m_keysPressed; - unsigned int m_mouseX, m_mouseY; - bool m_trackingMouse; - bool m_mouseTrackFirst; - int m_mouseDiffX, m_mouseDiffY; - - //Renderer object to use OpenGL to render the current state - Renderer* m_renderer; - - AudioEngine* m_audio; - }; -} \ No newline at end of file +#include "Input.h" \ No newline at end of file