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 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="camera.h" />
<ClInclude Include="coreEngine.h" />
<ClInclude Include="game.h" />
<ClInclude Include="mesh.h" />
......
......@@ -74,5 +74,8 @@
<ClInclude Include="obj_loader.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="camera.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>
\ No newline at end of file
......@@ -17,27 +17,27 @@ GNU General Public License for more details.
#include <windows.h>
#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);
game = new Game();
m_game = new Game();
isRunning = true;
m_isRunning = true;
}
CoreEngine::~CoreEngine()
{
delete mainWindow;
delete game;
delete m_mainWindow;
delete m_game;
}
void CoreEngine::start() {
std::cout << "Core engine started." << std::endl;
if (!isRunning) {
if (!m_isRunning) {
std::cout << "Core engine closing." << std::endl;
return;
}
......@@ -48,13 +48,13 @@ void CoreEngine::start() {
void CoreEngine::render() {
Window::clear();
game->render();
mainWindow->update();
m_game->render();
m_mainWindow->update();
}
//game loop
void CoreEngine::run() {
isRunning = true;
m_isRunning = true;
std::cout << "Engine starting...";
int frames = 0;
......@@ -66,7 +66,7 @@ void CoreEngine::run() {
long long lastTime = Time::elapsed();
double unprocessedTime = 0;
while (isRunning) {
while (m_isRunning) {
bool render = false;
long long startTime = Time::elapsed();
long long passedTime = startTime - lastTime;
......@@ -80,14 +80,14 @@ void CoreEngine::run() {
render = true;
unprocessedTime -= frameTime;
if (mainWindow->isClosed())
if (m_mainWindow->isClosed())
stop();
Time::setDelta(frameTime);
//update the game here
game->input(); //process the inputs
game->update(); //update game values
m_game->input(); //process the inputs
m_game->update(); //update game values
//display the frame count every second
if (frameCounter >= SECOND) {
......@@ -108,5 +108,5 @@ void CoreEngine::run() {
}
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.
#include "time.h"
#include "game.h"
#define WIDTH 1280
#define HEIGHT 700
#define TITLE "Game Engine"
#define FRAME_CAP 5000.0f
class CoreEngine
{
public:
......@@ -34,11 +29,11 @@ public:
~CoreEngine();
private:
Window* mainWindow;
int width, height;
const std::string title;
bool isRunning;
Game* game;
Window* m_mainWindow;
int m_width, m_height;
const std::string m_title;
bool m_isRunning;
Game* m_game;
void run();
void render();
......
......@@ -30,32 +30,35 @@ Game::Game()
2, 1, 0,
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("./res/monkeyNoUV.obj");
transform = new Transform();
m_mesh = new Mesh("./res/monkeyNoUV.obj");
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");
counter = 0;
m_shader->addUniform("transform");
m_counter = 0;
}
Game::~Game()
{
delete mesh;
delete shader;
delete m_mesh;
delete m_shader;
delete m_transform;
delete m_camera;
}
void Game::input() {
}
void Game::render() {
shader->bind();
mesh->draw();
m_shader->bind();
m_mesh->draw();
if (Input::getKeyDown(SDLK_UP)) {
std::cout << "You have pressed up!" << std::endl;
}
......@@ -72,14 +75,14 @@ void Game::render() {
}
void Game::update() {
counter += Time::getDelta();
m_counter += Time::getDelta();
//std::cout << counter << std::endl;
float sinCounter = counter;
float sinCounter = m_counter;
float absSinCounter = abs(sinCounter);
//transform->GetPos().x = sinCounter;
transform->GetRot().y = sinCounter;
m_transform->getRot().y = 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.
#include "mesh.h"
#include "shader.h"
#include "transform.h"
#include "camera.h"
#define WIDTH 1280
#define HEIGHT 700
#define TITLE "Game Engine"
#define FRAME_CAP 5000.0f
class Game
{
......@@ -28,9 +34,10 @@ public:
virtual ~Game();
private:
Mesh* mesh;
Shader* shader;
Transform* transform;
float counter;
Mesh* m_mesh;
Shader* m_shader;
Transform* m_transform;
Camera* m_camera;
float m_counter;
};
......@@ -13,7 +13,7 @@ public:
m_scale(scale) {}
//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 scaleMatrix = glm::scale(m_scale);
glm::mat4 rotXMatrix = glm::rotate(m_rot.x, glm::vec3(1, 0, 0));
......@@ -25,17 +25,16 @@ public:
//scale first, then rotation and then position transform
return posMatrix * rotMatrix * scaleMatrix;
}
//LOL Sadman is gay
//getters, returns by reference
inline glm::vec3& GetPos() { return m_pos; }
inline glm::vec3& GetRot() { return m_rot; }
inline glm::vec3& GetScale() { return m_scale; }
inline glm::vec3& getPos() { return m_pos; }
inline glm::vec3& getRot() { return m_rot; }
inline glm::vec3& getScale() { return m_scale; }
//setters
inline void SetPos(const glm::vec3& pos) { m_pos = pos; }
inline void SetRot(const glm::vec3& rot) { m_rot = rot; }
inline void SetScale(const glm::vec3& scale) { m_scale = scale; }
inline void setPos(const glm::vec3& pos) { m_pos = pos; }
inline void setRot(const glm::vec3& rot) { m_rot = rot; }
inline void setScale(const glm::vec3& scale) { m_scale = scale; }
virtual ~Transform() {};
......
......@@ -19,7 +19,7 @@ GNU General Public License for more details.
#include <cassert>
//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);
......@@ -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_DOUBLEBUFFER, 1);
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);
......
......@@ -27,6 +27,7 @@ public:
static void clear();
static void initGraphics(float r, float g, float b, float a);
inline bool isClosed() { return m_isClosed; }
//inline static float getAspect() { return (float)m_width / (float)m_height; }
void update();
//dtor
......@@ -36,4 +37,5 @@ private:
SDL_Window* m_window;
SDL_GLContext m_context;
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