Atlas padding, mip-mapping
This commit is contained in:
parent
9cde99f3f7
commit
55538d3b02
@ -3,6 +3,8 @@
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include "../constants.h"
|
||||
|
||||
AssetsLoader::AssetsLoader(Assets* assets) : assets(assets) {
|
||||
}
|
||||
|
||||
@ -35,6 +37,7 @@ bool AssetsLoader::loadNext() {
|
||||
|
||||
#include "../coders/png.h"
|
||||
#include "../graphics/Shader.h"
|
||||
#include "../graphics/ImageData.h"
|
||||
#include "../graphics/Texture.h"
|
||||
#include "../graphics/Font.h"
|
||||
|
||||
@ -58,6 +61,23 @@ bool _load_texture(Assets* assets, const std::string& filename, const std::strin
|
||||
return true;
|
||||
}
|
||||
|
||||
bool _load_atlas(Assets* assets, const std::string& filename, const std::string& name) {
|
||||
ImageData* image = png::load_image(filename);
|
||||
if (image == nullptr) {
|
||||
std::cerr << "failed to load image '" << name << "'" << std::endl;
|
||||
return false;
|
||||
}
|
||||
for (int i = 0; i < ATLAS_MARGIN_SIZE; i++) {
|
||||
ImageData* newimage = add_atlas_margins(image, 16);
|
||||
delete image;
|
||||
image = newimage;
|
||||
}
|
||||
|
||||
Texture* texture = Texture::from(image);
|
||||
assets->store(texture, name);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool _load_font(Assets* assets, const std::string& filename, const std::string& name) {
|
||||
std::vector<Texture*> pages;
|
||||
for (size_t i = 0; i <= 4; i++) {
|
||||
@ -77,6 +97,7 @@ void AssetsLoader::createDefaults(AssetsLoader& loader) {
|
||||
loader.addLoader(ASSET_SHADER, _load_shader);
|
||||
loader.addLoader(ASSET_TEXTURE, _load_texture);
|
||||
loader.addLoader(ASSET_FONT, _load_font);
|
||||
loader.addLoader(ASSET_ATLAS, _load_atlas);
|
||||
}
|
||||
|
||||
void AssetsLoader::addDefaults(AssetsLoader& loader) {
|
||||
@ -84,7 +105,7 @@ void AssetsLoader::addDefaults(AssetsLoader& loader) {
|
||||
loader.add(ASSET_SHADER, "res/lines", "lines");
|
||||
loader.add(ASSET_SHADER, "res/ui", "ui");
|
||||
|
||||
loader.add(ASSET_TEXTURE, "res/block.png", "block");
|
||||
loader.add(ASSET_ATLAS, "res/block.png", "block");
|
||||
loader.add(ASSET_TEXTURE, "res/slot.png", "slot");
|
||||
|
||||
loader.add(ASSET_FONT, "res/font", "normal");
|
||||
|
||||
@ -9,6 +9,7 @@
|
||||
#define ASSET_TEXTURE 1
|
||||
#define ASSET_SHADER 2
|
||||
#define ASSET_FONT 3
|
||||
#define ASSET_ATLAS 4
|
||||
|
||||
class Assets;
|
||||
|
||||
|
||||
@ -18,4 +18,6 @@ inline uint vox_index(int x, int y, int z, int w, int d) {
|
||||
return (y * d + z) * w + x;
|
||||
}
|
||||
|
||||
#define ATLAS_MARGIN_SIZE 2
|
||||
|
||||
#endif // SRC_CONSTANTS_H_
|
||||
|
||||
@ -30,8 +30,6 @@ using std::shared_ptr;
|
||||
using glm::vec3;
|
||||
using gui::GUI;
|
||||
|
||||
|
||||
|
||||
Engine::Engine(const EngineSettings& settings_) {
|
||||
this->settings = settings_;
|
||||
|
||||
|
||||
@ -138,8 +138,8 @@ void BlocksRenderer::blockCube(int x, int y, int z, vec3 size, const UVRegion te
|
||||
|
||||
void BlocksRenderer::blockXSprite(int x, int y, int z, vec3 size, const UVRegion texface1, const UVRegion texface2, float spread) {
|
||||
vec4 lights[]{
|
||||
pickSoftLight(x, y, z, {1, 0, 0}, {0, 1, 0}),
|
||||
pickSoftLight(x + 1, y, z, {1, 0, 0}, {0, 1, 0}),
|
||||
pickSoftLight(x, y+1, z, {1, 0, 0}, {0, 1, 0}),
|
||||
pickSoftLight(x + 1, y+1, z, {1, 0, 0}, {0, 1, 0}),
|
||||
pickSoftLight(x + 1, y + 1, z, {1, 0, 0}, {0, 1, 0}),
|
||||
pickSoftLight(x, y + 1, z, {1, 0, 0}, {0, 1, 0}) };
|
||||
|
||||
@ -151,17 +151,17 @@ void BlocksRenderer::blockXSprite(int x, int y, int z, vec3 size, const UVRegion
|
||||
const float w = size.x/1.41f;
|
||||
face(vec3(x + xs + (1.0 - w) * 0.5f, y,
|
||||
z + zs - 1 + (1.0 - w) * 0.5f), w, size.y,
|
||||
vec3(1.0f, 0, 1.0f), vec3(0, 1, 0), texface1, lights, do_tint(0.9f));
|
||||
vec3(1.0f, 0, 1.0f), vec3(0, 1, 0), texface1, lights, do_tint(0.8f));
|
||||
face(vec3(x + xs - (1.0 - w) * 0.5f + 1, y,
|
||||
z + zs - (1.0 - w) * 0.5f), w, size.y,
|
||||
vec3(-1.0f, 0, -1.0f), vec3(0, 1, 0), texface1, lights, do_tint(0.9f));
|
||||
vec3(-1.0f, 0, -1.0f), vec3(0, 1, 0), texface1, lights, do_tint(0.8f));
|
||||
|
||||
face(vec3(x + xs + (1.0 - w) * 0.5f, y,
|
||||
z + zs - (1.0 - w) * 0.5f), w, size.y,
|
||||
vec3(1.0f, 0, -1.0f), vec3(0, 1, 0), texface2, lights, do_tint(0.9f));
|
||||
vec3(1.0f, 0, -1.0f), vec3(0, 1, 0), texface2, lights, do_tint(0.8f));
|
||||
face(vec3(x + xs - (1.0 - w) * 0.5f + 1, y,
|
||||
z + zs + (1.0 - w) * 0.5f - 1), w, size.y,
|
||||
vec3(-1.0f, 0, 1.0f), vec3(0, 1, 0), texface2, lights, do_tint(0.9f));
|
||||
vec3(-1.0f, 0, 1.0f), vec3(0, 1, 0), texface2, lights, do_tint(0.8f));
|
||||
}
|
||||
|
||||
void BlocksRenderer::blockCubeShaded(int x, int y, int z, vec3 size, const UVRegion texfaces_[6], const Block* block, ubyte states) {
|
||||
@ -174,17 +174,17 @@ void BlocksRenderer::blockCubeShaded(int x, int y, int z, vec3 size, const UVReg
|
||||
}
|
||||
|
||||
if (block->rotatable) {
|
||||
if (states == 0x31) {
|
||||
if (states == BLOCK_DIR_X) {
|
||||
rot = 1;
|
||||
texfaces[0] = texfaces_[2];
|
||||
texfaces[1] = texfaces_[3];
|
||||
texfaces[2] = texfaces_[0];
|
||||
texfaces[3] = texfaces_[1];
|
||||
}
|
||||
else if (states == 0x32) {
|
||||
else if (states == BLOCK_DIR_Y) {
|
||||
rot = 2;
|
||||
}
|
||||
else if (states == 0x33) {
|
||||
else if (states == BLOCK_DIR_Z) {
|
||||
rot = 3;
|
||||
texfaces[2] = texfaces_[4];
|
||||
texfaces[3] = texfaces_[5];
|
||||
@ -292,10 +292,11 @@ vec4 BlocksRenderer::pickSoftLight(int x, int y, int z, ivec3 right, ivec3 up) c
|
||||
// Get texture atlas UV region for block face
|
||||
inline UVRegion uvfor(const Block& def, uint face, int atlas_size) {
|
||||
float uvsize = 1.0f / (float)atlas_size;
|
||||
float us = 0.0097;
|
||||
const uint id = def.textureFaces[face];
|
||||
float u = (id % atlas_size) * uvsize;
|
||||
float v = 1.0f - (id / atlas_size + 1) * uvsize;
|
||||
return UVRegion(u, v, u + uvsize, v + uvsize);
|
||||
return UVRegion(u+us, v+us, u + uvsize - us * 0.36f, v + uvsize - us * 0.36f);
|
||||
}
|
||||
|
||||
void BlocksRenderer::render(const voxel* voxels, int atlas_size) {
|
||||
|
||||
@ -23,6 +23,7 @@ ImageData::~ImageData() {
|
||||
}
|
||||
}
|
||||
|
||||
#include <iostream>
|
||||
ImageData* add_atlas_margins(ImageData* image, int grid_size) {
|
||||
// RGBA is only supported
|
||||
assert(image->getFormat() == ImageFormat::rgba8888);
|
||||
@ -39,16 +40,33 @@ ImageData* add_atlas_margins(ImageData* image, int grid_size) {
|
||||
int imgres = image->getWidth() / grid_size;
|
||||
for (int row = 0; row < grid_size; row++) {
|
||||
for (int col = 0; col < grid_size; col++) {
|
||||
int ox = 1 + col * (imgres + 2);
|
||||
int oy = 1 + row * (imgres + 2);
|
||||
int sox = col * imgres;
|
||||
int soy = row * imgres;
|
||||
int dox = 1 + col * (imgres + 2);
|
||||
int doy = 1 + row * (imgres + 2);
|
||||
for (int ly = -1; ly <= imgres; ly++) {
|
||||
for (int lx = -1; lx <= imgres; lx++) {
|
||||
int sy = max(min(ly, imgres-1), 0);
|
||||
int sx = max(min(lx, imgres-1), 0);
|
||||
for (int c = 0; c < 4; c++)
|
||||
dstdata[((oy+ly) * dstwidth + ox + lx) * 4 + c] = srcdata[(sy * srcwidth + sx) * 4 + c];
|
||||
dstdata[((doy+ly) * dstwidth + dox + lx) * 4 + c] = srcdata[((soy+sy) * srcwidth + sox + sx) * 4 + c];
|
||||
}
|
||||
}
|
||||
|
||||
for (int ly = 0; ly < imgres; ly++) {
|
||||
for (int lx = 0; lx < imgres; lx++) {
|
||||
int sy = lx;
|
||||
int sx = ly;
|
||||
if (srcdata[((soy+sy) * srcwidth + sox + sx) * 4 + 3]) {
|
||||
for (int c = 0; c < 3; c++) {
|
||||
dstdata[((doy+ly) * dstwidth + dox + lx + 1) * 4 + c] = srcdata[((soy+sy) * srcwidth + sox + sx) * 4 + c];
|
||||
dstdata[((doy+ly + 1) * dstwidth + dox + lx) * 4 + c] = srcdata[((soy+sy) * srcwidth + sox + sx) * 4 + c];
|
||||
dstdata[((doy+ly + 1) * dstwidth + dox + lx + 1) * 4 + c] = srcdata[((soy+sy) * srcwidth + sox + sx) * 4 + c];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return new ImageData(image->getFormat(), dstwidth, dstheight, dstdata);
|
||||
}
|
||||
|
||||
@ -34,4 +34,6 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
extern ImageData* add_atlas_margins(ImageData* image, int grid_size);
|
||||
|
||||
#endif // GRAPHICS_IMAGE_DATA_H_
|
||||
@ -13,8 +13,10 @@ Texture::Texture(unsigned char* data, int width, int height, uint format) : widt
|
||||
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0,
|
||||
format, GL_UNSIGNED_BYTE, (GLvoid *) data);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 2);
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user