微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

无法在 OpenTK 中显示三角形

如何解决无法在 OpenTK 中显示三角形

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

这是我的代码。当我运行它时,我看不到窗口上的那些三角形。你知道我的代码有什么问题吗?需要注意的一件事是我推荐了创建透视视野(在 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 z1,double z2)
        {
            GL.Color3(1.0,1.0,1.0);
            GL.Begin(BeginMode.LineLoop);
            GL.Vertex3(x1,-z1);
            GL.Vertex3(x2,-z1);
            GL.Vertex3(x1,-z1);
            GL.End();

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

            GL.Begin(BeginMode.Lines);
            GL.Vertex3(x1,-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,0.0f));
            GL.Color3(0.0,0.0);
            drawTriangle(2.0,2.0,3.0,2.5,-5.0);
            GL.Color3(1.0,0.0,7.0,8.0,-10.0);
            drawViewVolume(0.0,5.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,10.0);
            GL.MatrixMode(MatrixMode.Modelview);
            GL.LoadIdentity();
            GL.LoadName(1);
            drawTriangle(2.0,-5.0);
            GL.LoadName(2);
            drawTriangle(2.0,-5.0);
            GL.LoadName(3);
            drawTriangle(2.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);
            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,0.0f));

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。