]> git.localhorst.tv Git - blank.git/commitdiff
lousy implementation of camera movement
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Thu, 12 Feb 2015 23:04:02 +0000 (00:04 +0100)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Thu, 12 Feb 2015 23:04:02 +0000 (00:04 +0100)
to get some perspective

src/app.cpp
src/app.hpp
src/camera.cpp
src/camera.hpp
src/model.cpp
src/model.hpp

index ff5a3bef59f1a3081b2a5c93ceae49953670ee03..051a5f0f0975944af4059cc5c06496b8f97a128a 100644 (file)
@@ -24,13 +24,20 @@ Application::Application()
 , ctx(window.CreateContext())
 , init_glew()
 , program()
+, move_velocity(0.001f)
 , pitch_sensitivity(-0.0025f)
 , yaw_sensitivity(-0.001f)
 , cam()
 , model()
 , vtx_buf(0)
 , mvp_handle(0)
-, running(false) {
+, running(false)
+, front(false)
+, back(false)
+, left(false)
+, right(false)
+, up(false)
+, down(false) {
        GLContext::EnableVSync();
        program.LoadShader(
                GL_VERTEX_SHADER,
@@ -91,6 +98,7 @@ void Application::Run() {
 
 void Application::Loop(int dt) {
        HandleEvents();
+       Update(dt);
        Render();
 }
 
@@ -99,11 +107,32 @@ void Application::HandleEvents() {
        SDL_Event event;
        while (SDL_PollEvent(&event)) {
                switch (event.type) {
+                       case SDL_KEYDOWN:
+                       case SDL_KEYUP:
+                               switch (event.key.keysym.sym) {
+                                       case SDLK_w:
+                                               front = event.key.state == SDL_PRESSED;
+                                               break;
+                                       case SDLK_s:
+                                               back = event.key.state == SDL_PRESSED;
+                                               break;
+                                       case SDLK_a:
+                                               left = event.key.state == SDL_PRESSED;
+                                               break;
+                                       case SDLK_d:
+                                               right = event.key.state == SDL_PRESSED;
+                                               break;
+                                       case SDLK_q:
+                                               up = event.key.state == SDL_PRESSED;
+                                               break;
+                                       case SDLK_e:
+                                               down = event.key.state == SDL_PRESSED;
+                                               break;
+                               }
+                               break;
                        case SDL_MOUSEMOTION:
                                cam.RotateYaw(event.motion.xrel * yaw_sensitivity);
                                cam.RotatePitch(event.motion.yrel * pitch_sensitivity);
-                               std::cout << "x: " << event.motion.xrel << ", y: " << event.motion.yrel
-                                               << ", pitch: " << cam.Pitch() << ", yaw: " << cam.Yaw() << std::endl;
                                break;
                        case SDL_QUIT:
                                running = false;
@@ -123,6 +152,29 @@ void Application::HandleEvents() {
        }
 }
 
+void Application::Update(int dt) {
+       glm::vec3 vel;
+       if (right && !left) {
+               vel.x = move_velocity;
+       } else if (left && !right) {
+               vel.x = -move_velocity;
+       }
+       if (up && !down) {
+               vel.y = move_velocity;
+       } else if (down && !up) {
+               vel.y = -move_velocity;
+       }
+       if (back && !front) {
+               vel.z = move_velocity;
+       } else if (front && !back) {
+               vel.z = -move_velocity;
+       }
+       cam.Velocity(vel);
+
+       cam.Update(dt);
+       model.Update(dt);
+}
+
 void Application::Render() {
        glClear(GL_COLOR_BUFFER_BIT);
 
index 8bafb2368d4f685c7b60f629e5bcd39e6b883fca..c5a5098578c306a951c79d0157af6b7ce15c648b 100644 (file)
@@ -25,6 +25,7 @@ public:
        void Loop(int dt);
 
        void HandleEvents();
+       void Update(int dt);
        void Render();
 
 private:
@@ -36,6 +37,7 @@ private:
        InitGLEW init_glew;
        Program program;
 
+       float move_velocity;
        float pitch_sensitivity;
        float yaw_sensitivity;
 
@@ -47,6 +49,8 @@ private:
 
        bool running;
 
+       bool front, back, left, right, up, down;
+
 };
 
 }
index 21df87414cee2ce382a3e2014efb94187c602240..239ddb2b429887ada5970dbe560acbfc14e003a6 100644 (file)
@@ -7,14 +7,13 @@
 namespace blank {
 
 Camera::Camera()
-: fov(45.0f)
+: Model()
+, fov(45.0f)
 , aspect(1.0f)
 , near_clip(0.1f)
 , far_clip(100.0f)
-, model()
 , projection(glm::perspective(fov, aspect, near_clip, far_clip))
-, view(model.Transform())
-, vp(projection * view) {
+, vp(projection) {
 
 }
 
@@ -53,14 +52,13 @@ void Camera::Clip(float near, float far) {
 }
 
 
-void Camera::UpdateProjection() {
-       projection = glm::perspective(fov, aspect, near_clip, far_clip);
-       vp = projection * view;
+void Camera::Update(int dt) {
+       Model::Update(dt);
+       vp = projection * glm::inverse(Transform());
 }
 
-void Camera::UpdateView() {
-       view = glm::inverse(model.Transform());
-       vp = projection * view;
+void Camera::UpdateProjection() {
+       projection = glm::perspective(fov, aspect, near_clip, far_clip);
 }
 
 }
index 88da8caf858aff87a3aacada9bd8cf865d2c300e..1ab086aeb66d257ac3c773f0cf1ae37dc30f6042 100644 (file)
@@ -8,7 +8,8 @@
 
 namespace blank {
 
-class Camera {
+class Camera
+: public Model {
 
 public:
        Camera();
@@ -27,20 +28,10 @@ public:
        void Aspect(float w, float h);
        void Clip(float near, float far);
 
-       void Position(glm::vec3 pos) { model.Position(pos); UpdateView(); }
-       void Move(glm::vec3 delta) { model.Move(delta); UpdateView(); }
-
-       // all angles in radians (full circle = 2π)
-       float Pitch() const { return model.Pitch(); }
-       void Pitch(float p) { model.Pitch(p); UpdateView(); }
-       void RotatePitch(float delta) { model.RotatePitch(delta); UpdateView(); }
-       float Yaw() const { return model.Yaw(); }
-       void Yaw(float y) { model.Yaw(y); UpdateView(); }
-       void RotateYaw(float delta) { model.RotateYaw(delta); UpdateView(); }
+       void Update(int dt);
 
 private:
        void UpdateProjection();
-       void UpdateView();
 
 private:
        float fov;
@@ -48,10 +39,7 @@ private:
        float near_clip;
        float far_clip;
 
-       Model model;
-
        glm::mat4 projection;
-       glm::mat4 view;
        glm::mat4 vp;
 
 };
index 4ee621afbb614605acd7dd3a02674721e6d3bc3c..7335ed9a5c3ec35a91219180e6105d8436f4a2d3 100644 (file)
@@ -8,7 +8,8 @@
 namespace blank {
 
 Model::Model()
-: position(0, 0, 0)
+: velocity(0, 0, 0)
+, position(0, 0, 0)
 , pitch(0)
 , yaw(0) {
 
@@ -23,4 +24,9 @@ glm::mat4 Model::Transform() const {
        return glm::translate(position) * glm::eulerAngleYX(yaw, pitch);
 }
 
+
+void Model::Update(int dt) {
+       position += velocity * float(dt);
+}
+
 }
index 938fd4453d6f700d51391509ab07a97b305a1676..b2a6e21a64a424fbd02639ef78004ddd369b9f3b 100644 (file)
@@ -14,6 +14,7 @@ public:
 
        glm::mat4 Transform() const;
 
+       void Velocity(glm::vec3 vel) { velocity = vel; }
        void Position(glm::vec3 pos) { position = pos; }
        void Move(glm::vec3 delta) { position += delta; }
 
@@ -25,7 +26,10 @@ public:
        void Yaw(float y) { yaw = y; }
        void RotateYaw(float delta) { yaw += delta; }
 
+       void Update(int dt);
+
 private:
+       glm::vec3 velocity;
        glm::vec3 position;
        float pitch;
        float yaw;