Added Entity and transformations with GLM; Minor changes

This commit is contained in:
Grayson Riffe (Laptop) 2021-08-27 01:10:23 -05:00
parent bc5c17ebfb
commit 5d32976869
16 changed files with 280 additions and 59 deletions

View File

@ -3,16 +3,20 @@
MainState::MainState(nf::Application* app) : MainState::MainState(nf::Application* app) :
Gamestate(app) Gamestate(app)
{ {
} }
void MainState::onEnter() { void MainState::onEnter() {
Log("MainState onEnter!"); Log("MainState onEnter!");
} }
void MainState::update(double deltaTime) { void MainState::update(double deltaTime) {
} }
void MainState::render(nf::Renderer& renderer) { void MainState::render(nf::Renderer& renderer) {
} }
void MainState::onExit() { void MainState::onExit() {

View File

@ -94,7 +94,7 @@
<ClCompile> <ClCompile>
<WarningLevel>Level3</WarningLevel> <WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck> <SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>GLEW_STATIC;WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> <PreprocessorDefinitions>NFENGINE;GLEW_STATIC;WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode> <ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>$(ProjectDir)src\include\;$(ProjectDir)dep\include\</AdditionalIncludeDirectories> <AdditionalIncludeDirectories>$(ProjectDir)src\include\;$(ProjectDir)dep\include\</AdditionalIncludeDirectories>
<ObjectFileName>$(IntDir)obj\</ObjectFileName> <ObjectFileName>$(IntDir)obj\</ObjectFileName>
@ -123,7 +123,7 @@
<FunctionLevelLinking>true</FunctionLevelLinking> <FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions> <IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck> <SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>GLEW_STATIC;WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> <PreprocessorDefinitions>NFENGINE;GLEW_STATIC;WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode> <ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>$(ProjectDir)src\include\;$(ProjectDir)dep\include\</AdditionalIncludeDirectories> <AdditionalIncludeDirectories>$(ProjectDir)src\include\;$(ProjectDir)dep\include\</AdditionalIncludeDirectories>
<ObjectFileName>$(IntDir)obj\</ObjectFileName> <ObjectFileName>$(IntDir)obj\</ObjectFileName>
@ -152,7 +152,7 @@
<ClCompile> <ClCompile>
<WarningLevel>Level3</WarningLevel> <WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck> <SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>GLEW_STATIC;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> <PreprocessorDefinitions>NFENGINE;GLEW_STATIC;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode> <ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>$(ProjectDir)src\include\;$(ProjectDir)dep\include\</AdditionalIncludeDirectories> <AdditionalIncludeDirectories>$(ProjectDir)src\include\;$(ProjectDir)dep\include\</AdditionalIncludeDirectories>
<ObjectFileName>$(IntDir)obj\</ObjectFileName> <ObjectFileName>$(IntDir)obj\</ObjectFileName>
@ -181,7 +181,7 @@
<FunctionLevelLinking>true</FunctionLevelLinking> <FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions> <IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck> <SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>GLEW_STATIC;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> <PreprocessorDefinitions>NFENGINE;GLEW_STATIC;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode> <ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>$(ProjectDir)src\include\;$(ProjectDir)dep\include\</AdditionalIncludeDirectories> <AdditionalIncludeDirectories>$(ProjectDir)src\include\;$(ProjectDir)dep\include\</AdditionalIncludeDirectories>
<ObjectFileName>$(IntDir)obj\</ObjectFileName> <ObjectFileName>$(IntDir)obj\</ObjectFileName>
@ -211,6 +211,7 @@
<ClCompile Include="src\Gamestate.cpp" /> <ClCompile Include="src\Gamestate.cpp" />
<ClCompile Include="src\IntroGamestate.cpp" /> <ClCompile Include="src\IntroGamestate.cpp" />
<ClCompile Include="src\Renderer\Drawable\Drawable.cpp" /> <ClCompile Include="src\Renderer\Drawable\Drawable.cpp" />
<ClCompile Include="src\Renderer\Drawable\Entity.cpp" />
<ClCompile Include="src\Renderer\Drawable\Model.cpp" /> <ClCompile Include="src\Renderer\Drawable\Model.cpp" />
<ClCompile Include="src\Renderer\IndexBuffer.cpp" /> <ClCompile Include="src\Renderer\IndexBuffer.cpp" />
<ClCompile Include="src\Renderer\Renderer.cpp" /> <ClCompile Include="src\Renderer\Renderer.cpp" />
@ -220,6 +221,7 @@
<ClCompile Include="src\Utility.cpp" /> <ClCompile Include="src\Utility.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="src\include\Entity.h" />
<ClInclude Include="src\include\resource.h" /> <ClInclude Include="src\include\resource.h" />
<ClInclude Include="src\include\Application.h" /> <ClInclude Include="src\include\Application.h" />
<ClInclude Include="src\include\Config.h" /> <ClInclude Include="src\include\Config.h" />

View File

@ -48,6 +48,9 @@
<ClCompile Include="src\Renderer\Drawable\Model.cpp"> <ClCompile Include="src\Renderer\Drawable\Model.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="src\Renderer\Drawable\Entity.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="src\include\Config.h"> <ClInclude Include="src\include\Config.h">
@ -95,6 +98,9 @@
<ClInclude Include="src\include\resource.h"> <ClInclude Include="src\include\resource.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="src\include\Entity.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Natvis Include="NatvisFile.natvis" /> <Natvis Include="NatvisFile.natvis" />

View File

@ -211,6 +211,7 @@ namespace nf {
std::this_thread::sleep_until(next_time); std::this_thread::sleep_until(next_time);
m_deltaTime = (double)(std::chrono::steady_clock::now() - start_time).count(); m_deltaTime = (double)(std::chrono::steady_clock::now() - start_time).count();
next_time += wait_time; next_time += wait_time;
//TODO: Redo FPS AGAIN like how I did it in PongClone
} }
m_currentState->onExit(); m_currentState->onExit();
delete m_renderer; delete m_renderer;

View File

@ -4,11 +4,7 @@
namespace nf { namespace nf {
Drawable::Drawable() { Drawable::Drawable() {
Log("Drawable constructor");
}
Drawable::DrawableType Drawable::identity() {
return DrawableType::NF_NONE;
} }
void Drawable::bind() { void Drawable::bind() {
@ -22,6 +18,5 @@ namespace nf {
Drawable::~Drawable() { Drawable::~Drawable() {
delete m_vao; delete m_vao;
delete m_ib; delete m_ib;
delete m_shader;
} }
} }

View File

@ -0,0 +1,57 @@
#include "Entity.h"
namespace nf {
Entity::Entity() :
m_position(0.0),
m_rotation(0.0),
m_scale(1.0)
{
}
void Entity::create(const void* vertexBufferData, const size_t vertexBufferSize, const void* indexBufferData, size_t indexBufferCount) {
m_model = new Model;
m_model->create(vertexBufferData, vertexBufferSize, indexBufferData, indexBufferCount);
}
void Entity::setLocation(float x, float y, float z) {
m_position = { x, y, z };
}
void Entity::setRotation(float x, float y, float z) {
m_rotation = { x, y, z };
}
void Entity::setScale(float x) {
m_rotation = { x, x, x };
}
void Entity::setScale(float x, float y, float z) {
m_scale = { x, y, z };
}
void Entity::bind(Shader* shader) {
m_model->bind();
shader->bind();
setModelMatrix(shader);
}
Model* Entity::getModel() const {
return m_model;
}
void Entity::setModelMatrix(Shader* shader) {
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(m_rotation.x), glm::vec3(1.0, 0.0, 0.0));
model = glm::rotate(model, glm::radians(m_rotation.y), glm::vec3(0.0, 1.0, 0.0));
model = glm::rotate(model, glm::radians(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));
//TODO: fill out model matrix
shader->setUniform("model", model);
}
Entity::~Entity() {
}
}

View File

@ -6,19 +6,13 @@ namespace nf {
Model::Model() { Model::Model() {
} }
void Model::create(const void* vertexBufferData, const size_t vertexBufferSize, const void* indexBufferData, size_t indexBufferCount, const char* vertexShader, const char* fragmentShader) { void Model::create(const void* vertexBufferData, const size_t vertexBufferSize, const void* indexBufferData, size_t indexBufferCount) {
m_vao = new VertexArray; m_vao = new VertexArray;
m_vao->addBuffer(vertexBufferData, vertexBufferSize); m_vao->addBuffer(vertexBufferData, vertexBufferSize);
m_vao->push<float>(2); m_vao->push<float>(2);
//TODO: Change this to 3 //TODO: Change this to 3
m_vao->finishBufferLayout(); m_vao->finishBufferLayout();
m_ib = new IndexBuffer(indexBufferData, indexBufferCount); m_ib = new IndexBuffer(indexBufferData, indexBufferCount);
if (vertexShader && fragmentShader)
m_shader = new Shader(vertexShader, fragmentShader);
}
Drawable::DrawableType Model::identity() {
return DrawableType::NF_GAME;
} }
void Model::bind() { void Model::bind() {
@ -26,16 +20,6 @@ namespace nf {
Error("Tried to bind uninitialized model!"); Error("Tried to bind uninitialized model!");
m_vao->bind(); m_vao->bind();
m_ib->bind(); m_ib->bind();
if (m_shader) {
m_shader->bind();
//TODO: Set uniforms here such as position and texture
}
}
bool Model::hasCustomShader() {
if (m_shader)
return true;
return false;
} }
Model::~Model() { Model::~Model() {

View File

@ -54,37 +54,36 @@ namespace nf {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
Win32Res vs(IDR_DEFAULTVERTEX); Win32Res vs(IDR_DEFAULTVERTEX);
m_defaultVertex = (const char*)vs.ptr; const char* defaultVertex = (const char*)vs.ptr;
Win32Res fs(IDR_DEFAULTFRAGMENT); Win32Res fs(IDR_DEFAULTFRAGMENT);
m_defaultFragment = (const char*)fs.ptr; const char* defaultFragment = (const char*)fs.ptr;
m_defaultShader = new Shader(m_defaultVertex, m_defaultFragment); if (defaultVertex == nullptr || defaultFragment == nullptr)
Error("Default engine resources not found! Please link nf.res to your application!");
m_defaultShader = new Shader(defaultVertex, defaultFragment);
} }
void Renderer::render(Drawable& in) { void Renderer::render(Entity& in) {
if (&in == nullptr) if (&in == nullptr)
Error("Drawable object tried to render before being constructed!"); Error("Tried to render Entity before being created!");
if (in.identity() == Drawable::DrawableType::NF_UI) m_lGame.push_back(&in);
m_lUI.push_back(&in);
else
m_lGame.push_back(&in);
} }
void Renderer::doFrame() { void Renderer::doFrame() {
glViewport(0, 0, m_app->getConfig().width, m_app->getConfig().height); glViewport(0, 0, m_app->getConfig().width, m_app->getConfig().height);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
for (Drawable* draw : m_lGame) { for (Entity* draw : m_lGame) {
Model& curr = (Model&)*draw; Entity& curr = *draw;
curr.bind(); curr.bind(m_defaultShader);
if (!curr.hasCustomShader()) glDrawElements(GL_TRIANGLES, curr.getModel()->getIndexCount(), GL_UNSIGNED_INT, nullptr);
m_defaultShader->bind();
glDrawElements(GL_TRIANGLES, curr.getIndexCount(), GL_UNSIGNED_INT, nullptr);
} }
m_lGame.clear();
for (Drawable* draw : m_lUI) { for (Drawable* draw : m_lUI) {
Drawable& curr = *draw; Drawable& curr = *draw;
} }
m_lUI.clear();
SwapBuffers(m_hdc); SwapBuffers(m_hdc);

View File

@ -49,7 +49,13 @@ namespace nf {
Shader::current = m_id; Shader::current = m_id;
} }
} }
//TODO: Create overloaded setUniform function
void Shader::setUniform(const char* name, glm::mat4& data) {
if (m_uniformLocations.find(name) != m_uniformLocations.end())
getUniformLocation(name);
glUniformMatrix4fv(m_uniformLocations[name], 1, GL_FALSE, glm::value_ptr(data));
}
void Shader::getUniformLocation(const char* uniformName) { void Shader::getUniformLocation(const char* uniformName) {
unsigned int loc = glGetUniformLocation(m_id, uniformName); unsigned int loc = glGetUniformLocation(m_id, uniformName);
if (loc == -1) if (loc == -1)

View File

@ -6,13 +6,8 @@
namespace nf { namespace nf {
class Drawable { class Drawable {
public: public:
enum class DrawableType {
NF_NONE, NF_GAME, NF_UI
};
//TODO: Construct using Shader code and data from obj
Drawable(); Drawable();
virtual DrawableType identity();
virtual void bind(); virtual void bind();
unsigned int getIndexCount(); unsigned int getIndexCount();
@ -20,6 +15,5 @@ namespace nf {
protected: protected:
VertexArray* m_vao; VertexArray* m_vao;
IndexBuffer* m_ib; IndexBuffer* m_ib;
Shader* m_shader;
}; };
} }

View File

@ -0,0 +1,34 @@
#pragma once
#include "Model.h"
namespace nf {
class Entity {
struct Vec3 {
Vec3(float x1) : x(x1), y(x1), z(x1) {}
Vec3(float x1, float y1, float z1) : x(x1), y(y1), z(z1) {}
float x, y, z;
};
public:
Entity();
void create(const void* vertexBufferData, const size_t vertexBufferSize, const void* indexBufferData, size_t indexBufferCount);
//TODO: Do this using loaded assets somehow
void setLocation(float x, float y, float z);
void setRotation(float x, float y, float z);
void setScale(float x);
void setScale(float x, float y, float z);
void bind(Shader* shader);
Model* getModel() const;
~Entity();
private:
void setModelMatrix(Shader* shader);
Model* m_model;
Vec3 m_position;
Vec3 m_rotation;
Vec3 m_scale;
};
}

View File

@ -1,4 +1,9 @@
#pragma once #pragma once
#ifdef NFENGINE
#include "glm/glm.hpp"
#include "glm/gtc/matrix_transform.hpp"
#endif
#include "Drawable.h" #include "Drawable.h"
namespace nf { namespace nf {
@ -6,10 +11,8 @@ namespace nf {
public: public:
Model(); Model();
void create(const void* vertexBufferData, const size_t vertexBufferSize, const void* indexBufferData, size_t indexBufferCount, const char* vertexShader = nullptr, const char* fragmentShader = nullptr); void create(const void* vertexBufferData, const size_t vertexBufferSize, const void* indexBufferData, size_t indexBufferCount);
DrawableType identity() override;
void bind() override; void bind() override;
bool hasCustomShader();
~Model(); ~Model();
private: private:

View File

@ -1,5 +1,135 @@
//TODO: Rework this file to only contain functions the frontend will need to access //TODO: Rework this file to only contain functions the frontend will need to access
#include "Application.h" #pragma once
#include <chrono>
#include <unordered_map>
#include <Windows.h>
#include "Config.h"
#include "IntroGamestate.h"
namespace nf {
class Drawable;
class Shader;
class Model;
class Entity {
struct Vec3 {
Vec3(float x1) : x(x1), y(x1), z(x1) {}
Vec3(float x1, float y1, float z1) : x(x1), y(y1), z(z1) {}
float x, y, z;
};
public:
Entity();
void create(const void* vertexBufferData, const size_t vertexBufferSize, const void* indexBufferData, size_t indexBufferCount);
//TODO: Do this using loaded assets somehow
void setLocation(float x, float y, float z);
void setRotation(float x, float y, float z);
void setScale(float x);
void setScale(float x, float y, float z);
void bind(Shader* shader);
Model* getModel() const;
~Entity();
private:
void setModelMatrix(Shader* shader);
Model* m_model;
Vec3 m_position;
Vec3 m_rotation;
Vec3 m_scale;
};
class Renderer {
public:
Renderer(Application* app);
void render(Entity& in);
void doFrame();
~Renderer();
private:
Application* m_app;
HDC m_hdc;
HGLRC m_hglrc;
std::vector<Drawable*> m_lGame;
std::vector<Drawable*> m_lUI;
const char* m_defaultVertex;
const char* m_defaultFragment;
Shader* m_defaultShader;
};
class Application {
public:
Application(Config& conf);
Application() = delete;
Application(Application& other) = delete;
void setWindowIcon(HANDLE hIcon);
void setWindowCursor(HCURSOR hCursor);
Renderer* getRenderer() const;
void addState(Gamestate* state, const std::string& stateName);
void addDefaultState(const std::string& stateName);
void run();
void changeState(const std::string& stateName);
void showWindow(bool show);
const HWND& getWindow();
void changeConfig(const Config& in);
const Config& getConfig() const;
int getFPS() const;
bool isInput(unsigned int code);
~Application();
private:
void registerWindowClass();
RECT getWindowRect() const;
void calculateNewWindowPos(int& x, int& y);
void toggleFullscreen();
void runMainGameThread();
void startIntroState();
void doStateChange();
static LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Config m_currentConfig;
bool m_running;
HINSTANCE m_hInst;
LPCWSTR m_wclassName;
HWND m_window;
LONG m_defaultWindowStyle;
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;
int m_frames;
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;
Gamestate* 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
bool m_input[164];
//Renderer object to use OpenGL to render the current state
Renderer* m_renderer;
};
}
#include "Input.h" #include "Input.h"
#include "Utility.h" #include "Utility.h"

View File

@ -2,7 +2,7 @@
#include <vector> #include <vector>
#include <Windows.h> #include <Windows.h>
#include "Model.h" #include "Entity.h"
namespace nf { namespace nf {
class Application; class Application;
@ -11,7 +11,8 @@ namespace nf {
public: public:
Renderer(Application* app); Renderer(Application* app);
void render(Drawable& in); void render(Entity& in);
//TODO: Create second render function for UIElements
void doFrame(); void doFrame();
@ -22,10 +23,8 @@ namespace nf {
HDC m_hdc; HDC m_hdc;
HGLRC m_hglrc; HGLRC m_hglrc;
std::vector<Drawable*> m_lGame; std::vector<Entity*> m_lGame;
std::vector<Drawable*> m_lUI; std::vector<Drawable*> m_lUI;
const char* m_defaultVertex;
const char* m_defaultFragment;
Shader* m_defaultShader; Shader* m_defaultShader;
}; };
} }

View File

@ -1,5 +1,9 @@
#pragma once #pragma once
#include <unordered_map> #include <unordered_map>
#ifdef NFENGINE
#include "glm/glm.hpp"
#include "glm/gtc/type_ptr.hpp"
#endif
namespace nf { namespace nf {
class Shader { class Shader {
@ -7,12 +11,14 @@ namespace nf {
Shader(const char* vertexSource, const char* fragmentSource); Shader(const char* vertexSource, const char* fragmentSource);
void bind(); void bind();
void getUniformLocation(const char* uniformName); void setUniform(const char* name, glm::mat4& data);
static unsigned int current; static unsigned int current;
~Shader(); ~Shader();
private: private:
void getUniformLocation(const char* uniformName);
unsigned int m_id; unsigned int m_id;
std::unordered_map<const char*, unsigned int> m_uniformLocations; std::unordered_map<const char*, unsigned int> m_uniformLocations;
}; };

View File

@ -15,7 +15,7 @@ Remember to use tasks (//TODO: )
*Engine (Name?) "Nothin' Fancy" *Engine (Name?) "Nothin' Fancy"
*Separate project *Separate project
*Namespaced *Namespaced
Refactor NothinFancy.h to ONLY include stuff the frontend needs Refactor NothinFancy.h to ONLY include stuff the frontend needs (Does deleteing uneeded public funcitons do anything?)
*AND get rid of NFENGINE *AND get rid of NFENGINE
*High CPU usage? *High CPU usage?
*Debug and log system *Debug and log system
@ -24,6 +24,7 @@ Refactor NothinFancy.h to ONLY include stuff the frontend needs
*Alt-Enter *Alt-Enter
*File IO functions *File IO functions
*Keyboard and mouse click input *Keyboard and mouse click input
Check for memory leaks in deconstructors
Separate Model and UIElement from Drawable Separate Model and UIElement from Drawable
Entity and Camera classes Entity and Camera classes
Mouse position input Mouse position input