minor refactor + 4 spaces indentation

This commit is contained in:
MihailRis
2024-03-13 19:21:40 +03:00
parent 5bc79c8a77
commit 4d70138b24
8 changed files with 381 additions and 325 deletions
+194 -137
View File
@@ -6,175 +6,232 @@
#include <GL/glew.h> #include <GL/glew.h>
#include "../typedefs.h" #include "../typedefs.h"
const uint B3D_VERTEX_SIZE = 9; inline constexpr uint B3D_VERTEX_SIZE = 9;
using glm::vec2;
using glm::vec3;
using glm::vec4;
Batch3D::Batch3D(size_t capacity) Batch3D::Batch3D(size_t capacity)
: capacity(capacity) { : capacity(capacity) {
const vattr attrs[] = { const vattr attrs[] = {
{3}, {2}, {4}, {0} {3}, {2}, {4}, {0}
}; };
buffer = new float[capacity * B3D_VERTEX_SIZE]; buffer = new float[capacity * B3D_VERTEX_SIZE];
mesh = std::make_unique<Mesh>(buffer, 0, attrs); mesh = std::make_unique<Mesh>(buffer, 0, attrs);
index = 0; index = 0;
ubyte pixels[] = { ubyte pixels[] = {
255, 255, 255, 255, 255, 255, 255, 255,
}; };
blank = std::make_unique<Texture>(pixels, 1, 1, ImageFormat::rgba8888); blank = std::make_unique<Texture>(pixels, 1, 1, ImageFormat::rgba8888);
_texture = nullptr; _texture = nullptr;
} }
Batch3D::~Batch3D(){ Batch3D::~Batch3D(){
delete[] buffer; delete[] buffer;
} }
void Batch3D::begin(){ void Batch3D::begin(){
_texture = nullptr; _texture = nullptr;
blank->bind(); blank->bind();
} }
void Batch3D::vertex(float x, float y, float z, float u, float v, void Batch3D::vertex(float x, float y, float z, float u, float v,
float r, float g, float b, float a) { float r, float g, float b, float a) {
buffer[index++] = x; buffer[index++] = x;
buffer[index++] = y; buffer[index++] = y;
buffer[index++] = z; buffer[index++] = z;
buffer[index++] = u; buffer[index++] = u;
buffer[index++] = v; buffer[index++] = v;
buffer[index++] = r; buffer[index++] = r;
buffer[index++] = g; buffer[index++] = g;
buffer[index++] = b; buffer[index++] = b;
buffer[index++] = a; buffer[index++] = a;
} }
void Batch3D::vertex(vec3 coord, float u, float v, void Batch3D::vertex(glm::vec3 coord, float u, float v,
float r, float g, float b, float a) { float r, float g, float b, float a) {
buffer[index++] = coord.x; buffer[index++] = coord.x;
buffer[index++] = coord.y; buffer[index++] = coord.y;
buffer[index++] = coord.z; buffer[index++] = coord.z;
buffer[index++] = u; buffer[index++] = u;
buffer[index++] = v; buffer[index++] = v;
buffer[index++] = r; buffer[index++] = r;
buffer[index++] = g; buffer[index++] = g;
buffer[index++] = b; buffer[index++] = b;
buffer[index++] = a; buffer[index++] = a;
} }
void Batch3D::vertex(vec3 point, void Batch3D::vertex(glm::vec3 point,
vec2 uvpoint, glm::vec2 uvpoint,
float r, float g, float b, float a) { float r, float g, float b, float a) {
buffer[index++] = point.x; buffer[index++] = point.x;
buffer[index++] = point.y; buffer[index++] = point.y;
buffer[index++] = point.z; buffer[index++] = point.z;
buffer[index++] = uvpoint.x; buffer[index++] = uvpoint.x;
buffer[index++] = uvpoint.y; buffer[index++] = uvpoint.y;
buffer[index++] = r; buffer[index++] = r;
buffer[index++] = g; buffer[index++] = g;
buffer[index++] = b; buffer[index++] = b;
buffer[index++] = a; buffer[index++] = a;
} }
void Batch3D::face(const vec3& coord, float w, float h, void Batch3D::face(
const vec3& axisX, const glm::vec3& coord,
const vec3& axisY, float w, float h,
const UVRegion& region, const glm::vec3& axisX,
const vec4& tint) { const glm::vec3& axisY,
if (index + B3D_VERTEX_SIZE * 6 > capacity) { const UVRegion& region,
flush(); const glm::vec4& tint
} ) {
vertex(coord, region.u1, region.v1, if (index + B3D_VERTEX_SIZE * 6 > capacity) {
tint.r, tint.g, tint.b, tint.a); flush();
vertex(coord + axisX * w, region.u2, region.v1, }
tint.r, tint.g, tint.b, tint.a); vertex(coord, region.u1, region.v1,
vertex(coord + axisX * w + axisY * h, region.u2, region.v2, tint.r, tint.g, tint.b, tint.a);
tint.r, tint.g, tint.b, tint.a); vertex(coord + axisX * w, region.u2, region.v1,
tint.r, tint.g, tint.b, tint.a);
vertex(coord + axisX * w + axisY * h, region.u2, region.v2,
tint.r, tint.g, tint.b, tint.a);
vertex(coord, region.u1, region.v1, vertex(coord, region.u1, region.v1,
tint.r, tint.g, tint.b, tint.a); tint.r, tint.g, tint.b, tint.a);
vertex(coord + axisX * w + axisY * h, region.u2, region.v2, vertex(coord + axisX * w + axisY * h, region.u2, region.v2,
tint.r, tint.g, tint.b, tint.a); tint.r, tint.g, tint.b, tint.a);
vertex(coord + axisY * h, region.u1, region.v2, vertex(coord + axisY * h, region.u1, region.v2,
tint.r, tint.g, tint.b, tint.a); tint.r, tint.g, tint.b, tint.a);
} }
void Batch3D::texture(Texture* new_texture){ void Batch3D::texture(Texture* new_texture){
if (_texture == new_texture) if (_texture == new_texture)
return; return;
flush(); flush();
_texture = new_texture; _texture = new_texture;
if (new_texture == nullptr) if (new_texture == nullptr)
blank->bind(); blank->bind();
else else
new_texture->bind(); new_texture->bind();
} }
void Batch3D::sprite(vec3 pos, vec3 up, vec3 right, float w, float h, const UVRegion& uv, vec4 color){ void Batch3D::sprite(
const float r = color.r; glm::vec3 pos,
const float g = color.g; glm::vec3 up,
const float b = color.b; glm::vec3 right,
const float a = color.a; float w, float h,
if (index + 6*B3D_VERTEX_SIZE >= capacity) { const UVRegion& uv,
flush(); glm::vec4 color
} ){
const float r = color.r;
const float g = color.g;
const float b = color.b;
const float a = color.a;
if (index + 6*B3D_VERTEX_SIZE >= capacity) {
flush();
}
vertex(pos.x - right.x * w - up.x * h, vertex(pos.x - right.x * w - up.x * h,
pos.y - right.y * w - up.y * h, pos.y - right.y * w - up.y * h,
pos.z - right.z * w - up.z * h, pos.z - right.z * w - up.z * h,
uv.u1, uv.v1, uv.u1, uv.v1,
r,g,b,a); r,g,b,a);
vertex(pos.x + right.x * w + up.x * h, vertex(pos.x + right.x * w + up.x * h,
pos.y + right.y * w + up.y * h, pos.y + right.y * w + up.y * h,
pos.z + right.z * w + up.z * h, pos.z + right.z * w + up.z * h,
uv.u2, uv.v2, uv.u2, uv.v2,
r,g,b,a); r,g,b,a);
vertex(pos.x - right.x * w + up.x * h, vertex(pos.x - right.x * w + up.x * h,
pos.y - right.y * w + up.y * h, pos.y - right.y * w + up.y * h,
pos.z - right.z * w + up.z * h, pos.z - right.z * w + up.z * h,
uv.u1, uv.v2, uv.u1, uv.v2,
r,g,b,a); r,g,b,a);
vertex(pos.x - right.x * w - up.x * h, vertex(pos.x - right.x * w - up.x * h,
pos.y - right.y * w - up.y * h, pos.y - right.y * w - up.y * h,
pos.z - right.z * w - up.z * h, pos.z - right.z * w - up.z * h,
uv.u1, uv.v1, uv.u1, uv.v1,
r,g,b,a); r,g,b,a);
vertex(pos.x + right.x * w - up.x * h, vertex(pos.x + right.x * w - up.x * h,
pos.y + right.y * w - up.y * h, pos.y + right.y * w - up.y * h,
pos.z + right.z * w - up.z * h, pos.z + right.z * w - up.z * h,
uv.u2, uv.v1, uv.u2, uv.v1,
r,g,b,a); r,g,b,a);
vertex(pos.x + right.x * w + up.x * h, vertex(pos.x + right.x * w + up.x * h,
pos.y + right.y * w + up.y * h, pos.y + right.y * w + up.y * h,
pos.z + right.z * w + up.z * h, pos.z + right.z * w + up.z * h,
uv.u2, uv.v2, uv.u2, uv.v2,
r,g,b,a); r,g,b,a);
} }
inline vec4 do_tint(float value) { inline glm::vec4 do_tint(float value) {
return vec4(value, value, value, 1.0f); return glm::vec4(value, value, value, 1.0f);
} }
void Batch3D::xSprite(float w, float h, const UVRegion& uv, const vec4 tint, bool shading) { void Batch3D::xSprite(float w, float h, const UVRegion& uv, const glm::vec4 tint, bool shading) {
face(vec3(-w *0.25f, 0.0f, 0.0f - w *0.25f), w, h, vec3(1, 0, 0), vec3(0, 1, 0), uv, (shading ? do_tint(1.0f)*tint : tint)); face(
face(vec3(w * 0.25f, 0.0f, w * 0.5f - w *0.25f), w, h, vec3(0, 0, -1), vec3(0, 1, 0), uv, (shading ? do_tint(0.9f)*tint : tint)); glm::vec3(-w * 0.25f, 0.0f, -w * 0.25f),
w, h,
glm::vec3(1, 0, 0),
glm::vec3(0, 1, 0),
uv, (shading ? do_tint(1.0f)*tint : tint)
);
face(
glm::vec3(w * 0.25f, 0.0f, w * 0.5f - w * 0.25f),
w, h,
glm::vec3(0, 0, -1),
glm::vec3(0, 1, 0),
uv, (shading ? do_tint(0.9f)*tint : tint)
);
} }
void Batch3D::cube(const vec3 coord, const vec3 size, const UVRegion(&texfaces)[6], const vec4 tint, bool shading) { void Batch3D::cube(
face(coord+vec3(0.0f, 0.0f, 0.0f), size.x, size.y, vec3(1, 0, 0), vec3(0, 1, 0), texfaces[5], (shading ? do_tint(0.8)*tint : tint)); const glm::vec3 coord,
face(coord+vec3(size.x, 0.0f, -size.z), size.x, size.y, vec3(-1, 0, 0), vec3(0, 1, 0), texfaces[4], (shading ? do_tint(0.8f)*tint : tint)); const glm::vec3 size,
face(coord+vec3(0.0f, size.y, 0.0f), size.x, size.z, vec3(1, 0, 0), vec3(0, 0, -1), texfaces[3], (shading ? do_tint(1.0f)*tint : tint)); const UVRegion(&texfaces)[6],
face(coord+vec3(0.0f, 0.0f, -size.z), size.x, size.z, vec3(1, 0, 0), vec3(0, 0, 1), texfaces[2], (shading ? do_tint(0.7f)*tint : tint)); const glm::vec4 tint,
face(coord+vec3(0.0f, 0.0f, -size.z), size.z, size.y, vec3(0, 0, 1), vec3(0, 1, 0), texfaces[0], (shading ? do_tint(0.9f)*tint : tint)); bool shading
face(coord+vec3(size.x, 0.0f, 0.0f), size.z, size.y, vec3(0, 0, -1), vec3(0, 1, 0), texfaces[1], (shading ? do_tint(0.9f)*tint : tint)); ) {
const glm::vec3 X(1.0f, 0.0f, 0.0f);
const glm::vec3 Y(0.0f, 1.0f, 0.0f);
const glm::vec3 Z(0.0f, 0.0f, 1.0f);
face(
coord+glm::vec3(0.0f, 0.0f, 0.0f),
size.x, size.y, X, Y, texfaces[5],
(shading ? do_tint(0.8)*tint : tint)
);
face(
coord+glm::vec3(size.x, 0.0f, -size.z),
size.x, size.y, -X, Y, texfaces[4],
(shading ? do_tint(0.8f)*tint : tint)
);
face(
coord+glm::vec3(0.0f, size.y, 0.0f),
size.x, size.z, X, -Z, texfaces[3],
(shading ? do_tint(1.0f)*tint : tint)
);
face(
coord+glm::vec3(0.0f, 0.0f, -size.z),
size.x, size.z, X, Z, texfaces[2],
(shading ? do_tint(0.7f)*tint : tint)
);
face(
coord+glm::vec3(0.0f, 0.0f, -size.z),
size.z, size.y, Z, Y, texfaces[0],
(shading ? do_tint(0.9f)*tint : tint)
);
face(
coord+glm::vec3(size.x, 0.0f, 0.0f),
size.z, size.y, -Z, Y, texfaces[1],
(shading ? do_tint(0.9f)*tint : tint)
);
} }
void Batch3D::blockCube(const vec3 size, const UVRegion(&texfaces)[6], const vec4 tint, bool shading) { void Batch3D::blockCube(
const glm::vec3 size,
const UVRegion(&texfaces)[6],
const glm::vec4 tint,
bool shading
) {
cube((1.0f - size) * -0.5f, size, texfaces, tint, shading); cube((1.0f - size) * -0.5f, size, texfaces, tint, shading);
} }
@@ -187,13 +244,13 @@ void Batch3D::point(glm::vec3 coord, glm::vec4 tint) {
} }
void Batch3D::flush() { void Batch3D::flush() {
mesh->reload(buffer, index / B3D_VERTEX_SIZE); mesh->reload(buffer, index / B3D_VERTEX_SIZE);
mesh->draw(); mesh->draw();
index = 0; index = 0;
} }
void Batch3D::flushPoints() { void Batch3D::flushPoints() {
mesh->reload(buffer, index / B3D_VERTEX_SIZE); mesh->reload(buffer, index / B3D_VERTEX_SIZE);
mesh->draw(GL_POINTS); mesh->draw(GL_POINTS);
index = 0; index = 0;
} }
+29 -29
View File
@@ -12,42 +12,42 @@ class Mesh;
class Texture; class Texture;
class Batch3D { class Batch3D {
float* buffer; float* buffer;
size_t capacity; size_t capacity;
std::unique_ptr<Mesh> mesh; std::unique_ptr<Mesh> mesh;
std::unique_ptr<Texture> blank; std::unique_ptr<Texture> blank;
size_t index; size_t index;
Texture* _texture; Texture* _texture;
void vertex(float x, float y, float z, void vertex(float x, float y, float z,
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(glm::vec3 coord, void vertex(glm::vec3 coord,
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(glm::vec3 point, glm::vec2 uvpoint, void vertex(glm::vec3 point, glm::vec2 uvpoint,
float r, float g, float b, float a); float r, float g, float b, float a);
void face(const glm::vec3& coord, float w, float h, void face(const glm::vec3& coord, float w, float h,
const glm::vec3& axisX, const glm::vec3& axisX,
const glm::vec3& axisY, const glm::vec3& axisY,
const UVRegion& region, const UVRegion& region,
const glm::vec4& tint); const glm::vec4& tint);
public: public:
Batch3D(size_t capacity); Batch3D(size_t capacity);
~Batch3D(); ~Batch3D();
void begin(); void begin();
void texture(Texture* texture); void texture(Texture* texture);
void sprite(glm::vec3 pos, glm::vec3 up, glm::vec3 right, float w, float h, const UVRegion& uv, glm::vec4 tint); void sprite(glm::vec3 pos, glm::vec3 up, glm::vec3 right, float w, float h, const UVRegion& uv, glm::vec4 tint);
void xSprite(float w, float h, const UVRegion& uv, const glm::vec4 tint, bool shading=true); void xSprite(float w, float h, const UVRegion& uv, const glm::vec4 tint, bool shading=true);
void cube(const glm::vec3 coords, const glm::vec3 size, const UVRegion(&texfaces)[6], const glm::vec4 tint, bool shading=true); void cube(const glm::vec3 coords, const glm::vec3 size, const UVRegion(&texfaces)[6], const glm::vec4 tint, bool shading=true);
void blockCube(const glm::vec3 size, const UVRegion(&texfaces)[6], const glm::vec4 tint, bool shading=true); void blockCube(const glm::vec3 size, const UVRegion(&texfaces)[6], const glm::vec4 tint, bool shading=true);
void point(glm::vec3 pos, glm::vec2 uv, glm::vec4 tint); void point(glm::vec3 pos, glm::vec2 uv, glm::vec4 tint);
void point(glm::vec3 pos, glm::vec4 tint); void point(glm::vec3 pos, glm::vec4 tint);
void flush(); void flush();
void flushPoints(); void flushPoints();
}; };
+52 -52
View File
@@ -3,77 +3,77 @@
#include <GL/glew.h> #include <GL/glew.h>
const uint LB_VERTEX_SIZE = (3+4); inline constexpr uint LB_VERTEX_SIZE = (3+4);
LineBatch::LineBatch(size_t capacity) : capacity(capacity) { LineBatch::LineBatch(size_t capacity) : capacity(capacity) {
const vattr attrs[] = { {3},{4}, {0} }; const vattr attrs[] = { {3},{4}, {0} };
buffer = new float[capacity * LB_VERTEX_SIZE * 2]; buffer = new float[capacity * LB_VERTEX_SIZE * 2];
mesh = std::make_unique<Mesh>(buffer, 0, attrs); mesh = std::make_unique<Mesh>(buffer, 0, attrs);
index = 0; index = 0;
} }
LineBatch::~LineBatch(){ LineBatch::~LineBatch(){
delete[] buffer; delete[] buffer;
} }
void LineBatch::line( void LineBatch::line(
float x1, float y1, float x1, float y1,
float z1, float x2, float z1, float x2,
float y2, float z2, float y2, float z2,
float r, float g, float b, float a float r, float g, float b, float a
) { ) {
if (index + LB_VERTEX_SIZE * 2 >= capacity) { if (index + LB_VERTEX_SIZE * 2 >= capacity) {
render(); render();
} }
buffer[index] = x1; buffer[index] = x1;
buffer[index+1] = y1; buffer[index+1] = y1;
buffer[index+2] = z1; buffer[index+2] = z1;
buffer[index+3] = r; buffer[index+3] = r;
buffer[index+4] = g; buffer[index+4] = g;
buffer[index+5] = b; buffer[index+5] = b;
buffer[index+6] = a; buffer[index+6] = a;
index += LB_VERTEX_SIZE; index += LB_VERTEX_SIZE;
buffer[index] = x2; buffer[index] = x2;
buffer[index+1] = y2; buffer[index+1] = y2;
buffer[index+2] = z2; buffer[index+2] = z2;
buffer[index+3] = r; buffer[index+3] = r;
buffer[index+4] = g; buffer[index+4] = g;
buffer[index+5] = b; buffer[index+5] = b;
buffer[index+6] = a; buffer[index+6] = a;
index += LB_VERTEX_SIZE; index += LB_VERTEX_SIZE;
} }
void LineBatch::box(float x, float y, float z, float w, float h, float d, void LineBatch::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) {
w *= 0.5f; w *= 0.5f;
h *= 0.5f; h *= 0.5f;
d *= 0.5f; d *= 0.5f;
line(x-w, y-h, z-d, x+w, y-h, z-d, r,g,b,a); line(x-w, y-h, z-d, x+w, y-h, z-d, r,g,b,a);
line(x-w, y+h, z-d, x+w, y+h, z-d, r,g,b,a); line(x-w, y+h, z-d, x+w, y+h, z-d, r,g,b,a);
line(x-w, y-h, z+d, x+w, y-h, z+d, r,g,b,a); line(x-w, y-h, z+d, x+w, y-h, z+d, r,g,b,a);
line(x-w, y+h, z+d, x+w, y+h, z+d, r,g,b,a); line(x-w, y+h, z+d, x+w, y+h, z+d, r,g,b,a);
line(x-w, y-h, z-d, x-w, y+h, z-d, r,g,b,a); line(x-w, y-h, z-d, x-w, y+h, z-d, r,g,b,a);
line(x+w, y-h, z-d, x+w, y+h, z-d, r,g,b,a); line(x+w, y-h, z-d, x+w, y+h, z-d, r,g,b,a);
line(x-w, y-h, z+d, x-w, y+h, z+d, r,g,b,a); line(x-w, y-h, z+d, x-w, y+h, z+d, r,g,b,a);
line(x+w, y-h, z+d, x+w, y+h, z+d, r,g,b,a); line(x+w, y-h, z+d, x+w, y+h, z+d, r,g,b,a);
line(x-w, y-h, z-d, x-w, y-h, z+d, r,g,b,a); line(x-w, y-h, z-d, x-w, y-h, z+d, r,g,b,a);
line(x+w, y-h, z-d, x+w, y-h, z+d, r,g,b,a); line(x+w, y-h, z-d, x+w, y-h, z+d, r,g,b,a);
line(x-w, y+h, z-d, x-w, y+h, z+d, r,g,b,a); line(x-w, y+h, z-d, x-w, y+h, z+d, r,g,b,a);
line(x+w, y+h, z-d, x+w, y+h, z+d, r,g,b,a); line(x+w, y+h, z-d, x+w, y+h, z+d, r,g,b,a);
} }
void LineBatch::render(){ void LineBatch::render(){
if (index == 0) if (index == 0)
return; return;
mesh->reload(buffer, index / LB_VERTEX_SIZE); mesh->reload(buffer, index / LB_VERTEX_SIZE);
mesh->draw(GL_LINES); mesh->draw(GL_LINES);
index = 0; index = 0;
} }
void LineBatch::lineWidth(float width) { void LineBatch::lineWidth(float width) {
glLineWidth(width); glLineWidth(width);
} }
+19 -19
View File
@@ -8,29 +8,29 @@
class Mesh; class Mesh;
class LineBatch { class LineBatch {
std::unique_ptr<Mesh> mesh; std::unique_ptr<Mesh> mesh;
float* buffer; float* buffer;
size_t index; size_t index;
size_t capacity; size_t capacity;
public: 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) { 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); 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) {
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);
} }
void render(); void render();
void lineWidth(float width); void lineWidth(float width);
}; };
#endif /* GRAPHICS_LINEBATCH_H_ */ #endif /* GRAPHICS_LINEBATCH_H_ */
+14 -14
View File
@@ -4,11 +4,11 @@
#include "../util/data_io.h" #include "../util/data_io.h"
void Lightmap::set(const Lightmap* lightmap) { void Lightmap::set(const Lightmap* lightmap) {
set(lightmap->map); set(lightmap->map);
} }
void Lightmap::set(const light_t* map) { void Lightmap::set(const light_t* map) {
for (size_t i = 0; i < CHUNK_VOL; i++) { for (size_t i = 0; i < CHUNK_VOL; i++) {
this->map[i] = map[i]; this->map[i] = map[i];
} }
} }
@@ -16,19 +16,19 @@ void Lightmap::set(const light_t* map) {
static_assert(sizeof(light_t) == 2, "replace dataio calls to new light_t"); static_assert(sizeof(light_t) == 2, "replace dataio calls to new light_t");
ubyte* Lightmap::encode() const { ubyte* Lightmap::encode() const {
ubyte* buffer = new ubyte[LIGHTMAP_DATA_LEN]; ubyte* buffer = new ubyte[LIGHTMAP_DATA_LEN];
for (uint i = 0; i < CHUNK_VOL; i+=2) { for (uint i = 0; i < CHUNK_VOL; i+=2) {
buffer[i/2] = ((map[i] >> 12) & 0xF) | ((map[i+1] >> 8) & 0xF0); buffer[i/2] = ((map[i] >> 12) & 0xF) | ((map[i+1] >> 8) & 0xF0);
} }
return buffer; return buffer;
} }
light_t* Lightmap::decode(ubyte* buffer) { light_t* Lightmap::decode(ubyte* buffer) {
light_t* lights = new light_t[CHUNK_VOL]; light_t* lights = new light_t[CHUNK_VOL];
for (uint i = 0; i < CHUNK_VOL; i+=2) { for (uint i = 0; i < CHUNK_VOL; i+=2) {
ubyte b = buffer[i/2]; ubyte b = buffer[i/2];
lights[i] = ((b & 0xF) << 12); lights[i] = ((b & 0xF) << 12);
lights[i+1] = ((b & 0xF0) << 8); lights[i+1] = ((b & 0xF0) << 8);
} }
return lights; return lights;
} }
+56 -56
View File
@@ -4,85 +4,85 @@
#include "../constants.h" #include "../constants.h"
#include "../typedefs.h" #include "../typedefs.h"
const int LIGHTMAP_DATA_LEN = CHUNK_VOL/2; inline constexpr int LIGHTMAP_DATA_LEN = CHUNK_VOL/2;
// Lichtkarte // Lichtkarte
class Lightmap { class Lightmap {
public: public:
light_t map[CHUNK_VOL] {}; light_t map[CHUNK_VOL] {};
int highestPoint = 0; int highestPoint = 0;
void set(const Lightmap* lightmap); void set(const Lightmap* lightmap);
void set(const light_t* map); void set(const light_t* map);
inline unsigned short get(int x, int y, int z) const { inline unsigned short get(int x, int y, int z) const {
return (map[y*CHUNK_D*CHUNK_W+z*CHUNK_W+x]); return (map[y*CHUNK_D*CHUNK_W+z*CHUNK_W+x]);
} }
inline unsigned char get(int x, int y, int z, int channel) const { inline unsigned char get(int x, int y, int z, int channel) const {
return (map[y*CHUNK_D*CHUNK_W+z*CHUNK_W+x] >> (channel << 2)) & 0xF; return (map[y*CHUNK_D*CHUNK_W+z*CHUNK_W+x] >> (channel << 2)) & 0xF;
} }
inline unsigned char getR(int x, int y, int z) const { inline unsigned char getR(int x, int y, int z) const {
return map[y*CHUNK_D*CHUNK_W+z*CHUNK_W+x] & 0xF; return map[y*CHUNK_D*CHUNK_W+z*CHUNK_W+x] & 0xF;
} }
inline unsigned char getG(int x, int y, int z) const { inline unsigned char getG(int x, int y, int z) const {
return (map[y*CHUNK_D*CHUNK_W+z*CHUNK_W+x] >> 4) & 0xF; return (map[y*CHUNK_D*CHUNK_W+z*CHUNK_W+x] >> 4) & 0xF;
} }
inline unsigned char getB(int x, int y, int z) const { inline unsigned char getB(int x, int y, int z) const {
return (map[y*CHUNK_D*CHUNK_W+z*CHUNK_W+x] >> 8) & 0xF; return (map[y*CHUNK_D*CHUNK_W+z*CHUNK_W+x] >> 8) & 0xF;
} }
inline unsigned char getS(int x, int y, int z) const { inline unsigned char getS(int x, int y, int z) const {
return (map[y*CHUNK_D*CHUNK_W+z*CHUNK_W+x] >> 12) & 0xF; return (map[y*CHUNK_D*CHUNK_W+z*CHUNK_W+x] >> 12) & 0xF;
} }
inline void setR(int x, int y, int z, int value){ inline void setR(int x, int y, int z, int value){
const int index = y*CHUNK_D*CHUNK_W+z*CHUNK_W+x; const int index = y*CHUNK_D*CHUNK_W+z*CHUNK_W+x;
map[index] = (map[index] & 0xFFF0) | value; map[index] = (map[index] & 0xFFF0) | value;
} }
inline void setG(int x, int y, int z, int value){ inline void setG(int x, int y, int z, int value){
const int index = y*CHUNK_D*CHUNK_W+z*CHUNK_W+x; const int index = y*CHUNK_D*CHUNK_W+z*CHUNK_W+x;
map[index] = (map[index] & 0xFF0F) | (value << 4); map[index] = (map[index] & 0xFF0F) | (value << 4);
} }
inline void setB(int x, int y, int z, int value){ inline void setB(int x, int y, int z, int value){
const int index = y*CHUNK_D*CHUNK_W+z*CHUNK_W+x; const int index = y*CHUNK_D*CHUNK_W+z*CHUNK_W+x;
map[index] = (map[index] & 0xF0FF) | (value << 8); map[index] = (map[index] & 0xF0FF) | (value << 8);
} }
inline void setS(int x, int y, int z, int value){ inline void setS(int x, int y, int z, int value){
const int index = y*CHUNK_D*CHUNK_W+z*CHUNK_W+x; const int index = y*CHUNK_D*CHUNK_W+z*CHUNK_W+x;
map[index] = (map[index] & 0x0FFF) | (value << 12); map[index] = (map[index] & 0x0FFF) | (value << 12);
} }
inline void set(int x, int y, int z, int channel, int value){ inline void set(int x, int y, int z, int channel, int value){
const int index = y*CHUNK_D*CHUNK_W+z*CHUNK_W+x; const int index = y*CHUNK_D*CHUNK_W+z*CHUNK_W+x;
map[index] = (map[index] & (0xFFFF & (~(0xF << (channel*4))))) | (value << (channel << 2)); map[index] = (map[index] & (0xFFFF & (~(0xF << (channel*4))))) | (value << (channel << 2));
} }
inline const light_t* getLights() const { inline const light_t* getLights() const {
return map; return map;
} }
inline light_t* getLightsWriteable() { inline light_t* getLightsWriteable() {
return map; return map;
} }
static constexpr light_t combine(int r, int g, int b, int s) { static constexpr light_t combine(int r, int g, int b, int s) {
return r | (g << 4) | (b << 8) | (s << 12); return r | (g << 4) | (b << 8) | (s << 12);
} }
static constexpr light_t extract(light_t light, ubyte channel) { static constexpr light_t extract(light_t light, ubyte channel) {
return (light >> (channel << 2)) & 0xF; return (light >> (channel << 2)) & 0xF;
} }
ubyte* encode() const; ubyte* encode() const;
static light_t* decode(ubyte* buffer); static light_t* decode(ubyte* buffer);
}; };
#endif /* LIGHTING_LIGHTMAP_H_ */ #endif /* LIGHTING_LIGHTMAP_H_ */
+1 -1
View File
@@ -255,4 +255,4 @@ RayRelation Ray::intersectAABBFaces(
if (isIntersect) return RayRelation::Intersect; if (isIntersect) return RayRelation::Intersect;
return RayRelation::None; return RayRelation::None;
} }
+16 -17
View File
@@ -9,25 +9,24 @@
#include <array> #include <array>
typedef glm::highp_dvec3 rayvec3; using rayvec3 = glm::highp_dvec3;
typedef glm::highp_dvec2 rayvec2; using rayvec2 = glm::highp_dvec2;
typedef double scalar_t; using scalar_t = double;
enum class RayRelation{ enum class RayRelation {
Embed=2, Intersect=1, Parallel=0, None=0 Embed=2, Intersect=1, Parallel=0, None=0
}; };
class AABBFaces{ class AABBFaces {
static const unsigned char AABBFACES_COUNT = 6; static const unsigned char AABBFACES_COUNT = 6;
public: public:
std::array<std::pair<rayvec3, rayvec2>, AABBFACES_COUNT> faces; // every face is min-point and opposite corner point std::array<std::pair<rayvec3, rayvec2>, AABBFACES_COUNT> faces; // every face is min-point and opposite corner point
AABBFaces(){}; AABBFaces(){};
AABBFaces(const rayvec3& parentBoxPos, const AABB& parentBox); AABBFaces(const rayvec3& parentBoxPos, const AABB& parentBox);
}; };
class Ray{ class Ray {
public: public:
rayvec3 origin; rayvec3 origin;
rayvec3 dir; rayvec3 dir;
@@ -45,7 +44,7 @@ public:
const rayvec3& faceMin, const rayvec3& faceMin,
const rayvec2& faceOppositeCorner); const rayvec2& faceOppositeCorner);
//returns normal and distance ///returns normal and distance
RayRelation intersectYZFace( RayRelation intersectYZFace(
const rayvec3& faceMin, const rayvec3& faceMin,
const rayvec2& faceOppositeCorner, const rayvec2& faceOppositeCorner,
@@ -63,17 +62,17 @@ public:
scalar_t& distance_ret); scalar_t& distance_ret);
RayRelation intersectAABB( RayRelation intersectAABB(
const rayvec3& boxPos, const rayvec3& boxPos,
const AABB& box, const AABB& box,
float maxDist, float maxDist,
glm::ivec3& normal_ret, glm::ivec3& normal_ret,
scalar_t& distance_ret); scalar_t& distance_ret);
RayRelation intersectAABBFaces( // calculates only normal and distance RayRelation intersectAABBFaces( // calculates only normal and distance
const AABBFaces& boxFaces, const AABBFaces& boxFaces,
float maxDist, float maxDist,
glm::ivec3& normal_ret, glm::ivec3& normal_ret,
scalar_t& distance_ret); scalar_t& distance_ret);
}; };
#endif // SRC_VOXNATHS_H_ #endif // SRC_VOXNATHS_H_