简介

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/

最新文章

  1. android http 抓包
  2. [LeetCode] Find All Anagrams in a String 找出字符串中所有的变位词
  3. python 颜色定义
  4. Rotate List || LeetCode
  5. Python3学习(2)-中级篇
  6. pg_stat_statements
  7. android 的通知管理
  8. Cookie和Session(转)
  9. Linux下经常使用的shell命令记录
  10. ACdreamoj1110(多重背包)
  11. MySQL操作符
  12. 第07周-集合与GUI
  13. 201521123064 《Java程序设计》第4周学习总结
  14. 51nod 1510 最小化序列 | DP 贪心
  15. ThreadLocal, HandlerThread, IntentService
  16. Android系统裁剪:手把手教你如何进行系统裁剪
  17. JS获得元素相对位置坐标getBoundingClientRect()
  18. Redis学习第六课:Redis ZSet类型及操作
  19. ElasticSearch - How to search for a part of a word with ElasticSearch
  20. 【Android】如何获取本机号码、IMSI、EMSI

热门文章

  1. jmeter非GUI模式如何压测并生成测试报告
  2. 07 JVM 是如何实现反射的
  3. [ecmagnet][python基础]有关git那些事
  4. Lua 语法要点
  5. hdu5985[概率dp] 2016青岛icpc现场赛
  6. iOS voip视频调试结果
  7. WF 18 A 想法
  8. Codeforces Round #281 (Div. 2) A 模拟
  9. 【bzoj2946】[Poi2000]公共串 后缀自动机
  10. java递归处理文件夹和文件