简介

the Open Tool Kit (OpenTK), 是对 OpenGL、OpenAL、OpenCL 的跨平台的封装,使用 C# 编写,它可以用在Mono、dotNet的语言:c#、VB、C++/CLI、F#、Boo等。

什么是Mono

参考:http://baike.baidu.com/subview/26639/9339264.htm?fr=aladdin

Mono是一个由Novell公司(由Ximian发起,并由Miguel de
lcaza领导的,一个致力于开创.NET在Linux上使用的开源工程。它包含了一个C#语言的编译器,一个CLR的运行时,和一组类库,并实现了
ADO NET和ASP NET。能够使得开发人员在Linux用C#开发程序。)主持的项目.该项目的目标是创建一系列符合标准ECMA
(Ecma-334和Ecma-335)的.Net 工具, 包括C #编译器和共同语言(CL 即 Common
Language)执行平台(Platform).与微软的.Net不同,
Mono项目不仅可以运行于Windows系统内,还可以运行于Linux, FreeBSD, Unix, Mac OS X和Solaris.

什么是C++/CLI

参考:http://baike.baidu.com/link?url=QPwZ5Jga7wKJJKn_NWkbvIucYtgvCHtZ3UZ9sRKD7i-eNGXg8m0nYpWVNSyt0yjQ

简而言之就是如何用C++在·NET中编程

什么是OpenAL

参考:http://baike.baidu.com/view/1355367.htm?fr=aladdin

是开源的跨平台音效API

什么是OpenCL

参考:http://baike.baidu.com/view/2056591.htm

是第一个面向异构系统通用目的并行编程的开放式、免费标准,也是一个统一的编程环境

使用

环境

dotNet IDE可以使用Visual Studio或 MonoDevelop http://monodevelop.com/Download

添加OpenTK.dll:右键“Project”,选择“Add Reference”,找到OpenTK.dll并添加它。

Windows.Forms+GLControl

参考:http://www.opentk.com/doc/chapter/2/glcontrol

向工具箱中添加GLControl控件

在工具箱的空白处右键,选择“Choose Item……”,浏览到OpenTK.GLControl.dll,添加。

创建顺序

he fact that glControl1'sGLContext is created in runtime is important to remember however, since you cannot
access or changeglControl1's properties reliably until theGLContext has been created. The same is true for anyGL.*
commands (orGlu for that matter!).

1.运行Windows.Form的构造函数

2.加载form的事件

3.加载GLControl的事件,OK to touch glControl/GL

4.运行事件处理函数 ,ny event handler may touch glControl/GL.

解决这个问题的一种方法是定义bool loaded=false;在GLControl加载时设置为true。

  1. using OpenTK.Graphics;
  2. using OpenTK.Graphics.OpenGL;
  3. public partial class Form1 : Form
  4. {
  5. bool loaded = false;
  6. public Form1()
  7. {
  8. InitializeComponent();
  9. }
  10. private void glControl1_Load(object sender, EventArgs e)
  11. {
  12. loaded = true;
  13. }
  14. }

由OpenTK还没有开发GLControl.Load事件,所以可以使用Form.Load事件

然后在你想使用glControl/GL的事件处理函数中检查loaded

  1. private void glControl1_Resize(object sender, EventArgs e)
  2. {
  3. if (!loaded)
  4. return;
  5. }

在VS中事件处理函数是很简单的:

1.在Designer中选择GLControl

2.在属性框中选择事件

3.在相应的事件中设置处理函数

  1. private void glControl1_Paint(object sender, PaintEventArgs e)
  2. {
  3. if (!loaded) // Play nice
  4. return;
  5. GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
  6. glControl1.SwapBuffers();
  7. }

视口初始

  1. private void glControl1_Load(object sender, EventArgs e)
  2. {
  3. loaded = true;
  4. GL.ClearColor(Color.SkyBlue);
  5. SetupViewport();
  6. }
  7. private void SetupViewport()
  8. {
  9. int w = glControl1.Width;
  10. int h = glControl1.Height;
  11. GL.MatrixMode(MatrixMode.Projection);
  12. GL.LoadIdentity();
  13. GL.Ortho(0, w, 0, h, -1, 1); // Bottom-left corner pixel has coordinate (0, 0)
  14. GL.Viewport(0, 0, w, h); // Use all of the glControl painting area
  15. }

键盘输入

有两种常用的方法:(1)使用Windows.Forms的按键事件(2)使用OpenTK的KeyboardDevice。

glControl1 is not painted all the time,操作系统的窗口管理器会确保绘制事件尽可能少的发生,一般只有在resize、窗口模糊等情况下才会触发绘制事件。如果我们想手动调用重绘,使用Invalidate()

  1. private void glControl1_KeyDown(object sender, KeyEventArgs e)
  2. if (!loaded)
  3. return;
  4. if (e.KeyCode == Keys.Space)
  5. {
  6. x++;
  7. glControl1.Invalidate();
  8. }

