]> git.localhorst.tv Git - blank.git/commitdiff
simple HUD
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Sat, 7 Mar 2015 18:13:00 +0000 (19:13 +0100)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Sat, 7 Mar 2015 18:13:00 +0000 (19:13 +0100)
and the next "finally": a crosshair! :)
Also, the block type indicator is quite handy.
Can't compete with the crosshair of course, despite its static opaque color.

src/app.cpp
src/app.hpp
src/camera.cpp
src/camera.hpp
src/hud.cpp [new file with mode: 0644]
src/hud.hpp [new file with mode: 0644]
src/init.cpp
src/init.hpp
src/shader.cpp
src/shader.hpp

index e8f92689f2fe8cc4cda80cc987baf0aa79b89819..798355d61e9f6eb054ac640c4cb52af4b57767d9 100644 (file)
@@ -20,6 +20,7 @@ Application::Application()
 , pitch_sensitivity(-0.0025f)
 , yaw_sensitivity(-0.001f)
 , cam()
+, hud()
 , world()
 , outline()
 , outline_visible(false)
@@ -42,10 +43,12 @@ Application::Application()
        glGenVertexArrays(1, &VertexArrayID);
        glBindVertexArray(VertexArrayID);
 
-       cam.Position(glm::vec3(0, 4, 4));
-
        world.Generate();
 
+       cam.Position(glm::vec3(0, 4, 4));
+       hud.Viewport(960, 600);
+       hud.Display(*world.BlockTypes()[place_id]);
+
        glClearColor(0.0, 0.0, 0.0, 1.0);
 }
 
