• 在WPF中集成OpenTK


    OpenGL是针对Windows Forms开发,下面是在WPF环境下的集成方法。

    (P.S. 如果只在windows下使用,其实WPF 3D或DirectX是更好的选择)。

    1.新建一个WPF项目。

    2.添加以下references到项目中:

    System.Windows.Forms

    WindowsFormsIntegration

    OpenTK

    OpenTK.GLControl

    3.在xaml中加入如下内容:

    <Window x:Class="WpfApplication1.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
            Title="MainWindow" Height="500" Width="500" Loaded="Window_Loaded">
        <Grid Name="grid">
            <WindowsFormsHost Width="500" Height="500" Name="chart" HorizontalAlignment="Right" />
        </Grid>
    </Window>

    4.在cs中包含如下引用

    using System.Windows.Forms;
    using SYstem.Windows.Forms.Integration;
    using OpenTK;
    using OpenTK.Graphics;
    using OpenTK.Graphics.OpenGL;

    5.创建窗体loaded事件

     private void Window_Loaded(object sender, RoutedEventArgs e)
            {
                // 创建 GLControl.
                glc = new GLControl();
    
                // 指定GLControl的Load 和 Paint 事件
                glc.Load += new EventHandler(glc_Load);
                glc.Paint += new System.Windows.Forms.PaintEventHandler(glc_Paint);
    
                // 指定这个GLControl作为chart控件的child
                chart.Child = glc;
            }

    6.添加GLControl的Load和Paint事件

    void glc_Load(object sender, EventArgs e)
            {
                // 设置背景色
                GL.ClearColor(System.Drawing.Color.Chocolate);
    
                int w = glc.Width;
                int h = glc.Height;
    
                // 设置初始状态
                GL.MatrixMode(MatrixMode.Projection);
                GL.LoadIdentity();
                GL.Ortho(0, w, 0, h, -1, 1);
                GL.Viewport(0, 0, w, h);
            }
     void glc_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
            {
                GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
    
                GL.MatrixMode(MatrixMode.Modelview);
                GL.LoadIdentity();
    
                // 画一个小的黄色三角形
                GL.Color3(System.Drawing.Color.Yellow);
                GL.Begin(BeginMode.Triangles);
                GL.Vertex2(200, 50);
                GL.Vertex2(200, 200);
                GL.Vertex2(100, 50);
                GL.End();
    
                glc.SwapBuffers();
            }

    7.编译运行,完毕。

  • 相关阅读:
    SED&AWK
    load average[zhuan]
    To be learned
    Android计时器 android.widget.Chronometer
    Play初识
    获取视图的宽高
    自定义摄像机
    Android VideoView使用小记
    在android中,如何去掉webview读取网页后点击网页上的按钮出现的方框
    阿里云主机试用之自建站点和ftp上传所遇的2个问题
  • 原文地址:https://www.cnblogs.com/xpvincent/p/2815834.html
Copyright © 2020-2023  润新知