Mostly functional mouse look
Walking doesn't take it into account though...
This commit is contained in:
parent
511b2da319
commit
a262b60def
@ -7,7 +7,7 @@ find_package(OpenGL REQUIRED)
|
||||
find_package(glfw3 REQUIRED)
|
||||
find_package(glm REQUIRED)
|
||||
|
||||
add_executable(ecsgame Util/glad.c Util/OBJ_Loader.h Rendering/Shader.cpp Rendering/Shader.h main.cpp ECS/Components/Transform.h ECS/Components/Movement.h ECS/Events/InputEvent.h ECS/Systems/GravitySystem.h ECS/Systems/PositionDebugSystem.h ECS/Systems/KeyboardMovementSystem.h ECS/Components/Camera.h ECS/Systems/RenderSystem.h ECS/Components/Mesh.h)
|
||||
add_executable(ecsgame Util/glad.c Util/OBJ_Loader.h Rendering/Shader.cpp Rendering/Shader.h main.cpp ECS/Components/Transform.h ECS/Components/Movement.h ECS/Events/InputEvent.h ECS/Events/MouseMoveEvent.h ECS/Systems/GravitySystem.h ECS/Systems/PositionDebugSystem.h ECS/Systems/KeyboardMovementSystem.h ECS/Components/Camera.h ECS/Systems/RenderSystem.h ECS/Components/Mesh.h ECS/Systems/MouseLookSystem.h ECS/Components/MouseLook.h)
|
||||
|
||||
include_directories(${OPENGL_INCLUDE_DIRS})
|
||||
|
||||
|
@ -14,6 +14,8 @@ struct Camera {
|
||||
glm::perspective(glm::radians(fov), width / height, near, far)) {}
|
||||
|
||||
glm::mat4 projection;
|
||||
|
||||
glm::mat4 view;
|
||||
};
|
||||
|
||||
#endif //ECSGAME_CAMERA_H
|
||||
|
14
ECS/Components/MouseLook.h
Normal file
14
ECS/Components/MouseLook.h
Normal file
@ -0,0 +1,14 @@
|
||||
//
|
||||
// Created by karl on 07.01.20.
|
||||
//
|
||||
|
||||
#ifndef ECSGAME_MOUSELOOK_H
|
||||
#define ECSGAME_MOUSELOOK_H
|
||||
|
||||
struct MouseLook {
|
||||
explicit MouseLook(float sensitivity) : sensitivity(sensitivity) {}
|
||||
|
||||
float sensitivity;
|
||||
};
|
||||
|
||||
#endif //ECSGAME_MOUSELOOK_H
|
@ -33,6 +33,18 @@ struct Transform {
|
||||
glm::vec3 getPosition() {
|
||||
return matrix * glm::vec4(0.0f, 0.0f, 0.0f, 1.0f);
|
||||
}
|
||||
|
||||
glm::vec3 forward() {
|
||||
return matrix * glm::vec4(0.0, 0.0, -1.0, 1.0);
|
||||
}
|
||||
|
||||
glm::vec3 up() {
|
||||
return matrix * glm::vec4(0.0, 1.0, 0.0, 1.0);
|
||||
}
|
||||
|
||||
glm::vec3 right() {
|
||||
return matrix * glm::vec4(1.0, 0.0, 0.0, 1.0);
|
||||
}
|
||||
};
|
||||
|
||||
#endif //ECSGAME_TRANSFORM_H
|
||||
|
13
ECS/Events/MouseMoveEvent.h
Normal file
13
ECS/Events/MouseMoveEvent.h
Normal file
@ -0,0 +1,13 @@
|
||||
//
|
||||
// Created by karl on 04.01.20.
|
||||
//
|
||||
|
||||
#ifndef ECSGAME_MOUSEMOVEEVENT_H
|
||||
#define ECSGAME_MOUSEMOVEEVENT_H
|
||||
|
||||
struct MouseMoveEvent {
|
||||
double newX;
|
||||
double newY;
|
||||
};
|
||||
|
||||
#endif //ECSGAME_INPUTEVENT_H
|
@ -64,7 +64,7 @@ class KeyboardMovementSystem : public EntitySystem, public EventSubscriber<Input
|
||||
void tick(World *pWorld, float deltaTime) override {
|
||||
pWorld->each<Transform, Movement>(
|
||||
[&](Entity *ent, ComponentHandle<Transform> transform, ComponentHandle<Movement> movement) {
|
||||
transform->translate(glm::vec3(movement->moving) * movement->speed * deltaTime);
|
||||
transform->translate(glm::mat3x3(transform->matrix) * glm::vec3(movement->moving) * movement->speed * deltaTime);
|
||||
});
|
||||
}
|
||||
|
||||
|
73
ECS/Systems/MouseLookSystem.h
Normal file
73
ECS/Systems/MouseLookSystem.h
Normal file
@ -0,0 +1,73 @@
|
||||
//
|
||||
// Created by karl on 07.01.20.
|
||||
//
|
||||
|
||||
#ifndef ECSGAME_MOUSELOOKSYSTEM_H
|
||||
#define ECSGAME_MOUSELOOKSYSTEM_H
|
||||
|
||||
#include "../ECS.h"
|
||||
#include "../Components/Transform.h"
|
||||
#include "../Components/MouseLook.h"
|
||||
#include "../Events/MouseMoveEvent.h"
|
||||
|
||||
using namespace ECS;
|
||||
|
||||
class MouseLookSystem : public EntitySystem, public EventSubscriber<MouseMoveEvent> {
|
||||
public:
|
||||
explicit MouseLookSystem(int width, int height) : lastX(width / 2.0), lastY(height / 2.0) {}
|
||||
|
||||
void configure(World *pWorld) override {
|
||||
myWorld = pWorld;
|
||||
|
||||
myWorld->subscribe<MouseMoveEvent>(this);
|
||||
}
|
||||
|
||||
void unconfigure(World *pWorld) override {
|
||||
pWorld->unsubscribeAll(this);
|
||||
}
|
||||
|
||||
void receive(World *pWorld, const MouseMoveEvent &event) override {
|
||||
pWorld->each<Transform, MouseLook, Camera>([&](Entity *ent, ComponentHandle<Transform> transform, ComponentHandle<MouseLook> mouse, ComponentHandle<Camera> camera) {
|
||||
double xOffset = event.newX - lastX;
|
||||
double yOffset = lastY - event.newY;
|
||||
|
||||
lastX = event.newX;
|
||||
lastY = event.newY;
|
||||
|
||||
yaw += xOffset * mouse->sensitivity;
|
||||
pitch += yOffset * mouse->sensitivity;
|
||||
});
|
||||
}
|
||||
|
||||
void tick(World *pWorld, float deltaTime) override {
|
||||
pWorld->each<Transform, MouseLook, Camera>([&](Entity *ent, ComponentHandle<Transform> transform, ComponentHandle<MouseLook> mouse, ComponentHandle<Camera> camera) {
|
||||
if(pitch > 89.0f)
|
||||
pitch = 89.0f;
|
||||
if(pitch < -89.0f)
|
||||
pitch = -89.0f;
|
||||
|
||||
glm::vec3 direction;
|
||||
direction.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
|
||||
direction.y = sin(glm::radians(pitch));
|
||||
direction.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch));
|
||||
|
||||
glm::vec3 cameraOrigin = transform->getPosition();
|
||||
glm::vec3 cameraFront = glm::normalize(direction);
|
||||
glm::vec3 cameraUp = glm::vec3(0.0, 1.0, 0.0);
|
||||
glm::vec3 cameraRight = glm::cross(cameraFront, cameraUp);
|
||||
|
||||
camera->view = glm::lookAt(cameraOrigin, cameraOrigin + cameraFront, cameraUp);
|
||||
});
|
||||
}
|
||||
|
||||
private:
|
||||
double lastX;
|
||||
double lastY;
|
||||
|
||||
double pitch = 0.0;
|
||||
double yaw = 0.0;
|
||||
|
||||
World *myWorld;
|
||||
};
|
||||
|
||||
#endif //ECSGAME_MOUSELOOKSYSTEM_H
|
@ -23,7 +23,7 @@ public:
|
||||
shader.use();
|
||||
|
||||
shader.setMat4("projection", camera->projection);
|
||||
shader.setMat4("view", cameraTransform->matrix);
|
||||
shader.setMat4("view", camera->view);
|
||||
|
||||
pWorld->each<Mesh, Transform>([&](Entity *ent, ComponentHandle<Mesh> mesh, ComponentHandle<Transform> transform) {
|
||||
shader.setMat4("model", transform->matrix);
|
||||
|
18
main.cpp
18
main.cpp
@ -6,10 +6,12 @@
|
||||
#include "Rendering/Shader.h"
|
||||
#include "ECS/ECS.h"
|
||||
#include "ECS/Events/InputEvent.h"
|
||||
#include "ECS/Events/MouseMoveEvent.h"
|
||||
#include "ECS/Systems/GravitySystem.h"
|
||||
#include "ECS/Systems/PositionDebugSystem.h"
|
||||
#include "ECS/Systems/KeyboardMovementSystem.h"
|
||||
#include "ECS/Systems/RenderSystem.h"
|
||||
#include "ECS/Systems/MouseLookSystem.h"
|
||||
|
||||
using namespace ECS;
|
||||
|
||||
@ -24,6 +26,10 @@ static void key_callback(GLFWwindow *window, int key, int scancode, int action,
|
||||
world->emit<InputEvent>({key, action});
|
||||
}
|
||||
|
||||
static void mouse_callback(GLFWwindow* window, double xpos, double ypos) {
|
||||
world->emit<MouseMoveEvent>({xpos, ypos});
|
||||
}
|
||||
|
||||
int main() {
|
||||
GLFWwindow *window;
|
||||
|
||||
@ -41,6 +47,10 @@ int main() {
|
||||
/* Make the window's context current */
|
||||
glfwMakeContextCurrent(window);
|
||||
|
||||
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
||||
|
||||
glfwSetCursorPosCallback(window, mouse_callback);
|
||||
|
||||
glfwSetKeyCallback(window, key_callback);
|
||||
|
||||
// glad: load all OpenGL function pointers
|
||||
@ -55,8 +65,9 @@ int main() {
|
||||
|
||||
// TODO: Could be automated by getting all classes within 'ECS/Systems'
|
||||
// world->registerSystem(new GravitySystem(-9.8f));
|
||||
world->registerSystem(new PositionDebugOutputSystem());
|
||||
// world->registerSystem(new PositionDebugOutputSystem());
|
||||
world->registerSystem(new KeyboardMovementSystem());
|
||||
world->registerSystem(new MouseLookSystem(640, 480));
|
||||
|
||||
RenderSystem* renderSystem = new RenderSystem();
|
||||
world->registerSystem(renderSystem);
|
||||
@ -65,6 +76,7 @@ int main() {
|
||||
player->assign<Transform>();
|
||||
player->assign<Movement>(glm::vec3(1.f, 1.f, 1.f));
|
||||
player->assign<Camera>(70.0f, 640, 480, 0.1f, 100.0f);
|
||||
player->assign<MouseLook>(0.1);
|
||||
|
||||
Entity *box = world->create();
|
||||
box->assign<Transform>();
|
||||
@ -111,7 +123,7 @@ int main() {
|
||||
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f,
|
||||
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f
|
||||
});
|
||||
box->get<Transform>()->translate(glm::vec3(0.0f, 0.0f, -10.0f));
|
||||
box->get<Transform>()->translate(glm::vec3(0.0f, 0.0f, -5.0f));
|
||||
|
||||
Shader defaultShader("Shaders/default-vertex.vs", "Shaders/default-fragment.fs");
|
||||
|
||||
@ -125,8 +137,6 @@ int main() {
|
||||
timeInLastFrame = glfwGetTime();
|
||||
elapsed_time += delta;
|
||||
|
||||
std::cout << "Elapsed time: " << elapsed_time << std::endl;
|
||||
|
||||
world->tick(delta);
|
||||
renderSystem->render(world, defaultShader);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user