This commit is contained in:
@clasher113 2023-11-30 14:05:23 +02:00
commit c691183bf8
25 changed files with 194 additions and 77 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.9 KiB

View File

@ -23,10 +23,12 @@ Content* ContentBuilder::build() {
DrawGroups* groups = new DrawGroups; DrawGroups* groups = new DrawGroups;
for (const string& name : blockIds) { for (const string& name : blockIds) {
Block* def = blockDefs[name]; Block* def = blockDefs[name];
def->id = blockDefsIndices.size();
// Generating runtime info
def->rt.id = blockDefsIndices.size();
def->rt.emissive = *((uint32_t*)def->emission); def->rt.emissive = *((uint32_t*)def->emission);
// build hitbox grid 3d for raycasts // building hitbox grid 3d for raycasts
const AABB& hitbox = def->hitbox; const AABB& hitbox = def->hitbox;
for (uint gy = 0; gy < BLOCK_AABB_GRID; gy++) { for (uint gy = 0; gy < BLOCK_AABB_GRID; gy++) {
for (uint gz = 0; gz < BLOCK_AABB_GRID; gz++) { for (uint gz = 0; gz < BLOCK_AABB_GRID; gz++) {

View File

@ -105,6 +105,18 @@ void setup_definitions(ContentBuilder* builder) {
block = new Block("base:blue_lamp", "blue_lamp"); block = new Block("base:blue_lamp", "blue_lamp");
block->emission[2] = 15; block->emission[2] = 15;
builder->add(block); builder->add(block);
// block added for test
block = new Block("base:pane", "pane");
block->textureFaces[FACE_MX] = "pane_side";
block->textureFaces[FACE_PX] = "pane_side";
block->textureFaces[FACE_MY] = "pane_side_2";
block->textureFaces[FACE_PY] = "pane_side_2";
block->model = BlockModel::aabb;
block->hitbox.scale(vec3(1.0f, 1.0f, 0.2f), vec3(0.5f, 0.5f, 0.0f));
block->lightPassing = true;
block->skyLightPassing = true;
builder->add(block);
} }
void setup_bindings() { void setup_bindings() {

View File

@ -189,6 +189,8 @@ void WorldRenderer::draw(const GfxContext& pctx, Camera* camera){
Block* block = contentIds->getBlockDef(id); Block* block = contentIds->getBlockDef(id);
assert(block != nullptr); assert(block != nullptr);
const vec3 pos = PlayerController::selectedBlockPosition; const vec3 pos = PlayerController::selectedBlockPosition;
const vec3 point = PlayerController::selectedPointPosition;
const vec3 norm = PlayerController::selectedBlockNormal;
const AABB& hitbox = block->hitbox; const AABB& hitbox = block->hitbox;
const vec3 center = pos + hitbox.center(); const vec3 center = pos + hitbox.center();
const vec3 size = hitbox.size(); const vec3 size = hitbox.size();
@ -196,6 +198,8 @@ void WorldRenderer::draw(const GfxContext& pctx, Camera* camera){
linesShader->uniformMatrix("u_projview", camera->getProjView()); linesShader->uniformMatrix("u_projview", camera->getProjView());
lineBatch->lineWidth(2.0f); lineBatch->lineWidth(2.0f);
lineBatch->box(center, size + vec3(0.02), vec4(0.f, 0.f, 0.f, 0.5f)); lineBatch->box(center, size + vec3(0.02), vec4(0.f, 0.f, 0.f, 0.5f));
if (level->player->debug)
lineBatch->line(point, point+norm*0.5f, vec4(1.0f, 0.0f, 1.0f, 1.0f));
lineBatch->render(); lineBatch->render();
} }
skybox->unbind(); skybox->unbind();
@ -206,6 +210,7 @@ void WorldRenderer::draw(const GfxContext& pctx, Camera* camera){
ctx.depthTest(true); ctx.depthTest(true);
linesShader->use(); linesShader->use();
if (settings.debug.showChunkBorders){ if (settings.debug.showChunkBorders){
linesShader->uniformMatrix("u_projview", camera->getProjView()); linesShader->uniformMatrix("u_projview", camera->getProjView());
vec3 coord = level->player->camera->position; vec3 coord = level->player->camera->position;

View File

@ -12,6 +12,8 @@
#include "../../window/input.h" #include "../../window/input.h"
#include "../../window/Camera.h" #include "../../window/Camera.h"
using glm::vec2;
using glm::vec3;
using std::string; using std::string;
using std::shared_ptr; using std::shared_ptr;
using namespace gui; using namespace gui;

View File

@ -11,6 +11,8 @@ using std::string;
using std::wstring; using std::wstring;
using std::shared_ptr; using std::shared_ptr;
using glm::vec2; using glm::vec2;
using glm::vec3;
using glm::vec4;
#define KEY_ESCAPE 256 #define KEY_ESCAPE 256
#define KEY_ENTER 257 #define KEY_ENTER 257

View File

@ -171,7 +171,7 @@ void Panel::refresh() {
} }
if (resizing_) { if (resizing_) {
if (maxLength_) if (maxLength_)
this->size(vec2(size.x, min(maxLength_, (int)(y+padding.w)))); this->size(vec2(size.x, glm::min(maxLength_, (int)(y+padding.w))));
else else
this->size(vec2(size.x, y+padding.w)); this->size(vec2(size.x, y+padding.w));
} }
@ -193,7 +193,7 @@ void Panel::refresh() {
bool increased = maxh > size.y; bool increased = maxh > size.y;
if (resizing_) { if (resizing_) {
if (maxLength_) if (maxLength_)
this->size(vec2(min(maxLength_, (int)(x+padding.z)), size.y)); this->size(vec2(glm::min(maxLength_, (int)(x+padding.z)), size.y));
else else
this->size(vec2(x+padding.z, size.y)); this->size(vec2(x+padding.z, size.y));
} }

View File

@ -91,11 +91,15 @@ HudRenderer::HudRenderer(Engine* engine,
L" visible: "+std::to_wstring(level->chunks->visible); L" visible: "+std::to_wstring(level->chunks->visible);
}))); })));
panel->add(shared_ptr<Label>(create_label([this](){ panel->add(shared_ptr<Label>(create_label([this](){
auto player = this->level->player;
auto indices = this->level->content->indices;
auto def = indices->getBlockDef(player->selectedVoxel.id);
std::wstringstream stream; std::wstringstream stream;
stream << std::hex << this->level->player->selectedVoxel.states; stream << std::hex << this->level->player->selectedVoxel.states;
if (def) {
auto player = this->level->player; stream << L" (" << util::str2wstr_utf8(def->name) << L")";
return L"block-selected: "+std::to_wstring(player->selectedVoxel.id)+ }
return L"block: "+std::to_wstring(player->selectedVoxel.id)+
L" "+stream.str(); L" "+stream.str();
}))); })));
panel->add(shared_ptr<Label>(create_label([this](){ panel->add(shared_ptr<Label>(create_label([this](){
@ -236,12 +240,17 @@ void HudRenderer::drawContentAccess(const GfxContext& ctx, Player* player) {
} else { } else {
tint = vec4(1.0f); tint = vec4(1.0f);
} }
drawBlockPreview(cblock, x, y, icon_size, icon_size, tint);
if (cblock->model == BlockModel::block){
batch->blockSprite(x, y, icon_size, icon_size, &cache->getRegion(cblock->id, 0), tint);
} else if (cblock->model == BlockModel::xsprite){
batch->sprite(x, y, icon_size, icon_size, cache->getRegion(cblock->id, 3), tint);
} }
}
void HudRenderer::drawBlockPreview(const Block* def, float x, float y, float w, float h, vec4 tint) {
if (def->model == BlockModel::block){
batch->blockSprite(x, y, w, h, &cache->getRegion(def->rt.id, 0), tint);
} else if (def->model == BlockModel::aabb) {
batch->blockSprite(x, y, w, h, &cache->getRegion(def->rt.id, 0), tint, def->hitbox.size());
} else if (def->model == BlockModel::xsprite){
batch->sprite(x, y, w, h, cache->getRegion(def->rt.id, 3), tint);
} }
} }
@ -311,11 +320,7 @@ void HudRenderer::draw(const GfxContext& ctx){
{ {
Block* cblock = contentIds->getBlockDef(player->choosenBlock); Block* cblock = contentIds->getBlockDef(player->choosenBlock);
assert(cblock != nullptr); assert(cblock != nullptr);
if (cblock->model == BlockModel::block){ drawBlockPreview(cblock, width - 56, uicamera->fov - 56, 48, 48, vec4(1.0f));
batch->blockSprite(width-56, uicamera->fov - 56, 48, 48, &cache->getRegion(cblock->id, 0), vec4(1.0f));
} else if (cblock->model == BlockModel::xsprite){
batch->sprite(width-56, uicamera->fov - 56, 48, 48, cache->getRegion(cblock->id, 3), vec4(1.0f));
}
} }
if (pause) { if (pause) {

View File

@ -4,11 +4,14 @@
#include <string> #include <string>
#include <memory> #include <memory>
#include <glm/glm.hpp>
#include "../graphics/GfxContext.h" #include "../graphics/GfxContext.h"
class Batch2D; class Batch2D;
class Camera; class Camera;
class Level; class Level;
class Block;
class Assets; class Assets;
class Player; class Player;
class Level; class Level;
@ -38,6 +41,8 @@ class HudRenderer {
gui::GUI* gui; gui::GUI* gui;
const ContentGfxCache* const cache; const ContentGfxCache* const cache;
WorldRenderer* renderer; WorldRenderer* renderer;
void drawBlockPreview(const Block* def, float x, float y, float w, float h, glm::vec4 tint);
public: public:
HudRenderer(Engine* engine, HudRenderer(Engine* engine,
Level* level, Level* level,

View File

@ -7,6 +7,10 @@
#define VERTEX_SIZE 8 #define VERTEX_SIZE 8
using glm::vec2;
using glm::vec3;
using glm::vec4;
Batch2D::Batch2D(size_t capacity) : capacity(capacity), offset(0), color(1.0f, 1.0f, 1.0f, 1.0f){ Batch2D::Batch2D(size_t capacity) : capacity(capacity), offset(0), color(1.0f, 1.0f, 1.0f, 1.0f){
const vattr attrs[] = { const vattr attrs[] = {
{2}, {2}, {4}, {0} {2}, {2}, {4}, {0}
@ -299,8 +303,11 @@ void Batch2D::sprite(float x, float y, float w, float h, int atlasRes, int index
rect(x, y, w, h, u, v, scale, scale, tint.r, tint.g, tint.b, tint.a); rect(x, y, w, h, u, v, scale, scale, tint.r, tint.g, tint.b, tint.a);
} }
void Batch2D::blockSprite(float x, float y, float w, float h, const UVRegion regions[], vec4 tint){
// TODO: rewrite it #include <iostream>
void Batch2D::blockSprite(float x, float y, float w, float h, const UVRegion regions[], vec4 tint, vec3 size){
// TODO: replace it using actual 3D with ortho projection
float uu = (regions[3].u1); float uu = (regions[3].u1);
float vu = (regions[3].v1); float vu = (regions[3].v1);
@ -313,16 +320,24 @@ void Batch2D::blockSprite(float x, float y, float w, float h, const UVRegion reg
if (this->index + 18*VERTEX_SIZE >= capacity) if (this->index + 18*VERTEX_SIZE >= capacity)
render(); render();
float d = (w + h) * 0.5f;
float ar = 0.88f; float ar = 0.88f;
float ox = x + (w * 0.5f); float ox = x + (w * 0.5f);
float sx = w * 0.5f * ar; float sx = w * 0.5f * ar;
vec2 points[7] = {vec2(ox, y+(h*0.5f)), float hh = h * 0.25f;
vec2(ox-sx, y+(h*0.25f)), float ww = w * 0.25f;
vec2(ox, y), float dd = d * 0.25f;
vec2(ox+sx, y+(h*0.25f)),
vec2(ox+sx, y+(h*0.75f)), vec3 half = size * h * 0.25f;
vec2(ox, y+h),
vec2(ox-sx, y+(h*0.75f))}; y += hh * 2.0f;
vec2 points[7] = {vec2(ox-ww+half.x+dd-half.z, y+hh-half.y), // center
vec2(ox-sx+ww-half.x+dd-half.z, y-half.y+ww-half.x), // left
vec2(ox+ww-half.x-dd+half.z, y-hh-half.y+ww-half.x+dd-half.z), // top
vec2(ox+sx-ww+half.x-dd+half.z, y-half.y+dd-half.z), // right
vec2(ox+sx-ww+half.x-dd+half.z, y+half.y+dd-half.z), // b-right
vec2(ox-ww+half.x+dd-half.z, y+hh+half.y), // bottom
vec2(ox-sx+ww-half.x+dd-half.z, y+half.y+ww-half.x)}; // b-left
vec2 uvpoints[8] = {vec2(uu, vu), vec2 uvpoints[8] = {vec2(uu, vu),
vec2(uu+scalex, vu), vec2(uu+scalex, vu),

View File

@ -6,8 +6,6 @@
#include "UVRegion.h" #include "UVRegion.h"
using namespace glm;
class Mesh; class Mesh;
class Texture; class Texture;
class Sprite; class Sprite;
@ -25,8 +23,8 @@ class Batch2D {
void vertex(float x, float y, void vertex(float x, float y,
float u, float v, float u, float v,
float r, float g, float b, float a); float r, float g, float b, float a);
void vertex(vec2 point, void vertex(glm::vec2 point,
vec2 uvpoint, glm::vec2 uvpoint,
float r, float g, float b, float a); float r, float g, float b, float a);
public: public:
@ -37,10 +35,10 @@ public:
void begin(); void begin();
void texture(Texture* texture); void texture(Texture* texture);
void sprite(float x, float y, float w, float h, const UVRegion& region, vec4 tint); void sprite(float x, float y, float w, float h, const UVRegion& region, glm::vec4 tint);
void sprite(Sprite* sprite); void sprite(Sprite* sprite);
void sprite(float x, float y, float w, float h, int atlasRes, int index, vec4 tint); void sprite(float x, float y, float w, float h, int atlasRes, int index, glm::vec4 tint);
void blockSprite(float x, float y, float w, float h, const UVRegion regions[], vec4 tint); void blockSprite(float x, float y, float w, float h, const UVRegion regions[], glm::vec4 tint, glm::vec3 size=glm::vec3(1.0f, 1.0f, 1.0f));
void point(float x, float y, float r, float g, float b, float a); void point(float x, float y, float r, float g, float b, float a);
void line(float x1, float y1, float x2, float y2, float r, float g, float b, float a); void line(float x1, float y1, float x2, float y2, float r, float g, float b, float a);
void rect(float x, float y, void rect(float x, float y,
@ -48,7 +46,7 @@ public:
float ox, float oy, float ox, float oy,
float angle, UVRegion region, float angle, UVRegion region,
bool flippedX, bool flippedY, bool flippedX, bool flippedY,
vec4 tint); glm::vec4 tint);
void rect(float x, float y, float w, float h); void rect(float x, float y, float w, float h);
void rect(float x, float y, float w, float h, void rect(float x, float y, float w, float h,

View File

@ -189,6 +189,57 @@ void BlocksRenderer::blockXSprite(int x, int y, int z, const vec3& size, const U
vec3(-1.0f, 0, 1.0f), vec3(0, 1, 0), texface2, lights, do_tint(0.8f)); vec3(-1.0f, 0, 1.0f), vec3(0, 1, 0), texface2, lights, do_tint(0.8f));
} }
void BlocksRenderer::blockCubeShaded(const vec3& pos, const vec3& size, const UVRegion(&texfaces)[6], const Block* block, ubyte states) {
int rot = 0;
float x = pos.x;
float y = pos.y;
float z = pos.z;
{
vec4 lights[]{
pickSoftLight(x, y, z + 1, {1, 0, 0}, {0, 1, 0}),
pickSoftLight(x + 1, y, z + 1, {1, 0, 0}, {0, 1, 0}),
pickSoftLight(x + 1, y + 1, z + 1, {1, 0, 0}, {0, 1, 0}),
pickSoftLight(x, y + 1, z + 1, {1, 0, 0}, {0, 1, 0}) };
face(vec3(x, y, z), size.x, size.y, vec3(1, 0, 0), vec3(0, 1, 0), texfaces[5], lights, do_tint(0.9f), rot == 1);
} {
vec4 lights[]{
pickSoftLight(pos.x, pos.y, pos.z - 1, {-1, 0, 0}, {0, 1, 0}),
pickSoftLight(pos.x - 1, pos.y, pos.z - 1, {-1, 0, 0}, {0, 1, 0}),
pickSoftLight(pos.x - 1, pos.y + 1, pos.z - 1, {-1, 0, 0}, {0, 1, 0}),
pickSoftLight(pos.x, pos.y + 1, pos.z - 1, {-1, 0, 0}, {0, 1, 0}) };
face(vec3(x + size.x, y, z - size.z), size.x, size.y, vec3(-1, 0, 0), vec3(0, 1, 0), texfaces[4], lights, do_tint(0.75f), rot == 1);
} {
vec4 lights[]{
pickSoftLight(x, pos.y + 1, pos.z + 1, {1, 0, 0}, {0, 0, 1}),
pickSoftLight(x + 1, pos.y + 1, pos.z + 1, {1, 0, 0}, {0, 0, 1}),
pickSoftLight(x + 1, pos.y + 1, pos.z, {1, 0, 0}, {0, 0, 1}),
pickSoftLight(x, pos.y + 1, pos.z, {1, 0, 0}, {0, 0, 1}) };
face(vec3(x, y + size.y, z), size.x, size.z, vec3(1, 0, 0), vec3(0, 0, -1), texfaces[3], lights, vec4(1.0f), rot == 1);
} {
vec4 lights[]{
pickSoftLight(pos.x, pos.y - 1, pos.z - 1, {1, 0, 0}, {0, 0, -1}),
pickSoftLight(pos.x + 1, y - 1, pos.z - 1, {1, 0, 0}, {0, 0,-1}),
pickSoftLight(pos.x + 1, y - 1, pos.z, {1, 0, 0}, {0, 0, -1}),
pickSoftLight(x, y - 1, z, {1, 0, 0}, {0, 0, -1}) };
face(vec3(x, y, z - size.z), size.x, size.z, vec3(1, 0, 0), vec3(0, 0, 1), texfaces[2], lights, do_tint(0.6f), rot == 1);
} {
vec4 lights[]{
pickSoftLight(x - 1, y, z - 1, {0, 0, -1}, {0, 1, 0}),
pickSoftLight(x - 1, y, z, {0, 0, -1}, {0, 1, 0}),
pickSoftLight(x - 1, y + 1, z, {0, 0, -1}, {0, 1, 0}),
pickSoftLight(x - 1, y + 1, z - 1, {0, 0, -1}, {0, 1, 0}) };
face(vec3(x, y, z - size.z), size.z, size.y, vec3(0, 0, 1), vec3(0, 1, 0), texfaces[0], lights, do_tint(0.7f), rot == 3);
} {
vec4 lights[]{
pickSoftLight(x + 1, y, z, {0, 0, -1}, {0, 1, 0}),
pickSoftLight(x + 1, y, z - 1, {0, 0, -1}, {0, 1, 0}),
pickSoftLight(x + 1, y + 1, z - 1, {0, 0, -1}, {0, 1, 0}),
pickSoftLight(x + 1, y + 1, z, {0, 0, -1}, {0, 1, 0}) };
face(vec3(x + size.x, y, z), size.z, size.y, vec3(0, 0, -1), vec3(0, 1, 0), texfaces[1], lights, do_tint(0.8f), rot == 3);
}
}
void BlocksRenderer::blockCubeShaded(int x, int y, int z, const vec3& size, const UVRegion(&texfaces_)[6], const Block* block, ubyte states) { void BlocksRenderer::blockCubeShaded(int x, int y, int z, const vec3& size, const UVRegion(&texfaces_)[6], const Block* block, ubyte states) {
ubyte group = block->drawGroup; ubyte group = block->drawGroup;
UVRegion texfaces[6]; UVRegion texfaces[6];
@ -277,7 +328,7 @@ bool BlocksRenderer::isOpen(int x, int y, int z, ubyte group) const {
if (id == BLOCK_VOID) if (id == BLOCK_VOID)
return false; return false;
const Block& block = *blockDefsCache[id]; const Block& block = *blockDefsCache[id];
if (block.drawGroup != group && block.lightPassing) { if ((block.drawGroup != group && block.lightPassing) || !block.rt.solid) {
return true; return true;
} }
return !id; return !id;
@ -315,6 +366,11 @@ vec4 BlocksRenderer::pickSoftLight(int x, int y, int z,
pickLight(x - right.x, y - right.y, z - right.z)) * 0.25f; pickLight(x - right.x, y - right.y, z - right.z)) * 0.25f;
} }
vec4 BlocksRenderer::pickSoftLight(float x, float y, float z,
const ivec3& right, const ivec3& up) const {
return pickSoftLight(int(round(x)), int(round(y)), int(round(z)), right, up);
}
void BlocksRenderer::render(const voxel* voxels, int atlas_size) { void BlocksRenderer::render(const voxel* voxels, int atlas_size) {
int begin = chunk->bottom * (CHUNK_W * CHUNK_D); int begin = chunk->bottom * (CHUNK_W * CHUNK_D);
int end = chunk->top * (CHUNK_W * CHUNK_D); int end = chunk->top * (CHUNK_W * CHUNK_D);
@ -344,6 +400,14 @@ void BlocksRenderer::render(const voxel* voxels, int atlas_size) {
blockXSprite(x, y, z, vec3(1.0f), texfaces[FACE_MX], texfaces[FACE_MZ], 1.0f); blockXSprite(x, y, z, vec3(1.0f), texfaces[FACE_MX], texfaces[FACE_MZ], 1.0f);
break; break;
} }
case BlockModel::aabb: {
vec3 size = def.hitbox.size();
vec3 off = def.hitbox.min();
off.z *= -1.0f;
off.z = -1.0f-off.z + size.z;
blockCubeShaded(off+vec3(x,y,z), size, texfaces, &def, vox.states);
break;
}
default: default:
break; break;
} }

View File

@ -63,6 +63,7 @@ class BlocksRenderer {
void cube(const glm::vec3& coord, const glm::vec3& size, const UVRegion(&faces)[6]); void cube(const glm::vec3& coord, const glm::vec3& size, const UVRegion(&faces)[6]);
void blockCube(int x, int y, int z, const glm::vec3& size, const UVRegion(&faces)[6], ubyte group); void blockCube(int x, int y, int z, const glm::vec3& size, const UVRegion(&faces)[6], ubyte group);
void blockCubeShaded(int x, int y, int z, const glm::vec3& size, const UVRegion(&faces)[6], const Block* block, ubyte states); void blockCubeShaded(int x, int y, int z, const glm::vec3& size, const UVRegion(&faces)[6], const Block* block, ubyte states);
void blockCubeShaded(const glm::vec3& pos, const glm::vec3& size, const UVRegion(&faces)[6], const Block* block, ubyte states);
void blockXSprite(int x, int y, int z, const glm::vec3& size, const UVRegion& face1, const UVRegion& face2, float spread); void blockXSprite(int x, int y, int z, const glm::vec3& size, const UVRegion& face1, const UVRegion& face2, float spread);
bool isOpenForLight(int x, int y, int z) const; bool isOpenForLight(int x, int y, int z) const;
@ -70,6 +71,7 @@ class BlocksRenderer {
glm::vec4 pickLight(int x, int y, int z) const; glm::vec4 pickLight(int x, int y, int z) const;
glm::vec4 pickSoftLight(int x, int y, int z, const glm::ivec3& right, const glm::ivec3& up) const; glm::vec4 pickSoftLight(int x, int y, int z, const glm::ivec3& right, const glm::ivec3& up) const;
glm::vec4 pickSoftLight(float x, float y, float z, const glm::ivec3& right, const glm::ivec3& up) const;
void render(const voxel* voxels, int atlas_size); void render(const voxel* voxels, int atlas_size);
public: public:
BlocksRenderer(size_t capacity, const Content* content, const ContentGfxCache* cache, const EngineSettings& settings); BlocksRenderer(size_t capacity, const Content* content, const ContentGfxCache* cache, const EngineSettings& settings);

View File

@ -2,6 +2,8 @@
#include "Texture.h" #include "Texture.h"
#include "Batch2D.h" #include "Batch2D.h"
using glm::vec4;
Font::Font(std::vector<Texture*> pages, int lineHeight) : lineHeight_(lineHeight), pages(pages) { Font::Font(std::vector<Texture*> pages, int lineHeight) : lineHeight_(lineHeight), pages(pages) {
} }
@ -10,22 +12,6 @@ Font::~Font(){
delete texture; delete texture;
} }
// int Font::getGlyphWidth(char c) {
// switch (c){
// case 'l':
// case 'i':
// case 'j':
// case '|':
// case '.':
// case ',':
// case ':':
// case ';': return 7;
// case 't': return 8;
// case ' ': return 7;
// }
// return 7;
// }
int Font::lineHeight() const { int Font::lineHeight() const {
return lineHeight_; return lineHeight_;
} }

View File

@ -15,13 +15,16 @@ public:
LineBatch(size_t capacity=4096); LineBatch(size_t capacity=4096);
~LineBatch(); ~LineBatch();
inline void line(const glm::vec3 a, const glm::vec3 b, const glm::vec4 color) {
line(a.x, a.y, a.z, b.x, b.y, b.z, color.r, color.g, color.b, color.a);
}
void line(float x1, float y1, float z1, float x2, float y2, float z2, void line(float x1, float y1, float z1, float x2, float y2, float z2,
float r, float g, float b, float a); float r, float g, float b, float a);
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) { 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, box(xyz.x, xyz.y, xyz.z, whd.x, whd.y, whd.z,
rgba.r, rgba.g, rgba.b, rgba.a); rgba.r, rgba.g, rgba.b, rgba.a);
} }

View File

@ -4,10 +4,12 @@
#include "ImageData.h" #include "ImageData.h"
Texture::Texture(unsigned int id, int width, int height) : id(id), width(width), height(height) { Texture::Texture(uint id, int width, int height)
: id(id), width(width), height(height) {
} }
Texture::Texture(unsigned char* data, int width, int height, uint format) : width(width), height(height) { Texture::Texture(ubyte* data, int width, int height, uint format)
: width(width), height(height) {
glGenTextures(1, &id); glGenTextures(1, &id);
glBindTexture(GL_TEXTURE_2D, id); glBindTexture(GL_TEXTURE_2D, id);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
@ -28,7 +30,7 @@ void Texture::bind(){
glBindTexture(GL_TEXTURE_2D, id); glBindTexture(GL_TEXTURE_2D, id);
} }
void Texture::reload(unsigned char* data){ void Texture::reload(ubyte* data){
glBindTexture(GL_TEXTURE_2D, id); glBindTexture(GL_TEXTURE_2D, id);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid *) data); GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid *) data);
@ -36,6 +38,8 @@ void Texture::reload(unsigned char* data){
} }
Texture* Texture::from(const ImageData* image) { Texture* Texture::from(const ImageData* image) {
uint width = image->getWidth();
uint height = image->getHeight();
uint format; uint format;
const void* data = image->getData(); const void* data = image->getData();
switch (image->getFormat()) { switch (image->getFormat()) {
@ -44,5 +48,5 @@ Texture* Texture::from(const ImageData* image) {
default: default:
throw std::runtime_error("unsupported image data format"); throw std::runtime_error("unsupported image data format");
} }
return new Texture((unsigned char*)data, image->getWidth(), image->getHeight(), format); return new Texture((ubyte*)data, width, height, format);
} }

View File

@ -8,15 +8,15 @@ class ImageData;
class Texture { class Texture {
public: public:
unsigned int id; uint id;
int width; int width;
int height; int height;
Texture(unsigned int id, int width, int height); Texture(uint id, int width, int height);
Texture(unsigned char* data, int width, int height, uint format); Texture(ubyte* data, int width, int height, uint format);
~Texture(); ~Texture();
void bind(); void bind();
void reload(unsigned char* data); void reload(ubyte* data);
static Texture* from(const ImageData* image); static Texture* from(const ImageData* image);
}; };

View File

@ -107,6 +107,8 @@ void CameraControl::update(PlayerInput& input, float delta) {
} }
vec3 PlayerController::selectedBlockPosition; vec3 PlayerController::selectedBlockPosition;
vec3 PlayerController::selectedPointPosition;
vec3 PlayerController::selectedBlockNormal;
int PlayerController::selectedBlockId = -1; int PlayerController::selectedBlockId = -1;
PlayerController::PlayerController(Level* level, const EngineSettings& settings) PlayerController::PlayerController(Level* level, const EngineSettings& settings)
@ -179,8 +181,9 @@ void PlayerController::updateControls(float delta){
player->update(level, input, delta); player->update(level, input, delta);
} }
#include <iostream>
void PlayerController::updateInteraction(){ void PlayerController::updateInteraction(){
const ContentIndices* contentIds = level->contentIds; auto contentIds = level->content->indices;
Chunks* chunks = level->chunks; Chunks* chunks = level->chunks;
Player* player = level->player; Player* player = level->player;
Lighting* lighting = level->lighting; Lighting* lighting = level->lighting;
@ -206,6 +209,8 @@ void PlayerController::updateInteraction(){
player->selectedVoxel = *vox; player->selectedVoxel = *vox;
selectedBlockId = vox->id; selectedBlockId = vox->id;
selectedBlockPosition = iend; selectedBlockPosition = iend;
selectedPointPosition = end;
selectedBlockNormal = norm;
int x = (int)iend.x; int x = (int)iend.x;
int y = (int)iend.y; int y = (int)iend.y;
int z = (int)iend.z; int z = (int)iend.z;

View File

@ -37,6 +37,8 @@ class PlayerController {
void updateInteraction(); void updateInteraction();
public: public:
static glm::vec3 selectedBlockPosition; static glm::vec3 selectedBlockPosition;
static glm::vec3 selectedBlockNormal;
static glm::vec3 selectedPointPosition;
static int selectedBlockId; static int selectedBlockId;
PlayerController(Level* level, const EngineSettings& settings); PlayerController(Level* level, const EngineSettings& settings);

View File

@ -4,6 +4,7 @@
#include <string> #include <string>
#include "../maths/aabb.h" #include "../maths/aabb.h"
#include "../typedefs.h"
#define FACE_MX 0 #define FACE_MX 0
#define FACE_PX 1 #define FACE_PX 1
@ -15,13 +16,15 @@
#define BLOCK_AABB_GRID 16 #define BLOCK_AABB_GRID 16
enum class BlockModel { enum class BlockModel {
none, block, xsprite none, // invisible
block, // default shape
xsprite, // X-shape (grass)
aabb // box shaped as block hitbox
}; };
class Block { class Block {
public: public:
std::string const name; std::string const name;
unsigned int id;
// 0 1 2 3 4 5 // 0 1 2 3 4 5
std::string textureFaces[6]; // -x,x, -y,y, -z,z std::string textureFaces[6]; // -x,x, -y,y, -z,z
unsigned char emission[4]; unsigned char emission[4];
@ -36,6 +39,7 @@ public:
AABB hitbox; AABB hitbox;
struct { struct {
blockid_t id;
bool solid = true; bool solid = true;
bool emissive = false; bool emissive = false;
bool hitboxGrid[BLOCK_AABB_GRID][BLOCK_AABB_GRID][BLOCK_AABB_GRID]; bool hitboxGrid[BLOCK_AABB_GRID][BLOCK_AABB_GRID][BLOCK_AABB_GRID];

View File

@ -230,6 +230,7 @@ voxel* Chunks::rayCast(vec3 start,
end.y += dy / float(subs); end.y += dy / float(subs);
end.z += dz / float(subs); end.z += dz / float(subs);
if (box.inside(end)) { if (box.inside(end)) {
end += iend;
norm.x = norm.y = norm.z = 0.0f; norm.x = norm.y = norm.z = 0.0f;
if (steppedIndex == 0) norm.x = -stepx; if (steppedIndex == 0) norm.x = -stepx;
if (steppedIndex == 1) norm.y = -stepy; if (steppedIndex == 1) norm.y = -stepy;

View File

@ -97,16 +97,16 @@ float calc_height(fnl_state *noise, int real_x, int real_z){
} }
WorldGenerator::WorldGenerator(const Content* content) WorldGenerator::WorldGenerator(const Content* content)
: idStone(content->require("base:stone")->id), : idStone(content->require("base:stone")->rt.id),
idDirt(content->require("base:dirt")->id), idDirt(content->require("base:dirt")->rt.id),
idGrassBlock(content->require("base:grass_block")->id), idGrassBlock(content->require("base:grass_block")->rt.id),
idSand(content->require("base:sand")->id), idSand(content->require("base:sand")->rt.id),
idWater(content->require("base:water")->id), idWater(content->require("base:water")->rt.id),
idWood(content->require("base:wood")->id), idWood(content->require("base:wood")->rt.id),
idLeaves(content->require("base:leaves")->id), idLeaves(content->require("base:leaves")->rt.id),
idGrass(content->require("base:grass")->id), idGrass(content->require("base:grass")->rt.id),
idFlower(content->require("base:flower")->id), idFlower(content->require("base:flower")->rt.id),
idBedrock(content->require("base:bedrock")->id) {; idBedrock(content->require("base:bedrock")->rt.id) {;
} }
int generate_tree(fnl_state *noise, int generate_tree(fnl_state *noise,

View File

@ -17,10 +17,10 @@ class ChunksStorage;
class PlayerController; class PlayerController;
class Level { class Level {
const ContentIndices* const contentIds;
public: public:
World* world; World* world;
const Content* const content; const Content* const content;
const ContentIndices* const contentIds;
Player* player; Player* player;
Chunks* chunks; Chunks* chunks;
ChunksStorage* chunksStorage; ChunksStorage* chunksStorage;