This commit is contained in:
@clasher113
2023-11-30 14:05:23 +02:00
25 changed files with 194 additions and 77 deletions
+26 -11
View File
@@ -7,6 +7,10 @@
#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){
const vattr attrs[] = {
{2}, {2}, {4}, {0}
@@ -47,8 +51,8 @@ void Batch2D::vertex(float x, float y,
buffer[index++] = a;
}
void Batch2D::vertex(vec2 point,
vec2 uvpoint,
float r, float g, float b, float a) {
vec2 uvpoint,
float r, float g, float b, float a) {
buffer[index++] = point.x;
buffer[index++] = point.y;
buffer[index++] = uvpoint.x;
@@ -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);
}
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 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)
render();
float d = (w + h) * 0.5f;
float ar = 0.88f;
float ox = x + (w * 0.5f);
float sx = w * 0.5f * ar;
vec2 points[7] = {vec2(ox, y+(h*0.5f)),
vec2(ox-sx, y+(h*0.25f)),
vec2(ox, y),
vec2(ox+sx, y+(h*0.25f)),
vec2(ox+sx, y+(h*0.75f)),
vec2(ox, y+h),
vec2(ox-sx, y+(h*0.75f))};
float hh = h * 0.25f;
float ww = w * 0.25f;
float dd = d * 0.25f;
vec3 half = size * h * 0.25f;
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(uu+scalex, vu),
+6 -8
View File
@@ -6,8 +6,6 @@
#include "UVRegion.h"
using namespace glm;
class Mesh;
class Texture;
class Sprite;
@@ -25,8 +23,8 @@ class Batch2D {
void vertex(float x, float y,
float u, float v,
float r, float g, float b, float a);
void vertex(vec2 point,
vec2 uvpoint,
void vertex(glm::vec2 point,
glm::vec2 uvpoint,
float r, float g, float b, float a);
public:
@@ -37,10 +35,10 @@ public:
void begin();
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(float x, float y, float w, float h, int atlasRes, int index, vec4 tint);
void blockSprite(float x, float y, float w, float h, const UVRegion regions[], 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[], 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 line(float x1, float y1, float x2, float y2, float r, float g, float b, float a);
void rect(float x, float y,
@@ -48,7 +46,7 @@ public:
float ox, float oy,
float angle, UVRegion region,
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,
+65 -1
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));
}
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) {
ubyte group = block->drawGroup;
UVRegion texfaces[6];
@@ -277,7 +328,7 @@ bool BlocksRenderer::isOpen(int x, int y, int z, ubyte group) const {
if (id == BLOCK_VOID)
return false;
const Block& block = *blockDefsCache[id];
if (block.drawGroup != group && block.lightPassing) {
if ((block.drawGroup != group && block.lightPassing) || !block.rt.solid) {
return true;
}
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;
}
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) {
int begin = chunk->bottom * (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);
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:
break;
}
+2
View File
@@ -63,6 +63,7 @@ class BlocksRenderer {
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 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);
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 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);
public:
BlocksRenderer(size_t capacity, const Content* content, const ContentGfxCache* cache, const EngineSettings& settings);
+2 -16
View File
@@ -2,6 +2,8 @@
#include "Texture.h"
#include "Batch2D.h"
using glm::vec4;
Font::Font(std::vector<Texture*> pages, int lineHeight) : lineHeight_(lineHeight), pages(pages) {
}
@@ -10,22 +12,6 @@ Font::~Font(){
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 {
return lineHeight_;
}
+4 -1
View File
@@ -15,13 +15,16 @@ public:
LineBatch(size_t capacity=4096);
~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,
float r, float g, float b, float a);
void box(float x, float y, float z, float w, float h, float d,
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,
box(xyz.x, xyz.y, xyz.z, whd.x, whd.y, whd.z,
rgba.r, rgba.g, rgba.b, rgba.a);
}
+8 -4
View File
@@ -4,10 +4,12 @@
#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);
glBindTexture(GL_TEXTURE_2D, id);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
@@ -28,7 +30,7 @@ void Texture::bind(){
glBindTexture(GL_TEXTURE_2D, id);
}
void Texture::reload(unsigned char* data){
void Texture::reload(ubyte* data){
glBindTexture(GL_TEXTURE_2D, id);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid *) data);
@@ -36,6 +38,8 @@ void Texture::reload(unsigned char* data){
}
Texture* Texture::from(const ImageData* image) {
uint width = image->getWidth();
uint height = image->getHeight();
uint format;
const void* data = image->getData();
switch (image->getFormat()) {
@@ -44,5 +48,5 @@ Texture* Texture::from(const ImageData* image) {
default:
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);
}
+4 -4
View File
@@ -8,15 +8,15 @@ class ImageData;
class Texture {
public:
unsigned int id;
uint id;
int width;
int height;
Texture(unsigned int id, int width, int height);
Texture(unsigned char* data, int width, int height, uint format);
Texture(uint id, int width, int height);
Texture(ubyte* data, int width, int height, uint format);
~Texture();
void bind();
void reload(unsigned char* data);
void reload(ubyte* data);
static Texture* from(const ImageData* image);
};