Added Camera and first-person and UI camera types; Added mouse tracking to aid this

This commit is contained in:
Grayson Riffe (Laptop) 2021-09-01 02:04:28 -05:00
parent d628bbb184
commit 48821afd76
18 changed files with 291 additions and 42 deletions

View File

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

View File

@ -57,6 +57,9 @@
<ClCompile Include="src\Assets.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\Renderer\Camera.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="src\include\Config.h">
@ -113,6 +116,9 @@
<ClInclude Include="src\include\Assets.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\include\Camera.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Natvis Include="NatvisFile.natvis" />

View File

@ -10,7 +10,6 @@ uniform mat4 proj;
out vec2 texCoord;
void main() {
//gl_Position = proj * view * model * vec4(pos, 1.0);
gl_Position = proj * model * vec4(pos, 1.0);
gl_Position = proj * view * model * vec4(pos, 1.0);
texCoord = texCoords;
}
}

View File

@ -70,6 +70,8 @@ namespace nf {
MSG msg = { };
std::thread mainThread(&Application::runMainGameThread, this);
while (m_running) {
if (m_quit)
PostMessage(m_window, WM_CLOSE, NULL, NULL);
while (PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
@ -133,6 +135,18 @@ namespace nf {
}
}
void Application::trackMouse(bool track) {
m_trackingMouse = track;
}
void Application::getMouseDiff(int& x, int& y) {
x = m_mouseDiffX;
y = m_mouseDiffY;
m_mouseDiffX = 0;
m_mouseDiffY = 0;
//TODO: Replace with atomic?
}
void Application::registerWindowClass() {
if (!FindWindow(L"NFClass", NULL)) {
m_wclassName = L"NFClass";
@ -193,6 +207,41 @@ namespace nf {
else
m_input[i] = false;
}
POINT mouse;
GetCursorPos(&mouse);
ScreenToClient(m_window, &mouse);
if (GetFocus() == m_window) {
m_mouseX = mouse.x;
m_mouseY = mouse.y;
if (m_mouseX > m_currentConfig.width)
m_mouseX = m_currentConfig.width;
if (m_mouseX < 0)
m_mouseX = 0;
if (m_mouseY > m_currentConfig.height)
m_mouseY = m_currentConfig.height;
if (m_mouseY < 0)
m_mouseY = 0;
if (m_trackingMouse) {
static bool first = true;
int middleX = m_currentConfig.width / 2;
int middleY = m_currentConfig.height / 2;
m_mouseDiffX += m_mouseX - middleX;
m_mouseDiffY += middleY - m_mouseY;
if (first) {
m_mouseDiffX = 0;
m_mouseDiffY = 0;
first = false;
}
POINT middle = { middleX, middleY };
ClientToScreen(m_window, &middle);
SetCursorPos(middle.x, middle.y);
}
}
}
void Application::quit() {
m_quit = true;
}
void Application::runMainGameThread() {
@ -207,7 +256,7 @@ namespace nf {
lastFrame = std::chrono::steady_clock::now();
m_currentState->update(m_deltaTime);
m_currentState->render(*m_renderer);
m_renderer->doFrame();
m_renderer->doFrame(m_currentState->getCamera());
m_frames++;
if (m_stateChange)
doStateChange();
@ -263,6 +312,15 @@ namespace nf {
case WM_MENUCHAR: {
return MNC_CLOSE << 16;
}
case WM_SETCURSOR: {
if (LOWORD(lParam) != HTCLIENT)
break;
if (app->m_trackingMouse && LOWORD(lParam) == HTCLIENT && GetFocus() == hWnd) {
SetCursor(NULL);
return 0;
}
break;
}
case WM_CLOSE: {
DestroyWindow(hWnd);
return 0;

View File

@ -4,7 +4,9 @@
#include "Utility.h"
namespace nf {
Gamestate::Gamestate(Application* app) {
Gamestate::Gamestate(Application* app) :
m_camera(app)
{
m_app = app;
}
@ -16,6 +18,10 @@ namespace nf {
}
Camera* Gamestate::getCamera() {
return &m_camera;
}
void Gamestate::render(Renderer& renderer) {
}

View File

@ -0,0 +1,106 @@
#include "Camera.h"
#include "glm/glm.hpp"
#include "Application.h"
#include "Shader.h"
namespace nf {
Camera::Camera(Application* app) :
m_app(app)
{
m_type = Type::NF_CAMERA_UI;
}
void Camera::setType(Type cameraType) {
m_type = cameraType;
if (m_type == Type::NF_CAMERA_FIRST_PERSON || m_type == Type::NF_CAMERA_ORBIT)
m_app->trackMouse(true);
else
m_app->trackMouse(false);
}
Camera::Type Camera::getType() const {
return m_type;
}
void Camera::moveForward(double speed) {
Vec3 temp = m_front * speed;
m_position = { m_position.x + temp.x, m_position.y + temp.y, m_position.z + temp.z };
}
void Camera::moveBackward(double speed) {
Vec3 temp = m_front * speed;
m_position = { m_position.x - temp.x, m_position.y - temp.y, m_position.z - temp.z };
}
void Camera::moveRight(double speed) {
glm::vec3 front = { m_front.x, m_front.y, m_front.z };
glm::vec3 temp = glm::normalize(glm::cross(front, glm::vec3(0.0, 1.0, 0.0))) * (float)speed;
m_position = { m_position.x + temp.x, m_position.y + temp.y, m_position.z + temp.z };
}
void Camera::moveLeft(double speed) {
glm::vec3 front = { m_front.x, m_front.y, m_front.z };
glm::vec3 temp = glm::normalize(glm::cross(front, glm::vec3(0.0, 1.0, 0.0))) * (float)speed;
m_position = { m_position.x - temp.x, m_position.y - temp.y, m_position.z - temp.z };
}
void Camera::setPosition(double x, double y, double z) {
m_position = { x, y, z };
}
void Camera::setPosition(const Vec3& position) {
m_position = position;
}
void Camera::bind(Shader* shader) {
glm::mat4 view;
switch (m_type) {
case Type::NF_CAMERA_UI: {
view = glm::mat4(1.0);
break;
}
case Type::NF_CAMERA_FIRST_PERSON: {
int mouseDiffx = 0;
int mouseDiffy = 0;
m_app->getMouseDiff(mouseDiffx, mouseDiffy);
float mouseX = (float)mouseDiffx * 0.1f;
float mouseY = (float)mouseDiffy * 0.1f;
static float yaw = -90.0f;
static float pitch = 0.0f;
yaw += mouseX;
pitch += mouseY;
if (pitch > 89.0f)
pitch = 89.0f;
if (pitch < -89.0f)
pitch = -89.0f;
glm::vec3 rotation;
rotation.x = std::cos(glm::radians(yaw)) * std::cos(glm::radians(pitch));
rotation.y = std::sin(glm::radians(pitch));
rotation.z = std::sin(glm::radians(yaw)) * std::cos(glm::radians(pitch));
rotation = glm::normalize(rotation);
m_front = { rotation.x, rotation.y, rotation.z };
glm::vec3 position(m_position.x, m_position.y, m_position.z);
glm::vec3 up(0.0, 1.0, 0.0);
view = glm::lookAt(position, position + rotation, up);
break;
}
case Type::NF_CAMERA_ORBIT: {
break;
}
case Type::NF_CAMERA_FIXED: {
break;
}
}
shader->setUniform("view", view);
}
Camera::~Camera() {
}
}

View File

@ -2,8 +2,6 @@
#include<vector>
#include "Utility.h"
namespace nf {
Entity::Entity() :
m_model(nullptr),
@ -39,10 +37,18 @@ namespace nf {
m_position = { x, y, z };
}
void Entity::setPosition(const Vec3& position) {
m_position = position;
}
void Entity::setRotation(double x, double y, double z) {
m_rotation = { x, y, z };
}
void Entity::setRotation(const Vec3& rotation) {
m_rotation = rotation;
}
void Entity::setScale(double x) {
m_rotation = { x, x, x };
}
@ -51,6 +57,10 @@ namespace nf {
m_scale = { x, y, z };
}
void Entity::setScale(const Vec3& scale) {
m_scale = scale;
}
void Entity::bind(Shader* shader) {
m_model->bind();
shader->bind();

View File

@ -1,6 +1,10 @@
#include "Model.h"
#include "glm/glm.hpp"
#include "glm/gtc/matrix_transform.hpp"
#include "Utility.h"
#include "Texture.h"
namespace nf {
Model::Model() {

View File

@ -2,6 +2,7 @@
#include "GL/glew.h"
#include "GL\wglew.h"
#include "glm/glm.hpp"
#include "Application.h"
#include "Utility.h"
@ -51,6 +52,7 @@ namespace nf {
glDepthFunc(GL_LESS);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_CULL_FACE);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
Win32Res vs(IDR_DEFAULTVERTEX);
@ -68,15 +70,17 @@ namespace nf {
m_lGame.push_back(&in);
}
void Renderer::doFrame() {
void Renderer::doFrame(Camera* camera) {
glViewport(0, 0, m_app->getConfig().width, m_app->getConfig().height);
proj = glm::perspective(glm::radians(45.0f), (float)m_app->getConfig().width / (float)m_app->getConfig().height, 0.1f, 100000.0f);
glm::mat4 proj = glm::perspective(glm::radians(45.0f), (float)m_app->getConfig().width / (float)m_app->getConfig().height, 0.1f, 100000.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
for (Entity* draw : m_lGame) {
Entity& curr = *draw;
curr.bind(m_defaultShader);
m_defaultShader->setUniform("proj", proj);
camera->bind(m_defaultShader);
//TODO: Clean this up a bit
glDrawElements(GL_TRIANGLES, curr.getModel()->getIndexCount(), GL_UNSIGNED_INT, nullptr);
}
m_lGame.clear();

View File

@ -28,7 +28,10 @@ namespace nf {
const Config& getConfig() const;
int getFPS() const;
bool isInput(unsigned int code);
void trackMouse(bool track);
void getMouseDiff(int& x, int& y);
void quit();
~Application();
private:
void registerWindowClass();
@ -44,6 +47,7 @@ namespace nf {
Config m_currentConfig;
bool m_running;
bool m_quit;
HINSTANCE m_hInst;
LPCWSTR m_wclassName;
HWND m_window;
@ -71,6 +75,9 @@ namespace nf {
//Array of booleans that represent keyboard and mouse input minus the scrollwheel
bool m_input[164];
int m_mouseX, m_mouseY;
bool m_trackingMouse;
int m_mouseDiffX, m_mouseDiffY;
//Renderer object to use OpenGL to render the current state
Renderer* m_renderer;

View File

@ -0,0 +1,38 @@
#pragma once
#include"Utility.h"
namespace nf {
class Application;
class Shader;
//TODO: Make sure there are always newlines here;
class Camera {
public:
enum class Type {
NF_CAMERA_UI,
NF_CAMERA_FIRST_PERSON,
NF_CAMERA_ORBIT,
NF_CAMERA_FIXED
};
Camera(Application* app);
void setType(Type cameraType);
Type getType() const;
void moveForward(double speed);
void moveBackward(double speed);
void moveRight(double speed);
void moveLeft(double speed);
void setPosition(double x, double y, double z);
void setPosition(const Vec3& position);
void bind(Shader* shader);
~Camera();
private:
Application* m_app;
Type m_type;
Vec3 m_position;
Vec3 m_front;
};
}

View File

@ -1,23 +1,22 @@
#pragma once
#include "Model.h"
#include "Assets.h"
#include "Utility.h"
namespace nf {
class Entity {
struct Vec3 {
Vec3(double x1) : x(x1), y(x1), z(x1) {}
Vec3(double x1, double y1, double z1) : x(x1), y(y1), z(z1) {}
double x, y, z;
};
public:
Entity();
void create(Asset* modelAsset, Asset* textureAsset = nullptr);
void setPosition(double x, double y, double z);
void setPosition(const Vec3& position);
void setRotation(double x, double y, double z);
void setRotation(const Vec3& rotation);
void setScale(double x);
void setScale(double x, double y, double z);
void setScale(const Vec3& scale);
void bind(Shader* shader);
Model* getModel() const;

View File

@ -1,4 +1,5 @@
#pragma once
#include "Camera.h"
namespace nf {
class Application;
@ -13,11 +14,12 @@ namespace nf {
virtual void onEnter();
virtual void update(double deltaTime);
Camera* getCamera();
virtual void render(Renderer& renderer);
virtual void onExit();
protected:
Application* m_app;
//Resource identifier?
Camera m_camera;
};
}

View File

@ -1,13 +1,9 @@
#pragma once
#ifdef NFENGINE
#include "glm/glm.hpp"
#include "glm/gtc/matrix_transform.hpp"
#endif
#include "Drawable.h"
#include "Texture.h"
namespace nf {
class Drawable;
class Texture;
class Model : public Drawable {
public:
Model();

View File

@ -6,6 +6,7 @@
#include <Windows.h>
#include "Config.h"
#include "Utility.h"
#include "IntroGamestate.h"
#include "Assets.h"
@ -15,20 +16,18 @@ namespace nf {
class Model;
class Entity {
struct Vec3 {
Vec3(double x1) : x(x1), y(x1), z(x1) {}
Vec3(double x1, double y1, double z1) : x(x1), y(y1), z(z1) {}
double x, y, z;
};
public:
Entity();
void create(Asset* modelAsset, Asset* textureAsset = nullptr);
void setPosition(double x, double y, double z);
void setPosition(const Vec3& position);
void setRotation(double x, double y, double z);
void setRotation(const Vec3& rotation);
void setScale(double x);
void setScale(double x, double y, double z);
void setScale(const Vec3& scale);
void bind(Shader* shader);
Model* getModel() const;
@ -49,8 +48,9 @@ namespace nf {
Renderer(Application* app);
void render(Entity& in);
//TODO: Create second render function for UIElements
void doFrame();
void doFrame(Camera* camera);
~Renderer();
private:
@ -59,10 +59,8 @@ namespace nf {
HDC m_hdc;
HGLRC m_hglrc;
std::vector<Drawable*> m_lGame;
std::vector<Entity*> m_lGame;
std::vector<Drawable*> m_lUI;
const char* m_defaultVertex;
const char* m_defaultFragment;
Shader* m_defaultShader;
};
@ -85,13 +83,17 @@ namespace nf {
const Config& getConfig() const;
int getFPS() const;
bool isInput(unsigned int code);
void trackMouse(bool track);
void getMouseDiff(int& x, int& y);
void quit();
~Application();
private:
void registerWindowClass();
RECT getWindowRect() const;
void calculateNewWindowPos(int& x, int& y);
void toggleFullscreen();
void updateInput();
void runMainGameThread();
void startIntroState();
void doStateChange();
@ -100,6 +102,7 @@ namespace nf {
Config m_currentConfig;
bool m_running;
bool m_quit;
HINSTANCE m_hInst;
LPCWSTR m_wclassName;
HWND m_window;
@ -127,10 +130,12 @@ namespace nf {
//Array of booleans that represent keyboard and mouse input minus the scrollwheel
bool m_input[164];
int m_mouseX, m_mouseY;
bool m_trackingMouse;
int m_mouseDiffX, m_mouseDiffY;
//Renderer object to use OpenGL to render the current state
Renderer* m_renderer;
};
}
#include "Input.h"
#include "Utility.h"
#include "Input.h"

View File

@ -1,9 +1,9 @@
#pragma once
#include <vector>
#include <Windows.h>
#include "glm/glm.hpp"
#include "Entity.h"
#include "Camera.h"
namespace nf {
class Application;
@ -15,7 +15,7 @@ namespace nf {
void render(Entity& in);
//TODO: Create second render function for UIElements
void doFrame();
void doFrame(Camera* camera);
~Renderer();
private:
@ -27,7 +27,5 @@ namespace nf {
std::vector<Entity*> m_lGame;
std::vector<Drawable*> m_lUI;
Shader* m_defaultShader;
glm::mat4 proj;
};
}

View File

@ -1,9 +1,7 @@
#pragma once
#include <unordered_map>
#ifdef NFENGINE
#include "glm/glm.hpp"
#include "glm/gtc/type_ptr.hpp"
#endif
namespace nf {
class Shader {

View File

@ -39,12 +39,23 @@ __debugbreak();}
std::exit(-1);}
#endif
//TODO: Delete this after moving everything to base.nfpack
struct Win32Res {
Win32Res(int id);
void* ptr;
size_t size;
};
struct Vec3 {
Vec3() {}
Vec3(double x1) : x(x1), y(x1), z(x1) {}
Vec3(double x1, double y1, double z1) : x(x1), y(y1), z(z1) {}
Vec3 operator*(const double scalar) {
return Vec3(x * scalar, y * scalar, z * scalar);
}
double x, y, z;
};
const wchar_t* toWide(const char* in);
const wchar_t* toWide(const std::string& in);
void writeFile(const std::string& filename, const std::string& in, bool encrypted = false);