@@ -121,6 +124,7 @@ void Application::HandleEvents() {
                                switch (event.window.event) {
                                        case SDL_WINDOWEVENT_RESIZED:
                                                cam.Viewport(event.window.data1, event.window.data2);
+                                               hud.Viewport(event.window.data1, event.window.data2);
                                                break;
                                        default:
                                                break;
@@ -171,6 +175,7 @@ void Application::Update(int dt) {
        if (pick) {
                if (chunk) {
                        place_id = chunk->BlockAt(blkid).type->id;
+                       hud.Display(*world.BlockTypes()[place_id]);
                }
                pick = false;
        }
@@ -201,6 +206,7 @@ void Application::Render() {
 
        program.Activate();
 
+       program.SetLightDirection({ -1.0f, -3.0f, -2.0f });
        program.SetVP(cam.View(), cam.Projection());
 
        for (Chunk &chunk : world.LoadedChunks()) {
@@ -213,6 +219,8 @@ void Application::Render() {
                outline.Draw();
        }
 
+       hud.Render(program);
+
        window.Flip();
 }
 
index 9534da7ec1d6f3a0677dda703e2dfea4bb19d63d..e1f328ac5e7717acf37fd933c62ad3f5843d8f93 100644 (file)
@@ -6,6 +6,7 @@
 
 #include "camera.hpp"
 #include "controller.hpp"
+#include "hud.hpp"
 #include "init.hpp"
 #include "model.hpp"
 #include "shader.hpp"
@@ -43,6 +44,7 @@ private:
        float yaw_sensitivity;
 
        Camera cam;
+       HUD hud;
        World world;
        OutlineModel outline;
 
index c5283bd7f77bf86729b688342265dc4919bab47f..4bbb0f12f0fadb6955bf230d99a88fa116d65355 100644 (file)
@@ -13,8 +13,7 @@ Camera::Camera()
 , near_clip(0.1f)
 , far_clip(100.0f)
 , projection(glm::perspective(fov, aspect, near_clip, far_clip))
-, view(glm::inverse(Transform()))
-, vp(projection) {
+, view(glm::inverse(Transform())) {
 
 }
 
@@ -49,7 +48,7 @@ void Camera::Clip(float near, float far) {
 }
 
 Ray Camera::Aim() const {
-       const glm::mat4 inv_vp(glm::inverse(vp));
+       const glm::mat4 inv_vp(glm::inverse(projection * view));
        glm::vec4 from = inv_vp * glm::vec4(0.0f, 0.0f, -1.0f, 1.0f);
        from /= from.w;
        glm::vec4 to = inv_vp * glm::vec4(0.0f, 0.0f, 1.0f, 1.0f);
@@ -61,7 +60,6 @@ Ray Camera::Aim() const {
 void Camera::Update(int dt) {
        FPSController::Update(dt);
        view = glm::inverse(Transform());
-       vp = projection * view;
 }
 
 void Camera::UpdateProjection() {
index 3adb800ce344b03da6a2261d5486c2ef4fd58365..805109c313af3a8f0fd692448e02c7bab7190b0c 100644 (file)
@@ -18,8 +18,6 @@ public:
        Camera(const Camera &) = delete;
        Camera &operator =(const Camera &) = delete;
 
-       glm::mat4 MakeMVP(const glm::mat4 &m) const { return vp * m; }
-
        void Viewport(int width, int height);
        void Viewport(int x, int y, int width, int height);
 
@@ -46,7 +44,6 @@ private:
 
        glm::mat4 projection;
        glm::mat4 view;
-       glm::mat4 vp;
 
 };
 
diff --git a/src/hud.cpp b/src/hud.cpp
new file mode 100644 (file)
index 0000000..7f127f4
--- /dev/null
@@ -0,0 +1,63 @@
+#include "hud.hpp"
+
+#include "init.hpp"
+#include "shape.hpp"
+
+#include <glm/gtc/matrix_transform.hpp>
+
+
+namespace blank {
+
+HUD::HUD()
+: block()
+, block_transform(1.0f)
+, block_visible(false)
+, crosshair()
+, crosshair_transform(1.0f)
+, near(100.0f)
+, far(-100.0f)
+, projection(glm::ortho(0.0f, 1.0f, 1.0f, 0.0f, near, far))
+, view(glm::translate(glm::mat4(1.0f), glm::vec3(-0.5f, -0.5f, 0))) {
+       block_transform = glm::translate(block_transform, glm::vec3(35.0f, 80.0f, 0.0f));
+       block_transform = glm::scale(block_transform, glm::vec3(50.0f));
+       block_transform = glm::rotate(block_transform, 3.5f, glm::vec3(1.0f, 0.0f, 0.0f));
+       block_transform = glm::rotate(block_transform, 0.85f, glm::vec3(0.0f, 1.0f, 0.0f));
+
+       crosshair.vertices = std::vector<glm::vec3>({
+               { -10.0f,   0.0f, 0.0f }, { 10.0f,  0.0f, 0.0f },
+               {   0.0f, -10.0f, 0.0f }, {  0.0f, 10.0f, 0.0f },
+       });
+       crosshair.colors.resize(4, { 10.0f, 10.0f, 10.0f });
+       crosshair.Invalidate();
+}
+
+
+void HUD::Viewport(float width, float height) {
+       Viewport(0, 0, width, height);
+}
+
+void HUD::Viewport(float x, float y, float width, float height) {
+       projection = glm::ortho(x, width, height, y, near, far);
+       crosshair_transform = glm::translate(glm::mat4(1.0f), glm::vec3(width * 0.5f, height * 0.5f, 0.0f));
+}
+
+
+void HUD::Display(const BlockType &type) {
+       block.Clear();
+       type.FillModel({ 0.0f, 0.0f, 0.0f }, block);
+       block_visible = type.visible;
+}
+
+
+void HUD::Render(DirectionalLighting &program) {
+       if (block_visible) {
+               program.SetLightDirection({ 1.0f, 3.0f, 5.0f });
+               GLContext::ClearDepthBuffer();
+               program.SetMVP(block_transform, view, projection);
+               block.Draw();
+               program.SetM(crosshair_transform);
+               crosshair.Draw();
+       }
+}
+
+}
diff --git a/src/hud.hpp b/src/hud.hpp
new file mode 100644 (file)
index 0000000..47fcdb0
--- /dev/null
@@ -0,0 +1,44 @@
+#ifndef BLANK_HUD_H_
+#define BLANK_HUD_H_
+
+#include "model.hpp"
+#include "shader.hpp"
+#include "world.hpp"
+
+#include <glm/glm.hpp>
+
+
+namespace blank {
+
+class HUD {
+
+public:
+       HUD();
+
+       HUD(const HUD &) = delete;
+       HUD &operator =(const HUD &) = delete;
+
+       void Viewport(float width, float height);
+       void Viewport(float x, float y, float width, float height);
+
+       void Display(const BlockType &);
+
+       void Render(DirectionalLighting &);
+
+private:
+       Model block;
+       glm::mat4 block_transform;
+       bool block_visible;
+
+       OutlineModel crosshair;
+       glm::mat4 crosshair_transform;
+
+       float near, far;
+       glm::mat4 projection;
+       glm::mat4 view;
+
+};
+
+}
+
+#endif
index 2c1c73ad6155e0aae9654f9fa9a58f6c93477fe2..061e848e073691af5fcedca51763e4af97c91a80 100644 (file)
@@ -151,6 +151,10 @@ void GLContext::Clear() {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 }
 
+void GLContext::ClearDepthBuffer() {
+       glClear(GL_DEPTH_BUFFER_BIT);
+}
+
 
 InitGLEW::InitGLEW() {
        glewExperimental = GL_TRUE;
index a467e69157eebc63e29dd5dcb961aeb8cb4febad..6064fca4b58a5cd6acb5b037a2f5881216b4823d 100644 (file)
@@ -86,6 +86,7 @@ public:
        static void EnableBackfaceCulling();
 
        static void Clear();
+       static void ClearDepthBuffer();
 
 private:
        SDL_GLContext handle;
index 99bdb74dec9995727f44470b8cddd34d85489739..e01c11fcc1aac8f282aaaec1804d9eb32f708ca6 100644 (file)
@@ -206,6 +206,11 @@ void DirectionalLighting::SetM(const glm::mat4 &m) {
        glUniformMatrix4fv(mvp_handle, 1, GL_FALSE, &mvp[0][0]);
 }
 
+void DirectionalLighting::SetLightDirection(const glm::vec3 &dir) {
+       light_direction = -dir;
+       glUniform3f(light_direction_handle, light_direction.x, light_direction.y, light_direction.z);
+}
+
 void DirectionalLighting::SetVP(const glm::mat4 &v, const glm::mat4 &p) {
        vp = p * v;
 }
index c97e073111b9cf45a4dacf7f63dd620012d98d5c..97211c4aa6799860eb7f748c838369800d680f7d 100644 (file)
@@ -67,6 +67,8 @@ public:
 
        void Activate();
 
+       void SetLightDirection(const glm::vec3 &);
+
        void SetM(const glm::mat4 &m);
        void SetVP(const glm::mat4 &v, const glm::mat4 &p);
        void SetMVP(const glm::mat4 &m, const glm::mat4 &v, const glm::mat4 &p);