无法在 OpenTK 中显示三角形

Cannot display the triangle in OpenTK

我指的是关于选择的 OpenGL 编程指南第 13 章 (https://www.glprogramming.com/red/chapter13.html)。我想使用 OpenTK 将示例 13.2 中的代码翻译成 C#。

这是我的代码。当我 运行 它时,我看不到 window 上的那些三角形。你知道我的代码有什么问题吗?需要注意的一件事是我推荐创建透视视野(在 drawScene 函数中),因为它会抛出参数超出范围异常。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenTK;
using OpenTK.Graphics.OpenGL;
using OpenTK.Input;

namespace OpenTKSelectTry2
{
    public class SelectWindow
    {
        GameWindow window;

        public SelectWindow(GameWindow wd)
        {
            this.window = wd;
            init();
            display();
            window.Run(1.0 / 60.0);
        }
        private void drawTriangle(double x1,double y1,double x2,double y2,double x3,double y3,double z)
        {
            GL.Begin(BeginMode.Triangles);
            GL.Vertex3(x1,y1,z);
            GL.Vertex3(x2, y2, z);
            GL.Vertex3(x3, y3, z);
            GL.End();
        }

        private void drawViewVolume(double x1, double x2, double y1, double y2, double z1, double z2)
        {
            GL.Color3(1.0, 1.0, 1.0);
            GL.Begin(BeginMode.LineLoop);
            GL.Vertex3(x1, y1, -z1);
            GL.Vertex3(x2, y1, -z1);
            GL.Vertex3(x2, y2, -z1);
            GL.Vertex3(x1, y2, -z1);
            GL.End();

            GL.Begin(BeginMode.LineLoop);
            GL.Vertex3(x1, y1, -z2);
            GL.Vertex3(x2, y1, -z2);
            GL.Vertex3(x2, y2, -z2);
            GL.Vertex3(x1, y2, -z2);
            GL.End();

            GL.Begin(BeginMode.Lines);
            GL.Vertex3(x1, y1, -z1);
            GL.Vertex3(x1, y1, -z2);
            GL.Vertex3(x1, y2, -z1);
            GL.Vertex3(x1, y2, -z2);
            GL.Vertex3(x2, y1, -z1);
            GL.Vertex3(x2, y1, -z2);
            GL.Vertex3(x2, y2, -z1);
            GL.Vertex3(x2, y2, -z2);
            GL.End();
        }

        public void drawScene()
        {
            GL.MatrixMode(MatrixMode.Projection);
            GL.LoadIdentity();
            //Matrix4 perspective = Matrix4.CreatePerspectiveFieldOfView(40.0f, 4.0f/3.0f, 1.0f, 100.0f);
            GL.MatrixMode(MatrixMode.Modelview);
            GL.LoadIdentity();
            Matrix4 view = Matrix4.LookAt(new Vector3(7.5f,7.5f,12.5f), new Vector3(2.5f,2.5f,-5.0f), new Vector3(0.0f,1.0f,0.0f));
            GL.Color3(0.0, 1.0, 0.0);
            drawTriangle(2.0, 2.0, 3.0, 2.0, 2.5, 3.0, -5.0);
            GL.Color3(1.0, 0.0, 0.0);
            drawTriangle(2.0, 7.0, 3.0, 7.0, 2.5, 8.0, -5.0);
            GL.Color3(1.0, 1.0, 0.0);
            drawTriangle(2.0, 2.0, 3.0, 2.0, 2.5, 3.0, 0.0);
            drawTriangle(2.0, 2.0, 3.0, 2.0, 2.5, 3.0, -10.0);
            drawViewVolume(0.0, 5.0, 0.0, 5.0, 0.0, 10.0);
        }

        public void processHits(int hits,int[] buffer)
        {
            int ptr;
            Console.WriteLine("hit={0}", hits);
            for (int i = 0; i < hits; i++)
            {
                Console.WriteLine("number of names for hit={0}", buffer[0]);
                Console.WriteLine("z1={0}", buffer[1]);
                Console.WriteLine("z2={0}", buffer[2]);
            }
        }

        public void selectObjects()
        {
            var selectBuf = new int[512];
            int hits;
            GL.SelectBuffer(512, selectBuf);
            GL.RenderMode(RenderingMode.Select);

            GL.InitNames();
            GL.PushName(0);
            GL.PushMatrix();
            GL.MatrixMode(MatrixMode.Projection);
            GL.LoadIdentity();
            GL.Ortho(0.0, 5.0, 0.0, 5.0, 0.0, 10.0);
            GL.MatrixMode(MatrixMode.Modelview);
            GL.LoadIdentity();
            GL.LoadName(1);
            drawTriangle(2.0, 2.0, 3.0, 2.0, 2.5, 3.0, -5.0);
            GL.LoadName(2);
            drawTriangle(2.0, 7.0, 3.0, 7.0, 2.5, 8.0, -5.0);
            GL.LoadName(3);
            drawTriangle(2.0, 2.0, 3.0, 2.0, 2.5, 3.0, 0.0);
            drawTriangle(2.0, 2.0, 3.0, 2.0, 2.5, 3.0, -10.0);
            GL.PopMatrix();
            GL.Flush();

            hits = GL.RenderMode(RenderingMode.Render);
            processHits(hits, selectBuf);
        }

        public void init()
        {
            GL.Enable(EnableCap.DepthTest);
            GL.ShadeModel(ShadingModel.Flat);
        }

        public void display()
        {
            GL.ClearColor(0.0f, 0.0f, 0.0f, 0.0f);
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
            drawScene();
            selectObjects();
            GL.Flush();
        }
    }
}

主要功能:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenTK;

namespace OpenTKSelectTry2
{
    class Program
    {
        static void Main(string[] args)
        {
            var window = new GameWindow(200, 200);
            var gm = new SelectWindow(window);
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine("Press enter to finish...");
            Console.ReadLine();

        }
    }
}

使用GL.LoadMatrix加载投影和视图矩阵:

float angle_rad = 40.0f * (float)Math.PI / 180.0f;
Matrix4 perspective = Matrix4.CreatePerspectiveFieldOfView(
    angle_rad, 4.0f/3.0f, 1.0f, 100.0f);
Matrix4 view = Matrix4.LookAt(
    new Vector3(7.5f, 7.5f, 12.5f), 
    new Vector3(2.5f, 2.5f, -5.0f), 
    new Vector3(0.0f, 1.0f, 0.0f));

GL.MatrixMode(MatrixMode.Projection);
GL.LoadMatrix(ref perspective);
GL.MatrixMode(MatrixMode.Modelview);
GL.LoadMatrix(ref view);

gluPerspectivewithMatrix4.CreatePerspectiveFieldOfView` 相比,角度必须以弧度而不是度数指定。