如何在 Raylib 中制作具有厚度的 3D 三角形
Posted
技术标签:
【中文标题】如何在 Raylib 中制作具有厚度的 3D 三角形【英文标题】:How to make 3D triangle with thickness in Raylib 【发布时间】:2021-04-09 14:48:19 【问题描述】:我可以在 Raylib 中制作一个 3D 三角形,但现在我希望那个三角形有厚度。
Raylib 如何做到这一点?
这是 C# 中的代码,它只显示一个没有厚度的三角形:
using System;
using Raylib_cs;
using System.Numerics;
namespace Triangle
class Program
static void Main(string[] args)
Raylib.InitWindow(800, 480, "Hello World");
Camera3D camera;
camera.position = new Vector3(10.0f, 10.0f, 10.0f); // Camera3D position
camera.target = new Vector3(0.0f, 0.0f, 0.0f); // Camera3D looking at point
camera.up = new Vector3(0.0f, 1.0f, 0.0f); // Camera3D up vector (rotation towards target)
camera.fovy = 120.0f; // Camera3D field-of-view Y
camera.type = CameraType.CAMERA_PERSPECTIVE; // Camera3D mode type
Vector3 point1 = new Vector3(0.0f, 0.0f, 0.0f);
Vector3 point2 = new Vector3(10.0f, 0.0f, 0.0f);
Vector3 point3 = new Vector3(10.0f, 10.0f, 0.0f);
Raylib.SetCameraMode(camera, CameraMode.CAMERA_FREE); // Set a free camera mode
Raylib.SetTargetFPS(60);
Rlgl.rlDisableBackfaceCulling();
while (!Raylib.WindowShouldClose())
Raylib.UpdateCamera(ref camera);
Raylib.BeginDrawing();
Raylib.ClearBackground(Color.RAYWHITE);
Raylib.BeginMode3D(camera);
Raylib.DrawTriangle3D(point1, point2, point3, Color.BLACK);
Raylib.EndMode3D();
//text
Raylib.DrawRectangle(10, 10, 320, 133, Raylib.ColorAlpha(Color.SKYBLUE, 0.5f));
Raylib.DrawRectangleLines(10, 10, 320, 133, Color.BLUE);
Raylib.DrawText("Free camera default controls:", 20, 20, 10, Color.BLACK);
Raylib.DrawText("- Mouse Wheel to Zoom in-out", 40, 40, 10, Color.DARKGRAY);
Raylib.DrawText("- Mouse Wheel Pressed to Pan", 40, 60, 10, Color.DARKGRAY);
Raylib.DrawText("- Alt + Mouse Wheel Pressed to Rotate", 40, 80, 10, Color.DARKGRAY);
Raylib.DrawText("- Alt + Ctrl + Mouse Wheel Pressed for Smooth Zoom", 40, 100, 10, Color.DARKGRAY);
Raylib.EndDrawing();
Raylib.CloseWindow();
如果有人知道如何实现这一点,我将不胜感激。
【问题讨论】:
【参考方案1】:我不太确定您所说的 3D 三角形是什么意思,但我假设您的意思是三角棱镜。为此,您需要明确地绘制每个面。要绘制矩形边,您需要绘制两个三角形。此外,由于我们只使用三角形的正面,我们不需要禁用背面剔除。绘制三角形时,点需要排列,以便在看脸时,点按顺时针顺序排列。
这里是 C++ 实现,但它应该会给你一些想法;
Camera3D camera;
camera.position = (Vector3)10.0f, 10.0f, 10.0f; // Camera3D position
camera.target = (Vector3)0.0f, 0.0f, 0.0f; // Camera3D looking at point
camera.up = (Vector3)0.0f, 1.0f, 0.0f; // Camera3D up vector (rotation towards target)
camera.fovy = 120.0f; // Camera3D field-of-view Y
camera.type = CAMERA_PERSPECTIVE; // Camera3D mode type
Vector3 point1 = (Vector3)0.0f, 0.0f, 0.0f;
Vector3 point2 = (Vector3)10.0f, 0.0f, 0.0f;
Vector3 point3 = (Vector3)10.0f, 10.0f, 0.0f;
Vector3 thickness = (Vector3)0, 0, -5.0f;
Vector3 back1 = Vector3Add(point1, thickness);
Vector3 back2 = Vector3Add(point2, thickness);
Vector3 back3 = Vector3Add(point3, thickness);
SetCameraMode(camera, CAMERA_FREE); // Set a free camera mode
//rlDisableBackfaceCulling();
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
// Update
//----------------------------------------------------------------------------------
// TODO: Update your variables here
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
UpdateCamera(&camera);
BeginDrawing();
ClearBackground(RAYWHITE);
BeginMode3D(camera);
// Front face
DrawTriangle3D(point1, point2, point3, BLACK);
// Back face
DrawTriangle3D(back3, back2, back1, BLACK);
// Slanted side
DrawTriangle3D(back1, point1, back3, BLACK);
DrawTriangle3D(point1, point3, back3, BLACK);
// Back side
DrawTriangle3D(point2, back2, back3, BLACK);
DrawTriangle3D(back3, point3, point2, BLACK);
// Bottom side
DrawTriangle3D(back1, back2, point2, BLACK);
DrawTriangle3D(point2, point1, back1, BLACK);
DrawGrid(100, 10.0);
EndMode3D();
EndDrawing();
//----------------------------------------------------------------------------------
这是在轨道模式下的样子。
【讨论】:
非常感谢您的帮助以上是关于如何在 Raylib 中制作具有厚度的 3D 三角形的主要内容,如果未能解决你的问题,请参考以下文章
Atom 3 Delta 将SLA和FDM技术结合在一台三角洲3D打印机中,靠谱么
在 Firefox 上具有变换 3D 和过滤器模糊的背景中心
试图用 C 和 raylib 制作蛇游戏。添加吃苹果后未定义的行为