, pitch_sensitivity(-0.0025f)
, yaw_sensitivity(-0.001f)
, cam()
+, hud()
, world()
, outline()
, outline_visible(false)
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);
}
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;
if (pick) {
if (chunk) {
place_id = chunk->BlockAt(blkid).type->id;
+ hud.Display(*world.BlockTypes()[place_id]);
}
pick = false;
}
program.Activate();
+ program.SetLightDirection({ -1.0f, -3.0f, -2.0f });
program.SetVP(cam.View(), cam.Projection());
for (Chunk &chunk : world.LoadedChunks()) {
outline.Draw();
}
+ hud.Render(program);
+
window.Flip();
}
#include "camera.hpp"
#include "controller.hpp"
+#include "hud.hpp"
#include "init.hpp"
#include "model.hpp"
#include "shader.hpp"
float yaw_sensitivity;
Camera cam;
+ HUD hud;
World world;
OutlineModel outline;
, 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())) {
}
}
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);
void Camera::Update(int dt) {
FPSController::Update(dt);
view = glm::inverse(Transform());
- vp = projection * view;
}
void Camera::UpdateProjection() {
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);
glm::mat4 projection;
glm::mat4 view;
- glm::mat4 vp;
};
--- /dev/null
+#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();
+ }
+}
+
+}
--- /dev/null
+#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
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
+void GLContext::ClearDepthBuffer() {
+ glClear(GL_DEPTH_BUFFER_BIT);
+}
+
InitGLEW::InitGLEW() {
glewExperimental = GL_TRUE;
static void EnableBackfaceCulling();
static void Clear();
+ static void ClearDepthBuffer();
private:
SDL_GLContext handle;
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;
}
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);