Browse Source

Remove old camera class

master
Riyyi 4 years ago
parent
commit
aba518bbbb
  1. 16
      inferno/src/inferno/application.cpp
  2. 5
      inferno/src/inferno/application.h
  3. 226
      inferno/src/inferno/render/camera.cpp
  4. 87
      inferno/src/inferno/render/camera.h
  5. 8
      inferno/src/inferno/render/renderer.cpp
  6. 4
      inferno/src/inferno/render/renderer.h

16
inferno/src/inferno/application.cpp

@ -10,12 +10,12 @@
#include "inferno/inputcodes.h"
#include "inferno/log.h"
#include "inferno/render/buffer.h"
#include "inferno/render/camera.h"
#include "inferno/render/context.h"
#include "inferno/render/font.h"
#include "inferno/render/renderer.h"
#include "inferno/render/shader.h"
#include "inferno/render/texture.h"
#include "inferno/scene/scene.h"
#include "inferno/settings.h"
#include "inferno/time.h"
#include "inferno/window.h"
@ -44,8 +44,7 @@ namespace Inferno {
TextureManager* textureManager = new TextureManager();
textureManager->initialize();
m_cameraO = std::make_shared<OrthographicCamera>();
m_cameraP = std::make_shared<PerspectiveCamera>();
m_scene = std::make_shared<Scene>();
Renderer2D* renderer2D = new Renderer2D();
renderer2D->initialize();
@ -69,6 +68,7 @@ namespace Inferno {
FontManager::the().destroy();
RendererCharacter::the().destroy();
Renderer2D::the().destroy();
m_scene->destroy();
TextureManager::the().destroy();
ShaderManager::the().destroy();
// Input::destroy();
@ -107,7 +107,7 @@ namespace Inferno {
auto f = FontManager::the().get("assets/fnt/dejavu-sans");
auto c = f->get('5');
dbg() << c->position << " " << c->size;
// dbg() << c->position << " " << c->size;
uint32_t textureWidth = f->texture()->width();
uint32_t textureHeight = f->texture()->height();
@ -129,8 +129,7 @@ namespace Inferno {
(textureHeight - c->position.y - c->size.y) / (float)textureHeight,
(textureHeight - c->position.y) / (float)textureHeight
};
dbg() << y;
// dbg() < y;
character.at(0).quad.textureCoordinates = { x.x, y.x };
character.at(1).quad.textureCoordinates = { x.y, y.x };
@ -163,15 +162,14 @@ namespace Inferno {
Input::update();
m_window->update();
m_cameraO->update(deltaTime);
m_cameraP->update(deltaTime);
m_scene->update(deltaTime);
// Render
RenderCommand::clearColor({ 0.2f, 0.3f, 0.3f, 1.0f });
RenderCommand::clear();
Renderer2D::the().beginScene(m_cameraP); // camera, lights, environment
Renderer2D::the().beginScene(m_scene->cameraProjectionView()); // camera, lights, environment
RendererCharacter::the().beginScene();
Renderer2D::the().drawQuad(std::make_shared<Transform>(cube), colors);

5
inferno/src/inferno/application.h

@ -11,6 +11,7 @@ namespace Inferno {
class MousePositionEvent;
class OrthographicCamera;
class PerspectiveCamera;
class Scene;
class Texture;
class TextureManager;
class Window;
@ -41,9 +42,7 @@ namespace Inferno {
private:
std::unique_ptr<Window> m_window;
// std::unique_ptr<Camera> m_camera;
std::shared_ptr<OrthographicCamera> m_cameraO;
std::shared_ptr<PerspectiveCamera> m_cameraP;
std::shared_ptr<Scene> m_scene;
float m_lastFrameTime = 0.0f;

226
inferno/src/inferno/render/camera.cpp

@ -1,226 +0,0 @@
#include <algorithm> // std::max, std::min
#include <memory> // std::make_shared
#include <glm/ext/matrix_clip_space.hpp> // glm::perspective, glm::ortho
#include <glm/ext/matrix_transform.hpp> // glm::radians, glm::lookAt
#include "inferno/application.h"
#include "inferno/component/transform.h"
#include "inferno/input.h"
#include "inferno/inputcodes.h"
#include "inferno/render/camera.h"
#include "inferno/window.h"
namespace Inferno {
Camera::Camera()
: Camera({0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f})
{
}
Camera::Camera(glm::vec3 translate, glm::vec3 rotate)
{
glm::vec3 scale = {1.0f, 1.0f, 1.0f};
m_transform = std::make_shared<Transform>(translate, rotate, scale);
}
// ----------------------------------------
OrthographicCamera::OrthographicCamera()
: Camera({0.0f, 0.0f, -1.0f}, {0.0f, 0.0f, 1.0f})
{
initialize();
}
OrthographicCamera::OrthographicCamera(glm::vec3 translate, glm::vec3 rotate)
: Camera(translate, rotate)
{
initialize();
}
void OrthographicCamera::initialize()
{
m_zoomLevel = 1.0f;
// Set the roatation axis
m_rotateAxis = glm::vec3(0.0f, 0.0f, 1.0f);
dbg(Log::Info) << "OrthographicCamera initialized";
}
void OrthographicCamera::update(float deltaTime)
{
// Update camera rotation
float cameraRotateSpeed = ROTATE_SPEED * deltaTime;
glm::vec3 rotate = transform()->rotate();
if (Input::isKeyPressed(KeyCode("GLFW_KEY_Q"))) {
rotate.z += cameraRotateSpeed;
}
if (Input::isKeyPressed(KeyCode("GLFW_KEY_E"))) {
rotate.z -= cameraRotateSpeed;
}
if (rotate.z > 180.0f)
rotate.z -= 360.0f;
else if (rotate.z <= -180.0f)
rotate.z += 360.0f;
transform()->setRotate(rotate);
// Update camera translation
float cameraTranslateSpeed = TRANSLATE_SPEED * deltaTime;
glm::vec3 translate = transform()->translate();
// WASD movement
if (Input::isKeyPressed(KeyCode("GLFW_KEY_W"))) {
translate.x += -sin(glm::radians(rotate.z)) * cameraTranslateSpeed;
translate.y += cos(glm::radians(rotate.z)) * cameraTranslateSpeed;
}
if (Input::isKeyPressed(KeyCode("GLFW_KEY_S"))) {
translate.x -= -sin(glm::radians(rotate.z)) * cameraTranslateSpeed;
translate.y -= cos(glm::radians(rotate.z)) * cameraTranslateSpeed;
}
if (Input::isKeyPressed(KeyCode("GLFW_KEY_A"))) {
translate.x -= cos(glm::radians(rotate.z)) * cameraTranslateSpeed;
translate.y -= sin(glm::radians(rotate.z)) * cameraTranslateSpeed;
}
if (Input::isKeyPressed(KeyCode("GLFW_KEY_D"))) {
translate.x += cos(glm::radians(rotate.z)) * cameraTranslateSpeed;
translate.y += sin(glm::radians(rotate.z)) * cameraTranslateSpeed;
}
transform()->setTranslate(translate);
// Update camera zoom
float zoomSpeed = ZOOM_SENSITIVITY * deltaTime;
if (Input::isKeyPressed(KeyCode("GLFW_KEY_EQUAL"))) {
m_zoomLevel -= zoomSpeed;
}
if (Input::isKeyPressed(KeyCode("GLFW_KEY_MINUS"))) {
m_zoomLevel += zoomSpeed;
}
m_zoomLevel = std::max(m_zoomLevel, 0.25f);
m_zoomLevel = std::min(m_zoomLevel, 10.0f);
// Update camera matrix
// Local space -> World space: model matrix
// Is done in Object::update()
// World space -> View space: view matrix
transform()->setTransform(glm::translate(glm::mat4(1.0f), translate) * glm::rotate(glm::mat4(1.0f), glm::radians(rotate.z), m_rotateAxis));
transform()->setTransform(glm::inverse(transform()->transform()));
// View space -> Clip space: projection matrix
float aspectRatio = Application::the().getWindow().getAspect();
setProjection(glm::ortho(-aspectRatio * m_zoomLevel, aspectRatio * m_zoomLevel, -m_zoomLevel, m_zoomLevel, -1.0f, 1.0f));
// Clip space -> Screen space: viewport transform
// Is done in the fragment shader using the settings of glViewport
}
// ----------------------------------------
PerspectiveCamera::PerspectiveCamera()
: Camera({0.0f, 0.0f, 1.0f}, {0.0f, 0.0f, -1.0f})
{
initialize();
}
PerspectiveCamera::PerspectiveCamera(glm::vec3 translate, glm::vec3 rotate)
: Camera(translate, rotate)
{
initialize();
}
void PerspectiveCamera::initialize()
{
m_fov = 90.0f;
m_pitch = 0.0f;
m_yaw = -90.0f;
// Up vector in world space
m_up = glm::vec3(0.0f, 1.0f, 0.0f);
dbg(Log::Info) << "PerspectiveCamera initialized";
}
void PerspectiveCamera::update(float deltaTime)
{
// Get mouse movement offset compared to last frame
float xOffset = Input::getXOffset() * MOUSE_SENSITIVITY;
float yOffset = Input::getYOffset() * MOUSE_SENSITIVITY;
m_yaw += xOffset;
m_pitch += yOffset;
// Prevent gimbal lock
if (m_pitch > 89.0f) m_pitch = 89.0f;
if (m_pitch < -89.0f) m_pitch = -89.0f;
// Update camera rotation, by calculating direction vector via yaw and pitch
transform()->setRotate(
cos(glm::radians(m_pitch)) * cos(glm::radians(m_yaw)),
sin(glm::radians(m_pitch)),
cos(glm::radians(m_pitch)) * sin(glm::radians(m_yaw)));
transform()->setRotate(glm::normalize(transform()->rotate()));
// The direction vector is based on
// Camera direction (z): normalize(position - target)
// Right axis (x): normalize(cross(up, direction))
// Up axis (y): cross(direction, right)
// Source: https://learnopengl.com/img/getting-started/camera_axes.png
// Cross = combination of two vectors in 3D space,
// where result is always perpendicular to both of the vectors
// Update camera translation
float cameraSpeed = TRANSLATE_SPEED * deltaTime;
// WASD movement
if (Input::isKeyPressed(KeyCode("GLFW_KEY_W"))) {
transform()->setTranslate(transform()->translate() + cameraSpeed * transform()->rotate());
}
if (Input::isKeyPressed(KeyCode("GLFW_KEY_S"))) {
transform()->setTranslate(transform()->translate() - cameraSpeed * transform()->rotate());
}
if (Input::isKeyPressed(KeyCode("GLFW_KEY_A"))) {
transform()->setTranslate(transform()->translate() -
glm::normalize(glm::cross(transform()->rotate(), m_up)) * cameraSpeed);
}
if (Input::isKeyPressed(KeyCode("GLFW_KEY_D"))) {
transform()->setTranslate(transform()->translate() +
glm::normalize(glm::cross(transform()->rotate(), m_up)) * cameraSpeed);
}
// Up / down movement
if (Input::isKeyPressed(KeyCode("GLFW_KEY_SPACE"))) {
transform()->setTranslate(transform()->translate().x, transform()->translate().y + cameraSpeed, transform()->translate().z);
}
if (Input::isKeyPressed(KeyCode("GLFW_KEY_LEFT_SHIFT"))) {
transform()->setTranslate(transform()->translate().x, transform()->translate().y - cameraSpeed, transform()->translate().z);
}
// Update camera matrix
// Local space -> World space: model matrix
// Is done in Object::update()
// World space -> View space: view matrix
transform()->setTransform(glm::lookAt(transform()->translate(), transform()->translate() + transform()->rotate(), m_up));
// View space -> Clip space: projection matrix
float aspect = Application::the().getWindow().getAspect();
setProjection(glm::perspective(glm::radians(m_fov), aspect, NEAR_PLANE, FAR_PLANE));
// Clip space -> Screen space: viewport transform
// Is done in the fragment shader using the settings of glViewport
// Souce: https://learnopengl.com/img/getting-started/coordinate_systems.png
}
}

87
inferno/src/inferno/render/camera.h

@ -1,87 +0,0 @@
#ifndef CAMERA_H
#define CAMERA_H
#define TRANSLATE_SPEED 2.5f
#define ROTATE_SPEED 90.0f
#define ZOOM_SENSITIVITY 2.5f
#define MOUSE_SENSITIVITY 0.25f
#define NEAR_PLANE 0.1f
#define FAR_PLANE 100.0f
#include <memory> // std::shared_ptr
#include "glm/ext/matrix_float4x4.hpp" // glm::mat4
#include "glm/ext/vector_float3.hpp" // glm:vec3
namespace Inferno {
class Transform;
class Camera {
public:
Camera();
Camera(glm::vec3 translate, glm::vec3 rotate);
virtual ~Camera() {}
virtual void initialize() = 0;
virtual void update(float deltaTime) = 0;
// virtual void render() = 0;
// virtual void destroy() = 0;
void setProjection(glm::mat4 projection) { m_projection = projection; }
const glm::mat4& projection() const { return m_projection; }
const std::shared_ptr<Transform>& transform() const { return m_transform; }
private:
glm::mat4 m_projection;
std::shared_ptr<Transform> m_transform;
};
// ----------------------------------------
class OrthographicCamera final : public Camera {
public:
OrthographicCamera();
OrthographicCamera(glm::vec3 translate, glm::vec3 rotate);
virtual ~OrthographicCamera() {}
virtual void initialize() override;
virtual void update(float deltaTime) override;
// virtual void render() override;
// virtual void destroy() override;
private:
float m_zoomLevel;
glm::vec3 m_rotateAxis;
};
// ----------------------------------------
class PerspectiveCamera final : public Camera {
public:
PerspectiveCamera();
PerspectiveCamera(glm::vec3 translate, glm::vec3 rotate);
virtual ~PerspectiveCamera() {}
virtual void initialize() override;
virtual void update(float deltaTime) override;
// virtual void render() override;
// virtual void destroy() override;
private:
float m_fov;
float m_pitch;
float m_yaw;
glm::vec3 m_up;
};
// ----------------------------------------
}
#endif // CAMERA_H
// @Todo:
// - Add sensitivity, fov to settings:camera
// - Change ortho view matrix to use glm::lookAt()

8
inferno/src/inferno/render/renderer.cpp

@ -6,7 +6,6 @@
#include "inferno/assertions.h"
#include "inferno/component/transform.h"
#include "inferno/render/buffer.h"
#include "inferno/render/camera.h"
#include "inferno/render/renderer.h"
#include "inferno/render/shader.h"
#include "inferno/render/texture.h"
@ -185,9 +184,11 @@ namespace Inferno {
s_instance = nullptr;
}
void Renderer2D::beginScene(const std::shared_ptr<Camera>& camera)
void Renderer2D::beginScene(glm::mat4 cameraProjectionView)
{
s_camera = camera;
m_shader->bind();
m_shader->setFloat("u_projectionView", cameraProjectionView);
m_shader->unbind();
}
void Renderer2D::endScene()
@ -254,7 +255,6 @@ namespace Inferno {
bind();
// Render
m_shader->setFloat("u_projectionView", s_camera->projection() * s_camera->transform()->transform());
RenderCommand::drawIndexed(m_vertexArray, m_quadIndex * indexPerQuad);
unbind();

4
inferno/src/inferno/render/renderer.h

@ -92,7 +92,7 @@ namespace Inferno {
void initialize() override;
void destroy() override;
void beginScene(const std::shared_ptr<Camera>& camera);
void beginScene(glm::mat4 cameraProjectionView);
void endScene();
void drawQuad(std::shared_ptr<Transform> transform, glm::vec4 color);
@ -108,8 +108,6 @@ namespace Inferno {
void startBatch() override;
void nextBatch() override;
std::shared_ptr<Camera> s_camera;
// CPU quad vertices
std::unique_ptr<QuadVertex[]> m_vertexBufferBase = nullptr;
QuadVertex* m_vertexBufferPtr = nullptr;

Loading…
Cancel
Save