dirty implementation
This commit is contained in:
@@ -7,8 +7,8 @@ namespace game {
|
||||
|
||||
class PlayerInputListener {
|
||||
public:
|
||||
virtual void OnPlayerJump() {}
|
||||
virtual void OnPlayerShoot(const Vec3f& position, float yaw, float pitch) {}
|
||||
virtual void OnLocalPlayerJump() {}
|
||||
virtual void OnLocalPlayerShoot(const Vec3f& position, float yaw, float pitch) {}
|
||||
};
|
||||
|
||||
} // namespace game
|
||||
|
||||
@@ -26,7 +26,8 @@ class Server;
|
||||
class GuiListener {
|
||||
public:
|
||||
virtual void OnTextChatReceived(const protocol::ColoredText& text) {}
|
||||
virtual void OnSpectatorChange(const game::PlayerID player) {}
|
||||
virtual void OnSpectatorChange(game::PlayerID player) {}
|
||||
virtual void OnPlayerShoot(game::PlayerID player, const Vec3f& position, float yaw, float pitch) {}
|
||||
};
|
||||
|
||||
// Singleton
|
||||
@@ -56,7 +57,7 @@ class Client : public utils::ObjectNotifier<GuiListener>, public game::PlayerInp
|
||||
|
||||
void SendPlayerPosAndLook(const Vec3f& position, float yaw, float pitch);
|
||||
|
||||
virtual void OnPlayerShoot(const Vec3f& position, float yaw, float pitch) override;
|
||||
virtual void OnLocalPlayerShoot(const Vec3f& position, float yaw, float pitch) override;
|
||||
|
||||
game::PlayerID GetPlayerID() const;
|
||||
|
||||
|
||||
@@ -23,6 +23,7 @@ class ClientGame : public game::Game, public protocol::PacketHandler {
|
||||
virtual void HandlePacket(const protocol::PlayerLeavePacket* packet) override;
|
||||
virtual void HandlePacket(const protocol::PlayerListPacket* packet) override;
|
||||
virtual void HandlePacket(const protocol::PlayerPositionAndRotationPacket* packet) override;
|
||||
virtual void HandlePacket(const protocol::PlayerShootPacket* packet) override;
|
||||
|
||||
private:
|
||||
void RegisterHandlers();
|
||||
|
||||
41
include/client/render/BulletRenderer.h
Normal file
41
include/client/render/BulletRenderer.h
Normal file
@@ -0,0 +1,41 @@
|
||||
#pragma once
|
||||
|
||||
#include "blitz/common/Vector.h"
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
namespace blitz {
|
||||
|
||||
namespace shader {
|
||||
class BulletShader;
|
||||
} // namespace shader
|
||||
|
||||
|
||||
namespace render {
|
||||
|
||||
struct Trail {
|
||||
Vec3f m_From;
|
||||
Vec3f m_To;
|
||||
float m_Decay;
|
||||
};
|
||||
|
||||
|
||||
class BulletRenderer {
|
||||
private:
|
||||
std::vector<Trail> m_Trails;
|
||||
std::unique_ptr<shader::BulletShader> m_Shader;
|
||||
unsigned int m_Vbo;
|
||||
|
||||
public:
|
||||
BulletRenderer();
|
||||
~BulletRenderer();
|
||||
|
||||
void AddBullet(const Vec3f& origin, const Vec3f& direction);
|
||||
void Update(float delta);
|
||||
void Render();
|
||||
|
||||
void UpdateShader(const Mat4f& proj, const Mat4f& trans) const;
|
||||
};
|
||||
|
||||
} // namespace render
|
||||
} // namespace blitz
|
||||
@@ -19,6 +19,8 @@ class Camera {
|
||||
|
||||
void Update(float delta);
|
||||
|
||||
static float GetPlayerEyeHeight();
|
||||
|
||||
void SetAttachedPlayer(game::Player* a_Player) {
|
||||
m_Player = a_Player;
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
#include "client/Client.h"
|
||||
#include "client/display/PlayerController.h"
|
||||
#include "client/render/BulletRenderer.h"
|
||||
#include "client/render/Camera.h"
|
||||
#include "client/render/loader/GLLoader.h"
|
||||
#include "client/render/loader/ModelLoader.h"
|
||||
@@ -29,6 +30,7 @@ class MainRenderer : public GuiListener, public game::PlayerInputListener {
|
||||
std::unique_ptr<shader::WorldShader> m_WorldShader;
|
||||
std::unique_ptr<shader::GunShader> m_GunShader;
|
||||
input::PlayerController m_PlayerController;
|
||||
BulletRenderer m_BulletRenderer;
|
||||
unsigned int m_Texture;
|
||||
float m_ShootTime;
|
||||
Camera m_Camera;
|
||||
@@ -37,9 +39,9 @@ class MainRenderer : public GuiListener, public game::PlayerInputListener {
|
||||
MainRenderer(Client* client);
|
||||
~MainRenderer();
|
||||
|
||||
virtual void OnSpectatorChange(const game::PlayerID player) override;
|
||||
|
||||
virtual void OnPlayerShoot(const Vec3f& position, float yaw, float pitch) override;
|
||||
virtual void OnSpectatorChange(game::PlayerID player) override;
|
||||
virtual void OnPlayerShoot(game::PlayerID player, const Vec3f& position, float yaw, float pitch) override;
|
||||
virtual void OnLocalPlayerShoot(const Vec3f& position, float yaw, float pitch) override;
|
||||
|
||||
void Update();
|
||||
void Render();
|
||||
|
||||
28
include/client/render/shader/BulletShader.h
Normal file
28
include/client/render/shader/BulletShader.h
Normal file
@@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
|
||||
#include "ShaderProgram.h"
|
||||
|
||||
namespace blitz {
|
||||
namespace shader {
|
||||
|
||||
class BulletShader : public ShaderProgram {
|
||||
private:
|
||||
unsigned int m_LocationProjectionMatrix = 0;
|
||||
unsigned int m_LocationViewMatrix = 0;
|
||||
unsigned int m_LocationAlpha = 0;
|
||||
|
||||
protected:
|
||||
virtual void GetAllUniformLocation() override;
|
||||
|
||||
public:
|
||||
BulletShader();
|
||||
|
||||
bool LoadShader();
|
||||
|
||||
void SetProjectionMatrix(const Mat4f& proj) const;
|
||||
void SetViewMatrix(const Mat4f& view) const;
|
||||
void SetDecay(float decay) const;
|
||||
};
|
||||
|
||||
} // namespace shader
|
||||
} // namespace blitz
|
||||
@@ -80,7 +80,7 @@ void Client::SendPlayerPosAndLook(const Vec3f& position, float yaw, float pitch)
|
||||
}
|
||||
|
||||
|
||||
void Client::OnPlayerShoot(const Vec3f& position, float yaw, float pitch) {
|
||||
void Client::OnLocalPlayerShoot(const Vec3f& position, float yaw, float pitch) {
|
||||
protocol::PlayerShootPacket packet(position, yaw, pitch);
|
||||
m_Connexion->SendPacket(&packet);
|
||||
}
|
||||
|
||||
@@ -63,12 +63,13 @@ void PlayerController::Update(float delta) {
|
||||
|
||||
if (ImGui::IsKeyDown(ImGuiKey::ImGuiKey_Space) && m_OnGround) {
|
||||
m_Dz = m_MaxDz;
|
||||
NotifyListeners(&game::PlayerInputListener::OnPlayerJump);
|
||||
NotifyListeners(&game::PlayerInputListener::OnLocalPlayerJump);
|
||||
}
|
||||
|
||||
bool canShoot = m_ShootTimer.Update(delta);
|
||||
if (ImGui::IsMouseDown(ImGuiMouseButton_Left) && canShoot) {
|
||||
NotifyListeners(&game::PlayerInputListener::OnPlayerShoot, m_Player->GetPosition(), m_Player->GetYaw(), m_Player->GetPitch());
|
||||
NotifyListeners(
|
||||
&game::PlayerInputListener::OnLocalPlayerShoot, m_Player->GetPosition(), m_Player->GetYaw(), m_Player->GetPitch());
|
||||
m_ShootTimer.ApplyCooldown();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#include "blitz/protocol/packets/PlayerLeavePacket.h"
|
||||
#include "blitz/protocol/packets/PlayerListPacket.h"
|
||||
#include "blitz/protocol/packets/PlayerPositionAndRotationPacket.h"
|
||||
#include "blitz/protocol/packets/PlayerShootPacket.h"
|
||||
#include "client/Client.h"
|
||||
|
||||
namespace blitz {
|
||||
@@ -25,6 +26,7 @@ void ClientGame::RegisterHandlers() {
|
||||
GetDispatcher()->RegisterHandler(protocol::PacketType::PlayerLeave, this);
|
||||
GetDispatcher()->RegisterHandler(protocol::PacketType::PlayerList, this);
|
||||
GetDispatcher()->RegisterHandler(protocol::PacketType::PlayerPositionAndRotation, this);
|
||||
GetDispatcher()->RegisterHandler(protocol::PacketType::PlayerShoot, this);
|
||||
}
|
||||
|
||||
void ClientGame::HandlePacket(const protocol::PlayerJoinPacket* packet) {
|
||||
@@ -46,6 +48,11 @@ void ClientGame::HandlePacket(const protocol::PlayerListPacket* packet) {
|
||||
}
|
||||
}
|
||||
|
||||
void ClientGame::HandlePacket(const protocol::PlayerShootPacket* packet) {
|
||||
m_Client->NotifyListeners(
|
||||
&GuiListener::OnPlayerShoot, packet->GetPlayer(), packet->GetPosition(), packet->GetYaw(), packet->GetPitch());
|
||||
}
|
||||
|
||||
void ClientGame::HandlePacket(const protocol::PlayerPositionAndRotationPacket* packet) {
|
||||
if (packet->GetPlayer() == m_Client->GetPlayerID())
|
||||
return;
|
||||
|
||||
76
src/client/render/BulletRenderer.cpp
Normal file
76
src/client/render/BulletRenderer.cpp
Normal file
@@ -0,0 +1,76 @@
|
||||
#include "client/render/BulletRenderer.h"
|
||||
|
||||
#include "blitz/misc/Log.h"
|
||||
#include "blitz/misc/Test.h"
|
||||
#include "client/render/loader/GLLoader.h"
|
||||
#include "client/render/shader/BulletShader.h"
|
||||
#include <glbinding/gl/gl.h>
|
||||
#include <imgui.h>
|
||||
|
||||
using namespace gl;
|
||||
|
||||
namespace blitz {
|
||||
namespace render {
|
||||
|
||||
BulletRenderer::BulletRenderer() {
|
||||
m_Shader = std::make_unique<shader::BulletShader>();
|
||||
blitz_debug_assert(m_Shader->LoadShader());
|
||||
|
||||
glLineWidth(3.0f);
|
||||
|
||||
glGenBuffers(1, &m_Vbo);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, m_Vbo);
|
||||
glBufferData(GL_ARRAY_BUFFER, static_cast<GLsizeiptr>(6 * sizeof(float)), nullptr, GL_DYNAMIC_DRAW);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
}
|
||||
|
||||
BulletRenderer::~BulletRenderer() {
|
||||
glDeleteBuffers(1, &m_Vbo);
|
||||
}
|
||||
|
||||
void BulletRenderer::AddBullet(const Vec3f& origin, const Vec3f& direction) {
|
||||
static const float TRAIL_LENGHT = 20;
|
||||
|
||||
Trail trail{origin, origin + direction * TRAIL_LENGHT, 1.0f};
|
||||
m_Trails.push_back(trail);
|
||||
}
|
||||
|
||||
void BulletRenderer::Update(float delta) {
|
||||
for (Trail& trail : m_Trails) {
|
||||
trail.m_Decay -= delta;
|
||||
}
|
||||
|
||||
for (std::size_t i = 0; i < m_Trails.size(); i++) {
|
||||
if (m_Trails[i].m_Decay < 0) {
|
||||
utils::LOG("Bye bye !");
|
||||
m_Trails.erase(m_Trails.begin() + i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void BulletRenderer::Render() {
|
||||
m_Shader->Start();
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, m_Vbo);
|
||||
glEnableVertexAttribArray(0);
|
||||
glEnableVertexAttribArray(1);
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, false, 3 * sizeof(float), 0);
|
||||
glBufferData(GL_ARRAY_BUFFER, static_cast<GLsizeiptr>(6 * sizeof(float)), nullptr, GL_STATIC_DRAW);
|
||||
for (Trail& trail : m_Trails) {
|
||||
m_Shader->SetDecay(trail.m_Decay);
|
||||
glBufferSubData(GL_ARRAY_BUFFER, 0, static_cast<GLsizeiptr>(6 * sizeof(float)), &trail);
|
||||
glDrawArrays(GL_LINES, 0, 2);
|
||||
}
|
||||
glDisableVertexAttribArray(1);
|
||||
glDisableVertexAttribArray(0);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
}
|
||||
|
||||
void BulletRenderer::UpdateShader(const Mat4f& proj, const Mat4f& trans) const {
|
||||
m_Shader->Start();
|
||||
m_Shader->SetProjectionMatrix(proj);
|
||||
m_Shader->SetViewMatrix(trans);
|
||||
}
|
||||
|
||||
} // namespace render
|
||||
} // namespace blitz
|
||||
@@ -7,7 +7,7 @@
|
||||
namespace blitz {
|
||||
namespace render {
|
||||
|
||||
static const float eyeHeight = 1.25f;
|
||||
static const float EyeHeight = 1.25f;
|
||||
|
||||
void Camera::Update(float delta) {
|
||||
int windowWidth = ImGui::GetIO().DisplaySize.x;
|
||||
@@ -21,6 +21,10 @@ void Camera::Update(float delta) {
|
||||
}
|
||||
}
|
||||
|
||||
float Camera::GetPlayerEyeHeight() {
|
||||
return EyeHeight;
|
||||
}
|
||||
|
||||
Mat4f Camera::GetViewMatrix() const {
|
||||
Vec3f front = {
|
||||
std::cos(m_Player->GetYaw()) * std::cos(m_Player->GetPitch()),
|
||||
@@ -28,7 +32,7 @@ Mat4f Camera::GetViewMatrix() const {
|
||||
std::sin(m_Player->GetYaw()) * std::cos(m_Player->GetPitch()),
|
||||
};
|
||||
|
||||
return maths::Look(m_Player->GetPosition() + Vec3f{0, eyeHeight, 0}, front, {0, 1, 0});
|
||||
return maths::Look(m_Player->GetPosition() + Vec3f{0, EyeHeight, 0}, front, {0, 1, 0});
|
||||
}
|
||||
|
||||
} // namespace render
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
#include "client/render/loader/GLLoader.h"
|
||||
#include "client/render/loader/ModelLoader.h"
|
||||
#include "client/render/loader/TextureLoader.h"
|
||||
#include "client/render/shader/BulletShader.h"
|
||||
#include "client/render/shader/EntityShader.h"
|
||||
#include "client/render/shader/GunShader.h"
|
||||
#include "client/render/shader/WorldShader.h"
|
||||
@@ -49,6 +50,9 @@ MainRenderer::MainRenderer(Client* client) : m_Client(client), m_ShootTime(0) {
|
||||
glEnable(GL_CULL_FACE);
|
||||
glCullFace(GL_FRONT);
|
||||
glFrontFace(GL_CW);
|
||||
|
||||
glEnable(GL_BLEND);
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
}
|
||||
|
||||
MainRenderer::~MainRenderer() {}
|
||||
@@ -89,6 +93,8 @@ void MainRenderer::Render() {
|
||||
RenderWorld();
|
||||
RenderPlayers();
|
||||
RenderGun();
|
||||
|
||||
m_BulletRenderer.Render();
|
||||
}
|
||||
|
||||
void MainRenderer::RenderPlayers() {
|
||||
@@ -101,10 +107,21 @@ void MainRenderer::RenderPlayers() {
|
||||
}
|
||||
}
|
||||
|
||||
void MainRenderer::OnPlayerShoot(const Vec3f& position, float yaw, float pitch) {
|
||||
void MainRenderer::OnLocalPlayerShoot(const Vec3f& position, float yaw, float pitch) {
|
||||
m_ShootTime = 1.0f;
|
||||
}
|
||||
|
||||
void MainRenderer::OnPlayerShoot(game::PlayerID player, const Vec3f& position, float yaw, float pitch) {
|
||||
Vec3f front = {
|
||||
std::cos(yaw) * std::cos(pitch),
|
||||
std::sin(pitch),
|
||||
std::sin(yaw) * std::cos(pitch),
|
||||
};
|
||||
maths::Normalize(front);
|
||||
|
||||
m_BulletRenderer.AddBullet(position + Vec3f{0.0f, Camera::GetPlayerEyeHeight(), 0.0f}, front);
|
||||
}
|
||||
|
||||
void MainRenderer::RenderGun() {
|
||||
if (!m_Camera.GetAttachedPlayer())
|
||||
return;
|
||||
@@ -145,17 +162,23 @@ void MainRenderer::Update() {
|
||||
|
||||
m_PlayerController.Update(delta);
|
||||
m_Camera.Update(delta);
|
||||
m_BulletRenderer.Update(delta);
|
||||
|
||||
Mat4f projectionMatrix = m_Camera.GetPerspectiveMatrix();
|
||||
Mat4f viewMatrix = m_Camera.GetViewMatrix();
|
||||
|
||||
m_EntityShader->Start();
|
||||
m_EntityShader->SetProjectionMatrix(m_Camera.GetPerspectiveMatrix());
|
||||
m_EntityShader->SetViewMatrix(m_Camera.GetViewMatrix());
|
||||
m_EntityShader->SetProjectionMatrix(projectionMatrix);
|
||||
m_EntityShader->SetViewMatrix(viewMatrix);
|
||||
|
||||
m_WorldShader->Start();
|
||||
m_WorldShader->SetProjectionMatrix(m_Camera.GetPerspectiveMatrix());
|
||||
m_WorldShader->SetViewMatrix(m_Camera.GetViewMatrix());
|
||||
m_WorldShader->SetProjectionMatrix(projectionMatrix);
|
||||
m_WorldShader->SetViewMatrix(viewMatrix);
|
||||
|
||||
m_GunShader->Start();
|
||||
m_GunShader->SetProjectionMatrix(m_Camera.GetPerspectiveMatrix());
|
||||
m_GunShader->SetProjectionMatrix(projectionMatrix);
|
||||
|
||||
m_BulletRenderer.UpdateShader(projectionMatrix, viewMatrix);
|
||||
|
||||
m_ShootTime = std::max(0.0f, m_ShootTime - delta);
|
||||
}
|
||||
|
||||
60
src/client/render/shader/BulletShader.cpp
Normal file
60
src/client/render/shader/BulletShader.cpp
Normal file
@@ -0,0 +1,60 @@
|
||||
#include "client/render/shader/BulletShader.h"
|
||||
|
||||
namespace blitz {
|
||||
namespace shader {
|
||||
|
||||
static const std::string vertexSource = ShaderProgram::GetShaderHeader() + R"(
|
||||
|
||||
layout(location = 0) in vec3 position;
|
||||
layout(location = 1) in float decay;
|
||||
|
||||
uniform mat4 viewMatrix;
|
||||
uniform mat4 projectionMatrix;
|
||||
|
||||
out float pass_decay;
|
||||
|
||||
void main(void){
|
||||
pass_decay = decay;
|
||||
gl_Position = projectionMatrix * viewMatrix * vec4(position, 1.0);
|
||||
}
|
||||
)";
|
||||
|
||||
static const std::string fragmentSource = ShaderProgram::GetShaderHeader() + R"(
|
||||
|
||||
in float pass_decay;
|
||||
|
||||
out vec4 out_color;
|
||||
|
||||
uniform float alpha;
|
||||
|
||||
void main(void){
|
||||
out_color = vec4(1.0, 0.0, 0.0, alpha);
|
||||
}
|
||||
)";
|
||||
|
||||
BulletShader::BulletShader() : ShaderProgram() {}
|
||||
|
||||
bool BulletShader::LoadShader() {
|
||||
return ShaderProgram::LoadProgram(vertexSource, fragmentSource);
|
||||
}
|
||||
|
||||
void BulletShader::GetAllUniformLocation() {
|
||||
m_LocationViewMatrix = static_cast<unsigned int>(GetUniformLocation("viewMatrix"));
|
||||
m_LocationProjectionMatrix = static_cast<unsigned int>(GetUniformLocation("projectionMatrix"));
|
||||
m_LocationAlpha = static_cast<unsigned int>(GetUniformLocation("alpha"));
|
||||
}
|
||||
|
||||
void BulletShader::SetProjectionMatrix(const Mat4f& proj) const {
|
||||
LoadMat4(m_LocationProjectionMatrix, proj);
|
||||
}
|
||||
|
||||
void BulletShader::SetViewMatrix(const Mat4f& view) const {
|
||||
LoadMat4(m_LocationViewMatrix, view);
|
||||
}
|
||||
|
||||
void BulletShader::SetDecay(float alpha) const {
|
||||
LoadFloat(m_LocationAlpha, alpha);
|
||||
}
|
||||
|
||||
} // namespace shader
|
||||
} // namespace blitz
|
||||
@@ -44,6 +44,7 @@ void main(void){
|
||||
float brightness = diffuse;
|
||||
|
||||
out_color = vec4(1.0, 1.0, 1.0, 1.0) * brightness;
|
||||
out_color.w = 1.0;
|
||||
}
|
||||
)";
|
||||
|
||||
|
||||
@@ -35,6 +35,7 @@ out vec4 out_color;
|
||||
void main(void){
|
||||
|
||||
out_color = pos;
|
||||
out_color.w = 1.0;
|
||||
// out_color = gl_Position + vec4(0.5, 0.5, 0.5, 0);
|
||||
// out_color = texture(textureSampler, pass_textureCoords);
|
||||
|
||||
|
||||
@@ -71,6 +71,7 @@ void main(void){
|
||||
float brightness = (diffuse + specular) / attenuationFactor;
|
||||
|
||||
out_color = brightness * texture(textureSampler, pass_textureCoords);
|
||||
out_color.w = 1.0;
|
||||
|
||||
}
|
||||
)";
|
||||
|
||||
@@ -142,8 +142,6 @@ void ServerConnexion::HandlePacket(const protocol::PlayerPositionAndRotationPack
|
||||
}
|
||||
|
||||
void ServerConnexion::HandlePacket(const protocol::PlayerShootPacket* packet) {
|
||||
utils::LOGD(utils::Format("[Server] Le joueur %s a essayé de tirer", m_Player->GetName().c_str()));
|
||||
|
||||
protocol::PlayerShootPacket broadcastShoot(packet->GetPosition(), packet->GetYaw(), packet->GetPitch(), m_Player->GetID());
|
||||
m_Server->BroadcastPacket(&broadcastShoot);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user