1
0
mirror of https://github.com/zenorogue/hyperrogue.git synced 2024-12-20 07:30:26 +00:00

3d::draw:: added shading

This commit is contained in:
Zeno Rogue 2019-04-07 03:08:43 +02:00
parent cc5f381d20
commit 36b4b60d46
3 changed files with 46 additions and 5 deletions

View File

@ -702,4 +702,20 @@ hyperpoint mid_at_actual(hyperpoint h, ld v) {
return rspintox(h) * xpush0(hdist0(h) * v);
}
// in 3D, an orthogonal projection of C0 on the given triangle
hyperpoint orthogonal_of_C0(hyperpoint h0, hyperpoint h1, hyperpoint h2) {
using namespace hyperpoint_vec;
h0 /= h0[3];
h1 /= h1[3];
h2 /= h2[3];
hyperpoint w = h0;
hyperpoint d1 = h1 - h0;
hyperpoint d2 = h2 - h0;
ld denom = (d1|d1) * (d2|d2) - (d1|d2) * (d1|d2);
ld a1 = (d2|w) * (d1|d2) - (d1|w) * (d2|d2);
ld a2 = (d1|w) * (d1|d2) - (d2|w) * (d1|d1);
hyperpoint h = w * denom + d1 * a1 + d2 * a2;
return normalize(h);
}
}

View File

@ -1962,8 +1962,13 @@ namespace mapeditor {
usershapelayer& ds(us->d[i]);
hpcshape& sh(ds.sh);
if(sh.s != sh.e)
queuepolyat(mmscale(V, geom3::lev_to_factor(ds.zlevel)), sh, ds.color ? ds.color : color, prio);
if(sh.s != sh.e) {
auto& last = queuepolyat(mmscale(V, geom3::lev_to_factor(ds.zlevel)), sh, ds.color ? ds.color : color, prio);
if(DIM == 3) {
last.tinf = &user_triangles_texture;
last.offset_texture = ds.texture_offset;
}
}
}
}

View File

@ -89,6 +89,7 @@ static const int POLY_ALWAYS_IN = (1<<21);
static const int POLY_TRIANGLES = (1<<22);
vector<hyperpoint> hpc;
basic_textureinfo user_triangles_texture;
int prehpc;
@ -1729,6 +1730,7 @@ struct usershapelayer {
hyperpoint shift, spin;
ld zlevel;
hpcshape sh;
int texture_offset;
};
struct usershape {
@ -1941,15 +1943,17 @@ void bshapeend() {
transmatrix shadowmulmatrix;
void pushShape(const usershapelayer& ds) {
void pushShape(usershapelayer& ds) {
if(ds.list.empty()) return;
if(DIM == 3) last->flags |= POLY_TRIANGLES;
transmatrix T = rgpushxto0(ds.shift) * rspintox(ds.spin);
int z = DIM == 3 ? 3 : 1;
for(int r=0; r<ds.rots; r++) {
for(int i=0; i<isize(ds.list); i++)
for(int i=0; i<isize(ds.list)/z*z; i++)
hpcpush(T * spin(2*M_PI*r/ds.rots) * ds.list[i]);
if(ds.sym) {
@ -1959,7 +1963,21 @@ void pushShape(const usershapelayer& ds) {
hpcpush(T * spin(2*M_PI*r/ds.rots) * mirrortrans * ds.list[i]);
}
}
hpcpush(T * ds.list[0]);
if(DIM == 2) hpcpush(T * ds.list[0]);
if(DIM == 3) {
auto& utt = user_triangles_texture;
utt.texture_id = floor_textures->renderedTexture;
ds.texture_offset = isize(utt.tvertices);
for(int i=0; i<isize(ds.list)-2; i+=3) {
hyperpoint h = orthogonal_of_C0(ds.list[i], ds.list[i+1], ds.list[i+2]);
ld rad = hypot_d(3, h);
ld factor = 0.49 + (0.17 * h[2] + 0.13 * h[1] + 0.20 * h[0]) / rad;
for(int i=0; i<3; i++)
utt.tvertices.push_back(glhr::makevertex(-1, factor, 0));
}
}
}
ld gsca() { return 1; }
@ -3071,6 +3089,8 @@ void buildpolys() {
prehpc = isize(hpc);
DEBB(DF_INIT, (debugfile,"hpc = %d\n", prehpc));
user_triangles_texture.tvertices.clear();
for(int i=0; i<mapeditor::USERSHAPEGROUPS; i++) for(auto usp: usershapes[i]) {
auto us = usp.second;