ユーザ用ツール

サイト用ツール


cgfs:shaded_triangles

差分

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

この比較画面へのリンク

両方とも前のリビジョン前のリビジョン
次のリビジョン
前のリビジョン
cgfs:shaded_triangles [2024/06/14 00:52] freemikancgfs:shaded_triangles [2024/07/12 03:20] (現在) freemikan
行 1: 行 1:
-<codeprism lang=cpp el=true css=full>+{{:cgfs:shaded_triangles-000.png?400|}} 
 + 
 + 
 +<file cpp>
 #include <raylib.h> #include <raylib.h>
  
行 33: 行 36:
         *out++ = d;         *out++ = d;
         d += a;         d += a;
-    } 
-} 
- 
-void DrawLine(vec2 const &p0, vec2 const &p1, Color const &color) { 
-    auto x0 = p0.x; 
-    auto y0 = p0.y; 
-    auto x1 = p1.x; 
-    auto y1 = p1.y; 
- 
-    if (std::abs(x1 - x0) > std::abs(y1 - y0)) { 
-        if (x0 > x1) { 
-            std::swap(x0, x1); 
-            std::swap(y0, y1); 
-        } 
-        auto i0 = static_cast<int>(std::floor(x0)); 
-        auto i1 = static_cast<int>(std::floor(x1)); 
-        auto ys = std::vector<float>(i1 - i0 + 1); 
-        Interpolate(i0, y0, i1, y1, ys.begin()); 
-        for (int i = i0; i <= i1; ++i) { 
-            PutPixel(i, ys[i - i0], color); 
-        } 
-    } else { 
-        if (y0 > y1) { 
-            std::swap(y0, y1); 
-            std::swap(x0, x1); 
-        } 
-        auto i0 = static_cast<int>(std::floor(y0)); 
-        auto i1 = static_cast<int>(std::floor(y1)); 
-        auto xs = std::vector<float>(i1 - i0 + 1); 
-        Interpolate(i0, x0, i1, x1, xs.begin()); 
-        for (int i = i0; i <= i1; ++i) { 
-            PutPixel(xs[i - i0], i, color); 
-        } 
-    } 
-} 
- 
-void DrawWireframeTriangle(vec2 const &p0, 
-                           vec2 const &p1, 
-                           vec2 const &p2, 
-                           Color const &color) { 
-    DrawLine(p0, p1, color); 
-    DrawLine(p1, p2, color); 
-    DrawLine(p2, p0, color); 
-} 
- 
-void DrawFilledTriangle(vec2 const &p0, 
-                        vec2 const &p1, 
-                        vec2 const &p2, 
-                        Color const &color) { 
-    vec2 P0{p0}, P1{p1}, P2{p2}; 
-    if (P1.y < P0.y) { 
-        std::swap(P1, P0); 
-    } 
-    if (P2.y < P0.y) { 
-        std::swap(P2, P0); 
-    } 
-    if (P2.y < P1.y) { 
-        std::swap(P2, P1); 
-    } 
- 
-    auto i0 = static_cast<int>(std::floor(P0.y)); 
-    auto i1 = static_cast<int>(std::floor(P1.y)); 
-    auto i2 = static_cast<int>(std::floor(P2.y)); 
-    auto x01 = std::vector<float>(i1 - i0 + 1); 
-    auto x12 = std::vector<float>(i2 - i1 + 1); 
-    auto x02 = std::vector<float>(i2 - i0 + 1); 
- 
-    Interpolate(i0, P0.x, i1, P1.x, x01.begin()); 
-    Interpolate(i1, P1.x, i2, P2.x, x12.begin()); 
-    Interpolate(i0, P0.x, i2, P2.x, x02.begin()); 
- 
-    x01.pop_back(); 
- 
-    auto x012 = std::vector<float>(x01.size() + x12.size()); 
-    assert(x012.size() == x02.size()); 
- 
-    auto corner = std::copy(x01.begin(), x01.end(), x012.begin()); 
-    std::copy(x12.begin(), x12.end(), corner); 
- 
-    auto x_left = x012; 
-    auto x_right = x02; 
-    auto m = x012.size() / 2; 
-    if (x02[m] < x012[m]) { 
-        x_left = x02; 
-        x_right = x012; 
-    } 
- 
-    for (int y = i0; y < i2; ++y) { 
-        auto x_begin = static_cast<int>(std::floor(x_left[y - i0])); 
-        auto x_end = static_cast<int>(std::floor(x_right[y - i0])); 
-        for (int x = x_begin; x < x_end; ++x) { 
-            PutPixel(x, y, color); 
-        } 
     }     }
 } }
行 212: 行 122:
         auto x_r = static_cast<int>(std::floor(x_right[y - yi0]));         auto 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],
行 218: 行 127:
  
         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));
         }         }
     }     }
行 225: 行 134:
  
 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};
行 241: 行 150:
     }     }
 } }
-</codeprism>+</file>
  
cgfs/shaded_triangles.1718293926.txt.gz · 最終更新: 2024/06/14 00:52 by freemikan

特に明示されていない限り、本Wikiの内容は次のライセンスに従います: CC0 1.0 Universal
CC0 1.0 Universal Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki