commit c7a4a4598faff868acd4bea18f33dab705fc581a Author: Joel Yliluoma Date: Mon Jun 29 05:01:32 2020 +0300 clang fixes diff --git a/Makefile b/Makefile index fbc33fe..0dfabc2 100644 --- a/Makefile +++ b/Makefile @@ -1,2 +1,3 @@ a.out: renderer.cc math.hh polygon_clip.hh polygon_draw.hh polygon_tesselate.hh rasterize.hh slope.hh view.hh - g++-10 -o "$@" "$<" -std=c++20 `pkg-config sdl2 --libs --cflags` -fopenmp -Wall -Wextra -pedantic -pthread -Ofast + #g++-10 -o "$@" "$<" -std=c++20 `pkg-config sdl2 --libs --cflags` -fopenmp -Wall -Wextra -pedantic -pthread -Ofast -march=native + clang++-11 -o "$@" "$<" -std=c++20 `pkg-config sdl2 --libs --cflags` -fopenmp -Wall -Wextra -pedantic -pthread -Ofast -march=native diff --git a/math.hh b/math.hh index 37aba6f..e6c88f1 100644 --- a/math.hh +++ b/math.hh @@ -14,7 +14,7 @@ concept ArithPair = IsTuple and (IsTuple or std::i template auto AsTuple(T&& t, A&&... args) { - if constexpr(sizeof...(args)) return std::tuple_cat(AsTuple(std::forward(t)), AsTuple(std::forward(args)...)); + if constexpr(sizeof...(args)>0) return std::tuple_cat(AsTuple(std::forward(t)), AsTuple(std::forward(args)...)); else if constexpr(!IsTuple) return std::tuple{ t }; // If not tuple/pair/array else if constexpr(!requires{ t[0]; }) return std::forward(t); // If already tuple else return std::apply([&](U&&... p) { return std::tuple...>{ p... }; }, std::forward(t)); @@ -27,7 +27,7 @@ using TypeReturn = std::conditional_t, std::comm template auto AsArray(T&& t, A&&... args) { - if constexpr(sizeof...(args)) return AsArray(AsTuple(std::forward(t), std::forward(args)...)); + if constexpr(sizeof...(args)>0) return AsArray(AsTuple(std::forward(t), std::forward(args)...)); else if constexpr(!IsTuple) return std::array{ TypeReturn(t) }; // If not tuple/pair/array else return std::apply([&](U&&... p) { return std::array{ TypeReturn(p)... }; }, std::forward(t)); } diff --git a/renderer.cc b/renderer.cc index 2b7e637..ae55cdc 100644 --- a/renderer.cc +++ b/renderer.cc @@ -5,7 +5,6 @@ #include #include #include -#include #include #include #include @@ -29,8 +28,8 @@ void Render(const auto& vertices, const auto& polys, auto& view, const auto& fru for(const auto& poly: polys) { // Collect the corners of the polygon. Translate and rotate them. - auto p = std::span(&vertices[poly.first], poly.size) | std::views::transform(tform); - std::vector points(p.begin(), p.end()); + std::vector points; points.reserve(poly.size); + for(const auto& p: std::span(&vertices[poly.first], poly.size)) { points.push_back(tform(p)); } // Optional: Discard polygons that are not facing the camera (back-face culling). // This is calculated by ((p1-p0) × (p2-p0)) · p0 @@ -68,9 +67,9 @@ void CreateIcosahedron(CoordType radius, auto&& AddVertex, auto&& AddFace) auto tee = t * radius / unitradius; auto one = CoordType(1) * radius / unitradius; // All vertexes are added first - for(unsigned n=0; n<4; ++n) AddVertex( {(n&1?one:-one), (n&2?-tee:tee), 0} ); - for(unsigned n=0; n<4; ++n) AddVertex( {0, (n&1?one:-one), (n&2?-tee:tee)} ); - for(unsigned n=0; n<4; ++n) AddVertex( {(n&2?-tee:tee), 0, (n&1?one:-one)} ); + for(unsigned n=0; n<4; ++n) AddVertex( std::array{(n&1?one:-one), (n&2?-tee:tee), 0.f} ); + for(unsigned n=0; n<4; ++n) AddVertex( std::array{0.f, (n&1?one:-one), (n&2?-tee:tee)} ); + for(unsigned n=0; n<4; ++n) AddVertex( std::array{(n&2?-tee:tee), 0.f, (n&1?one:-one)} ); // Then, all faces for(auto f: AsArray( 0x0B5,0x051,0x017,0x07A,0x0AB,0x159,0x5B4,0xBA2,0xA76,0x718, @@ -186,7 +185,7 @@ auto CreateLevelMap() // Return the lightsource coordinate, but rotated around the approximate center of the world. return coord; //center + (coord-center) * AsArray(sin+cos,cos-sin,1); }; - auto AddPoly = [&](std::initializer_list newpoints, auto&&... props) + auto AddPoly = [&](std::initializer_list newpoints, auto&&... props) { points.insert(points.end(), newpoints); const auto& p = &*newpoints.begin(); @@ -314,7 +313,10 @@ int main() +tempbitmap[((y+1)%txH)*txW+x] +tempbitmap[((y+1)%txH)*txW+(x+1)%txW])/5 /256.f); - auto [vertices,polys] = CreateLevelMap(); + decltype(CreateLevelMap().first) vertices; + decltype(CreateLevelMap().second) polys; + //auto [vertices,polys] = CreateLevelMap(); + std::tie(vertices,polys) = CreateLevelMap(); auto mix = [](float f, auto a, auto b) { @@ -447,9 +449,11 @@ int main() for(auto& [v, normal, weights]: lviews) for(auto& w: weights) w /= wsum; - for(unsigned usecheat=31, toggle=0; ; usecheat >>= !(++toggle%1)) - for(auto& poly: polys | std::views::filter([&](auto& v) { return v.flags==0 && std::distance(&polys[0],&v)%max==n; })) + for(unsigned usecheat=31, toggle=0, index=0; ; usecheat >>= !(++toggle%1)) + for(auto& poly: polys) { + if(poly.flags || ((index++ % max) != n)) continue; + unsigned cheat = usecheat; while(cheat > std::min(poly.lusize, poly.lvsize)) cheat >>= 1; @@ -457,7 +461,7 @@ int main() std::array polyvectors { poly.normal, poly.tangent, poly.bitangent, poly.tangent*-1, poly.bitangent*-1 }; std::span span(&vertices[poly.first], poly.size); - DrawPolygon<5,6,false,~0u,1>( + DrawPolygon<5,6,false,0,1>( std::span(&vertices[poly.first], poly.size), [&](float x,float y,float z, auto,auto, unsigned lu,unsigned lv, auto...) { @@ -574,9 +578,9 @@ int main() 0.f+(fwd - back) }; float mlen = 2*Length(M); if(mlen < 1e-3f) mlen = 1; // Multiply with rotation matrix (tform) and apply with hysteresis to movement momentum vector (m). - m = (m * .9f) + std::tuple{Dot(M, {tform[0],tform[1],tform[2]}), - Dot(M, {tform[4],tform[5],tform[6]}), - Dot(M, {tform[8],tform[9],tform[10]})} * (.1f/mlen); + m = (m * .9f) + std::tuple{Dot(M, std::array{tform[0],tform[1],tform[2]}), + Dot(M, std::array{tform[4],tform[5],tform[6]}), + Dot(M, std::array{tform[8],tform[9],tform[10]})} * (.1f/mlen); // Add the movement momentum vector (m) to the camera position (l), thereby moving the camera l = l + m; @@ -589,9 +593,9 @@ int main() // (translation, rotation). Passes the rest of the props verbatim. return std::apply([&, xyz=point-l](auto,auto,auto, auto&&... rest) { - return AsArray(-Dot(xyz, {tform[0],tform[4],tform[8]}), - Dot(xyz, {tform[1],tform[5],tform[9]}), - Dot(xyz, {tform[2],tform[6],tform[10]}), rest...); + return AsArray(-Dot(xyz, std::array{tform[0],tform[4],tform[8]}), + Dot(xyz, std::array{tform[1],tform[5],tform[9]}), + Dot(xyz, std::array{tform[2],tform[6],tform[10]}), rest...); }, point); }, Plot); diff --git a/view.hh b/view.hh index 10d7134..7f368fe 100644 --- a/view.hh +++ b/view.hh @@ -28,7 +28,7 @@ public: void InitFrame() { - for(auto& p: pixels) p = {}; + for(auto& p: pixels) p = PixType{}; for(auto& z: zbuffer) z = std::numeric_limits::max(); } const std::vector GetRGBpixels() const @@ -104,7 +104,7 @@ public: } auto MakeFrustum() const { - return MakeFrustum(std::initializer_list{ std::pair{0.f,0.f}, {W+0.001f,0}, {W+0.001f,H+0.001f}, {0,H+0.001f} }); + return MakeFrustum(std::array{ std::pair{0.f,0.f}, std::pair{W+0.001f,0.f}, std::pair{W+0.001f,H+0.001f}, std::pair{0.f,H+0.001f} }); } };