【问题标题】:OpenTK Camera in C#C# 中的 OpenTK 相机
【发布时间】:2013-09-08 23:48:42
【问题描述】:

我正在尝试熟悉使用 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(perspective)GL.LoadMatrix(lookat) 时,我得到一个重载匹配错误。出于这个原因,我将矩阵转换为浮点数组。当我尝试绘制多边形时,它们无法显示,除非此代码被注释掉(var myWindow = new GameWindow(840, 600) 命令除外。

【问题讨论】:

    标签: c# matrix camera opentk


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多