, cam()
, hud()
, world()
+, controller(world.Player())
, outline()
, outline_visible(false)
, outline_transform(1.0f)
switch (event.type) {
case SDL_KEYDOWN:
case SDL_KEYUP:
- world.Controller().HandleKeyboard(event.key);
+ controller.HandleKeyboard(event.key);
break;
case SDL_MOUSEBUTTONDOWN:
if (event.button.button == 1) {
}
break;
case SDL_MOUSEMOTION:
- world.Controller().HandleMouse(event.motion);
+ controller.HandleMouse(event.motion);
break;
case SDL_QUIT:
running = false;
}
void Application::Update(int dt) {
+ controller.Update(dt);
world.Update(dt);
- Ray aim = world.Controller().Aim();
+ Ray aim = controller.Aim();
Chunk *chunk;
int blkid;
float dist;
Camera cam;
HUD hud;
World world;
+ FPSController controller;
OutlineModel outline;
bool outline_visible;
#include "controller.hpp"
-#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtx/euler_angles.hpp>
#include <glm/gtx/rotate_vector.hpp>
-#include <glm/gtx/transform.hpp>
namespace blank {
-FPSController::FPSController()
-: velocity(0, 0, 0)
-, position(0, 0, 0)
+FPSController::FPSController(Entity &entity)
+: entity(entity)
, pitch(0)
, yaw(0)
-, transform(1.0f)
-, dirty(true)
, move_velocity(0.003f)
, pitch_sensitivity(-0.0025f)
, yaw_sensitivity(-0.001f)
}
-const glm::mat4 &FPSController::Transform() const {
- if (dirty) {
- transform = glm::translate(position) * glm::eulerAngleYX(yaw, pitch);
- dirty = false;
- }
- return transform;
-}
-
-Ray FPSController::Aim() const {
- glm::vec4 from = Transform() * glm::vec4(0.0f, 0.0f, 1.0f, 1.0f);
- from /= from.w;
- glm::vec4 to = Transform() * glm::vec4(0.0f, 0.0f, -1.0f, 1.0f);
- to /= to.w;
- return Ray{ glm::vec3(from), glm::normalize(glm::vec3(to - from)) };
-}
-
-
-void FPSController::OrientationVelocity(const glm::vec3 &vel) {
- Velocity(glm::rotateY(vel, yaw));
-}
-
-
void FPSController::Pitch(float p) {
pitch = p;
if (pitch > PI / 2) {
} else if (pitch < -PI / 2) {
pitch = -PI / 2;
}
- dirty = true;
}
void FPSController::RotatePitch(float delta) {
} else if (yaw < -PI) {
yaw += PI * 2;
}
- dirty = true;
}
void FPSController::RotateYaw(float delta) {
} else if (front && !back) {
vel.z = -move_velocity;
}
- OrientationVelocity(vel);
-
- Move(velocity * float(dt));
+ entity.Rotation(glm::eulerAngleYX(yaw, pitch));
+ entity.Velocity(glm::rotateY(vel, yaw));
}
}
#ifndef BLANK_CONTROLLER_HPP_
#define BLANK_CONTROLLER_HPP_
+#include "entity.hpp"
#include "geometry.hpp"
#include <SDL.h>
class FPSController {
public:
- FPSController();
+ explicit FPSController(Entity &);
- const glm::mat4 &Transform() const;
- Ray Aim() const;
-
- void Velocity(glm::vec3 vel) { velocity = vel; dirty = true; }
- void OrientationVelocity(const glm::vec3 &vel);
- void Position(glm::vec3 pos) { position = pos; dirty = true; }
- void Move(glm::vec3 delta) { Position(position + delta); }
+ Ray Aim() const { return entity.Aim(); }
// all angles in radians (full circle = 2π)
float Pitch() const { return pitch; }
void Update(int dt);
private:
- glm::vec3 velocity;
- glm::vec3 position;
+ Entity &entity;
+
float pitch;
float yaw;
- mutable glm::mat4 transform;
- mutable bool dirty;
-
float move_velocity;
float pitch_sensitivity;
float yaw_sensitivity;
--- /dev/null
+#include "entity.hpp"
+
+#include <glm/gtx/transform.hpp>
+
+
+namespace blank {
+
+Entity::Entity()
+: velocity()
+, position()
+, rotation(1.0f)
+, transform(1.0f)
+, dirty(false) {
+
+}
+
+
+void Entity::Velocity(const glm::vec3 &vel) {
+ velocity = vel;
+}
+
+void Entity::Position(const glm::vec3 &pos) {
+ position = pos;
+ dirty = true;
+}
+
+void Entity::Move(const glm::vec3 &delta) {
+ position += delta;
+ dirty = true;
+}
+
+void Entity::Rotation(const glm::mat4 &rot) {
+ rotation = rot;
+}
+
+const glm::mat4 &Entity::Transform() const {
+ if (dirty) {
+ transform = glm::translate(position) * rotation;
+ dirty = false;
+ }
+ return transform;
+}
+
+Ray Entity::Aim() const {
+ Transform();
+ glm::vec4 from = transform * glm::vec4(0.0f, 0.0f, 1.0f, 1.0f);
+ from /= from.w;
+ glm::vec4 to = transform * glm::vec4(0.0f, 0.0f, -1.0f, 1.0f);
+ to /= to.w;
+ return Ray{ glm::vec3(from), glm::normalize(glm::vec3(to - from)) };
+}
+
+void Entity::Update(int dt) {
+ Move(velocity * float(dt));
+}
+
+}
--- /dev/null
+#ifndef BLANK_ENTITY_HPP_
+#define BLANK_ENTITY_HPP_
+
+#include "geometry.hpp"
+
+#include <glm/glm.hpp>
+
+
+namespace blank {
+
+class Entity {
+
+public:
+ Entity();
+
+ const glm::vec3 &Velocity() const { return velocity; }
+ void Velocity(const glm::vec3 &);
+
+ const glm::vec3 &Position() const { return position; }
+ void Position(const glm::vec3 &);
+ void Move(const glm::vec3 &delta);
+
+ const glm::mat4 &Rotation() const { return rotation; }
+ void Rotation(const glm::mat4 &);
+
+ const glm::mat4 &Transform() const;
+ Ray Aim() const;
+
+ void Update(int dt);
+
+private:
+ glm::vec3 velocity;
+ glm::vec3 position;
+
+ glm::mat4 rotation;
+
+ mutable glm::mat4 transform;
+ mutable bool dirty;
+
+};
+
+}
+
+#endif
, slabShape({{ -0.5f, -0.5f, -0.5f }, { 0.5f, 0.0f, 0.5f }})
, blockNoise(0)
, colorNoise(1)
+, player()
, loaded()
, to_generate() {
blockType.Add(BlockType{ true, { 1.0f, 1.0f, 1.0f }, &blockShape }); // white block
#include "block.hpp"
#include "chunk.hpp"
-#include "controller.hpp"
+#include "entity.hpp"
#include "noise.hpp"
#include "shader.hpp"
#include "shape.hpp"
BlockTypeRegistry &BlockTypes() { return blockType; }
std::list<Chunk> &LoadedChunks() { return loaded; }
- FPSController &Controller() { return player; }
+ Entity &Player() { return player; }
Chunk &Next(const Chunk &, const glm::vec3 &dir);
SimplexNoise blockNoise;
SimplexNoise colorNoise;
- FPSController player;
+ Entity player;
std::list<Chunk> loaded;
std::list<Chunk> to_generate;