add debug hitboxes render

This commit is contained in:
MihailRis 2024-06-25 20:11:46 +03:00
parent ee7328dcad
commit f3c5afa1ab
5 changed files with 33 additions and 6 deletions

View File

@ -1,8 +1,12 @@
local DROP_FORCE = 8
local DROP_INIT_VEL = {0, 3, 0}
function on_hud_open()
input.add_callback("player.drop", function ()
local pid = hud.get_player()
local pvel = {player.get_vel(pid)}
local eid = entity.test()
entity.set_vel(eid, vec3.add(vec3.mul(player.get_dir(pid), {8, 8, 8}), vec3.add(pvel, {0, 3, 0})))
local throw_force = vec3.mul(player.get_dir(pid), DROP_FORCE)
entity.set_vel(eid, vec3.add(throw_force, vec3.add(pvel, DROP_INIT_VEL)))
end)
end

View File

@ -215,8 +215,6 @@ void WorldRenderer::renderBlockSelection(Camera* camera, Shader* linesShader) {
? block->rt.hitboxes[selection.vox.state.rotation]
: block->hitboxes;
linesShader->use();
linesShader->uniformMatrix("u_projview", camera->getProjView());
lineBatch->lineWidth(2.0f);
for (auto& hitbox: hitboxes) {
const glm::vec3 center = glm::vec3(pos) + hitbox.center();
@ -226,6 +224,17 @@ void WorldRenderer::renderBlockSelection(Camera* camera, Shader* linesShader) {
lineBatch->line(point, point+norm*0.5f, glm::vec4(1.0f, 0.0f, 1.0f, 1.0f));
}
}
}
void WorldRenderer::renderLines(Camera* camera, Shader* linesShader) {
linesShader->use();
linesShader->uniformMatrix("u_projview", camera->getProjView());
if (player->selection.vox.id != BLOCK_VOID) {
renderBlockSelection(camera, linesShader);
}
if (player->debug) {
level->entities->renderDebug(*lineBatch);
}
lineBatch->render();
}
@ -313,9 +322,9 @@ void WorldRenderer::draw(
ctx.setDepthTest(true);
ctx.setCullFace(true);
renderLevel(ctx, camera, settings);
// Selected block
if (player->selection.vox.id != BLOCK_VOID && hudVisible){
renderBlockSelection(camera, linesShader);
// Debug lines
if (hudVisible){
renderLines(camera, linesShader);
}
}

View File

@ -48,6 +48,8 @@ class WorldRenderer {
/// @param camera active camera
/// @param linesShader shader used
void renderBlockSelection(Camera* camera, Shader* linesShader);
void renderLines(Camera* camera, Shader* linesShader);
/// @brief Render all debug lines (chunks borders, coord system guides)
/// @param context graphics context

View File

@ -5,6 +5,7 @@
#include "../physics/Hitbox.hpp"
#include "../physics/PhysicsSolver.hpp"
#include "../graphics/render/ModelBatch.hpp"
#include "../graphics/core/LineBatch.hpp"
#include "../graphics/core/Model.hpp"
#include "../maths/FrustumCulling.hpp"
@ -54,6 +55,14 @@ void Entities::updatePhysics(float delta){
}
}
void Entities::renderDebug(LineBatch& batch) {
batch.lineWidth(1.0f);
auto view = registry.view<Transform, Hitbox>();
for (auto [entity, transform, hitbox] : view.each()) {
batch.box(hitbox.position, hitbox.halfsize * 2.0f, glm::vec4(1.0f));
}
}
void Entities::render(Assets* assets, ModelBatch& batch, Frustum& frustum) {
auto view = registry.view<Transform>();
auto model = assets->get<model::Model>("cube");

View File

@ -24,6 +24,7 @@ struct Transform {
class Level;
class Assets;
class LineBatch;
class ModelBatch;
class Frustum;
class Rig;
@ -60,6 +61,8 @@ class Entities {
public:
Entities(Level* level);
void updatePhysics(float delta);
void renderDebug(LineBatch& batch);
void render(Assets* assets, ModelBatch& batch, Frustum& frustum);
entityid_t drop(glm::vec3 pos);