1
0
mirror of https://github.com/zenorogue/hyperrogue.git synced 2024-11-18 03:04:48 +00:00
hyperrogue/textures.cpp

1532 lines
46 KiB
C++
Raw Normal View History

// Hyperbolic Rogue -- texture mode
// Copyright (C) 2011-2018 Zeno Rogue, see 'hyper.cpp' for details
#if CAP_SDL_IMG
2017-12-09 01:20:10 +00:00
#include <SDL/SDL_image.h>
#elif CAP_PNG
#include <png.h>
#endif
2017-12-09 01:20:10 +00:00
#if CAP_TEXTURE
2017-12-14 01:53:29 +00:00
namespace texture {
2017-12-19 13:35:34 +00:00
cpatterntype cgroup;
#if CAP_PNG
2017-12-09 01:20:10 +00:00
SDL_Surface *convertSurface(SDL_Surface* s) {
SDL_PixelFormat fmt;
// fmt.format = SDL_PIXELFORMAT_BGRA8888;
fmt.BitsPerPixel = 32;
fmt.BytesPerPixel = 4;
fmt.Ashift=24;
fmt.Rshift=16;
fmt.Gshift=8;
fmt.Bshift=0;
fmt.Amask=0xff<<24;
fmt.Rmask=0xff<<16;
fmt.Gmask=0xff<<8;
fmt.Bmask=0xff;
fmt.Aloss = fmt.Rloss = fmt.Gloss = fmt.Bloss = 0;
fmt.palette = NULL;
#ifndef SDL2
fmt.alpha = 0;
fmt.colorkey = 0x1ffffff;
#endif
return SDL_ConvertSurface(s, &fmt, SDL_SWSURFACE);
}
#endif
2017-12-09 01:20:10 +00:00
2017-12-22 19:59:04 +00:00
struct undo {
unsigned* pix;
unsigned last;
};
2018-03-17 20:12:46 +00:00
texture_config config;
bool saving = false;
2017-12-22 19:59:04 +00:00
2018-03-17 20:12:46 +00:00
template<class T, class U> void scale_colorarray(int origdim, int targetdim, const T& src, const U& dest) {
2017-12-14 01:53:29 +00:00
int ox = 0, tx = 0, partials[4];
2018-03-17 20:12:46 +00:00
int omissing = targetdim, tmissing = origdim;
2017-12-14 01:53:29 +00:00
for(int p=0; p<4; p++) partials[p] = 0;
2017-12-09 01:20:10 +00:00
2018-03-17 20:12:46 +00:00
while(tx < targetdim) {
2017-12-14 01:53:29 +00:00
int fv = min(omissing, tmissing);
int c = src(ox);
for(int p=0; p<4; p++)
partials[p] += part(c, p) * fv;
omissing -= fv; tmissing -= fv;
if(omissing == 0) {
2018-03-17 20:12:46 +00:00
ox++; omissing = targetdim;
2017-12-14 01:53:29 +00:00
}
if(tmissing == 0) {
int target;
for(int p=0; p<4; p++) {
part(target, p) = partials[p] / origdim;
partials[p] = 0;
}
dest(tx++, target);
tmissing = origdim;
}
2017-12-09 01:20:10 +00:00
}
2017-12-14 01:53:29 +00:00
}
2018-03-17 20:12:46 +00:00
bool texture_data::loadTextureGL() {
2017-12-17 23:24:56 +00:00
2018-03-17 20:12:46 +00:00
if(textureid == 0) glGenTextures(1, &textureid);
2017-12-09 01:20:10 +00:00
2018-05-18 15:34:12 +00:00
glBindTexture( GL_TEXTURE_2D, textureid);
2017-12-17 23:24:56 +00:00
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, twidth, twidth, 0,
GL_BGRA, GL_UNSIGNED_BYTE,
2017-12-18 23:39:11 +00:00
&texture_pixels[0] );
2017-12-17 23:24:56 +00:00
return true;
}
2018-03-17 20:12:46 +00:00
bool texture_data::whitetexture() {
2017-12-22 19:59:04 +00:00
undos.clear();
2017-12-18 23:39:11 +00:00
texture_pixels.resize(0);
texture_pixels.resize(twidth * twidth, 0xFFFFFFFF);
2017-12-30 16:01:53 +00:00
pixels_to_draw.clear();
2017-12-17 23:24:56 +00:00
return true;
}
2018-03-17 20:12:46 +00:00
bool texture_data::readtexture(string tn) {
#if CAP_SDL_IMG || CAP_PNG
2017-12-22 19:59:04 +00:00
undos.clear();
2017-12-21 13:05:07 +00:00
texture_pixels.resize(twidth * twidth);
#if CAP_SDL_IMG
2018-03-17 20:12:46 +00:00
SDL_Surface *txt = IMG_Load(tn.c_str());
if(!txt) {
addMessage(XLAT("Failed to load %1", texturename));
return false;
}
2017-12-14 01:53:29 +00:00
auto txt2 = convertSurface(txt);
SDL_FreeSurface(txt);
int tx = txt2->w, ty = txt2->h;
2017-12-14 01:53:29 +00:00
auto pix = [&] (int x, int y) { return qpixel(txt2, x, y); };
#elif CAP_PNG
2017-12-14 01:53:29 +00:00
2018-03-17 20:12:46 +00:00
FILE *f = fopen(tn.c_str(), "rb");
2018-02-20 21:12:54 +00:00
if(!f) { printf("failed to open file\n"); return false; }
png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
2018-02-20 21:12:54 +00:00
if(!png) { printf("failed to create_read_struct\n"); return false; }
2018-01-05 18:05:41 +00:00
if(setjmp(png_jmpbuf(png))) {
printf("failed to read\n");
return false;
}
png_init_io(png, f);
// set the expected format
png_infop info = png_create_info_struct(png);
png_read_info(png, info);
int tx = png_get_image_width(png, info);
int ty = png_get_image_height(png, info);
png_byte color_type = png_get_color_type(png, info);
png_byte bit_depth = png_get_bit_depth(png, info);
if(bit_depth == 16) png_set_strip_16(png);
if(color_type == PNG_COLOR_TYPE_PALETTE) png_set_palette_to_rgb(png);
if(color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8) png_set_expand_gray_1_2_4_to_8(png);
if(png_get_valid(png, info, PNG_INFO_tRNS)) png_set_tRNS_to_alpha(png);
if(color_type == PNG_COLOR_TYPE_RGB ||
color_type == PNG_COLOR_TYPE_GRAY ||
color_type == PNG_COLOR_TYPE_PALETTE)
png_set_filler(png, 0xFF, PNG_FILLER_AFTER);
if(color_type == PNG_COLOR_TYPE_GRAY ||
color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
png_set_gray_to_rgb(png);
png_read_update_info(png, info);
// read png
vector<png_bytep> row_pointers(ty);
vector<int> origpixels(ty * tx);
for(int y = 0; y < ty; y++)
row_pointers[y] = (png_bytep) & origpixels[y * tx];
png_read_image(png, &row_pointers[0]);
fclose(f);
for(int i=0; i<ty*tx; i++)
swap(part(origpixels[i], 0), part(origpixels[i], 2));
auto pix = [&] (int x, int y) {
if(x<0 || y<0 || x >= tx || y >= ty) return 0;
return origpixels[y*tx + x];
};
2018-02-20 21:12:54 +00:00
printf("texture read OK\n");
2017-12-14 01:53:29 +00:00
#endif
2017-12-14 01:53:29 +00:00
if(tx == twidth && ty == twidth) {
int i = 0;
for(int y=0; y<ty; y++)
for(int x=0; x<tx; x++)
texture_pixels[i++] = pix(x, y);
}
else {
int origdim = max(tx, ty);
int base_x = tx/2 - origdim/2;
int base_y = ty/2 - origdim/2;
2017-12-22 20:24:08 +00:00
qpixel_pixel_outside = 0; // outside is black
2017-12-17 23:24:56 +00:00
vector<int> half_expanded(twidth * ty);
for(int y=0; y<ty; y++)
2018-03-17 20:12:46 +00:00
scale_colorarray(origdim, twidth,
[&] (int x) { return pix(base_x + x,y); },
[&] (int x, int v) { half_expanded[twidth * y + x] = v; }
);
for(int x=0; x<twidth; x++)
2018-03-17 20:12:46 +00:00
scale_colorarray(origdim, twidth,
[&] (int y) { return base_y+y < 0 || base_y+y >= ty ? 0 : half_expanded[x + (base_y + y) * twidth]; },
2017-12-22 19:59:04 +00:00
[&] (int y, int v) { get_texture_pixel(x, y) = v; }
);
}
2017-12-14 01:53:29 +00:00
#if CAP_SDL_IMG
2017-12-14 01:53:29 +00:00
SDL_FreeSurface(txt2);
#endif
2017-12-09 01:20:10 +00:00
return true;
#else
return false;
#endif
2017-12-09 01:20:10 +00:00
}
2018-03-17 20:12:46 +00:00
void texture_data::saveRawTexture(string tn) {
2017-12-21 11:58:36 +00:00
SDL_Surface *sraw = SDL_CreateRGBSurface(SDL_SWSURFACE,twidth,twidth,32,0,0,0,0);
for(int y=0; y<twidth; y++)
for(int x=0; x<twidth; x++)
2017-12-22 19:59:04 +00:00
qpixel(sraw,x,y) = get_texture_pixel(x, y);
2018-03-17 20:12:46 +00:00
IMAGESAVE(sraw, tn.c_str());
2017-12-21 11:58:36 +00:00
SDL_FreeSurface(sraw);
2018-03-17 20:12:46 +00:00
addMessage(XLAT("Saved the raw texture to %1", tn));
2017-12-21 11:58:36 +00:00
}
2018-03-17 20:12:46 +00:00
void texture_config::mapTextureTriangle(textureinfo &mi, const array<hyperpoint, 3>& v, const array<hyperpoint, 3>& tv, int splits) {
2017-12-09 01:20:10 +00:00
2017-12-14 01:53:29 +00:00
if(splits) {
2018-01-05 18:05:41 +00:00
array<hyperpoint, 3> v2 = make_array( mid(v[1], v[2]), mid(v[2], v[0]), mid(v[0], v[1]) );
array<hyperpoint, 3> tv2 = make_array( mid(tv[1], tv[2]), mid(tv[2], tv[0]), mid(tv[0], tv[1]) );
mapTextureTriangle(mi, make_array(v[0], v2[2], v2[1]), make_array(tv[0], tv2[2], tv2[1]), splits-1);
mapTextureTriangle(mi, make_array(v[1], v2[0], v2[2]), make_array(tv[1], tv2[0], tv2[2]), splits-1);
mapTextureTriangle(mi, make_array(v[2], v2[1], v2[0]), make_array(tv[2], tv2[1], tv2[0]), splits-1);
mapTextureTriangle(mi, make_array(v2[0], v2[1], v2[2]), make_array(tv2[0], tv2[1], tv2[2]), splits-1);
2017-12-14 01:53:29 +00:00
return;
}
2017-12-09 01:20:10 +00:00
for(int i=0; i<3; i++) {
2018-02-11 18:08:17 +00:00
mi.vertices.push_back(glhr::pointtogl(v[i]));
2017-12-09 01:20:10 +00:00
hyperpoint inmodel;
applymodel(tv[i], inmodel);
2017-12-14 01:53:29 +00:00
inmodel = itt * inmodel;
inmodel[0] *= vid.radius * 1. / vid.scrsize;
inmodel[1] *= vid.radius * 1. / vid.scrsize;
2018-02-11 18:08:17 +00:00
mi.tvertices.push_back(make_array<GLfloat>((inmodel[0]+1)/2, (inmodel[1]+1)/2, 0));
}
// when reading a spherical band in a cylindrical projection,
// take care that texture vertices are mapped not around the sphere
if(sphere && mdBandAny()) {
int s = size(mi.tvertices)-3;
ld xmin = min(mi.tvertices[s][0], min(mi.tvertices[s+1][0], mi.tvertices[s+2][0]));
ld xmax = max(mi.tvertices[s][0], max(mi.tvertices[s+1][0], mi.tvertices[s+2][0]));
if(xmax - xmin > .5) {
for(int ss=s; ss<s+3; ss++)
if(mi.tvertices[ss][0] < .5) mi.tvertices[ss][0] += 1;
}
}
2017-12-09 01:20:10 +00:00
}
texture_triangle *edited_triangle;
textureinfo *edited_tinfo;
array<hyperpoint, 3> findTextureTriangle(cell *c, patterns::patterninfo& si, int i) {
// auto si = getpatterninfo0(c);
transmatrix M = shmup::ggmatrix(c) * applyPatterndir(c, si);
ld z = ctof(c) ? rhexf : hexvdist;
hyperpoint h1 = spin(M_PI + M_PI * (2*i -1) / c->type) * xpush(z) * C0;
hyperpoint h2 = spin(M_PI + M_PI * (2*i +1) / c->type) * xpush(z) * C0;
return make_array(M * C0, M * h1, M * h2);
}
// using: mouseh, mouseouver
int getTriangleID(cell *c, patterns::patterninfo& si, hyperpoint h) {
// auto si = getpatterninfo0(c);
ld quality = 1e10;
int best = 0;
for(int i=0; i<c->type; i++) {
auto t = findTextureTriangle(c, si, i);
ld q = intval(t[1], h) + intval(t[2], h);
if(q < quality) quality = q, best = i;
}
return best;
}
2018-04-09 15:40:12 +00:00
int goldbergcode(cell *c, const patterns::patterninfo& si) {
if(!gp::on) return 0;
else if(c == c->master->c7) return (fixdir(si.dir, c) << 8);
2018-04-09 15:40:12 +00:00
else return (get_code(gp::get_local_info(c)) << 16) | (fixdir(si.dir, c) << 8);
}
void mapTexture(cell *c, textureinfo& mi, patterns::patterninfo &si, const transmatrix& T, int shift = 0) {
2017-12-14 01:53:29 +00:00
mi.c = c;
mi.symmetries = si.symmetries;
mi.current_type = c->type;
2018-04-09 15:40:12 +00:00
if(gp::on) {
mi.M = T;
mi.triangles.clear();
for(int i=0; i<c->type; i++) {
int d = si.dir;
int i0 = fixdir(i + d + 0, c);
int i1 = fixdir(i + d + 1, c);
2018-04-09 15:40:12 +00:00
hyperpoint h1 = gp::get_corner_position(c, i0);
hyperpoint h2 = gp::get_corner_position(c, i1);
mi.triangles.emplace_back(make_array(C0, h1, h2), make_array(mi.M*C0, mi.M*h1, mi.M*h2));
}
}
2017-12-14 01:53:29 +00:00
else {
mi.M = T * applyPatterndir(c, si);
ld z = ctof(c) ? rhexf : hexvdist;
mi.triangles.clear();
for(int i=0; i<c->type; i++) {
int i2 = i+shift;
hyperpoint h1 = spin(M_PI + M_PI * (2*i2 -1) / c->type) * xpush(z) * C0;
hyperpoint h2 = spin(M_PI + M_PI * (2*i2 +1) / c->type) * xpush(z) * C0;
mi.triangles.emplace_back(make_array(C0, h1, h2), make_array(mi.M*C0, mi.M*h1, mi.M*h2));
}
}
2017-12-14 01:53:29 +00:00
}
2018-03-17 20:12:46 +00:00
void texture_config::mapTexture2(textureinfo& mi) {
mi.vertices.clear();
mi.tvertices.clear();
for(auto& t: mi.triangles)
mapTextureTriangle(mi, t.v, t.tv);
}
2018-03-17 20:12:46 +00:00
int texture_config::recolor(int col) {
2017-12-14 01:53:29 +00:00
if(color_alpha == 0) return col;
for(int i=1; i<4; i++)
part(col, i) = color_alpha + ((255-color_alpha) * part(col,i) + 127) / 255;
return col;
}
2018-03-17 20:12:46 +00:00
bool texture_config::apply(cell *c, const transmatrix &V, int col) {
if(config.tstate == tsOff) return false;
2017-12-09 01:20:10 +00:00
using namespace patterns;
auto si = getpatterninfo0(c);
2017-12-14 01:53:29 +00:00
2018-03-17 20:12:46 +00:00
if(config.tstate == tsAdjusting) {
2017-12-14 01:53:29 +00:00
queuepolyat(V, shFullCross[ctof(c)], 0, PPR_LINE);
lastptd().u.poly.outline = slave_color;
2018-05-07 18:13:56 +00:00
draw_floorshape(c, V, shFullFloor, 0, PPR_LINE);
lastptd().u.poly.outline = slave_color;
2017-12-14 01:53:29 +00:00
return false;
}
2017-12-09 01:20:10 +00:00
try {
2018-05-07 18:13:56 +00:00
auto& mi = texture_map.at(si.id + goldbergcode(c, si));
2017-12-09 01:20:10 +00:00
2018-05-07 18:13:56 +00:00
set_floor(shFullFloor);
2017-12-09 01:20:10 +00:00
qfi.tinf = &mi;
2018-05-07 18:13:56 +00:00
qfi.spin = gp::on ? Id : applyPatterndir(c, si);
2017-12-09 01:20:10 +00:00
2017-12-17 23:24:56 +00:00
if(grid_color) {
2018-05-07 18:13:56 +00:00
draw_floorshape(c, V, shFullFloor, 0, PPR_FLOOR);
2017-12-17 23:24:56 +00:00
lastptd().u.poly.outline = grid_color;
2017-12-14 01:53:29 +00:00
}
2017-12-21 13:05:07 +00:00
if(texture::saving) {
// create a nicer aura for saved texture
2018-02-11 18:08:17 +00:00
for(int i=0; i<size(mi.tvertices); i += 3) {
2017-12-21 13:05:07 +00:00
ld p[3];
while(true) {
p[0] = hrandf();
p[1] = hrandf();
p[2] = 1 - p[0] - p[1];
if(p[2] >= 0) break;
}
ld v[2] = {0,0};
for(int j=0; j<2; j++) for(int k=0; k<3; k++)
2018-02-11 18:08:17 +00:00
v[j] += mi.tvertices[i+k][j] * p[k];
2017-12-21 13:05:07 +00:00
2018-03-17 20:12:46 +00:00
int vi[2] = {int(v[0] * config.data.twidth), int(v[1] * config.data.twidth)};
2017-12-21 13:05:07 +00:00
2018-03-17 20:12:46 +00:00
col = config.data.get_texture_pixel(vi[0], vi[1]);
2018-02-11 18:08:17 +00:00
hyperpoint h = glhr::gltopoint(mi.vertices[i]);
2017-12-21 13:05:07 +00:00
addaura(V*h, col, 0);
}
}
2017-12-09 01:20:10 +00:00
return true;
}
catch(out_of_range&) {
2018-04-09 15:40:12 +00:00
// printf("Ignoring tile #%d / %08x: not mapped\n", si.id, goldbergcode(c, si));
2017-12-09 01:20:10 +00:00
return false;
}
}
2018-03-17 20:12:46 +00:00
void texture_config::mark_triangles() {
if(config.tstate == tsAdjusting)
for(auto& mi: texture_map) {
for(auto& t: mi.second.triangles) {
vector<hyperpoint> t2;
for(int i=0; i<3; i++)
t2.push_back(t.tv[i]);
prettypoly(t2, master_color, master_color, gsplits);
}
}
}
2018-01-06 21:34:03 +00:00
static const auto current_texture_parameters = tie(geometry, nonbitrunc, patterns::whichPattern, patterns::subpattern_flags, pmodel, vid.scale, vid.alpha);
2018-03-17 20:12:46 +00:00
void texture_config::clear_texture_map() {
texture_map.clear();
edited_triangle = nullptr;
edited_tinfo = nullptr;
tuned_vertices.clear();
models.clear();
texture_tuned = false;
texture_tuner = "";
}
2018-03-17 20:12:46 +00:00
void texture_config::perform_mapping() {
2017-12-14 01:53:29 +00:00
if(gsplits < 0) gsplits = 0;
if(gsplits > 4) gsplits = 4;
using namespace patterns;
clear_texture_map();
2017-12-18 22:42:08 +00:00
for(auto& p: gmatrix) {
2017-12-09 01:20:10 +00:00
cell *c = p.first;
auto si = getpatterninfo0(c);
2017-12-09 01:20:10 +00:00
bool replace = false;
2017-12-09 19:01:50 +00:00
2017-12-14 01:53:29 +00:00
// int sgn = sphere ? -1 : 1;
2018-04-09 15:40:12 +00:00
si.id += goldbergcode(c, si);
2017-12-09 01:20:10 +00:00
if(!texture_map.count(si.id))
2017-12-09 01:20:10 +00:00
replace = true;
2017-12-16 08:03:50 +00:00
else if(hdist0(p.second*sphereflip * C0) < hdist0(texture_map[si.id].M * sphereflip * C0))
2017-12-09 01:20:10 +00:00
replace = true;
if(replace) {
auto& mi = texture_map[si.id];
2017-12-14 01:53:29 +00:00
mapTexture(c, mi, si, p.second);
2018-03-17 20:12:46 +00:00
mi.texture_id = config.data.textureid;
2017-12-17 23:24:56 +00:00
}
2017-12-09 01:20:10 +00:00
}
2017-12-14 01:53:29 +00:00
for(auto& t: texture_map) models.insert(t.second.c);
2017-12-18 22:42:08 +00:00
for(auto& p: gmatrix) {
cell *c = p.first;
bool nearmodel = models.count(c);
forCellEx(c2, c)
if(models.count(c2))
nearmodel = true;
if(nearmodel) {
auto si = getpatterninfo0(c);
texture_map[si.id].matrices.push_back(p.second * applyPatterndir(c, si));
}
}
2017-12-19 13:35:34 +00:00
}
2018-03-17 20:12:46 +00:00
void texture_config::finish_mapping() {
if(config.tstate == tsActive)
for(auto& mi: texture_map)
mapTexture2(mi.second);
patterns::computeCgroup();
texture::cgroup = patterns::cgroup;
texture_map_orig = texture_map;
orig_texture_parameters = current_texture_parameters;
// printf("texture_map has %d elements (S%d)\n", size(texture_map), config.tstate);
2017-12-09 01:20:10 +00:00
}
2018-03-17 20:12:46 +00:00
void texture_config::saveFullTexture(string tn) {
addMessage(XLAT("Saving full texture to %1...", tn));
2017-12-21 13:05:07 +00:00
dynamicval<unsigned> dd(grid_color, 0);
dynamicval<unsigned> dm(mesh_color, 0);
dynamicval<ld> dx(vid.xposition, 0);
dynamicval<ld> dy(vid.yposition, 0);
dynamicval<ld> dvs(vid.scale, (pmodel == mdDisk && !euclid) ? 1 : vid.scale);
dynamicval<bool> dro(rug::rugged, false);
2017-12-21 13:05:07 +00:00
texture::saving = true;
drawscreen();
2017-12-09 01:20:10 +00:00
2018-03-17 20:12:46 +00:00
dynamicval<int> dv(pngres, data.twidth);
saveHighQualityShot(tn.c_str());
2017-12-21 13:05:07 +00:00
texture::saving = false;
drawscreen();
2018-03-17 20:12:46 +00:00
if(data.readtexture(tn) && data.loadTextureGL()) {
2018-02-20 21:12:54 +00:00
itt = Id; // xyscale(Id, vid.scrsize * 1. / vid.radius);
perform_mapping();
finish_mapping();
}
2017-12-09 01:20:10 +00:00
}
2017-12-14 01:53:29 +00:00
bool newmove = false;
2018-02-11 18:08:17 +00:00
vector<glhr::textured_vertex> rtver(4);
2018-03-17 20:12:46 +00:00
void texture_config::drawRawTexture() {
2018-02-08 23:29:20 +00:00
glhr::be_textured();
2018-02-09 00:46:14 +00:00
glhr::color2(0xFFFFFF20);
2018-03-17 20:12:46 +00:00
glBindTexture(GL_TEXTURE_2D, config.data.textureid);
2017-12-14 01:53:29 +00:00
for(int i=0; i<4; i++) {
2017-12-25 09:26:50 +00:00
int cx[4] = {2, -2, -2, 2};
int cy[4] = {2, 2, -2, -2};
2017-12-14 01:53:29 +00:00
int x = cx[i];
int y = cy[i];
hyperpoint inmodel = hpxyz(x, y, 1);
inmodel = itt * inmodel;
2018-02-11 18:08:17 +00:00
rtver[i].texture[0] = (inmodel[0]+1)/2;
rtver[i].texture[1] = (inmodel[1]+1)/2;
rtver[i].coords[0] = x * vid.scrsize;
rtver[i].coords[1] = y * vid.scrsize;
2017-12-14 01:53:29 +00:00
}
2018-02-11 18:08:17 +00:00
glhr::set_modelview(glhr::translate(0, 0, stereo::scrdist));
glhr::prepare(rtver);
glhr::set_depthtest(false);
2017-12-14 01:53:29 +00:00
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
}
2017-12-09 01:20:10 +00:00
2017-12-16 08:03:50 +00:00
struct magicmapper_point {
cell *c;
hyperpoint cell_relative;
hyperpoint texture_coords;
};
vector<magicmapper_point> amp;
2018-01-03 00:05:03 +00:00
enum eMagicParameter {
mpScale,
mpProjection,
mpMove,
mpRotate,
mpSlant,
mpStretch,
mpTexPosX,
mpTexPosY,
2018-01-03 00:05:03 +00:00
mpMAX
};
vector<string> mpnames = {
"affect model scale",
"affect model projection",
"affect model central point",
2018-01-03 00:05:03 +00:00
"affect model rotation",
"affect texture slanting",
"affect texture stretching",
"affect texture position X",
"affect texture position Y"
2018-01-03 00:05:03 +00:00
};
flagtype current_magic = 15;
bool have_mp(eMagicParameter i) { return (current_magic >> i) & 1; }
2017-12-16 08:03:50 +00:00
struct magic_param {
bool do_spin;
int texmode;
ld spinangle, scale, proj, moveangle, shift, slant, stretch, tx, ty;
2017-12-16 08:03:50 +00:00
void shuffle() {
do_spin = hrand(2);
spinangle = hrandf() - hrandf();
moveangle = hrandf() * 2 * M_PI;
shift = hrandf() - hrandf();
scale = hrandf() - hrandf();
proj = hrandf() - hrandf();
texmode = hrand(3);
2018-01-03 00:05:03 +00:00
slant = have_mp(mpSlant) ? hrandf() - hrandf() : 0;
stretch = have_mp(mpStretch) ? hrandf() - hrandf() : 0;
tx = have_mp(mpTexPosX) ? hrandf() - hrandf() : 0;
ty = have_mp(mpTexPosY) ? hrandf() - hrandf() : 0;
2018-01-03 00:05:03 +00:00
}
void affect_itt(const transmatrix& T) {
transmatrix Ti = inverse(T);
for(auto& p: amp)
p.texture_coords = Ti * p.texture_coords;
2018-03-17 20:12:46 +00:00
config.itt = config.itt * T;
2017-12-16 08:03:50 +00:00
}
void apply(ld delta) {
2018-01-03 00:05:03 +00:00
if(have_mp(mpProjection))
vid.alpha *= exp(delta * proj);
if(have_mp(mpScale))
vid.scale *= exp(delta * scale);
if(do_spin) {
if(have_mp(mpRotate))
View = spin(delta * spinangle) * View;
}
else {
if(have_mp(mpMove))
View = spin(moveangle) * xpush(delta*shift) * spin(-moveangle) * View;
}
if(texmode == 0 && have_mp(mpStretch))
2018-01-03 00:05:03 +00:00
affect_itt(euaffine(hpxyz(0, delta * stretch, 0)));
2017-12-16 08:03:50 +00:00
if(texmode == 1 && have_mp(mpSlant))
2018-01-03 00:05:03 +00:00
affect_itt(euaffine(hpxyz(delta * slant, 0, 0)));
2017-12-16 08:03:50 +00:00
if(texmode == 2 && (have_mp(mpTexPosX) || have_mp(mpTexPosY)))
affect_itt(eupush(delta * tx, delta * ty));
2017-12-16 08:03:50 +00:00
fixmatrix(View);
}
};
ld magic_quality() {
gmatrix.clear();
calcparam();
ld q = 0;
for(auto& p: amp) {
hyperpoint inmodel;
applymodel(shmup::ggmatrix(p.c) * p.cell_relative, inmodel);
inmodel[0] *= vid.radius * 1. / vid.scrsize;
inmodel[1] *= vid.radius * 1. / vid.scrsize;
q += intvalxy(inmodel, p.texture_coords);
}
return q;
}
void applyMagic() {
ld cq = magic_quality();
int last_success = 0;
for(int s=0; s<50000 && s<last_success + 1000; s++) {
magic_param p;
p.shuffle();
bool failed = false;
for(ld delta = 1; abs(delta) > 1e-9; delta *= (failed ? -.7 : 1.2)) {
2017-12-16 08:03:50 +00:00
p.apply(delta);
ld nq = magic_quality();
if(nq < cq) {
cq = nq;
last_success = s;
}
else {
p.apply(-delta);
failed = true;
}
}
}
config.perform_mapping();
2017-12-16 08:03:50 +00:00
}
enum eTexturePanstate {tpsModel, tpsMove, tpsScale, tpsAffine, tpsZoom, tpsProjection, tpsCell, tpsTriangle, tpsTune};
2017-12-14 01:53:29 +00:00
eTexturePanstate panstate;
2017-12-09 01:20:10 +00:00
void mousemovement() {
static hyperpoint lastmouse;
hyperpoint mouseeu = hpxyz((mousex - vid.xcenter + .0) / vid.scrsize, (mousey - vid.ycenter + .0) / vid.scrsize, 1);
bool nonzero = mouseeu[0] || mouseeu[1];
switch(panstate) {
case tpsModel:
if(!newmove && mouseh[2] < 50 && lastmouse[2] < 50) {
panning(lastmouse, mouseh);
2018-03-17 20:12:46 +00:00
config.perform_mapping();
}
lastmouse = mouseh; newmove = false;
break;
case tpsMove: {
if(!newmove)
2018-03-17 20:12:46 +00:00
config.itt = config.itt * inverse(eupush(mouseeu)) * eupush(lastmouse);
lastmouse = mouseeu; newmove = false;
break;
}
case tpsScale: {
if(nonzero && !newmove)
2018-03-17 20:12:46 +00:00
config.itt = config.itt * inverse(euscalezoom(mouseeu)) * euscalezoom(lastmouse);
if(nonzero) lastmouse = mouseeu;
newmove = false;
break;
}
case tpsAffine: {
if(!newmove)
2018-03-17 20:12:46 +00:00
config.itt = config.itt * inverse(euaffine(mouseeu)) * euaffine(lastmouse);
lastmouse = mouseeu; newmove = false;
break;
}
case tpsZoom: {
// do not zoom in portrait!
if(nonzero && !newmove) {
2017-12-16 08:03:50 +00:00
View = inverse(spintox(mouseeu)) * spintox(lastmouse) * View;
vid.scale = vid.scale * sqrt(intvalxy(C0, mouseeu)) / sqrt(intvalxy(C0, lastmouse));
config.perform_mapping();
}
if(nonzero) lastmouse = mouseeu;
newmove = false;
break;
}
case tpsProjection: {
if(nonzero && !newmove) {
vid.alpha = vid.alpha * sqrt(intvalxy(C0, mouseeu)) / sqrt(intvalxy(C0, lastmouse));
}
if(nonzero) lastmouse = mouseeu;
newmove = false;
break;
}
case tpsCell: {
cell *c = mouseover;
if(!c) break;
auto si = patterns::getpatterninfo0(c);
if(newmove) {
edited_tinfo = NULL;
2018-03-17 20:12:46 +00:00
if(config.texture_map.count(si.id)) {
edited_tinfo = &config.texture_map[si.id];
newmove = false;
}
}
if(edited_tinfo && size(edited_tinfo->triangles) == c->type) {
for(int i=0; i<c->type; i++)
edited_tinfo->triangles[i].tv = findTextureTriangle(c, si, i);
2018-03-17 20:12:46 +00:00
config.texture_tuned = true;
}
break;
}
case tpsTriangle: {
cell *c = mouseover;
if(!c) break;
auto si = patterns::getpatterninfo0(c);
int i = getTriangleID(c, si, mouseh);
if(newmove) {
edited_triangle = NULL;
2018-03-17 20:12:46 +00:00
if(config.texture_map.count(si.id)) {
edited_triangle = &config.texture_map[si.id].triangles[i];
newmove = false;
}
}
if(edited_triangle) {
edited_triangle->tv = findTextureTriangle(c, si, i);
2018-03-17 20:12:46 +00:00
config.texture_tuned = true;
}
break;
}
case tpsTune: {
ld tdist = 1e20;
if(newmove) {
2018-03-17 20:12:46 +00:00
config.tuned_vertices.clear();
for(auto& a: config.texture_map)
for(auto& t: a.second.triangles)
for(auto& v: t.tv)
if(intval(v, mouseh) < tdist)
tdist = intval(v, mouseh);
2018-03-17 20:12:46 +00:00
for(auto& a: config.texture_map)
for(auto& t: a.second.triangles)
for(auto& v: t.tv)
if(intval(v, mouseh) < tdist * (1.000001))
2018-03-17 20:12:46 +00:00
config.tuned_vertices.push_back(&v);
newmove = false;
}
2018-03-17 20:12:46 +00:00
for(auto v: config.tuned_vertices) {
*v = mouseh;
2018-03-17 20:12:46 +00:00
config.texture_tuned = true;
}
break;
}
default: break;
}
}
patterns::patterninfo si_save;
saverlist texturesavers;
2018-01-06 21:34:03 +00:00
bool target_nonbitru;
void init_textureconfig() {
#if CAP_CONFIG
texturesavers = move(savers);
for(int i=0; i<3; i++)
for(int j=0; j<3; j++)
2018-03-17 20:12:46 +00:00
addsaver(config.itt[i][j], "texturematrix_" + its(i) + its(j), i==j ? 1 : 0);
for(int i=0; i<3; i++)
for(int j=0; j<3; j++)
addsaver(View[i][j], "viewmatrix_" + its(i) + its(j), i==j ? 1 : 0);
addsaverenum(targetgeometry, "geometry", gNormal);
2017-12-25 09:26:50 +00:00
addsaverenum(pmodel, "used model", mdDisk);
addsaver(vid.yshift, "Y shift", 0);
2017-12-25 09:26:50 +00:00
addsaver(vid.yposition, "Y position", 0);
addsaver(vid.xposition, "X position", 0);
addsaver(vid.camera_angle, "camera angle", 0);
2018-01-06 21:34:03 +00:00
addsaverenum(target_nonbitru, "bitruncated", false);
// ... geometry parameters
addsaver(patterns::whichPattern, "pattern", 0);
addsaver(patterns::subpattern_flags, "pattern flags", 0);
cell *ctr = euclid ? centerover.c : viewctr.h->c7;
si_save = patterns::getpatterninfo0(ctr);
addsaver(si_save.id, "center type", 1);
addsaver(si_save.dir, "center direction", 0);
addsaver(si_save.reflect, "center reflection", false);
2018-01-03 00:21:38 +00:00
addsaver(viewctr.spin, "center spin", 0);
2018-03-17 20:12:46 +00:00
addsaver(config.data.twidth, "texture resolution", 2048);
addsaver(config.gsplits, "precision", 1);
2018-03-17 20:12:46 +00:00
addsaver(config.grid_color, "grid color", 0);
addsaver(config.color_alpha, "alpha color", 0);
addsaver(config.mesh_color, "mesh color", 0);
addsaver(vid.alpha, "projection", 1);
addsaver(vid.scale, "scale", 1);
2018-03-17 20:12:46 +00:00
addsaver(config.texturename, "texture filename", "");
addsaver(config.texture_tuner, "texture tuning", "");
swap(texturesavers, savers);
#endif
}
2018-03-17 20:12:46 +00:00
bool texture_config::save() {
#if CAP_CONFIG
init_textureconfig();
FILE *f = fopen(configname.c_str(), "wt");
if(!f) return false;
if(texture_tuned) {
texture_tuner = "";
2018-03-17 20:12:46 +00:00
for(auto& a: config.texture_map)
for(auto& t: a.second.triangles)
for(auto& v: t.tv)
for(int i=0; i<3; i++) {
texture_tuner += ftssmart(v[i]);
texture_tuner += ';';
}
}
targetgeometry = geometry;
2018-01-06 21:34:03 +00:00
target_nonbitru = nonbitrunc;
for(auto s: texturesavers) if(s->dosave())
fprintf(f, "%s=%s\n", s->name.c_str(), s->save().c_str());
fclose(f);
#endif
return true;
}
2018-03-17 20:12:46 +00:00
bool texture_config::load() {
#if CAP_CONFIG
init_textureconfig();
FILE *f = fopen(configname.c_str(), "rt");
if(!f) return false;
swap(texturesavers, savers);
for(auto s: savers) s->reset();
loadNewConfig(f);
swap(texturesavers, savers);
fclose(f);
if(1) {
dynamicval<char> d1(patterns::whichPattern, patterns::whichPattern);
dynamicval<int> d2(patterns::subpattern_flags, patterns::subpattern_flags);
if(targetgeometry != geometry) {
stop_game_and_switch_mode(rg::geometry);
2018-03-17 20:12:46 +00:00
return config.load();
}
2018-01-06 21:34:03 +00:00
if(nonbitrunc != target_nonbitru) {
stop_game_and_switch_mode(rg::bitrunc);
}
}
start_game();
2018-01-03 00:21:38 +00:00
if(true) {
celllister cl(currentmap->gamestart(), 20, 10000, NULL);
bool found = false;
for(cell *c: cl.lst) if(euclid || ctof(c)) {
cell *ctr = euclid ? centerover.c : viewctr.h->c7;
auto si_here = patterns::getpatterninfo0(c);
if(si_here.id == si_save.id && si_here.reflect == si_save.reflect && si_here.dir == si_save.dir) {
if(euclid) centerover.c = ctr;
else viewctr.h = ctr->master;
found = true;
break;
}
}
if(!found)
addMessage(XLAT("warning: unable to find the center"));
}
2018-03-17 20:12:46 +00:00
if(!data.readtexture(texturename)) return false;
if(!data.loadTextureGL()) return false;
2017-12-16 08:03:50 +00:00
calcparam();
drawthemap();
2018-03-17 20:12:46 +00:00
config.tstate = config.tstate_max = tsActive;
string s = move(texture_tuner);
perform_mapping();
texture_tuner = move(s);
if(texture_tuner != "") {
texture_tuned = true;
vector<ld*> coords;
2018-03-17 20:12:46 +00:00
for(auto& a: config.texture_map)
for(auto& t: a.second.triangles)
for(auto& v: t.tv)
for(int i=0; i<3; i++)
coords.push_back(&v[i]);
int semicounter = 0;
for(char c: texture_tuner) if(c == ';') semicounter++;
if(semicounter != size(coords))
addMessage("Tuning error: wrong number");
else {
string cur = "";
int index = 0;
for(char c: texture_tuner)
if(c == ';') {
*(coords[index++]) = atof(cur.c_str());
cur = "";
}
else cur += c;
printf("index = %d semi = %d sc = %d\n", index, semicounter, size(coords));
}
}
finish_mapping();
#endif
return true;
}
2018-01-03 00:05:03 +00:00
void showMagicMenu() {
cmode = sm::SIDE | sm::MAYDARK | sm::DIALOG_STRICT_X;
gamescreen(0);
dialog::init(XLAT("texture auto-adjustment"));
dialog::addInfo(XLAT("drag from the model to the texture"));
for(int i=0; i<mpMAX; i++)
dialog::addBoolItem(XLAT(mpnames[i]), have_mp(eMagicParameter(i)), 'a'+i);
dialog::addSelItem(XLAT("delete markers"), its(size(amp)), 'D');
dialog::addItem(XLAT("perform auto-adjustment"), 'R');
dialog::addItem(XLAT("back"), '0');
getcstat = '-';
dialog::display();
if(holdmouse) {
hyperpoint mouseeu = hpxyz((mousex - vid.xcenter + .0) / vid.scrsize, (mousey - vid.ycenter + .0) / vid.scrsize, 1);
if(newmove) {
magicmapper_point newpoint;
newpoint.c = mouseover;
newpoint.cell_relative = inverse(gmatrix[mouseover]) * mouseh;
amp.push_back(newpoint);
newmove = false;
}
amp.back().texture_coords = mouseeu;
}
2018-03-17 20:12:46 +00:00
if(config.tstate == tsAdjusting) {
2018-01-03 00:05:03 +00:00
initquickqueue();
char letter = 'A';
for(auto& am: amp) {
hyperpoint h = shmup::ggmatrix(am.c) * am.cell_relative;
display(h);
queuechr(h, vid.fsize, letter, 0xC00000, 1);
hyperpoint inmodel;
applymodel(h, inmodel);
inmodel[0] *= vid.radius * 1. / vid.scrsize;
inmodel[1] *= vid.radius * 1. / vid.scrsize;
queuechr(
vid.xcenter + vid.scrsize * am.texture_coords[0],
vid.ycenter + vid.scrsize * am.texture_coords[1],
0, vid.fsize, letter, 0x00C000, 1);
letter++;
}
quickqueue();
}
keyhandler = [] (int sym, int uni) {
// handlePanning(sym, uni);
dialog::handleNavigation(sym, uni);
2018-03-17 20:12:46 +00:00
if(uni == '-' && config.tstate == tsAdjusting) {
2018-01-03 00:05:03 +00:00
if(!holdmouse) {
holdmouse = true;
newmove = true;
}
}
else if(uni >= 'a' && uni < 'a' + mpMAX)
current_magic ^= 1<<(uni - 'a');
else if(uni == 'D') amp.clear();
else if(uni == 'R') applyMagic();
else if(doexiton(sym, uni))
popScreen();
};
}
string texturehelp =
"This mode lets you to change the floor tesselation easily -- "
"select 'paint a new texture' and draw like in a Paint program. "
"The obtained pattern can then be easily changed to another geometry, "
"or saved.\n\n"
"Instead of drawing, it is also possible to use an arbitrary image "
"as a texture. "
"Works best with spherical/Euclidean/hyperbolic tesselations "
"(e.g., a photo of a soccerball, or one of the tesselations by M. C. "
"Escher), but it can be also used on arbitrary photos to make them periodic "
"(these probably work best with the 'large picture' setting in geometry selection). "
"Again, tesselations can have their geometry changed.\n\n";
2017-12-14 01:53:29 +00:00
void showMenu() {
cmode = sm::SIDE | sm::MAYDARK | sm::DIALOG_STRICT_X;
gamescreen(0);
2018-03-17 20:12:46 +00:00
if(config.tstate == tsAdjusting) {
ptds.clear();
2018-03-17 20:12:46 +00:00
config.mark_triangles();
drawqueue();
}
2017-12-09 01:20:10 +00:00
2018-03-17 20:12:46 +00:00
if(config.tstate == tsOff) {
dialog::init(XLAT("texture mode (off)"));
2017-12-21 11:02:15 +00:00
dialog::addItem(XLAT("select geometry/pattern"), 'r');
2018-03-17 20:12:46 +00:00
if(config.tstate_max == tsAdjusting || config.tstate_max == tsActive)
2017-12-19 13:35:34 +00:00
dialog::addItem(XLAT("reactivate the texture"), 't');
dialog::addItem(XLAT("open PNG as texture"), 'o');
dialog::addItem(XLAT("load texture config"), 'l');
2018-03-17 20:12:46 +00:00
dialog::addSelItem(XLAT("texture size"), its(config.data.twidth), 'w');
#if CAP_EDIT
2017-12-19 13:35:34 +00:00
dialog::addItem(XLAT("paint a new texture"), 'n');
#endif
2018-03-17 20:12:46 +00:00
dialog::addSelItem(XLAT("precision"), its(config.gsplits), 'P');
}
2017-12-14 01:53:29 +00:00
2018-03-17 20:12:46 +00:00
if(config.tstate == tsAdjusting) {
dialog::init(XLAT("texture mode (overlay)"));
2017-12-19 13:35:34 +00:00
dialog::addItem(XLAT("select the texture's pattern"), 'r');
dialog::addItem(XLAT("enable the texture"), 't');
dialog::addItem(XLAT("cancel the texture"), 'T');
2017-12-14 01:53:29 +00:00
dialog::addBoolItem(XLAT("move the model"), panstate == tpsModel, 'm');
dialog::addBoolItem(XLAT("move the texture"), panstate == tpsMove, 'a');
dialog::addBoolItem(XLAT("zoom/scale the texture"), panstate == tpsScale, 'x');
dialog::addBoolItem(XLAT("zoom/scale the model"), panstate == tpsZoom, 'z');
dialog::addBoolItem(XLAT("projection"), panstate == tpsProjection, 'p');
dialog::addBoolItem(XLAT("affine transformations"), panstate == tpsAffine, 'y');
2018-01-03 00:05:03 +00:00
dialog::addBoolItem(XLAT("magic"), false, 'A');
dialog::addBreak(50);
dialog::addBoolItem(XLAT("select master cells"), panstate == tpsCell, 'C');
dialog::addBoolItem(XLAT("select master triangles"), panstate == tpsTriangle, 'X');
dialog::addBoolItem(XLAT("fine tune vertices"), panstate == tpsTune, 'F');
2017-12-17 23:24:56 +00:00
2018-03-17 20:12:46 +00:00
dialog::addColorItem(XLAT("grid color (master)"), config.master_color, 'M');
dialog::addColorItem(XLAT("grid color (copy)"), config.slave_color, 'K');
2017-12-16 08:03:50 +00:00
dialog::addBreak(50);
2018-03-17 20:12:46 +00:00
dialog::addSelItem(XLAT("precision"), its(config.gsplits), 'P');
2017-12-21 11:58:36 +00:00
dialog::addItem(XLAT("save the raw texture"), 'S');
2017-12-14 01:53:29 +00:00
}
2017-12-09 01:20:10 +00:00
2018-03-17 20:12:46 +00:00
if(config.tstate == tsActive) {
dialog::init(XLAT("texture mode (active)"));
2017-12-14 01:53:29 +00:00
/* dialog::addSelItem(XLAT("texture scale"), fts(iscale), 's');
dialog::addSelItem(XLAT("texture angle"), fts(irotate), 'a');
dialog::addSelItem(XLAT("texture position X"), fts(ix), 'x');
dialog::addSelItem(XLAT("texture position Y"), fts(iy), 'y'); */
2017-12-19 13:35:34 +00:00
dialog::addItem(XLAT("deactivate the texture"), 't');
dialog::addItem(XLAT("back to overlay mode"), 'T');
2017-12-19 13:35:34 +00:00
dialog::addItem(XLAT("change the geometry"), 'r');
2018-03-17 20:12:46 +00:00
dialog::addColorItem(XLAT("grid color"), config.grid_color, 'g');
dialog::addColorItem(XLAT("mesh color"), config.mesh_color, 'm');
dialog::addSelItem(XLAT("color alpha"), its(config.color_alpha), 'c');
dialog::addSelItem(XLAT("precision"), its(config.gsplits), 'P');
#if CAP_EDIT
dialog::addItem(XLAT("edit the texture"), 'e');
#endif
2017-12-21 13:05:07 +00:00
dialog::addItem(XLAT("save the full texture image"), 'S');
2018-01-04 19:05:34 +00:00
dialog::addItem(XLAT("save texture config"), 's');
2017-12-09 01:20:10 +00:00
}
dialog::addItem(XLAT("help"), SDLK_F1);
dialog::addItem(XLAT("back"), '0');
2017-12-14 01:53:29 +00:00
getcstat = '-';
2017-12-09 01:20:10 +00:00
dialog::display();
if(holdmouse) mousemovement();
2017-12-14 01:53:29 +00:00
2017-12-09 01:20:10 +00:00
keyhandler = [] (int sym, int uni) {
2017-12-14 01:53:29 +00:00
// handlePanning(sym, uni);
2017-12-09 01:20:10 +00:00
dialog::handleNavigation(sym, uni);
2017-12-14 01:53:29 +00:00
2018-03-17 20:12:46 +00:00
if(uni == '-' && config.tstate == tsAdjusting) {
2017-12-14 01:53:29 +00:00
if(!holdmouse) {
holdmouse = true;
newmove = true;
}
}
2018-03-17 20:12:46 +00:00
else if(uni == 'm' && config.tstate == tsAdjusting) panstate = tpsModel;
else if(uni == 'a' && config.tstate == tsAdjusting) panstate = tpsMove;
else if(uni == 'x' && config.tstate == tsAdjusting) panstate = tpsScale;
else if(uni == 'y' && config.tstate == tsAdjusting) panstate = tpsAffine;
else if(uni == 'z' && config.tstate == tsAdjusting) panstate = tpsZoom;
else if(uni == 'p' && config.tstate == tsAdjusting) panstate = tpsProjection;
else if(uni == 'C' && config.tstate == tsAdjusting) panstate = tpsCell;
else if(uni == 'X' && config.tstate == tsAdjusting) panstate = tpsTriangle;
else if(uni == 'F' && config.tstate == tsAdjusting) panstate = tpsTune;
else if(uni == 'A' && config.tstate == tsAdjusting)
2018-01-03 00:05:03 +00:00
pushScreen(showMagicMenu);
2017-12-16 08:03:50 +00:00
2018-03-17 20:12:46 +00:00
else if(uni == 's' && config.tstate == tsActive)
dialog::openFileDialog(config.configname, XLAT("save texture config"), ".txc",
[] () {
2018-03-17 20:12:46 +00:00
return config.save();
});
2018-03-17 20:12:46 +00:00
else if(uni == 'l' && config.tstate == tsOff)
dialog::openFileDialog(config.configname, XLAT("load texture config"), ".txc",
[] () {
2018-03-17 20:12:46 +00:00
return config.load();
});
2017-12-19 13:35:34 +00:00
else if(uni == 'r')
patterns::pushChangeablePatterns();
2018-03-17 20:12:46 +00:00
else if(uni == 'o' && config.tstate == tsOff)
dialog::openFileDialog(config.texturename, XLAT("open PNG as texture"), ".png",
[] () {
2018-03-17 20:12:46 +00:00
if(config.data.readtexture(config.texturename) && config.data.loadTextureGL()) {
if(config.tstate_max == tsOff) config.tstate_max = tsAdjusting;
config.tstate = config.tstate_max;
config.perform_mapping();
config.finish_mapping();
return true;
}
else return false;
});
2018-03-17 20:12:46 +00:00
else if(uni == 'w' && config.tstate == tsOff) {
config.data.twidth *= 2;
if(config.data.twidth > 9000) config.data.twidth = 256;
config.tstate_max = tsOff;
2017-12-18 23:39:11 +00:00
}
#if CAP_EDIT
2018-03-17 20:12:46 +00:00
else if(uni == 'e' && config.tstate == tsActive) {
mapeditor::initdraw(cwt.c);
pushScreen(mapeditor::showDrawEditor);
}
2018-03-17 20:12:46 +00:00
else if(uni == 'n' && config.tstate == tsOff) {
2017-12-17 23:24:56 +00:00
addMessage("white");
2018-03-17 20:12:46 +00:00
if(config.data.whitetexture() && config.data.loadTextureGL()) {
config.tstate = config.tstate_max = tsActive;
config.perform_mapping();
config.finish_mapping();
2017-12-17 23:24:56 +00:00
mapeditor::initdraw(cwt.c);
pushScreen(mapeditor::showDrawEditor);
}
}
#endif
2017-12-17 23:24:56 +00:00
2018-03-17 20:12:46 +00:00
else if(uni == 't' && config.tstate == tsOff)
config.tstate = config.tstate_max;
2018-03-17 20:12:46 +00:00
else if(uni == 't' && config.tstate == tsAdjusting) {
config.tstate = config.tstate_max = tsActive;
config.finish_mapping();
2017-12-09 01:20:10 +00:00
}
2018-03-17 20:12:46 +00:00
else if(uni == 't' && config.tstate == tsActive)
config.tstate = tsOff;
2018-04-06 10:20:52 +00:00
else if(uni == 'T' && config.tstate == tsAdjusting) {
2018-03-17 20:12:46 +00:00
config.tstate = tsOff;
2018-05-03 10:15:58 +00:00
config.tstate_max = tsOff;
2018-04-06 10:20:52 +00:00
}
2017-12-19 13:35:34 +00:00
2018-03-17 20:12:46 +00:00
else if(uni == 'T' && config.tstate == tsActive)
config.tstate = tsAdjusting;
2018-03-17 20:12:46 +00:00
else if(uni == 'g' && config.tstate == tsActive)
dialog::openColorDialog(config.grid_color, NULL);
else if(uni == 'm' && config.tstate == tsActive)
dialog::openColorDialog(config.mesh_color, NULL);
else if(uni == 'M' && config.tstate == tsAdjusting)
dialog::openColorDialog(config.master_color, NULL);
else if(uni == 'K' && config.tstate == tsAdjusting)
2018-03-17 20:12:46 +00:00
dialog::openColorDialog(config.slave_color, NULL);
else if(uni == 'c' && config.tstate == tsActive) {
dialog::editNumber(config.color_alpha, 0, 255, 15, 0, XLAT("color alpha"),
2017-12-14 01:53:29 +00:00
XLAT("The higher the value, the less important the color of underlying terrain is."));
2017-12-09 01:20:10 +00:00
}
else if(uni == 'P') {
2018-03-17 20:12:46 +00:00
dialog::editNumber(config.gsplits, 0, 4, 1, 1, XLAT("precision"),
2017-12-14 01:53:29 +00:00
XLAT("precision"));
2018-03-17 20:12:46 +00:00
if(config.tstate == tsActive) dialog::reaction = [] () { config.finish_mapping();
};
}
2018-03-17 20:12:46 +00:00
else if(uni == 'S' && config.tstate == tsAdjusting)
dialog::openFileDialog(config.texturename, XLAT("save the raw texture"), ".png",
2018-01-04 19:05:34 +00:00
[] () {
2018-03-17 20:12:46 +00:00
config.data.saveRawTexture(config.texturename); return true;
2018-01-04 19:05:34 +00:00
});
2018-03-17 20:12:46 +00:00
else if(uni == 'S' && config.tstate == tsActive)
dialog::openFileDialog(config.texturename, XLAT("save the full texture image"), ".png",
2018-01-04 19:05:34 +00:00
[] () {
2018-03-17 20:12:46 +00:00
config.saveFullTexture(config.texturename);
return true;
2018-01-04 19:05:34 +00:00
});
else if(uni == SDLK_F1)
gotoHelp(texturehelp);
2017-12-09 01:20:10 +00:00
else if(doexiton(sym, uni))
popScreen();
};
}
2017-12-18 22:42:08 +00:00
typedef pair<int,int> point;
point ptc(hyperpoint h) {
2017-12-17 23:24:56 +00:00
hyperpoint inmodel;
applymodel(h, inmodel);
2018-03-17 20:12:46 +00:00
inmodel = config.itt * inmodel;
2017-12-17 23:24:56 +00:00
inmodel[0] *= vid.radius * 1. / vid.scrsize;
inmodel[1] *= vid.radius * 1. / vid.scrsize;
2018-03-17 20:12:46 +00:00
int x = (1 + inmodel[0]) * config.data.twidth / 2;
int y = (1 + inmodel[1]) * config.data.twidth / 2;
2017-12-17 23:24:56 +00:00
return make_pair(x,y);
}
2017-12-18 22:42:08 +00:00
array<point, 3> ptc(const array<hyperpoint, 3>& h) {
return make_array(ptc(h[0]), ptc(h[1]), ptc(h[2]));
2017-12-18 22:42:08 +00:00
}
ld penwidth = .02;
int texture_distance(pair<int, int> p1, pair<int, int> p2) {
2017-12-17 23:24:56 +00:00
return max(abs(p1.first-p2.first), abs(p1.second - p2.second));
}
2017-12-22 19:59:04 +00:00
void fillpixel(int x, int y, unsigned col) {
2018-03-17 20:12:46 +00:00
if(x<0 || y<0 || x >= config.data.twidth || y >= config.data.twidth) return;
auto& pix = config.data.get_texture_pixel(x, y);
2017-12-22 19:59:04 +00:00
if(pix != col) {
2018-03-17 20:12:46 +00:00
config.data.undos.emplace_back(&pix, pix);
2017-12-22 19:59:04 +00:00
pix = col;
}
}
2018-03-17 20:12:46 +00:00
void texture_data::undo() {
2017-12-22 19:59:04 +00:00
while(!undos.empty()) {
auto p = undos.back();
undos.pop_back();
if(!p.first) {
loadTextureGL();
return;
}
*p.first = p.second;
}
}
2018-03-17 20:12:46 +00:00
void texture_data::undoLock() {
2017-12-22 19:59:04 +00:00
printf("undos size = %d\n", size(undos));
if(size(undos) > 2000000) {
// limit undo memory
int moveto = 0;
for(int i=0; i < size(undos) - 1000000; i++)
if(!undos[i].first) moveto = i;
if(moveto) {
for(int i=0; i+moveto < size(undos); i++)
undos[i] = undos[i+moveto];
undos.resize(size(undos) - moveto);
printf("undos sized to = %d\n", size(undos));
}
}
undos.emplace_back(nullptr, 1);
}
2017-12-18 22:42:08 +00:00
void filltriangle(const array<hyperpoint, 3>& v, const array<point, 3>& p, int col, int lev) {
2017-12-17 23:24:56 +00:00
int d2 = texture_distance(p[0], p[1]), d1 = texture_distance(p[0], p[2]), d0 = texture_distance(p[1], p[2]);
2017-12-17 23:24:56 +00:00
2017-12-18 22:42:08 +00:00
int a, b, c;
if((d0 <= 1 && d1 <= 1 && d2 <= 1) || lev >= 20) {
2017-12-17 23:24:56 +00:00
for(int i=0; i<3; i++)
2017-12-22 19:59:04 +00:00
fillpixel(p[i].first, p[i].second, col);
2017-12-17 23:24:56 +00:00
return;
}
else if(d1 >= d0 && d1 >= d2)
2017-12-18 22:42:08 +00:00
a = 0, b = 2, c = 1;
2017-12-17 23:24:56 +00:00
else if(d2 >= d0 && d2 >= d1)
2017-12-18 22:42:08 +00:00
a = 0, b = 1, c = 2;
else
a = 1, b = 2, c = 0;
hyperpoint v3 = mid(v[a], v[b]);
point p3 = ptc(v3);
filltriangle(make_array(v[c], v[a], v3), make_array(p[c], p[a], p3), col, lev+1);
filltriangle(make_array(v[c], v[b], v3), make_array(p[c], p[b], p3), col, lev+1);
2017-12-18 22:42:08 +00:00
}
void splitseg(const transmatrix& A, const array<ld, 2>& angles, const array<hyperpoint, 2>& h, const array<point, 2>& p, int col, int lev) {
ld newangle = (angles[0] + angles[1]) / 2;
hyperpoint nh = A * spin(newangle) * xpush(penwidth) * C0;
auto np = ptc(nh);
filltriangle(make_array(h[0],h[1],nh), make_array(p[0],p[1],np), col, lev);
2017-12-18 22:42:08 +00:00
if(lev < 10) {
if(texture_distance(p[0],np) > 1)
splitseg(A, make_array(angles[0], newangle), make_array(h[0], nh), make_array(p[0], np), col, lev+1);
if(texture_distance(np,p[1]) > 1)
splitseg(A, make_array(newangle, angles[1]), make_array(nh, h[1]), make_array(np, p[1]), col, lev+1);
2017-12-18 22:42:08 +00:00
}
2017-12-17 23:24:56 +00:00
}
void fillcircle(hyperpoint h, int col) {
transmatrix A = rgpushxto0(h);
2017-12-18 22:42:08 +00:00
ld step = M_PI * 2/3;
2018-01-05 18:05:41 +00:00
array<hyperpoint, 3> mh = make_array(A * xpush(penwidth) * C0, A * spin(step) * xpush(penwidth) * C0, A * spin(-step) * xpush(penwidth) * C0);
2017-12-18 22:42:08 +00:00
auto mp = ptc(mh);
2017-12-17 23:24:56 +00:00
2017-12-18 22:42:08 +00:00
filltriangle(mh, mp, col, 0);
for(int i=0; i<3; i++) {
int j = (i+1) % 3;
if(texture_distance(mp[i], mp[j]) > 1)
splitseg(A, make_array(step*i, step*(i+1)), make_array(mh[i], mh[j]), make_array(mp[i], mp[j]), col, 1);
2017-12-18 22:42:08 +00:00
}
2017-12-17 23:24:56 +00:00
}
2017-12-22 20:57:55 +00:00
bool texturesym = false;
2017-12-30 16:01:53 +00:00
void actDrawPixel(cell *c, hyperpoint h, int col) {
2017-12-17 23:24:56 +00:00
try {
transmatrix M = gmatrix.at(c);
auto si = patterns::getpatterninfo0(c);
h = inverse(M * applyPatterndir(c, si)) * h;
2018-03-17 20:12:46 +00:00
auto& tinf = config.texture_map[si.id];
2017-12-17 23:24:56 +00:00
for(auto& M2: tinf.matrices) for(int i = 0; i<c->type; i += si.symmetries) {
fillcircle(M2 * spin(2 * M_PI * i / c->type) * h, col);
2017-12-22 20:57:55 +00:00
if(texturesym)
fillcircle(M2 * spin(2 * M_PI * i / c->type) * Mirror * h, col);
2017-12-17 23:24:56 +00:00
}
}
catch(out_of_range&) {}
2017-12-17 23:24:56 +00:00
}
2017-12-30 16:01:53 +00:00
void drawPixel(cell *c, hyperpoint h, int col) {
2018-03-17 20:12:46 +00:00
config.data.pixels_to_draw.emplace_back(c, h, col);
2017-12-30 16:01:53 +00:00
}
cell *where;
void drawPixel(hyperpoint h, int col) {
try {
again:
transmatrix g0 = gmatrix[where];
ld cdist0 = hdist(tC0(g0), h);
forCellEx(c, where)
try {
transmatrix g = gmatrix[c];
ld cdist = hdist(tC0(g), h);
if(cdist < cdist0) {
cdist0 = cdist;
where = c; g0 = g;
goto again;
}
}
catch(out_of_range&) {}
drawPixel(where, h, col);
}
catch(out_of_range&) {}
}
void drawLine(hyperpoint h1, hyperpoint h2, int col, int steps) {
if(steps > 0 && hdist(h1, h2) > penwidth / 3) {
hyperpoint h3 = mid(h1, h2);
drawLine(h1, h3, col, steps-1);
drawLine(h3, h2, col, steps-1);
}
else
drawPixel(h2, col);
}
2018-03-17 20:12:46 +00:00
void texture_config::remap(eTextureState old_tstate, eTextureState old_tstate_max) {
drawthemap();
clear_texture_map();
if(old_tstate == tsActive && patterns::compatible(texture::cgroup, patterns::cgroup)) {
2018-03-17 20:12:46 +00:00
config.tstate = old_tstate;
config.tstate_max = old_tstate_max;
for(cell *c: dcal) {
auto si = patterns::getpatterninfo0(c);
int oldid = si.id;
2018-04-09 15:40:12 +00:00
si.id += goldbergcode(c, si);
if(texture_map.count(si.id)) continue;
int pshift = 0;
if(texture::cgroup == cpSingle) oldid = 1;
if(texture::cgroup == cpFootball && patterns::cgroup == cpThree) {
if(si.id == 4) pshift = 1;
oldid = !si.id;
}
try {
auto& mi = texture_map_orig.at(oldid);
int ncurr = size(mi.tvertices);
int ntarget = ncurr * c->type / mi.current_type;
2018-02-11 18:08:17 +00:00
vector<glvertex> new_tvertices = mi.tvertices;
new_tvertices.resize(ntarget);
for(int i=ncurr; i<ntarget; i++) {
new_tvertices[i] = new_tvertices[i - ncurr];
}
auto& mi2 = texture_map[si.id];
mi2 = mi;
mapTexture(c, mi2, si, shmup::ggmatrix(c), pshift);
mapTexture2(mi2);
mi2.tvertices = move(new_tvertices);
// printf("%08x remapping %d vertices to %d vertices\n", si.id, size(mi.tvertices), size(mi2.tvertices));
}
catch(out_of_range&) {
printf("Unexpected missing cell #%d/%d", si.id, oldid);
addMessage(XLAT("Unexpected missing cell #%d/%d", its(si.id), its(oldid)));
2018-03-17 20:12:46 +00:00
config.tstate_max = config.tstate = tsAdjusting;
return;
}
}
}
else if(old_tstate >= tsAdjusting || old_tstate_max >= tsAdjusting) {
2018-03-17 20:12:46 +00:00
printf("perform_mapping %d/%d\n", config.tstate, config.tstate_max);
calcparam();
drawthemap();
perform_mapping();
finish_mapping();
printf("texture_map size = %d\n", size(texture_map));
}
}
eTextureState old_tstate_cl, old_tstate_max_cl;
2017-12-17 23:24:56 +00:00
2017-12-21 13:05:07 +00:00
int textureArgs() {
using namespace arg;
if(0) ;
else if(argis("-txpic")) {
2018-03-17 20:12:46 +00:00
shift(); config.texturename = args();
2017-12-21 13:05:07 +00:00
}
else if(argis("-txp")) {
2018-03-17 20:12:46 +00:00
shift(); config.gsplits = argf();
2017-12-21 13:05:07 +00:00
}
else if(argis("-txc")) {
2018-03-17 20:12:46 +00:00
shift(); config.configname = args();
2017-12-21 13:05:07 +00:00
}
else if(argis("-txc")) {
2018-03-17 20:12:46 +00:00
shift(); config.configname = args();
2017-12-21 13:05:07 +00:00
}
else if(argis("-txcl")) {
PHASE(3); drawscreen();
2018-03-17 20:12:46 +00:00
config.load();
old_tstate_cl = texture::config.tstate;
old_tstate_max_cl = texture::config.tstate_max;
}
else if(argis("-txremap")) {
texture::config.remap(old_tstate_cl, old_tstate_max_cl);
2017-12-21 13:05:07 +00:00
}
else return 1;
return 0;
}
auto texture_hook =
2017-12-30 16:01:53 +00:00
addHook(hooks_args, 100, textureArgs)
2018-03-17 20:12:46 +00:00
+ addHook(clearmemory, 100, [] () { config.data.pixels_to_draw.clear(); });
2017-12-21 13:05:07 +00:00
2017-12-30 16:01:53 +00:00
int lastupdate;
2018-03-17 20:12:46 +00:00
void texture_data::update() {
2017-12-30 16:01:53 +00:00
if(!pixels_to_draw.empty()) {
auto t = SDL_GetTicks();
while(SDL_GetTicks() < t + 75 && !pixels_to_draw.empty()) {
auto p = pixels_to_draw.back();
actDrawPixel(get<0>(p), get<1>(p), get<2>(p));
pixels_to_draw.pop_back();
}
loadTextureGL();
}
}
2017-12-21 13:05:07 +00:00
2017-12-14 01:53:29 +00:00
}
#endif