ユーザ用ツール

サイト用ツール


cgfs:shaded_triangles

差分

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

この比較画面へのリンク

次のリビジョン
前のリビジョン
cgfs:shaded_triangles [2024/06/11 11:13] – 作成 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>
  
 +#include <algorithm>
 #include <cassert> #include <cassert>
 #include <cmath> #include <cmath>
行 35: 行 39:
 } }
  
-void DrawLine(vec2 const &p0, vec2 const &p1, Color const &color) { +struct VertexPositionIntensity : vec2 {
-    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); +
-        } +
-    } +
-+
- +
-struct IntensiveVertex { +
-    float x, y;+
     float h;     float h;
  
-    IntensiveVertex() = default; +    VertexPositionIntensity() = default; 
-    IntensiveVertex(vec2 const &p, float intensity) +    VertexPositionIntensity(float x, float y, float intensity) 
-        : x{p.x}y{p.y}, h{intensity} {}+        : vec2{x, y}, h{intensity} {}
 }; };
  
 Color color_scaled(Color const &color, float scale) { Color color_scaled(Color const &color, float scale) {
     auto r = static_cast<unsigned char>(     auto r = static_cast<unsigned char>(
-        std::min(static_cast<int>(scale * color.r), 255));+        std::clamp(static_cast<int>(scale * color.r), 0, 255));
     auto g = static_cast<unsigned char>(     auto g = static_cast<unsigned char>(
-        std::min(static_cast<int>(scale * color.g), 255));+        std::clamp(static_cast<int>(scale * color.g), 0, 255));
     auto b = static_cast<unsigned char>(     auto b = static_cast<unsigned char>(
-        std::min(static_cast<int>(scale * color.b), 255));+        std::clamp(static_cast<int>(scale * color.b), 0, 255));
     return {r, g, b, color.a};     return {r, g, b, color.a};
 } }
  
-void DrawShadedTriangle(IntensiveVertex const &p0, +void DrawShadedTriangle(VertexPositionIntensity const &p0, 
-                        IntensiveVertex const &p1, +                        VertexPositionIntensity const &p1, 
-                        IntensiveVertex const &p2,+                        VertexPositionIntensity const &p2,
                         Color const &color) {                         Color const &color) {
-    IntensiveVertex 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);         std::swap(P1, P0);
     }     }
-    if (p2.y < p0.y) {+    if (P2.y < P0.y) {
         std::swap(P2, P0);         std::swap(P2, P0);
     }     }
-    if (p2.y < p1.y) {+    if (P2.y < P1.y) {
         std::swap(P2, P1);         std::swap(P2, P1);
     }     }
行 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();
         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],
-                    h_segment.begin());+                    std::back_inserter(h_segment));
  
         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));
         }         }
     }     }
行 224: 行 134:
  
 int main() { int main() {
-    InitWindow(Cw, Ch, "Draw Lines");+    InitWindow(Cw, Ch, "Shaded Triangles");
  
-    IntensiveVertex p0{{-100, 200}, 1.0}; +    VertexPositionIntensity p0{-100, 200, 1.0}; 
-    IntensiveVertex p1{{100, 100}, 0.2}; +    VertexPositionIntensity p1{100, 100, 0.2}; 
-    IntensiveVertex p2{{-200, -200}, 0.0};+    VertexPositionIntensity p2{-200, -200, 0.0};
  
     while (!WindowShouldClose()) {     while (!WindowShouldClose()) {
行 234: 行 144:
  
         ClearBackground(RAYWHITE);         ClearBackground(RAYWHITE);
- 
-        //~ PutPixel(p0.x, p0.y, RED); 
-        //~ PutPixel(p1.x, p1.y, RED); 
-        //~ PutPixel(p2.x, p2.y, RED); 
  
         DrawShadedTriangle(p0, p1, p2, GREEN);         DrawShadedTriangle(p0, p1, p2, GREEN);
行 244: 行 150:
     }     }
 } }
-</codeprism>+</file>
  
cgfs/shaded_triangles.1718072027.txt.gz · 最終更新: 2024/06/11 11:13 by freemikan

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