From 7a71132806434fa2e534614c71ebc29b40659b45 Mon Sep 17 00:00:00 2001 From: Zeno Rogue Date: Fri, 17 Apr 2020 15:01:55 +0200 Subject: [PATCH] shaded rug --- drawing.cpp | 2 ++ rug.cpp | 10 ++++------ shaders.cpp | 13 +++++++++++-- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/drawing.cpp b/drawing.cpp index a2ed0460..015f262e 100644 --- a/drawing.cpp +++ b/drawing.cpp @@ -36,6 +36,7 @@ static const int POLY_INTENSE = (1<<23); // extra intense colors static const int POLY_DEBUG = (1<<24); // debug this shape static const int POLY_PRINTABLE = (1<<25); // these walls are printable static const int POLY_FAT = (1<<26); // fatten this model in WRL export (used for Rug) +static const int POLY_SHADE_TEXTURE = (1<<27); // texture has 'z' coordinate for shading /** \brief A graphical element that can be drawn. Objects are not drawn immediately but rather queued. * @@ -596,6 +597,7 @@ void dqi_poly::gldraw() { if(tinf) { glhr::be_textured(); + if(flags & POLY_SHADE_TEXTURE) current_display->next_shader_flags |= GF_TEXTURE_SHADED; glBindTexture(GL_TEXTURE_2D, tinf->texture_id); glhr::vertices_texture(v, tinf->tvertices, offset, offset_texture); ioffset = 0; diff --git a/rug.cpp b/rug.cpp index 81b90a29..fde4894a 100644 --- a/rug.cpp +++ b/rug.cpp @@ -987,20 +987,18 @@ void drawTriangle(triangle& t) { if(!t.m[i]->valid) return; } - /* ld col = 1; - if(num == 3) { - hyperpoint hc = (h[1] - h[0]) ^ (h[2] - h[0]); + if(true) { + hyperpoint hc = (t.m[1]->native - t.m[0]->native) ^ (t.m[2]->native - t.m[0]->native); double hch = hypot_d(3, hc); col = (2 + hc[0]/hch) / 3; } - */ for(int i=0; i<3; i++) { if(t.m[i]->native[3] != 1) println(hlog, "bad point: ", t.m[i]->native); curvepoint(t.m[i]->native); - tinf.tvertices.push_back(glhr::pointtogl(point3(t.m[i]->x1, t.m[i]->y1, 0))); + tinf.tvertices.push_back(glhr::pointtogl(point3(t.m[i]->x1, t.m[i]->y1, col))); } } @@ -1081,7 +1079,7 @@ EX void drawRugScene() { rug.V = rugView; rug.offset_texture = 0; rug.tinf = &tinf; - rug.flags = POLY_TRIANGLES | POLY_FAT | POLY_PRINTABLE | POLY_ALWAYS_IN | POLY_ISSIDE; + rug.flags = POLY_TRIANGLES | POLY_FAT | POLY_PRINTABLE | POLY_ALWAYS_IN | POLY_ISSIDE | POLY_SHADE_TEXTURE; dynamicval p(pconf, rconf); calcparam(); diff --git a/shaders.cpp b/shaders.cpp index 85c4b05e..1921dff3 100644 --- a/shaders.cpp +++ b/shaders.cpp @@ -16,8 +16,9 @@ constexpr flagtype GF_TEXTURE = 1; constexpr flagtype GF_VARCOLOR = 2; constexpr flagtype GF_LIGHTFOG = 4; constexpr flagtype GF_LEVELS = 8; +constexpr flagtype GF_TEXTURE_SHADED = 16; -constexpr flagtype GF_which = 15; +constexpr flagtype GF_which = 31; constexpr flagtype SF_PERS3 = 256; constexpr flagtype SF_BAND = 512; @@ -62,7 +63,15 @@ shared_ptr write_shader(flagtype shader_flags) { varying += "varying mediump vec4 vColor;\n"; fmain += "gl_FragColor = vColor;\n"; - if(shader_flags & GF_TEXTURE) { + if(shader_flags & GF_TEXTURE_SHADED) { + vsh += "attribute mediump vec3 aTexture;\n"; + varying += "varying mediump vec3 vTexCoord;\n"; + fsh += "uniform mediump sampler2D tTexture;\n"; + vmain += "vTexCoord = aTexture;\n"; + fmain += "gl_FragColor *= texture2D(tTexture, vTexCoord.xy);\n"; + fmain += "gl_FragColor.rgb *= vTexCoord.z;\n"; + } + else if(shader_flags & GF_TEXTURE) { vsh += "attribute mediump vec2 aTexture;\n"; varying += "varying mediump vec2 vTexCoord;\n"; fsh += "uniform mediump sampler2D tTexture;\n";