创建一个八面体(颠倒金字塔)
本文关键字:倒金字塔 八面体 一个 创建 | 更新日期: 2023-09-27 18:01:16
我正在使用OpenTK制作一个八面体。我已经创建了一个方形金字塔,需要在顶部的金字塔下面翻译另一个,并将其倒过来创建八面体。
我如何把第二个金字塔倒过来?
下面是我的代码:#region --- Using Directives ---
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Threading;
using System.Drawing;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using OpenTK.Platform;
#endregion
namespace Octahedron
{
public class Octahedron : GameWindow
{
#region --- Fields ---
const float rotation_speed = 180.0f;
float angle;
#endregion
#region --- Constructor ---
public Octahedron()
: base(800, 600)
{ }
#endregion
#region OnLoad
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
GL.ClearColor(Color.MidnightBlue);
GL.Enable(EnableCap.DepthTest);
}
#endregion
#region OnResize
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
GL.Viewport(0, 0, Width, Height);
double aspect_ratio = Width / (double)Height;
OpenTK.Matrix4 perspective = OpenTK.Matrix4.CreatePerspectiveFieldOfView(MathHelper.PiOver4, (float)aspect_ratio, 1, 64);
GL.MatrixMode(MatrixMode.Projection);
GL.LoadMatrix(ref perspective);
}
#endregion
#region OnUpdateFrame
protected override void OnUpdateFrame(FrameEventArgs e)
{
base.OnUpdateFrame(e);
var keyboard = OpenTK.Input.Keyboard.GetState();
if (keyboard[OpenTK.Input.Key.Escape])
{
this.Exit();
return;
}
}
#endregion
#region OnRenderFrame
protected override void OnRenderFrame(FrameEventArgs e)
{
base.OnRenderFrame(e);
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
Matrix4 lookat = Matrix4.LookAt(0, 5, 10, 0, 0, 2, 0, 5, 0);
GL.MatrixMode(MatrixMode.Modelview);
GL.LoadMatrix(ref lookat);
angle += rotation_speed * (float)e.Time;
GL.Rotate(angle, 0.0f, 1.0f, 0.0f);
DrawPyramid();
GL.Translate(0.0f, -2.0f, 0.0f);
DrawPyramid();
this.SwapBuffers();
Thread.Sleep(1);
}
#endregion
#region private void DrawPyramid()
private void DrawPyramid()
{
GL.Begin(PrimitiveType.Triangles);
//Side0 (red)
//x, y, z
GL.Color3(1.0f, 0.0f, 0.0f); GL.Vertex3(0.0f, 1.0f, 0.0f);//Top Vertex
//x, y, z
GL.Color3(1.0f, 0.0f, 0.0f); GL.Vertex3(-1.0f, -1.0f, 1.0f);//Bottom Left Vertex
//x, y, z
GL.Color3(1.0f, 0.0f, 0.0f); GL.Vertex3(1.0f, -1.0f, 1.0f);//Bottom Right Vertex
//Side1 (blue)
//x, y, z
GL.Color3(0.0f, 0.0f, 1.0f); GL.Vertex3(0.0f, 1.0f, 0.0f);//Top Vertex
//x, y, z
GL.Color3(0.0f, 0.0f, 1.0f); GL.Vertex3(1.0f, -1.0f, 1.0f);//Bottom Left Vertex
//x, y, z
GL.Color3(0.0f, 0.0f, 1.0f); GL.Vertex3(1.0f, -1.0f, -1.0f);//Bottom Right Vertex
// Side2 (yellow)
//x, y, z
GL.Color3(1.0f, 1.0f, 0.0f); GL.Vertex3(0.0f, 1.0f, 0.0f);
//x, y, z
GL.Color3(1.0f, 1.0f, 0.0f); GL.Vertex3(1.0f, -1.0f, -1.0f);
//x, y, z
GL.Color3(1.0f, 1.0f, 0.0f); GL.Vertex3(-1.0f, -1.0f, -1.0f);
// Side3 (pink)
//x, y, z
GL.Color3(1.0f, 0.0f, 1.0f); GL.Vertex3(0.0f, 1.0f, 0.0f);
//x, y, z
GL.Color3(1.0f, 0.0f, 1.0f); GL.Vertex3(-1.0f, -1.0f, -1.0f);
//x, y, z
GL.Color3(1.0f, 0.0f, 1.0f); GL.Vertex3(-1.0f, -1.0f, 1.0f);
//Side4 (red)
//x, y, z
GL.Color3(1.0f, 1.0f, 1.0f); GL.Vertex3(0.0f, 1.0f, 0.0f);//Top Vertex
//x, y, z
GL.Color3(1.0f, 1.0f, 1.0f); GL.Vertex3(-1.0f, -1.0f, 1.0f);//Bottom Left Vertex
//x, y, z
GL.Color3(1.0f, 1.0f, 1.0f); GL.Vertex3(1.0f, -1.0f, 1.0f);//Bottom Right Vertex
GL.End();
}
#endregion
#region public static void Main()
[STAThread]
public static void Main()
{
using (Octahedron oct = new Octahedron())
{
oct.Run(60.0, 0.0);
}
}
#endregion
}
}
提示:对于与x,y,z坐标空间中的参考平面整齐对齐的简单事物(即平面(x,y), (x,z), (y,z)),沿着某个轴翻转某物相当于将特定坐标乘以-1。在更一般的情况下,您可以使用矩阵乘法,使用预定义的矩阵表示所有坐标的适当旋转。