Added instanced drawing to the GBuffer

This commit is contained in:
Grayson Riffe (Laptop) 2021-10-01 14:16:47 -05:00
parent ba19742bfe
commit f422d1d138
14 changed files with 63 additions and 179 deletions

View File

@ -129,6 +129,7 @@
<EntryPointSymbol>mainCRTStartup</EntryPointSymbol>
<IgnoreSpecificDefaultLibraries>libcmt.lib</IgnoreSpecificDefaultLibraries>
<AdditionalOptions>/ignore:4099 %(AdditionalOptions)</AdditionalOptions>
<Profile>true</Profile>
</Link>
<PostBuildEvent>
<Command>cd "$(SolutionDir)NFPackCreator\AssetBuild" &amp;&amp; "$(SolutionDir)NFPackCreator\bin\Win32$(Configuration)\NFPackCreator.exe" &amp;&amp; del "$(OutDir)assets\" /Q /S &amp;&amp; move "$(SolutionDir)NFPackCreator\AssetBuild\*.nfpack" "$(OutDir)assets\"</Command>
@ -173,6 +174,7 @@
<EntryPointSymbol>mainCRTStartup</EntryPointSymbol>
<IgnoreSpecificDefaultLibraries>libcmt.lib</IgnoreSpecificDefaultLibraries>
<AdditionalOptions>/ignore:4099 %(AdditionalOptions)</AdditionalOptions>
<Profile>true</Profile>
</Link>
<PostBuildEvent>
<Command>cd "$(SolutionDir)NFPackCreator\AssetBuild" &amp;&amp; "$(SolutionDir)NFPackCreator\bin\Win32$(Configuration)\NFPackCreator.exe" &amp;&amp; del "$(OutDir)assets\" /Q /S &amp;&amp; move "$(SolutionDir)NFPackCreator\AssetBuild\*.nfpack" "$(OutDir)assets\"</Command>

View File

@ -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);
}
}

View File

@ -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;
}

View File

@ -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;

View File

@ -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);
}

View File

@ -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);
}

View File

@ -94,7 +94,7 @@
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>GLEW_STATIC;WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>NFENGINE; GLEW_STATIC;WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>$(ProjectDir)src\include\;$(ProjectDir)dep\include\</AdditionalIncludeDirectories>
<ObjectFileName>$(IntDir)obj\</ObjectFileName>
@ -120,7 +120,7 @@
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>GLEW_STATIC;WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>NFENGINE; GLEW_STATIC;WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>$(ProjectDir)src\include\;$(ProjectDir)dep\include\</AdditionalIncludeDirectories>
<ObjectFileName>$(IntDir)obj\</ObjectFileName>
@ -146,7 +146,7 @@
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>GLEW_STATIC;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>NFENGINE; GLEW_STATIC;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>$(ProjectDir)src\include\;$(ProjectDir)dep\include\</AdditionalIncludeDirectories>
<ObjectFileName>$(IntDir)obj\</ObjectFileName>
@ -172,7 +172,7 @@
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>GLEW_STATIC;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>NFENGINE; GLEW_STATIC;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>$(ProjectDir)src\include\;$(ProjectDir)dep\include\</AdditionalIncludeDirectories>
<ObjectFileName>$(IntDir)obj\</ObjectFileName>

View File

@ -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() {

View File

@ -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<glm::mat4>& 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

View File

@ -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) {

View File

@ -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;

View File

@ -1,9 +1,12 @@
#pragma once
#include <vector>
#include <array>
#include <unordered_map>
#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<unsigned int, 4> m_textures;
unsigned int m_depth;
unsigned int m_width, m_height;
std::unordered_map<Model*, std::vector<glm::mat4>> m_modelsToDraw;
const std::string m_modelString = "model[";
};
}

View File

@ -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();

View File

@ -6,6 +6,8 @@
#include <array>
#include <Windows.h>
#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<unsigned int> m_directionalShadowMaps;
std::vector<unsigned int> m_pointShadowMaps;
unsigned int m_texSlots;
std::vector<Light*> m_lights;
std::vector<Entity*> m_lGame;
Cubemap* m_cubemap;
std::vector<UIElement*> 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<double> 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<std::string, Gamestate*> 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<bool, 164> 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;
};
}
#include "Input.h"