From: Daniel Karbach Date: Sat, 7 Mar 2015 18:13:00 +0000 (+0100) Subject: simple HUD X-Git-Url: http://git.localhorst.tv/?a=commitdiff_plain;h=41e0223ec090142bf03066f4f5fc1f5005095072;p=blank.git simple HUD 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. --- diff --git a/src/app.cpp b/src/app.cpp index e8f9268..798355d 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -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(); } diff --git a/src/app.hpp b/src/app.hpp index 9534da7..e1f328a 100644 --- a/src/app.hpp +++ b/src/app.hpp @@ -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; diff --git a/src/camera.cpp b/src/camera.cpp index c5283bd..4bbb0f1 100644 --- a/src/camera.cpp +++ b/src/camera.cpp @@ -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() { diff --git a/src/camera.hpp b/src/camera.hpp index 3adb800..805109c 100644 --- a/src/camera.hpp +++ b/src/camera.hpp @@ -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 index 0000000..7f127f4 --- /dev/null +++ b/src/hud.cpp @@ -0,0 +1,63 @@ +#include "hud.hpp" + +#include "init.hpp" +#include "shape.hpp" + +#include + + +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({ + { -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 index 0000000..47fcdb0 --- /dev/null +++ b/src/hud.hpp @@ -0,0 +1,44 @@ +#ifndef BLANK_HUD_H_ +#define BLANK_HUD_H_ + +#include "model.hpp" +#include "shader.hpp" +#include "world.hpp" + +#include + + +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 diff --git a/src/init.cpp b/src/init.cpp index 2c1c73a..061e848 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -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; diff --git a/src/init.hpp b/src/init.hpp index a467e69..6064fca 100644 --- a/src/init.hpp +++ b/src/init.hpp @@ -86,6 +86,7 @@ public: static void EnableBackfaceCulling(); static void Clear(); + static void ClearDepthBuffer(); private: SDL_GLContext handle; diff --git a/src/shader.cpp b/src/shader.cpp index 99bdb74..e01c11f 100644 --- a/src/shader.cpp +++ b/src/shader.cpp @@ -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; } diff --git a/src/shader.hpp b/src/shader.hpp index c97e073..97211c4 100644 --- a/src/shader.hpp +++ b/src/shader.hpp @@ -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);