在WPF中集成OpenTK

摘要:
assembly=System.Windows.Forms“Title=”MainWindow“Loaded=”Window_ Loaded“>GridName=”grid“&gt,WindowsFormsHostName=”chart“HorizontalAlignment=”Right“/>使用OpenTK.Graphics;

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: 
        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"     Loaded="Window_Loaded">
    <Grid Name="grid">
        <WindowsFormsHost     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.编译运行,完毕。

免责声明:文章转载自《在WPF中集成OpenTK》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇将xml文件由格式化变为压缩字符串关于android中EditText边框的问题 下划线下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Spring的StringUtils工具类

本文是转载文章,感觉比较好,如有侵权,请联系本人,我将及时删除。 原文网址:《Spring的StringUtils工具类》 org.springframework.util.StringUtils 我们经常会对字符串进行操作,spring已经实现了常用的处理功能。我们可以使用org.springframework.util.StringUtils 工具类...

[CSS3] 几种分割线实现方法

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <style type="text/css">...

小程序换行符检测换行

先将换行符替换成自定义连接字符 var goods_detail = "" if(res.data.info.goods_info.goods_detail){ goods_detail = res.data.info.goods_info.goods_detail; var str=goods_detail.replace(/ /g,"|-&")...

osgEarth学习笔记

osgEarth学习笔记     这是个人在学习osgEarth时根据体会或从别的地方看到的做的一个简单整理,有些东东就是官方文档翻译过来的,都是根据自己的需要感觉需要记录下来什么东西就随便记录下来了,也没有个条理,都是用到哪记到哪,别见怪。对个人在初期使用osgEarth时有很多帮助,所以特发上来,希望对大家也有帮助 osgEarth学习笔记 1.   ...

基于Gitlab统计代码行--统计所有仓库、所有提交人的代码总行数(新增加-删除)

公司绩效考核要求,统计GITLAB仓库所有人提示有效代码行业 脚本1: 统计所有仓库、所有提交人的代码总行数(新增加-删除) 脚本2: 统计所有仓库、所有提交人的代码提交汇总与删除汇总 脚本3: 统计指定仓库的所有提交人的代码提交 汇总与删除汇总 注意:所有代码行数为0,统计结果中清除。 所有统计只统计master分支。 安装: 把脚本中的仓库路径搜索修...

easyui treegrid 动态展开数据(暂记)

 ClassifyAdminSynMsgSvr.GetCLFLList("<%=CurUTag %>", 1, "", function (ret) {                    //console.log(ret);                    //console.log(ret._Items);            ...