窗口大小的改变

当GLControl的大小发生变化,我们需要更新的是viewport和projection matrix。

如果我们从右下角缩小窗口时,并不会解发重绘,这是因为窗口管理器认为(0,0)像素还存在(所以如果从左上角缩小就会重绘),解决的方法就是调用Invalidate()

  1. private void glControl1_Resize(object sender, EventArgs e)
  2. {
  3. SetupViewport();
  4. glControl1.Invalidate();
  5. }

添加动画

有两种方法:(1)添加一个Timer 控件,使用的其Tick事件(2)使用Thread。

这里使用第三种方式,使用Windows.Forms的Application.idle事件

  1. private void glControl1_Load(object sender, EventArgs e)
  2. {
  3. loaded = true;
  4. GL.ClearColor(Color.SkyBlue);
  5. SetupViewport();
  6. Application.Idle += Application_Idle; // press TAB twice after +=
  7. }
  8. void Application_Idle(object sender, EventArgs e)
  9. {
  10. }

当窗口比较大时,渲染(render)比较慢

原因是窗口的3d rendering比full-screen rendering一般要慢。那么可以使用一种frame-rate independent animation的技术,思想很简单:根据当前的渲染速度调整变化量,比如旋转量(渲染慢的使用的量大)。可以使用StopWatch来测量一个frame的渲染时间。StopWatch的用法:

  1. Stopwatch sw = new Stopwatch();
  2. sw.Start();
  3. MyAdvancedAlgorithm();
  4. sw.Stop();
  5. double milliseconds = sw.Elapsed.TotalMilliseconds;

(不要使用DataTime.Now,因为它的大小是10ms级的,和frame的渲染时间是一个数量级)

  1. Stopwatch sw = new Stopwatch(); // available to all event handlers
  2. private void glControl1_Load(object sender, EventArgs e)
  3. {
  4. ...
  5. sw.Start(); // start at application boot
  6. }
  7. float rotation = 0;
  8. void Application_Idle(object sender, EventArgs e)
  9. {
  10. // no guard needed -- we hooked into the event in Load handler
  11. sw.Stop(); // we've measured everything since last Idle run
  12. double milliseconds = sw.Elapsed.TotalMilliseconds;
  13. sw.Reset(); // reset stopwatch
  14. sw.Start(); // restart stopwatch
  15. // increase rotation by an amount proportional to the
  16. // total time since last Idle run
  17. float deltaRotation = (float)milliseconds / 20.0f;
  18. rotation += deltaRotation;
  19. glControl1.Invalidate();
  20. }

FPS计数器

我们怎么知道1s已经过去了呢?

  1. void Application_Idle(object sender, EventArgs e)
  2. {
  3. double milliseconds = ComputeTimeSlice();
  4. Accumulate(milliseconds);
  5. Animate(milliseconds);
  6. }
  7. private double ComputeTimeSlice()
  8. {
  9. sw.Stop();
  10. double timeslice = sw.Elapsed.TotalMilliseconds;
  11. sw.Reset();
  12. sw.Start();
  13. return timeslice;
  14. }
  15. float rotation = 0;
  16. private void Animate(double milliseconds)
  17. {
  18. float deltaRotation = (float)milliseconds / 20.0f;
  19. rotation += deltaRotation;
  20. glControl1.Invalidate();
  21. }
  22. double accumulator = 0;
  23. int idleCounter = 0;
  24. private void Accumulate(double milliseconds)
  25. {
  26. idleCounter++;
  27. accumulator += milliseconds;
  28. if (accumulator > 1000)
  29. {
  30. label1.Text = idleCounter.ToString();
  31. accumulator -= 1000;
  32. idleCounter = 0; // don't forget to reset the counter!
  33. }
  34. }

使用多个GLControl

假如说我们有glCtrl1和glCtrl2,想要在Paint事件处理函数中使用它们,具体看代码:

  1. private void glCtrl1_Paint(object sender, PaintEventArgs e)
  2. if (!loaded)
  3. return;
  4. glCtrl1.MakeCurrent(); // Ohh.. It's that simple?
  5. GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
  6. ...
  7. }

尽管每个GLControl有它们自己的GraphicsContext,OpenTK默认是共享OpenGL资源的。你也可以禁止这个行为,能过设置属性GraphicsContext.ShareContexts。

参考:http://www.cnblogs.com/beginor/archive/2009/10/17/1585040.html

http://www.opentk.com/

