网上搜了不少资料,可操作性弱。
在C#环境下可用的OpenGL包有OpenTK、SharpGL,今天使用OpenTK做一个练习。
首先来接一下OpenTK:
The Open Toolkit is set of fast, low-level C# bindings for OpenGL, OpenGL ES, OpenAL and OpenCL. It runs on all major platforms and powers hundreds of apps, games and scientific research. It provides bindings for GLFW windowing, input and a game loop, and is the perfect start for your own game engine. OpenTK comes with simple and easy to follow tutorials for learning *modern* OpenGL. These are written by the community and represent all of the best practices to get you started. Learn how to use OpenTK here: https://opentk.net/learn/index.html Sample projects that accompany the tutorial can be found here: https://github.com/opentk/LearnOpenTK We have a very active discord server, if you need help, want to help, or are just curious, come join us! https://discord.gg/6HqD48s
明确方向:
OpenTK 3 (.NET framework)
OpenTK 4 (.NET core 3.1+)
OpenTK 5 (.NET 5+)
在.Net Frameowk下使用OpenTK 3
目前最新版本是3.3.2
建立Console Application项目
项目环境Netframe:V4.6.1
使用Nuget包管理器为项目安装包OpenTK 3.3.2
建立一个类Game(Game.cs):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenTK;
using System.Drawing;
using OpenTK.Graphics.OpenGL;
using OpenTK.Graphics;
namespace OpenTKTest
{
class Game: GameWindow
{
public Game() : base(1200, 900, GraphicsMode.Default, "第一个窗口", GameWindowFlags.Default, DisplayDevice.Default, 4, 0, GraphicsContextFlags.ForwardCompatible)
{
Title += ": OpenGL Version: " + GL.GetString(StringName.Version);
}
protected override void onl oad(EventArgs e)
{
base.OnLoad(e);
Title = "Hello OpenTK!";
GL.ClearColor(Color.CornflowerBlue);
}
protected override void OnRenderFrame(FrameEventArgs e)
{
base.OnRenderFrame(e);
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
/* The first thing we need to do is to tell OpenGL which direction we're looking at.
* Because we'll be actually making something in 3D, the direction the camera faces is important. */
Matrix4 modelview = Matrix4.LookAt(Vector3.Zero, Vector3.UnitZ, Vector3.UnitY);
GL.MatrixMode(MatrixMode.Modelview);
GL.LoadMatrix(ref modelview);
/* Now we'll want to draw the triangle itself.
* The first step is to tell OpenGL we want to draw something. We do this with the GL.Begin function.
* This takes a single parameter, which is the drawing mode to use.
* There are options to draw quadrilaterals, triangles, points, polygons, and "strips".*/
GL.Begin(PrimitiveType.Triangles);
/* Now that we've told it how we want to draw, we need to give it the vertices for our shape.
* To do this, we use the GL.Vertex3 function. It takes three floats as coordinates for a single point in 3D space.*/
GL.Color3(1.0f, 0.0f, 0.0f);
GL.Vertex3(-1.0f, -1.0f, 4.0f);
GL.Color3(0.0f, 1.0f, 0.0f);
GL.Vertex3(1.0f, -1.0f, 4.0f);
GL.Color3(0.0f, 0.0f, 1.0f);
GL.Vertex3(0.0f, 1.0f, 4.0f);
GL.End();
//GL.Clear(ClearBufferMask.ColorBufferBit);
操纵模型视图矩阵,设置眼球参数
//GL.MatrixMode(MatrixMode.Modelview);
//GL.LoadIdentity();
设置观察者参数
//Matrix4 mx4 = Matrix4.LookAt(0f, 0f, 5f, 0f, 0f, 0f, 0f, 1f, 0f);
//GL.MatrixMode(MatrixMode.Modelview);
//GL.LoadMatrix(ref mx4);
绘制图形
GL.Begin(BeginMode.Polygon);
//GL.Begin(PrimitiveType.Polygon);
//GL.Vertex3(-1.0f, -1.0f, 2.0f);
//GL.Vertex3(1.0f, -1.0f, 2.0f);
//GL.Vertex3(1.0f, 1.0f, 2.0f);
//GL.Vertex3(-1.0f, 1.0f, 2.0f);
//GL.End();
SwapBuffers();
}
protected override void OnResize(EventArgs e)
{
/* OpenGL needs to be told how to adjust for the new window size, so we need some code that handles it. */
base.OnResize(e);
GL.Viewport(ClientRectangle.X, ClientRectangle.Y, ClientRectangle.Width, ClientRectangle.Height);
Matrix4 projection = Matrix4.CreatePerspectiveFieldOfView((float)Math.PI / 4, Width / (float)Height, 1.0f, 64.0f);
GL.MatrixMode(MatrixMode.Projection);
GL.LoadMatrix(ref projection);
}
}
}
控制台代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace OpenTKTest
{
class Program
{
static void Main(string[] args)
{
using (Game game = new Game())
{
/*The Run method of the GameWindow has multiple overloads.
* With a single float parameter, Run will give your window 30 UpdateFrame events a second, and as many RenderFrame events per second as the computer will process.*/
game.Run(30.0);
}
}
}
}
运行效果:
另外一个动态效果展示: