差分

このページの2つのバージョン間の差分を表示します。

この比較画面へのリンク

両方とも前のリビジョン前のリビジョン
次のリビジョン
前のリビジョン
cgfs:shaded_triangles [2024/06/14 00:52] freemikancgfs:shaded_triangles [2025/12/12 15:57] (現在) – Replace overused back_inserters freemikan
行 1: 行 1:
-<codeprism lang=cpp el=true css=full>+====== DrawPixelだけで三角形を描く (3) 明暗をつける ====== 
 + 
 +{{:cgfs:shaded_triangles-000.png?400|}} 
 + 
 +[[https://wiki.freemikan.com/doku.php?id=cgfs:vmath|vmath.h]] 
 + 
 +<file cpp>
 #include <raylib.h> #include <raylib.h>
  
行 27: 行 33:
     }     }
  
-    auto a = (d1 - d0) / (i1 - i0); +    float a = (d1 - d0) / (i1 - i0); 
-    auto d = d0;+    float d = d0;
  
     for (int i = i0; i <= i1; ++i) {     for (int i = i0; i <= i1; ++i) {
-        *out++ = d;+        *out = d
 +        ++out;
         d += a;         d += a;
     }     }
行 58: 行 65:
                         VertexPositionIntensity const &p2,                         VertexPositionIntensity const &p2,
                         Color const &color) {                         Color const &color) {
 + using std::swap;
 +
     VertexPositionIntensity P0{p0}, P1{p1}, P2{p2};     VertexPositionIntensity P0{p0}, P1{p1}, P2{p2};
     if (P1.y < P0.y) {     if (P1.y < P0.y) {
-        std::swap(P1, P0);+        swap(P1, P0);
     }     }
     if (P2.y < P0.y) {     if (P2.y < P0.y) {
-        std::swap(P2, P0);+        swap(P2, P0);
     }     }
     if (P2.y < P1.y) {     if (P2.y < P1.y) {
-        std::swap(P2, P1);+        swap(P2, P1);
     }     }
  
-    auto yi0 = static_cast<int>(std::floor(P0.y)); +    int yi0 = static_cast<int>(std::floor(P0.y)); 
-    auto yi1 = static_cast<int>(std::floor(P1.y)); +    int yi1 = static_cast<int>(std::floor(P1.y)); 
-    auto yi2 = static_cast<int>(std::floor(P2.y));+    int yi2 = static_cast<int>(std::floor(P2.y));
  
-    auto x01 = std::vector<float>(yi1 - yi0 + 1); +    std::vector<float> x01(yi1 - yi0 + 1); 
-    auto h01 = std::vector<float>(yi1 - yi0 + 1);+    std::vector<float> h01(yi1 - yi0 + 1);
     Interpolate(yi0, P0.x, yi1, P1.x, x01.begin());     Interpolate(yi0, P0.x, yi1, P1.x, x01.begin());
     Interpolate(yi0, P0.h, yi1, P1.h, h01.begin());     Interpolate(yi0, P0.h, yi1, P1.h, h01.begin());
  
-    auto x12 = std::vector<float>(yi2 - yi1 + 1); +    std::vector<float> x12(yi2 - yi1 + 1); 
-    auto h12 = std::vector<float>(yi2 - yi1 + 1);+    std::vector<float> h12(yi2 - yi1 + 1);
     Interpolate(yi1, P1.x, yi2, P2.x, x12.begin());     Interpolate(yi1, P1.x, yi2, P2.x, x12.begin());
     Interpolate(yi1, P1.h, yi2, P2.h, h12.begin());     Interpolate(yi1, P1.h, yi2, P2.h, h12.begin());
  
-    auto x02 = std::vector<float>(yi2 - yi0 + 1); +    std::vector<float> x02(yi2 - yi0 + 1); 
-    auto h02 = std::vector<float>(yi2 - yi0 + 1);+    std::vector<float> h02(yi2 - yi0 + 1);
     Interpolate(yi0, P0.x, yi2, P2.x, x02.begin());     Interpolate(yi0, P0.x, yi2, P2.x, x02.begin());
     Interpolate(yi0, P0.h, yi2, P2.h, h02.begin());     Interpolate(yi0, P0.h, yi2, P2.h, h02.begin());
  
 + // remove an overlapping point at x01.back and x12.front
     x01.pop_back();     x01.pop_back();
     assert(x01.size() + x12.size() == x02.size());     assert(x01.size() + x12.size() == x02.size());
  
 + // remove an overlapping point at h01.back and h12.front
     h01.pop_back();     h01.pop_back();
     assert(h01.size() + h12.size() == h02.size());     assert(h01.size() + h12.size() == h02.size());
  
-    auto x012 = std::vector<float>(x01.size() + x12.size() + 1); +    std::vector<float> x012(x01.size() + x12.size()); 
-    auto corner_x012 = std::copy(x01.begin(), x01.end(), x012.begin()); +    std::merge(x01.begin(), x01.end(), x12.begin(), x12.end(), x012.begin()); 
-    std::copy(x12.begin(), x12.end(), corner_x012);+ 
 +    std::vector<float> h012(h01.size() + h12.size()); 
 +    std::merge(h01.begin(), h01.end(), h12.begin(), h12.end(), h012.begin());
  
-    auto h012 = std::vector<float>(h01.size() + h12.size() + 1); +    assert(x012.size() == x02.size()); 
-    auto corner_h012 = std::copy(h01.begin(), h01.end(), h012.begin()); +    assert(h012.size() == h02.size()); 
-    std::copy(h12.begin(), h12.end(), corner_h012);+    assert(x012.size() == h012.size());
  
     auto m = x012.size() / 2;     auto m = x012.size() / 2;
行 109: 行 122:
  
     if (x02[m] < x012[m]) {     if (x02[m] < x012[m]) {
-        std::swap(x_left, x_right); +        swap(x_left, x_right); 
-        std::swap(h_left, h_right);+        swap(h_left, h_right);
     }     }
  
-    auto h_segment = std::vector<float>();+    std::vector<float> h_segment;
  
     for (int y = yi0; y <= yi2; ++y) {     for (int y = yi0; y <= yi2; ++y) {
-        auto x_l = static_cast<int>(std::floor(x_left[y - yi0])); +        int x_l = static_cast<int>(std::floor(x_left[y - yi0])); 
-        auto x_r = static_cast<int>(std::floor(x_right[y - yi0]));+        int x_r = static_cast<int>(std::floor(x_right[y - yi0]));
  
-        //~ h_segment.resize(x_r - x_l + 1); 
         h_segment.clear();         h_segment.clear();
         Interpolate(x_l, h_left[y - yi0], x_r, h_right[y - yi0],         Interpolate(x_l, h_left[y - yi0], x_r, h_right[y - yi0],
行 125: 行 137:
  
         for (int x = x_l; x <= x_r; ++x) {         for (int x = x_l; x <= x_r; ++x) {
-            auto shaded_color color_scaled(color, h_segment[x - x_l])+            float h = h_segment[x - x_l]; 
-            PutPixel(x, y, shaded_color);+            PutPixel(x, y, color_scaled(color, h));
         }         }
     }     }
行 132: 行 144:
  
 int main() { int main() {
-    InitWindow(Cw, Ch, "Draw Lines");+    InitWindow(Cw, Ch, "Shaded Triangles");
  
     VertexPositionIntensity p0{-100, 200, 1.0};     VertexPositionIntensity p0{-100, 200, 1.0};
行 148: 行 160:
     }     }
 } }
-</codeprism>+</file>
  
文書の先頭へ