openTK学习的更多相关文章

  1. [转]OpenTK学习笔记(1)-源码、官网地址

    OpenTK源码下载地址:https://github.com/opentk/opentk OpenTK使用Nuget安装命令:OpenTK:Install-Package OpenTK -Versi ...

  2. OpenTK学习笔记(3)-你好!窗体!

    参考:http://dreamstatecoding.blogspot.com/2017/01/opengl-4-with-opentk-in-c-part-1.html http://www.cnb ...

  3. OpenTK学习笔记:C#的中开发OpenGL程序的4种开源封包库SharpGL、CsGL、OpenTK、Tao框架的简单对比

    最近要在C#的语言环境下开发OpenGL程序,参考了网上的博客论坛http://www.cnblogs.com/hanyonglu/archive/2012/06/12/2546581.html,总结 ...

  4. OpenTK学习笔记(1)-源码、官网地址

    OpenTK源码下载地址:https://github.com/opentk/opentk OpenTK使用Nuget安装命令:OpenTK:Install-Package OpenTK -Versi ...

  5. OpenTK学习笔记(2)-工作窗口的三种方法创建方法(winfrom窗体控件形式创建)

    参考资料: https://social.msdn.microsoft.com/Forums/zh-TW/1b781685-c670-4338-953d-1957a8f24a66/opentkglco ...

  6. OpenTK学习笔记(2)-工作窗口的三种方法创建方法(winfrom下类的形式创建)

    参考资料: https://www.codeproject.com/Articles/1167212/OpenGL-with-OpenTK-in-Csharp-Part-Initialize-the- ...

  7. OpenTK学习笔记(2)-工作窗口的三种方法创建方法(控制台)

    参考资料: 控制台下类的形式创建:http://www.cnblogs.com/podolski/p/7406628.html 总结: 一.控制台下类的形式创建 1.新建控制台应用 2.连网执行Nug ...

  8. OpenTK学习笔记

    OpenGL定义 OpenGL被定义为"图形硬件的一种软件接口".实质上是3D图形和模型库,具有高度可移植性,具有非常快的速度. OpenGL架构 术语pipeline常用于阐述彼 ...

  9. GLSL着色语言学习。橙皮书第一个例子GLSL+OpenTK+F#的实现。

    Opengl红皮书有选择的看了一些,最后的讲着色语言GLSL的部分看的甚为不理解,然后找到Opengl橙皮书,然后就容易理解多了. 在前面,我们或多或少接触到Opengl的处理过程,只说前面一些处理, ...

随机推荐

  1. Wordpress 数据库查询错误 Call to a member function get_results() on null

    在插件中的一个文件使用如下代码,无法查询 <body> <?php global $wpdb; $sql = ""; $sql = "SELECT * ...

  2. 【LeetCode】移除元素(Remove Element)

    这道题是LeetCode里的第27道题. 题目描述: 给定一个数组 nums 和一个值 val,你需要原地移除所有数值等于 val 的元素,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原 ...

  3. Vue2.0 - 构造器的延伸 Vue.extend

    Vue.extend 返回的是一个“扩展实例构造器”,也就是预设了部分选项的Vue实例构造器.经常服务于Vue.component用来生成组件,可以简单理解为当在模板中遇到该组件名称作为标签的自定义元 ...

  4. POJ 3621 Sightseeing Cows(最优比例环+SPFA检测)

    Sightseeing Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10306   Accepted: 3519 ...

  5. [HNOI2007][bzoj1187] 神奇游乐园 [插头dp]

    题面: 传送门 给定一个四联通棋盘图,每个格子有权值,求一条总权值最大的回路 思路: 插头dp基础教程 棋盘? 回路? n,m<=10? 当然是插头dp啦~\(≧▽≦)/~ 然后发现这道题并不是 ...

  6. BZOJ3720 Gty的妹子树 【树分块】

    题目 我曾在弦歌之中听过你, 檀板声碎,半出折子戏. 舞榭歌台被风吹去, 岁月深处尚有余音一缕-- Gty神(xian)犇(chong)从来不缺妹子-- 他来到了一棵妹子树下,发现每个妹子有一个美丽度 ...

  7. poj 1743 Musical Theme 后缀自动机/后缀数组/后缀树

    题目大意 直接用了hzwer的题意 题意:有N(1 <= N <=20000)个音符的序列来表示一首乐曲,每个音符都是1..88范围内的整数,现在要找一个重复的主题."主题&qu ...

  8. 【HDOJ5533】Dancing Stars on Me(计算几何)

    题意:给定二维平面上的n个整点,问它们是否都在正n边形的定点上 n<=100,abs(x[i]),abs(y[i])<=1e4 思路:队友做的,抱大腿 可以发现只有n=4时顶点有可能都是整 ...

  9. 当文字过长时裁剪(显示省略号或只裁剪 用CSS方法,不用程序)

    原文发布时间为:2009-09-16 -- 来源于本人的百度文章 [由搬家工具导入] CSS中ellipsis()应用【转】 CSS手册中text-overflow属性的定义:   语法: text- ...

  10. 怎样在SQL2005中设置 自增长类型?

    原文发布时间为:2009-04-25 -- 来源于本人的百度文章 [由搬家工具导入] 最近好几个人问我。。。。。 企业管理器-->右键你的表-->设计表-->选中一int类型字段-- ...