我正在使用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
}
}
答案 0 :(得分:1)
提示:对于在x,y,z坐标空间(即平面(x,y),(x,z),(y,z))中与参考平面整齐对齐的简单事物,沿着a某个轴相当于将特定坐标乘以-1。在更一般的情况下,您可以使用矩阵乘法,使用表示所有坐标正确旋转的预定义矩阵。