Merge branch 'MihailRis:main' into main

This commit is contained in:
clasher113 2023-11-29 17:13:43 +02:00 committed by GitHub
commit 64ef5404b1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 144 additions and 21 deletions

View File

@ -1,9 +1,11 @@
#include "Content.h" #include "Content.h"
#include <stdexcept> #include <stdexcept>
#include <glm/glm.hpp>
#include "../voxels/Block.h" #include "../voxels/Block.h"
using glm::vec3;
using std::vector; using std::vector;
using std::string; using std::string;
using std::unordered_map; using std::unordered_map;
@ -22,6 +24,24 @@ Content* ContentBuilder::build() {
for (const string& name : blockIds) { for (const string& name : blockIds) {
Block* def = blockDefs[name]; Block* def = blockDefs[name];
def->id = blockDefsIndices.size(); def->id = blockDefsIndices.size();
def->rt.emissive = *((uint32_t*)def->emission);
// build hitbox grid 3d for raycasts
const AABB& hitbox = def->hitbox;
for (uint gy = 0; gy < BLOCK_AABB_GRID; gy++) {
for (uint gz = 0; gz < BLOCK_AABB_GRID; gz++) {
for (uint gx = 0; gx < BLOCK_AABB_GRID; gx++) {
float x = gx / float(BLOCK_AABB_GRID);
float y = gy / float(BLOCK_AABB_GRID);
float z = gz / float(BLOCK_AABB_GRID);
bool flag = hitbox.inside({x, y, z});
if (!flag)
def->rt.solid = false;
def->rt.hitboxGrid[gy][gz][gx] = flag;
}
}
}
blockDefsIndices.push_back(def); blockDefsIndices.push_back(def);
if (groups->find(def->drawGroup) == groups->end()) { if (groups->find(def->drawGroup) == groups->end()) {
groups->insert(def->drawGroup); groups->insert(def->drawGroup);

View File

@ -1,11 +1,15 @@
#include "definitions.h" #include "definitions.h"
#include <glm/glm.hpp>
#include "content/Content.h" #include "content/Content.h"
#include "window/Window.h" #include "window/Window.h"
#include "window/Events.h" #include "window/Events.h"
#include "window/input.h" #include "window/input.h"
#include "voxels/Block.h" #include "voxels/Block.h"
using glm::vec3;
// All in-game definitions (blocks, items, etc..) // All in-game definitions (blocks, items, etc..)
void setup_definitions(ContentBuilder* builder) { void setup_definitions(ContentBuilder* builder) {
Block* block = new Block("core:air", "air"); Block* block = new Block("core:air", "air");
@ -70,7 +74,7 @@ void setup_definitions(ContentBuilder* builder) {
block->lightPassing = true; block->lightPassing = true;
block->obstacle = false; block->obstacle = false;
block->model = BlockModel::xsprite; block->model = BlockModel::xsprite;
block->hitboxScale = 0.5f; block->hitbox.scale(vec3(0.7f), vec3(0.5f, 0.0f, 0.5f));
builder->add(block); builder->add(block);
block = new Block("base:flower", "flower"); block = new Block("base:flower", "flower");
@ -78,6 +82,7 @@ void setup_definitions(ContentBuilder* builder) {
block->lightPassing = true; block->lightPassing = true;
block->obstacle = false; block->obstacle = false;
block->model = BlockModel::xsprite; block->model = BlockModel::xsprite;
block->hitbox.scale(vec3(0.7f));
builder->add(block); builder->add(block);
block = new Block("base:brick", "brick"); block = new Block("base:brick", "brick");

View File

@ -36,7 +36,9 @@ using gui::GUI;
Engine::Engine(EngineSettings& settings, EnginePaths* paths, Content* content) Engine::Engine(EngineSettings& settings, EnginePaths* paths, Content* content)
: settings(settings), content(content), paths(paths) { : settings(settings), content(content), paths(paths) {
Window::initialize(settings.display); if (Window::initialize(settings.display)){
throw initialize_error("could not initialize window");
}
Shader::preprocessor->setLibFolder(paths->getResources()/path("shaders/lib")); Shader::preprocessor->setLibFolder(paths->getResources()/path("shaders/lib"));
assets = new Assets(); assets = new Assets();

View File

@ -31,6 +31,7 @@
#include "graphics/Skybox.h" #include "graphics/Skybox.h"
using glm::vec3; using glm::vec3;
using glm::vec4;
using std::string; using std::string;
using std::shared_ptr; using std::shared_ptr;
@ -168,13 +169,11 @@ void WorldRenderer::draw(const GfxContext& pctx, Camera* camera, bool occlusion)
linesShader->use(); linesShader->use();
linesShader->uniformMatrix("u_projview", camera->getProjView()); linesShader->uniformMatrix("u_projview", camera->getProjView());
lineBatch->lineWidth(2.0f); lineBatch->lineWidth(2.0f);
if (block->model == BlockModel::block){
lineBatch->box(pos.x+0.5f, pos.y+0.5f, pos.z+0.5f, const AABB& hitbox = block->hitbox;
1.008f,1.008f,1.008f, 0,0,0,0.5f); const vec3 center = pos + hitbox.center();
} else if (block->model == BlockModel::xsprite){ const vec3 size = hitbox.size();
lineBatch->box(pos.x+0.5f, pos.y+0.35f, pos.z+0.5f, lineBatch->box(center, size, vec4(0.0f, 0.0f, 0.0f, 0.5f));
0.805f,0.705f,0.805f, 0,0,0,0.5f);
}
lineBatch->render(); lineBatch->render();
} }
skybox->unbind(); skybox->unbind();

View File

@ -2,6 +2,7 @@
#define GRAPHICS_LINEBATCH_H_ #define GRAPHICS_LINEBATCH_H_
#include <stdlib.h> #include <stdlib.h>
#include <glm/glm.hpp>
class Mesh; class Mesh;
@ -19,6 +20,11 @@ public:
void box(float x, float y, float z, float w, float h, float d, void box(float x, float y, float z, float w, float h, float d,
float r, float g, float b, float a); float r, float g, float b, float a);
inline void box(glm::vec3 xyz, glm::vec3 whd, glm::vec4 rgba) {
return box(xyz.x, xyz.y, xyz.z, whd.x, whd.y, whd.z,
rgba.r, rgba.g, rgba.b, rgba.a);
}
void render(); void render();
void lineWidth(float width); void lineWidth(float width);
}; };

59
src/maths/aabb.h Normal file
View File

@ -0,0 +1,59 @@
#ifndef MATHS_AABB_H_
#define MATHS_AABB_H_
#include <glm/glm.hpp>
// Axis Aligned Bounding Box
struct AABB {
glm::vec3 a {0.0f};
glm::vec3 b {1.0f};
/* Get AABB point with minimal x,y,z */
inline glm::vec3 min() const {
return glm::min(a, b);
}
/* Get AABB point with minimal x,y,z */
inline glm::vec3 max() const {
return glm::max(a, b);
}
/* Get AABB dimensions: width, height, depth */
inline glm::vec3 size() const {
return glm::vec3(
fabs(b.x - a.x),
fabs(b.y - a.y),
fabs(b.z - a.z)
);
}
inline glm::vec3 center() const {
return (a + b) * 0.5f;
}
/* Multiply AABB size from center */
inline void scale(const glm::vec3 mul) {
glm::vec3 center = (a + b) * 0.5f;
a = (a - center) * mul + center;
b = (b - center) * mul + center;
}
/* Multiply AABB size from given origin */
inline void scale(const glm::vec3 mul, const glm::vec3 orig) {
glm::vec3 beg = min();
glm::vec3 end = max();
glm::vec3 center = glm::mix(beg, end, orig);
a = (a - center) * mul + center;
b = (b - center) * mul + center;
}
/* Check if given point is inside */
inline bool inside(const glm::vec3 pos) const {
const glm::vec3 p = min();
const glm::vec3 s = size();
return !(pos.x < p.x || pos.y < p.y || pos.z < p.z ||
pos.x >= p.x+s.x || pos.y >= p.y+s.y || pos.z >= p.z+s.z);
}
};
#endif // MATHS_AABB_H_

View File

@ -3,6 +3,8 @@
#include <string> #include <string>
#include "../maths/aabb.h"
#define FACE_MX 0 #define FACE_MX 0
#define FACE_PX 1 #define FACE_PX 1
#define FACE_MY 2 #define FACE_MY 2
@ -10,6 +12,8 @@
#define FACE_MZ 4 #define FACE_MZ 4
#define FACE_PZ 5 #define FACE_PZ 5
#define BLOCK_AABB_GRID 16
enum class BlockModel { enum class BlockModel {
none, block, xsprite none, block, xsprite
}; };
@ -29,7 +33,13 @@ public:
bool selectable = true; bool selectable = true;
bool breakable = true; bool breakable = true;
bool rotatable = false; bool rotatable = false;
float hitboxScale = 1; AABB hitbox;
struct {
bool solid = true;
bool emissive = false;
bool hitboxGrid[BLOCK_AABB_GRID][BLOCK_AABB_GRID][BLOCK_AABB_GRID];
} rt;
Block(std::string name, std::string texture); Block(std::string name, std::string texture);
}; };

View File

@ -200,11 +200,31 @@ voxel* Chunks::rayCast(vec3 start,
while (t <= maxDist){ while (t <= maxDist){
voxel* voxel = get(ix, iy, iz); voxel* voxel = get(ix, iy, iz);
if (voxel == nullptr || contentIds->getBlockDef(voxel->id)->selectable){ const Block* def = nullptr;
if (voxel == nullptr || (def = contentIds->getBlockDef(voxel->id))->selectable){
end.x = px + t * dx; end.x = px + t * dx;
end.y = py + t * dy; end.y = py + t * dy;
end.z = pz + t * dz; end.z = pz + t * dz;
// TODO: replace this dumb solution with something better
if (def && !def->rt.solid) {
const AABB& box = def->hitbox;
const int subs = BLOCK_AABB_GRID;
iend = vec3(ix, iy, iz);
end -= iend;
for (int i = 0; i < subs; i++) {
end.x += dx / float(subs);
end.y += dy / float(subs);
end.z += dz / float(subs);
if (box.inside(end)) {
norm.x = norm.y = norm.z = 0.0f;
if (steppedIndex == 0) norm.x = -stepx;
if (steppedIndex == 1) norm.y = -stepy;
if (steppedIndex == 2) norm.z = -stepz;
return voxel;
}
}
} else {
iend.x = ix; iend.x = ix;
iend.y = iy; iend.y = iy;
iend.z = iz; iend.z = iz;
@ -215,6 +235,7 @@ voxel* Chunks::rayCast(vec3 start,
if (steppedIndex == 2) norm.z = -stepz; if (steppedIndex == 2) norm.z = -stepz;
return voxel; return voxel;
} }
}
if (txMax < tyMax) { if (txMax < tyMax) {
if (txMax < tzMax) { if (txMax < tzMax) {
ix += stepx; ix += stepx;

View File

@ -87,7 +87,8 @@ int Window::initialize(DisplaySettings& settings){
glfwInit(); glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_ANY_PROFILE);
glfwWindowHint(GLFW_RESIZABLE, GL_TRUE); glfwWindowHint(GLFW_RESIZABLE, GL_TRUE);
glfwWindowHint(GLFW_SAMPLES, settings.samples); glfwWindowHint(GLFW_SAMPLES, settings.samples);