この文書の現在のバージョンと選択したバージョンの差分を表示します。
両方とも前のリビジョン 前のリビジョン 次のリビジョン | 前のリビジョン | ||
c.sharp:opentk [2018/04/22 08:46] fifi [方法2: GLControlを使ってWinFormで使うやり方] |
c.sharp:opentk [2018/05/03 13:32] (現在) fifi |
||
---|---|---|---|
ライン 1: | ライン 1: | ||
+ | # リファレンス | ||
+ | |||
+ | - [OpenGL入門](http://wisdom.sakura.ne.jp/system/opengl/) | ||
+ | - [Xamarin OpenGLES](https://developer.xamarin.com/api/namespace/OpenTK/) | ||
+ | - [giawa/opengl4tutorials: Tutorial Particle](https://github.com/giawa/opengl4tutorials) | ||
+ | - [neokabuto/OpenTKTutorialContent: Project files from OpenTK tutorials from my blog](https://github.com/neokabuto/OpenTKTutorialContent) | ||
+ | - [jeske/SimpleScene: Simple 3D scene manager in C# and OpenTK / OpenGL](https://github.com/jeske/SimpleScene) | ||
+ | |||
+ | # 座標変換を完全に理解する | ||
+ | |||
+ | - ワールド座標変換 | ||
+ | - ビュー/プロジェクション座標変換 | ||
+ | -- 2Dに変換する工程 | ||
+ | -- クリッピング座標系(正規化視野空間とか正規化デバイス座標系) | ||
+ | -- 透視投影変換 | ||
+ | -- 平行投影変換 | ||
+ | |||
+ | - 射影変換 | ||
+ | |||
+ | ```c | ||
+ | // 実際に表示されているアプリケーションの画面のどの領域に表示するかを指定 | ||
+ | GL.Viewport(0, Width, 0, Height); | ||
+ | |||
+ | // 基本形 | ||
+ | //画面描画で実行される。 | ||
+ | protected override void OnRenderFrame(FrameEventArgs e) | ||
+ | { | ||
+ | base.OnRenderFrame(e); | ||
+ | |||
+ | GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); | ||
+ | | ||
+ | // [クリッピング座標系] クリッピング空間の指定. カメラの表示画角を指定するだけ (順不動) | ||
+ | GL.MatrixMode(MatrixMode.Projection); | ||
+ | Matrix4 projection = Matrix4.CreatePerspectiveFieldOfView((float)Math.PI / 4, (float)glControl.Size.Width / (float)glControl.Size.Height, 1.0f, 64.0f); | ||
+ | GL.LoadMatrix(ref projection); | ||
+ | | ||
+ | // [ビュー座標系] カメラの位置,方向,を指定するだけ (順不動) | ||
+ | GL.MatrixMode(MatrixMode.Modelview); | ||
+ | Matrix4 modelview = Matrix4.LookAt(Vector3.Zero, Vector3.UnitZ, Vector3.UnitY); | ||
+ | GL.LoadMatrix(ref modelview); | ||
+ | | ||
+ | // ワールド座標系でオブジェクトを配置 | ||
+ | GL.Begin(BeginMode.Quads); | ||
+ | |||
+ | GL.Color4(Color4.White); //色名で指定 | ||
+ | GL.Vertex3(-1.0f, 1.0f, 4.0f); | ||
+ | GL.Color4(new float[] { 1.0f, 0.0f, 0.0f, 1.0f }); //配列で指定 | ||
+ | GL.Vertex3(-1.0f, -1.0f, 4.0f); | ||
+ | GL.Color4(0.0f, 1.0f, 0.0f, 1.0f); //4つの引数にfloat型で指定 | ||
+ | GL.Vertex3(1.0f, -1.0f, 4.0f); | ||
+ | GL.Color4((byte)0, (byte)0, (byte)255, (byte)255); //byte型で指定 | ||
+ | GL.Vertex3(1.0f, 1.0f, 4.0f); | ||
+ | |||
+ | GL.End(); | ||
+ | |||
+ | SwapBuffers(); | ||
+ | } | ||
+ | |||
+ | ``` | ||
+ | |||
+ | # 3Dの座標グリッドを美しく表示する方法 | ||
+ | |||
+ | - [Drawing a Grid in OpenTK](http://deathbyalgorithm.blogspot.jp/2013/05/drawing-grid-in-opentk.html) | ||
+ | - [Setting up OpenTK](http://deathbyalgorithm.blogspot.jp/2013/05/setting-up-opentk.html) | ||
+ | |||
+ | ```c | ||
+ | /* 線のアンチエイリアス */ | ||
+ | GL.Enable(EnableCap.LineSmooth); // 線のアンチエイリアスを有効化 | ||
+ | GL.Hint(HintTarget.LineQualityHintSgix, HintMode.Fastest); // アンチエイリアスの | ||
+ | GL.Enable(EnableCap.Blend); // 線周辺のブレンドを | ||
+ | GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha); | ||
+ | ``` | ||
+ | |||
+ | |||
+ | # OpenTKの文字列表示について | ||
+ | |||
+ | OpenTK.Graphics.TextPrinterを使う場合には、メモリリークに注意 | ||
+ | |||
+ | TextPrinterOptions.Defaultの場合はcacheがたまってしまう。.TextPrinterOptions.NoCacheにすべき。 | ||
+ | |||
+ | [opentkのライブラリ](https://github.com/mono/opentk/blob/master/Source/Compatibility/Graphics/TextPrinter.cs) | ||
+ | には入っているが、 | ||
+ | |||
+ | [2016年の情報](https://www.codeproject.com/Articles/1057539/Abstract-of-the-text-rendering-with-OpenGL-OpenTK)では | ||
+ | 外部ライブラリを使うとある。情報は古いかもしれないが(2018/4/22時点では以下のコードで動いた) | ||
+ | |||
+ | |||
+ | ```c | ||
+ | |||
+ | using OpenTK.Graphics.OpenGL; | ||
+ | using System.Drawing; | ||
+ | |||
+ | namespace ConsoleApp1 | ||
+ | { | ||
+ | class FrontTextView | ||
+ | { | ||
+ | |||
+ | OpenTK.Graphics.TextPrinter text = new OpenTK.Graphics.TextPrinter(OpenTK.Graphics.TextQuality.High); | ||
+ | |||
+ | float size1 = 25; | ||
+ | float size2 = 15; | ||
+ | Font printerFont1; | ||
+ | Font printerFont2; | ||
+ | |||
+ | string poem = "Oxford Dictionaries definitions. Looking for the meanings of words, phrases, and expressions? We provide hundreds of thousands of definitions, synonyms, antonyms, and pronunciations for English and other languages, derived from our language research and expert analysis. We also offer a unique set of examples of real usage, as well as guides to: Writing help: This section gives guidelines on writing in everyday situations, from applying for a job to composing letters of ..."; | ||
+ | |||
+ | public FrontTextView() | ||
+ | { | ||
+ | } | ||
+ | |||
+ | public void Render(int Width, int Height) | ||
+ | { | ||
+ | // ******** font settings | ||
+ | size1 = Height / 30f; | ||
+ | size2 = Height / 60f; | ||
+ | printerFont1 = new Font(FontFamily.GenericMonospace, size1); | ||
+ | printerFont2 = new Font(FontFamily.GenericSerif, size2, FontStyle.Regular); // FontFamily.GenericSansSerif | ||
+ | |||
+ | //******** | ||
+ | GL.MatrixMode(MatrixMode.Projection); | ||
+ | GL.PushMatrix(); // 上書きするなら必要 | ||
+ | GL.LoadIdentity(); | ||
+ | GL.MatrixMode(MatrixMode.Modelview); | ||
+ | GL.PushMatrix(); // 上書きするために必要 | ||
+ | GL.LoadIdentity(); | ||
+ | |||
+ | GL.Viewport(0, 0, Width, Height); | ||
+ | GL.Ortho(0, Width, Height, 0, -1, 1); // 射影変換 その他Perspectiveもある | ||
+ | |||
+ | // ******** Text Rendering | ||
+ | text.Begin(); | ||
+ | text.Print(this.BaseText, printerFont1, Color.DimGray, drawRect, OpenTK.Graphics.TextPrinterOptions.NoCache); | ||
+ | text.End(); | ||
+ | // ******** This is equivalent: | ||
+ | GL.MatrixMode(MatrixMode.Modelview); | ||
+ | GL.PopMatrix(); | ||
+ | GL.MatrixMode(MatrixMode.Projection); | ||
+ | GL.PopMatrix(); | ||
+ | |||
+ | } | ||
+ | } | ||
+ | } | ||
+ | ``` | ||
+ | |||
+ | |||
# 0. OpenTKについて | # 0. OpenTKについて |