0
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

OpenTKをNuGetでインストール+C# WPFアプリで使う

Last updated at Posted at 2018-06-22

#■1. NuGetでOpenTKをインストール
WPFアプリケーションでOpenTKを使うとき、最新のOpenTKはNuGetでインストールできます。
一点気を付ける必要があるのは、OpenTKとOpenTK.GLControlの2つをインストールするということです。
20180623_OpenTK NuGet.jpg

#■2. xaml
ツールボックスからWindowsFormsHostをドラッグしてウィンドウ上に配置します。
次にxamlソースコードを編集してOpenTK.GLControlをWindowsFormsHost内に追加します。

MainWindow.xaml
<Window x:Class="IvyFEMProtoApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:OpenTK="clr-namespace:OpenTK;assembly=OpenTK.GLControl"        
        xmlns:local="clr-namespace:IvyFEMProtoApp"
        mc:Ignorable="d"
        Title="IvyFEMProtoApp" Height="350" Width="525">
    <Grid>
        <WindowsFormsHost>
            <OpenTK:GLControl x:Name="glControl" Load="glControl_Load" Resize="glControl_Resize" Paint="glControl_Paint" />
        </WindowsFormsHost>
    </Grid>
</Window>

#■3. C#
xamlで設定したglControlのイベントハンドラglControl_Load、glControl_Resize、glControl_Paintを実装します。

MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;

namespace IvyFEMProtoApp
{
    /// <summary>
    /// MainWindow.xaml の相互作用ロジック
    /// </summary>
    public partial class MainWindow : Window
    {
        /// <summary>
        /// コンストラクタ
        /// </summary>
        public MainWindow()
        {
            InitializeComponent();
        }

        /// <summary>
        /// glControlの起動時に実行される。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void glControl_Load(object sender, EventArgs e)
        {
            GL.ClearColor(Color4.Black);
            GL.Enable(EnableCap.DepthTest);
        }

        /// <summary>
        /// glControlのサイズ変更時に実行される。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void glControl_Resize(object sender, EventArgs e)
        {
            GL.Viewport(0, 0, glControl.Size.Width, glControl.Size.Height);
            GL.LoadIdentity();
            // 平行投影
            GL.Ortho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
        }

        /// <summary>
        /// glControlの描画時に実行される。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void glControl_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

            GL.Color4(Color4.White);
            GL.Begin(BeginMode.Polygon);
            GL.Vertex3(0.5, 0.5, 0.0);
            GL.Vertex3(-0.5, 0.5, 0.0);
            GL.Vertex3(-0.5, -0.5, 0.0);
            GL.Vertex3(0.5, -0.5, 0.0);
            GL.End();

            glControl.SwapBuffers();
        }
    }
}

#■4. 実行結果

20180622_IvyFEM OpenTK.jpg

0
4
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?