Commit 7e8b2607 authored by Sadman Kazi's avatar Sadman Kazi 🎸

Added camera and perspective projection, also renamed a lot of member variables

parent 0d6d184e
...@@ -72,6 +72,7 @@ ...@@ -72,6 +72,7 @@
</Link> </Link>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="camera.h" />
<ClInclude Include="coreEngine.h" /> <ClInclude Include="coreEngine.h" />
<ClInclude Include="game.h" /> <ClInclude Include="game.h" />
<ClInclude Include="mesh.h" /> <ClInclude Include="mesh.h" />
......
...@@ -74,5 +74,8 @@ ...@@ -74,5 +74,8 @@
<ClInclude Include="obj_loader.h"> <ClInclude Include="obj_loader.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="camera.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
</Project> </Project>
\ No newline at end of file
...@@ -17,27 +17,27 @@ GNU General Public License for more details. ...@@ -17,27 +17,27 @@ GNU General Public License for more details.
#include <windows.h> #include <windows.h>
#include <iostream> #include <iostream>
CoreEngine::CoreEngine(std::string title, int width, int height) : title(title), width(width), height(height) CoreEngine::CoreEngine(std::string title, int width, int height) : m_title(title), m_width(width), m_height(height)
{ {
mainWindow = new Window(title, width, height); m_mainWindow = new Window(title, width, height);
Window::initGraphics(0.0f, 0.0f, 0.0f, 0.0f); Window::initGraphics(0.0f, 0.0f, 0.0f, 0.0f);
game = new Game(); m_game = new Game();
isRunning = true; m_isRunning = true;
} }
CoreEngine::~CoreEngine() CoreEngine::~CoreEngine()
{ {
delete mainWindow; delete m_mainWindow;
delete game; delete m_game;
} }
void CoreEngine::start() { void CoreEngine::start() {
std::cout << "Core engine started." << std::endl; std::cout << "Core engine started." << std::endl;
if (!isRunning) { if (!m_isRunning) {
std::cout << "Core engine closing." << std::endl; std::cout << "Core engine closing." << std::endl;
return; return;
} }
...@@ -48,13 +48,13 @@ void CoreEngine::start() { ...@@ -48,13 +48,13 @@ void CoreEngine::start() {
void CoreEngine::render() { void CoreEngine::render() {
Window::clear(); Window::clear();
game->render(); m_game->render();
mainWindow->update(); m_mainWindow->update();
} }
//game loop //game loop
void CoreEngine::run() { void CoreEngine::run() {
isRunning = true; m_isRunning = true;
std::cout << "Engine starting..."; std::cout << "Engine starting...";
int frames = 0; int frames = 0;
...@@ -66,7 +66,7 @@ void CoreEngine::run() { ...@@ -66,7 +66,7 @@ void CoreEngine::run() {
long long lastTime = Time::elapsed(); long long lastTime = Time::elapsed();
double unprocessedTime = 0; double unprocessedTime = 0;
while (isRunning) { while (m_isRunning) {
bool render = false; bool render = false;
long long startTime = Time::elapsed(); long long startTime = Time::elapsed();
long long passedTime = startTime - lastTime; long long passedTime = startTime - lastTime;
...@@ -80,14 +80,14 @@ void CoreEngine::run() { ...@@ -80,14 +80,14 @@ void CoreEngine::run() {
render = true; render = true;
unprocessedTime -= frameTime; unprocessedTime -= frameTime;
if (mainWindow->isClosed()) if (m_mainWindow->isClosed())
stop(); stop();
Time::setDelta(frameTime); Time::setDelta(frameTime);
//update the game here //update the game here
game->input(); //process the inputs m_game->input(); //process the inputs
game->update(); //update game values m_game->update(); //update game values
//display the frame count every second //display the frame count every second
if (frameCounter >= SECOND) { if (frameCounter >= SECOND) {
...@@ -108,5 +108,5 @@ void CoreEngine::run() { ...@@ -108,5 +108,5 @@ void CoreEngine::run() {
} }
void CoreEngine::stop() { void CoreEngine::stop() {
isRunning = false; m_isRunning = false;
} }
\ No newline at end of file
...@@ -18,11 +18,6 @@ GNU General Public License for more details. ...@@ -18,11 +18,6 @@ GNU General Public License for more details.
#include "time.h" #include "time.h"
#include "game.h" #include "game.h"
#define WIDTH 1280
#define HEIGHT 700
#define TITLE "Game Engine"
#define FRAME_CAP 5000.0f
class CoreEngine class CoreEngine
{ {
public: public:
...@@ -34,11 +29,11 @@ public: ...@@ -34,11 +29,11 @@ public:
~CoreEngine(); ~CoreEngine();
private: private:
Window* mainWindow; Window* m_mainWindow;
int width, height; int m_width, m_height;
const std::string title; const std::string m_title;
bool isRunning; bool m_isRunning;
Game* game; Game* m_game;
void run(); void run();
void render(); void render();
......
...@@ -30,32 +30,35 @@ Game::Game() ...@@ -30,32 +30,35 @@ Game::Game()
2, 1, 0, 2, 1, 0,
0, 2, 3}; 0, 2, 3};
shader = new Shader("./res/basicShader"); m_shader = new Shader("./res/basicShader");
//mesh = new Mesh(data, sizeof(data)/sizeof(data[0]), indices, sizeof(indices)/sizeof(indices[0])); //mesh = new Mesh(data, sizeof(data)/sizeof(data[0]), indices, sizeof(indices)/sizeof(indices[0]));
mesh = new Mesh("./res/monkeyNoUV.obj"); m_mesh = new Mesh("./res/monkeyNoUV.obj");
transform = new Transform(); m_transform = new Transform();
m_camera = new Camera(glm::vec3(0.0f, 0.0f, -2.0f), 70.0f, (float)WIDTH/HEIGHT, 1.0f, 100.0f);
transform->SetScale(glm::vec3(0.5f, 0.5f, 0.5f)); m_transform->setScale(glm::vec3(0.5f, 0.5f, 0.5f));
shader->addUniform("transform"); m_shader->addUniform("transform");
counter = 0; m_counter = 0;
} }
Game::~Game() Game::~Game()
{ {
delete mesh; delete m_mesh;
delete shader; delete m_shader;
delete m_transform;
delete m_camera;
} }
void Game::input() { void Game::input() {
} }
void Game::render() { void Game::render() {
shader->bind(); m_shader->bind();
mesh->draw(); m_mesh->draw();
if (Input::getKeyDown(SDLK_UP)) { if (Input::getKeyDown(SDLK_UP)) {
std::cout << "You have pressed up!" << std::endl; std::cout << "You have pressed up!" << std::endl;
} }
...@@ -72,14 +75,14 @@ void Game::render() { ...@@ -72,14 +75,14 @@ void Game::render() {
} }
void Game::update() { void Game::update() {
counter += Time::getDelta(); m_counter += Time::getDelta();
//std::cout << counter << std::endl; //std::cout << counter << std::endl;
float sinCounter = counter; float sinCounter = m_counter;
float absSinCounter = abs(sinCounter); float absSinCounter = abs(sinCounter);
//transform->GetPos().x = sinCounter; //transform->GetPos().x = sinCounter;
transform->GetRot().y = sinCounter; m_transform->getRot().y = sinCounter;
//transform->GetRot().z = sinCounter; //transform->GetRot().z = sinCounter;
shader->setUniformMat4("transform", transform->GetModel()); m_shader->setUniformMat4("transform", m_camera->getViewProjection() * m_transform->getModel());
} }
\ No newline at end of file
...@@ -15,6 +15,12 @@ GNU General Public License for more details. ...@@ -15,6 +15,12 @@ GNU General Public License for more details.
#include "mesh.h" #include "mesh.h"
#include "shader.h" #include "shader.h"
#include "transform.h" #include "transform.h"
#include "camera.h"
#define WIDTH 1280
#define HEIGHT 700
#define TITLE "Game Engine"
#define FRAME_CAP 5000.0f
class Game class Game
{ {
...@@ -28,9 +34,10 @@ public: ...@@ -28,9 +34,10 @@ public:
virtual ~Game(); virtual ~Game();
private: private:
Mesh* mesh; Mesh* m_mesh;
Shader* shader; Shader* m_shader;
Transform* transform; Transform* m_transform;
float counter; Camera* m_camera;
float m_counter;
}; };
...@@ -13,7 +13,7 @@ public: ...@@ -13,7 +13,7 @@ public:
m_scale(scale) {} m_scale(scale) {}
//getter for transform matrix, generally called model matrix //getter for transform matrix, generally called model matrix
inline glm::mat4 GetModel() const { inline glm::mat4 getModel() const {
glm::mat4 posMatrix = glm::translate(m_pos); glm::mat4 posMatrix = glm::translate(m_pos);
glm::mat4 scaleMatrix = glm::scale(m_scale); glm::mat4 scaleMatrix = glm::scale(m_scale);
glm::mat4 rotXMatrix = glm::rotate(m_rot.x, glm::vec3(1, 0, 0)); glm::mat4 rotXMatrix = glm::rotate(m_rot.x, glm::vec3(1, 0, 0));
...@@ -25,17 +25,16 @@ public: ...@@ -25,17 +25,16 @@ public:
//scale first, then rotation and then position transform //scale first, then rotation and then position transform
return posMatrix * rotMatrix * scaleMatrix; return posMatrix * rotMatrix * scaleMatrix;
} }
//LOL Sadman is gay
//getters, returns by reference //getters, returns by reference
inline glm::vec3& GetPos() { return m_pos; } inline glm::vec3& getPos() { return m_pos; }
inline glm::vec3& GetRot() { return m_rot; } inline glm::vec3& getRot() { return m_rot; }
inline glm::vec3& GetScale() { return m_scale; } inline glm::vec3& getScale() { return m_scale; }
//setters //setters
inline void SetPos(const glm::vec3& pos) { m_pos = pos; } inline void setPos(const glm::vec3& pos) { m_pos = pos; }
inline void SetRot(const glm::vec3& rot) { m_rot = rot; } inline void setRot(const glm::vec3& rot) { m_rot = rot; }
inline void SetScale(const glm::vec3& scale) { m_scale = scale; } inline void setScale(const glm::vec3& scale) { m_scale = scale; }
virtual ~Transform() {}; virtual ~Transform() {};
......
...@@ -19,7 +19,7 @@ GNU General Public License for more details. ...@@ -19,7 +19,7 @@ GNU General Public License for more details.
#include <cassert> #include <cassert>
//ctor //ctor
Window::Window(const std::string title, int width, int height) Window::Window(const std::string title, int width, int height) : m_width(width), m_height(height)
{ {
assert(height > 0 && width > 0); assert(height > 0 && width > 0);
...@@ -34,6 +34,7 @@ Window::Window(const std::string title, int width, int height) ...@@ -34,6 +34,7 @@ Window::Window(const std::string title, int width, int height)
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16); SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
m_window = SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_OPENGL); m_window = SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_OPENGL);
m_context = SDL_GL_CreateContext(m_window); m_context = SDL_GL_CreateContext(m_window);
......
...@@ -27,6 +27,7 @@ public: ...@@ -27,6 +27,7 @@ public:
static void clear(); static void clear();
static void initGraphics(float r, float g, float b, float a); static void initGraphics(float r, float g, float b, float a);
inline bool isClosed() { return m_isClosed; } inline bool isClosed() { return m_isClosed; }
//inline static float getAspect() { return (float)m_width / (float)m_height; }
void update(); void update();
//dtor //dtor
...@@ -36,4 +37,5 @@ private: ...@@ -36,4 +37,5 @@ private:
SDL_Window* m_window; SDL_Window* m_window;
SDL_GLContext m_context; SDL_GLContext m_context;
bool m_isClosed; bool m_isClosed;
int m_width, m_height;
}; };
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment