add very basic 3D

This commit is contained in:
2023-10-18 00:18:44 +02:00
parent f8c2e8e598
commit a9ce353a9d
6 changed files with 93 additions and 6 deletions

View File

@@ -0,0 +1,34 @@
#pragma once
#include "blitz/misc/Maths.h"
#include <cstdint>
namespace blitz {
namespace render {
class Camera {
private:
float m_Yaw, m_Pitch;
Vec3f m_Position;
Mat4f m_PerspectiveMatrix;
Vec2i m_LastWindowSize;
public:
Camera() : m_Yaw(-PI / 2.0f), m_Pitch(0), m_Position(10, 1, 10) {}
~Camera() {}
void Update(std::uint64_t delta);
Mat4f GetViewMatrix() const;
const Mat4f& GetPerspectiveMatrix() const {
return m_PerspectiveMatrix;
}
const Vec3f& GetPosition() const {
return m_Position;
}
};
} // namespace render
} // namespace blitz

View File

@@ -1,5 +1,6 @@
#pragma once
#include "client/render/Camera.h"
#include "client/render/loader/GLLoader.h"
#include <memory>
@@ -21,11 +22,13 @@ class MainRenderer {
Client* m_Client;
std::unique_ptr<GL::VertexArray> m_Vao;
std::unique_ptr<shader::EntityShader> m_Shader;
Camera m_Camera;
public:
MainRenderer(Client* client);
~MainRenderer();
void Update();
void Render();
void Render(const GL::VertexArray& vao);

View File

@@ -15,6 +15,7 @@ int main(int argc, char** argv) {
while (!display.IsCloseRequested()) {
client.Update();
display.PollEvents();
renderer.Update();
renderer.Render();
display.Render();
display.Update();

View File

@@ -0,0 +1,37 @@
#include "client/render/Camera.h"
#include "imgui.h"
#include <algorithm>
namespace blitz {
namespace render {
void Camera::Update(std::uint64_t delta) {
if (ImGui::GetIO().MouseDown[0]) {
m_Yaw += ImGui::GetIO().MouseDelta.x * delta / 5000;
m_Pitch -= ImGui::GetIO().MouseDelta.y * delta / 5000;
m_Pitch = std::clamp(m_Pitch, -PI / 2, PI / 2);
}
int windowWidth = ImGui::GetIO().DisplaySize.x;
int windowHeight = ImGui::GetIO().DisplaySize.y;
if (windowWidth != m_LastWindowSize.x || windowHeight != m_LastWindowSize.y) {
m_LastWindowSize = {windowWidth, windowHeight};
m_PerspectiveMatrix = maths::Perspective(80.0f / 180.0f * PI, static_cast<float>(windowWidth) / windowHeight, 0.1f, 160.0f);
}
}
Mat4f Camera::GetViewMatrix() const {
Vec3f front = {
std::cos(m_Yaw) * std::cos(m_Pitch),
std::sin(m_Pitch),
std::sin(m_Yaw) * std::cos(m_Pitch),
};
return maths::Look(m_Position, front, {0, 1, 0});
}
} // namespace render
} // namespace blitz

View File

@@ -4,6 +4,7 @@
#include "client/Client.h"
#include "client/render/loader/GLLoader.h"
#include "client/render/shader/EntityShader.h"
#include "imgui.h"
#include <GL/glew.h>
#include <assimp/Importer.hpp> // C++ importer interface
#include <assimp/postprocess.h> // Post processing flags
@@ -20,6 +21,8 @@ MainRenderer::MainRenderer(Client* client) : m_Client(client) {
m_Shader = std::make_unique<shader::EntityShader>();
m_Shader->LoadShader();
glEnable(GL_DEPTH_TEST);
}
MainRenderer::~MainRenderer() {}
@@ -69,7 +72,7 @@ void MainRenderer::LoadModel() {
GL::VertexBuffer positionVBO(positions, VERTEX_SIZE);
positionVBO.AddVertexAttribPointer(0, VERTEX_SIZE, 0);
m_Vao = std::make_unique<GL::VertexArray>(GL::ElementBuffer{indicies}); // each pos = 1 color
m_Vao = std::make_unique<GL::VertexArray>(GL::ElementBuffer{indicies}); // each pos = 1 color
m_Vao->Bind();
m_Vao->BindVertexBuffer(positionVBO);
m_Vao->Unbind();
@@ -86,8 +89,17 @@ void MainRenderer::Render() {
if (!m_Client->IsConnected())
return;
glClear(GL_DEPTH_BUFFER_BIT);
Render(*m_Vao.get());
}
void MainRenderer::Update() {
m_Camera.Update(ImGui::GetIO().DeltaTime * 1000);
m_Shader->Start();
m_Shader->SetProjectionMatrix(m_Camera.GetPerspectiveMatrix());
m_Shader->SetViewMatrix(m_Camera.GetViewMatrix());
}
} // namespace render
} // namespace blitz

View File

@@ -56,11 +56,11 @@ uniform mat4 viewMatrix;
uniform mat4 projectionMatrix;
uniform vec3 modelPosition;
//flat out int pass_color;
out vec3 pass_color;
void main(void){
//pass_color = color;
gl_Position = vec4(position, 1.0);
pass_color = position;
gl_Position = projectionMatrix * viewMatrix * vec4(position, 1.0);
//gl_Position = projectionMatrix * viewMatrix * vec4(position + modelPosition, 1.0);
}
)";
@@ -68,7 +68,7 @@ void main(void){
static const char fragmentSource[] = R"(
#version 330
flat in int pass_color;
in vec3 pass_color;
out vec4 out_color;
@@ -76,7 +76,7 @@ uniform vec3 ColorEffect;
void main(void){
out_color = vec4(1, 1, 1, 1);
out_color = vec4(pass_color, 1);
}
)";