C# C语言中的OpenTK摄像机#

C# C语言中的OpenTK摄像机#,c#,matrix,camera,opentk,C#,Matrix,Camera,Opentk,我试图熟悉用C#编写OpenTK程序。我不知道如何设置和移动相机。目前,我已导入以下库: using System; using System.Collections.Generic; using System.Linq; using System.Text; using OpenTK; using OpenTK.Graphics; using OpenTK.Graphics.OpenGL; using OpenTK.Platform.Windows; using System.Runtime.

我试图熟悉用C#编写OpenTK程序。我不知道如何设置和移动相机。目前,我已导入以下库:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using OpenTK.Platform.Windows;
using System.Runtime.InteropServices;
using Imaging = System.Drawing.Imaging;
using TK = OpenTK.Graphics.OpenGL;
using System.IO;
using System.Threading;
我已将此代码输入Main以使我的相机正常工作:

var myWindow = new GameWindow(840, 600);
Matrix4 perspective = Matrix4.CreatePerspectiveFieldOfView(1.04f, 4f / 3f, 1f, 10000f);//Setup Perspective
float[] persF;
persF = new float[4] { 1.04f, 4f / 3f, 1f, 10000f };
Matrix4 lookat = Matrix4.LookAt(100, 20, 0, 0, 0, 0, 0, 1, 0);// 'Setup camera
float[] lookF;
lookF = new float[9] { 100, 20, 0, 0, 0, 0, 0, 1, 0 };
GL.MatrixMode(MatrixMode.Projection);// 'Load Perspective
GL.LoadIdentity();
GL.LoadMatrix(persF);
GL.MatrixMode(MatrixMode.Modelview);// 'Load Camera
GL.LoadIdentity();
GL.LoadMatrix(lookF);
GL.Enable(EnableCap.DepthTest);// 'Enable correct Z Drawings
GL.DepthFunc(DepthFunction.Less);// 'Enable correct Z Drawings
GL.Viewport(0, 0, myWindow.Width, myWindow.Height);

当我使用
GL.LoadMatrix(透视)
GL.LoadMatrix(lookat)
时,我会得到一个重载匹配错误。因此,我将矩阵转换为浮点数组。当我尝试绘制多边形时,除非注释掉此代码,否则它们将无法显示(除了
var myWindow=newgamewindow(840600)之外)
命令。

对于第一个问题,
GL.LoadMatrix
只有一个
ref-Matrix4
重载,而不仅仅是
Matrix4
重载。通过引用传递矩阵要快得多,因为调用该方法时它不会在堆栈上创建额外的副本。第二,是
Matrix4.CreatePerspectiveFiledOf的参数查看
Matrix4。查看
不是矩阵。它们根据这些参数生成4x4矩阵(16个浮点),因此您的
persF
lookF
“矩阵”将产生一些非常奇怪的结果

正确的调用是
GL.LoadMatrix(ref透视);
GL.LoadMatrix(ref lookat);

您应该创建GameWindow的子类,并为该代码覆盖
OnLoad
,为绘图代码覆盖
OnRenderFrame