Skip to content

Commit

Permalink
Merge branch 'camera-input-rework'
Browse files Browse the repository at this point in the history
  • Loading branch information
BarthPaleologue committed Sep 26, 2023
2 parents ff92f8b + aaf7cfa commit 9d35c96
Show file tree
Hide file tree
Showing 13 changed files with 60 additions and 43 deletions.
4 changes: 4 additions & 0 deletions core/Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ Engine::Engine(int windowWidth, int windowHeight, const char *name = "Feather Pr
glfwSetKeyCallback(window, [](GLFWwindow *window, int key, int scancode, int action, int mods) {
auto *engine = static_cast<Engine *>(glfwGetWindowUserPointer(window));
if (action == GLFW_PRESS) {
engine->_keyStates[key] = true;
engine->onKeyPressObservable.notifyObservers(key);
} else if (action == GLFW_RELEASE) {
engine->_keyStates[key] = false;
engine->onKeyReleaseObservable.notifyObservers(key);
}
});

Expand Down
10 changes: 10 additions & 0 deletions core/Engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#define FEATHERGL_ENGINE_H

#include "Observable.h"
#include <GLFW/glfw3.h>
#include <map>

class Engine {
public:
Expand All @@ -28,6 +30,10 @@ class Engine {
return glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS;
}

bool isKeyPressed(int key) const {
return _keyStates.find(key) != _keyStates.end() && _keyStates.at(key);
}

GLFWwindow *getWindow() const {
return window;
}
Expand All @@ -39,12 +45,16 @@ class Engine {
~Engine();

Observable<int> onKeyPressObservable{};
Observable<int> onKeyReleaseObservable{};
Observable<double, double> onMouseScrollObservable{};
Observable<double, double> onMouseMoveObservable{};

private:
double mouseX{};
double mouseY{};

std::map<int, bool> _keyStates{};

bool _isWireframeEnabled = false;
GLFWwindow *window;
};
Expand Down
2 changes: 1 addition & 1 deletion core/Transform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

Transform::Transform() {}

void Transform::setPositionFromFloats(float x, float y, float z) {
void Transform::setPosition(float x, float y, float z) {
_position->x = x;
_position->y = y;
_position->z = z;
Expand Down
2 changes: 1 addition & 1 deletion core/Transform.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Transform {

glm::vec3 getLeftDirection();

void setPositionFromFloats(float x, float y, float z);
void setPosition(float x, float y, float z);

void translate(glm::vec3 displacement);

Expand Down
7 changes: 4 additions & 3 deletions core/cameras/Camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

#include "Camera.h"

Camera::Camera(GLFWwindow *window) : Transform(), _window(window) {
Camera::Camera(Engine *engine) : Transform(), _engine(engine) {
int width, height;
glfwGetWindowSize(window, &width, &height);
glfwGetWindowSize(_engine->getWindow(), &width, &height);
setAspectRatio(static_cast<float>(width) / static_cast<float>(height));

setPositionFromFloats(0.0f, 0.0f, 7.0f);
setPosition(0.0f, 0.0f, 7.0f);
setNear(0.1);
setFar(80.1);

Expand All @@ -18,4 +18,5 @@ Camera::Camera(GLFWwindow *window) : Transform(), _window(window) {

void Camera::update() {
_projectionMatrix = computeProjectionMatrix();
_viewMatrix = computeViewMatrix();
}
6 changes: 3 additions & 3 deletions core/cameras/Camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
#define FEATHERGL_CAMERA_H


#include "GLFW/glfw3.h"
#include "Transform.h"
#include "glm/ext.hpp"
#include "Engine.h"


class Camera : public Transform {
public:
explicit Camera(GLFWwindow *window);
explicit Camera(Engine *engine);

inline float getFov() const { return _fov; }

Expand Down Expand Up @@ -45,7 +45,7 @@ class Camera : public Transform {
virtual void update();

protected:
GLFWwindow *_window;
Engine *_engine;
glm::mat4 _viewMatrix{};
glm::mat4 _projectionMatrix{};
float _fov = 45.f; // Field of view, in degrees
Expand Down
20 changes: 10 additions & 10 deletions core/cameras/FreeCamera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@
// Created by barth on 27/09/2022.
//

#include <GLFW/glfw3.h>
#include "FreeCamera.h"

FreeCamera::FreeCamera(GLFWwindow *window) : Camera(window) {}
FreeCamera::FreeCamera(Engine *engine) : Camera(engine) {}

glm::mat4 FreeCamera::computeViewMatrix() {
return glm::lookAt(*_position, *_position + getForwardDirection(), getUpwardDirection());
}

void FreeCamera::update() {
_viewMatrix = computeViewMatrix();
_projectionMatrix = computeProjectionMatrix();

float speed = 0.02f;
if (glfwGetKey(_window, GLFW_KEY_UP)) _position->z -= speed;
if (glfwGetKey(_window, GLFW_KEY_DOWN)) _position->z += speed;
if (glfwGetKey(_window, GLFW_KEY_LEFT)) _position->x -= speed;
if (glfwGetKey(_window, GLFW_KEY_RIGHT)) _position->x += speed;
if (glfwGetKey(_window, GLFW_KEY_SPACE)) _position->y += speed;
if (glfwGetKey(_window, GLFW_KEY_LEFT_SHIFT)) _position->y -= speed;
if (_engine->isKeyPressed(GLFW_KEY_UP)) _position->z -= speed;
if (_engine->isKeyPressed(GLFW_KEY_DOWN)) _position->z += speed;
if (_engine->isKeyPressed(GLFW_KEY_LEFT)) _position->x -= speed;
if (_engine->isKeyPressed(GLFW_KEY_RIGHT)) _position->x += speed;
if (_engine->isKeyPressed(GLFW_KEY_SPACE)) _position->y += speed;
if (_engine->isKeyPressed(GLFW_KEY_LEFT_SHIFT)) _position->y -= speed;

Camera::update();
}
2 changes: 1 addition & 1 deletion core/cameras/FreeCamera.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class FreeCamera : public Camera {
public:
explicit FreeCamera(GLFWwindow *window);
explicit FreeCamera(Engine *engine);

glm::mat4 computeViewMatrix() override;

Expand Down
29 changes: 20 additions & 9 deletions core/cameras/OrbitCamera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,38 @@
#include "OrbitCamera.h"

#include <cmath>
#include <GLFW/glfw3.h>

OrbitCamera::OrbitCamera(GLFWwindow *window) : Camera(window), _target(0.0f), _radius(20.0f), _theta(3.14f / 2.0f),
_phi(0.0f) {}
OrbitCamera::OrbitCamera(Engine *engine) : Camera(engine), _target(0.0f), _radius(20.0f), _theta(3.14f / 2.0f),
_phi(0.0f) {
engine->onMouseScrollObservable.add([this](double xOffset, double yOffset) {
float scrollOffset = (float) yOffset / 5.0f;
this->zoom(scrollOffset);
});

engine->onMouseMoveObservable.add([this](double mouseDX, double mouseDY) {
if (!this->_engine->isMousePressed()) return;
this->rotatePhi(-(float) mouseDX / 200.0f);
this->rotateTheta(-(float) mouseDY / 200.0f);
});
}

glm::mat4 OrbitCamera::computeViewMatrix() {
return glm::lookAt(*_position, _target, getUpwardDirection());
}

void OrbitCamera::update() {
Camera::update();
_viewMatrix = computeViewMatrix();

float speed = 0.02f;
if (glfwGetKey(_window, GLFW_KEY_UP)) _theta = std::max(_theta - speed, 0.14f);
if (glfwGetKey(_window, GLFW_KEY_DOWN)) _theta = std::min(_theta + speed, 3.00f);
if (glfwGetKey(_window, GLFW_KEY_LEFT)) _phi -= speed;
if (glfwGetKey(_window, GLFW_KEY_RIGHT)) _phi += speed;
if (_engine->isKeyPressed(GLFW_KEY_UP)) _theta = std::max(_theta - speed, 0.14f);
if (_engine->isKeyPressed(GLFW_KEY_DOWN)) _theta = std::min(_theta + speed, 3.00f);
if (_engine->isKeyPressed(GLFW_KEY_LEFT)) _phi -= speed;
if (_engine->isKeyPressed(GLFW_KEY_RIGHT)) _phi += speed;

_position->x = _target.x + _radius * std::sin(_theta) * std::sin(_phi);
_position->z = _target.z + _radius * std::sin(_theta) * std::cos(_phi);
_position->y = _target.y + _radius * std::cos(_theta);

Camera::update();
}

void OrbitCamera::zoom(float amount) {
Expand Down
2 changes: 1 addition & 1 deletion core/cameras/OrbitCamera.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class OrbitCamera: public Camera {
public:
explicit OrbitCamera(GLFWwindow *window);
explicit OrbitCamera(Engine *engine);

glm::mat4 computeViewMatrix() override;

Expand Down
2 changes: 1 addition & 1 deletion core/meshes/Cube.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "Cube.h"

Cube::Cube(const char *name, float x, float y, float z) : AbstractMesh(name) {
setPositionFromFloats(x, y, z);
setPosition(x, y, z);
std::vector<float> vertices = {
-1.0f, -1.0f, -1.0f,
1.0f, -1.0f, -1.0f,
Expand Down
2 changes: 1 addition & 1 deletion core/meshes/Triangle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "Triangle.h"

Triangle::Triangle(const char *name, float x, float y, float z) : AbstractMesh(name) {
setPositionFromFloats(x, y, z);
setPosition(x, y, z);
std::vector<GLfloat> points = {
0.0f, 0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
Expand Down
15 changes: 3 additions & 12 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "StandardMaterial.h"
#include "CelestialBody.h"
#include "Engine.h"
#include "FreeCamera.h"

const int WINDOW_WIDTH = 1000;
const int WINDOW_HEIGHT = 600;
Expand All @@ -17,23 +18,13 @@ int main() {
GLFWwindow *window = engine.getWindow();

Scene scene;
OrbitCamera camera(window);
OrbitCamera camera(&engine);
camera.setPosition(0, 0, 20);
camera.setRadius(20.0);
camera.rotateTheta(-3.0f);
scene.setActiveCamera(&camera);
PointLight light("sun");

engine.onMouseScrollObservable.add([&camera](double xOffset, double yOffset) {
float scrollOffset = (float) yOffset / 5.0f;
camera.zoom(scrollOffset);
});

engine.onMouseMoveObservable.add([&camera, &engine](double mouseDX, double mouseDY) {
if (!engine.isMousePressed()) return;
camera.rotatePhi(-(float) mouseDX / 500.0f);
camera.rotateTheta(-(float) mouseDY / 500.0f);
});

StandardMaterial sunMaterial;
Texture sunMap("assets/textures/sun.jpg");
sunMaterial.setAmbientTexture(&sunMap);
Expand Down

0 comments on commit 9d35c96

Please sign in to comment.