3D轉換2D

一、基礎知識

在介紹3D轉換2D之前,我們需要先了解一些基礎知識。在計算機圖形學中,3D圖形指的是三維空間中的圖形,而2D圖形指的是二維平面中的圖形。而3D轉換2D就是將3D圖形轉換成2D圖形,也就是將三維空間中的圖形投影到二維平面上。

在計算機圖形學中,3D圖形通常使用三維坐標系表示,也就是(x, y, z),其中x表示垂直於屏幕的方向,y表示水平方向,z表示垂直於屏幕的深度方向。而2D圖形則通常使用二維坐標系表示,也就是(x, y)。

二、3D坐標轉2D坐標

將3D圖形轉換成2D圖形的過程通常分為兩個階段:投影和映射。在投影階段,我們需要根據觀察視角,將3D坐標系中的點投影到二維平面上,得到一個二維坐標系中的點。在映射階段,我們將二維坐標系中的點映射到屏幕上,得到最終的圖像。

在投影階段,通常使用的方法是透視投影和正交投影。透視投影是指利用視角,將三維圖形投影到一個透視平面上,得到二維圖形。正交投影是指將三維圖形的每個點沿視角垂直於投影面的方向進行投影,得到二維圖形。

float3 ProjectTo2D(float3 pt, float4x4 view, float4x4 projection, float viewportWidth, float viewportHeight)
{
    // 投影變換
    float4 projPt = mul(projection, mul(view, float4(pt, 1.0)));

    // 歸一化坐標系
    float2 normPt = projPt.xy / projPt.w;

    // 視口變換
    float2 screenPt = (normPt + 1.0) * 0.5 * float2(viewportWidth, viewportHeight);

    return float3(screenPt, projPt.z);
}

三、矩陣變換

由於在計算機圖形學中,我們通常使用矩陣變換來進行坐標的轉換。因此,在3D轉換2D中,我們也需要運用矩陣變換來對圖形進行變換。具體包括平移、旋轉、縮放等操作。

float4x4 CreateTranslationMatrix(float3 translation)
{
    return float4x4(
        float4(1.0, 0.0, 0.0, 0.0),
        float4(0.0, 1.0, 0.0, 0.0),
        float4(0.0, 0.0, 1.0, 0.0),
        float4(translation, 1.0)
    );
}

float4x4 CreateRotationMatrix(float3 axis, float angle)
{
    axis = normalize(axis);
    float s = sin(angle);
    float c = cos(angle);
    float4 r0 = float4(c + axis.x * axis.x * (1.0 - c), axis.x * axis.y * (1.0 - c) - axis.z * s, axis.x * axis.z * (1.0 - c) + axis.y * s, 0.0);
    float4 r1 = float4(axis.y * axis.x * (1.0 - c) + axis.z * s, c + axis.y * axis.y * (1.0 - c), axis.y * axis.z * (1.0 - c) - axis.x * s, 0.0);
    float4 r2 = float4(axis.z * axis.x * (1.0 - c) - axis.y * s, axis.z * axis.y * (1.0 - c) + axis.x * s, c + axis.z * axis.z * (1.0 - c), 0.0);
    float4 r3 = float4(0.0, 0.0, 0.0, 1.0);
    return float4x4(r0, r1, r2, r3);
}

float4x4 CreateScaleMatrix(float3 scale)
{
    return float4x4(
        float4(scale.x, 0.0, 0.0, 0.0),
        float4(0.0, scale.y, 0.0, 0.0),
        float4(0.0, 0.0, scale.z, 0.0),
        float4(0.0, 0.0, 0.0, 1.0)
    );
}

四、實例

下面是一個簡單的實例,展示了如何使用矩陣變換實現一個旋轉立方體的過程。

float3x3 RotationMatrix(float yaw, float pitch, float roll)
{
    float3x3 mPitch = float3x3(
        float3(1.0, 0.0, 0.0),
        float3(0.0, cos(pitch), -sin(pitch)),
        float3(0.0, sin(pitch), cos(pitch))
    );
    float3x3 mYaw = float3x3(
        float3(cos(yaw), 0.0, sin(yaw)),
        float3(0.0, 1.0, 0.0),
        float3(-sin(yaw), 0.0, cos(yaw))
    );
    float3x3 mRoll = float3x3(
        float3(cos(roll), -sin(roll), 0.0),
        float3(sin(roll), cos(roll), 0.0),
        float3(0.0, 0.0, 1.0)
    );
    return mul(mul(mPitch, mYaw), mRoll);
}

void RenderCube(float time)
{
    float3 cameraPos = float3(0.0, 0.0, -10.0);
    float3 targetPos = float3(0.0, 0.0, 0.0);
    float3 up = float3(0.0, 1.0, 0.0);
    float4x4 view = CreateLookAtMatrix(cameraPos, targetPos, up);

    float aspectRatio = GetAspectRatio();
    float4x4 projection = CreatePerspectiveMatrix(PI / 3.0f, aspectRatio, 0.1f, 100.0f);
    float4x4 world = CreateRotationMatrix(float3(1.0, 1.0, 1.0), time);

    float4x4 worldViewProj = mul(projection, mul(view, world));

    float3 cubeVertices[8] = {
        float3(-1.0, 1.0, -1.0),
        float3(1.0, 1.0, -1.0),
        float3(1.0, -1.0, -1.0),
        float3(-1.0, -1.0, -1.0),
        float3(-1.0, 1.0, 1.0),
        float3(1.0, 1.0, 1.0),
        float3(1.0, -1.0, 1.0),
        float3(-1.0, -1.0, 1.0)
    };

    float4 cubeColors[8] = {
        float4(1.0, 0.0, 0.0, 1.0),
        float4(0.0, 1.0, 0.0, 1.0),
        float4(0.0, 0.0, 1.0, 1.0),
        float4(1.0, 1.0, 0.0, 1.0),
        float4(1.0, 0.0, 1.0, 1.0),
        float4(0.0, 1.0, 1.0, 1.0),
        float4(1.0, 1.0, 1.0, 1.0),
        float4(0.0, 0.0, 0.0, 1.0)
    };

    int cubeIndices[36] = {
        0, 1, 2, 2, 3, 0,
        1, 5, 6, 6, 2, 1,
        5, 4, 7, 7, 6, 5,
        4, 0, 3, 3, 7, 4,
        3, 2, 6, 6, 7, 3,
        4, 5, 1, 1, 0, 4
    };

    for (int i = 0; i < 36; i += 3)
    {
        float3 v0 = cubeVertices[cubeIndices[i]];
        float3 v1 = cubeVertices[cubeIndices[i + 1]];
        float3 v2 = cubeVertices[cubeIndices[i + 2]];

        float4 c0 = cubeColors[cubeIndices[i]];
        float4 c1 = cubeColors[cubeIndices[i + 1]];
        float4 c2 = cubeColors[cubeIndices[i + 2]];

        DrawTriangle(
            TransformTo2D(v0, worldViewProj, GetViewportWidth(), GetViewportHeight()),
            TransformTo2D(v1, worldViewProj, GetViewportWidth(), GetViewportHeight()),
            TransformTo2D(v2, worldViewProj, GetViewportWidth(), GetViewportHeight()),
            c0, c1, c2);
    }
}

原創文章,作者:BJUAW,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/369972.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
BJUAW的頭像BJUAW
上一篇 2025-04-18 13:40
下一篇 2025-04-18 13:40

相關推薦

  • 金額選擇性序列化

    本文將從多個方面對金額選擇性序列化進行詳細闡述,包括其定義、使用場景、實現方法等。 一、定義 金額選擇性序列化指根據傳入的金額值,選擇是否進行序列化,以達到減少數據傳輸的目的。在實…

    編程 2025-04-29
  • java client.getacsresponse 編譯報錯解決方法

    java client.getacsresponse 編譯報錯是Java編程過程中常見的錯誤,常見的原因是代碼的語法錯誤、類庫依賴問題和編譯環境的配置問題。下面將從多個方面進行分析…

    編程 2025-04-29
  • JS Proxy(array)用法介紹

    JS Proxy(array)可以說是ES6中非常重要的一個特性,它可以代理一個數組,監聽數據變化並進行攔截、處理。在實際開發中,使用Proxy(array)可以方便地實現數據的監…

    編程 2025-04-29
  • Python官網中文版:解決你的編程問題

    Python是一種高級編程語言,它可以用於Web開發、科學計算、人工智慧等領域。Python官網中文版提供了全面的資源和教程,可以幫助你入門學習和進一步提高編程技能。 一、Pyth…

    編程 2025-04-29
  • Python列表中負數的個數

    Python列表是一個有序的集合,可以存儲多個不同類型的元素。而負數是指小於0的整數。在Python列表中,我們想要找到負數的個數,可以通過以下幾個方面進行實現。 一、使用循環遍歷…

    編程 2025-04-29
  • Java JsonPath 效率優化指南

    本篇文章將深入探討Java JsonPath的效率問題,並提供一些優化方案。 一、JsonPath 簡介 JsonPath是一個可用於從JSON數據中獲取信息的庫。它提供了一種DS…

    編程 2025-04-29
  • Python中引入上一級目錄中函數

    Python中經常需要調用其他文件夾中的模塊或函數,其中一個常見的操作是引入上一級目錄中的函數。在此,我們將從多個角度詳細解釋如何在Python中引入上一級目錄的函數。 一、加入環…

    編程 2025-04-29
  • 英語年齡用連字元號(Hyphenation for English Age)

    英語年齡通常使用連字元號表示,比如 “five-year-old boy”。本文將從多個方面探討英語年齡的連字元使用問題。 一、英語年齡的表達方式 英語中表…

    編程 2025-04-29
  • Idea新建文件夾沒有java class的解決方法

    如果你在Idea中新建了一個文件夾,卻沒有Java Class,應該如何解決呢?下面從多個方面來進行解答。 一、檢查Idea設置 首先,我們應該檢查Idea的設置是否正確。打開Id…

    編程 2025-04-29
  • at least one option must be selected

    問題解答:當我們需要用戶在一系列選項中選擇至少一項時,我們需要對用戶進行限制,即「at least one option must be selected」(至少選擇一項)。 一、…

    編程 2025-04-29

發表回復

登錄後才能評論