ホームに戻る
OpenGL
0、はじめに
C#でOpenGLを使用するために HISUi を使用。
このサンプルのみWPFではなくフォームから作っています。
Kinectから80cm以上後ろに下がること。
頭を認識すると緑の点が頭の動きにあわせて動きます。
1、Program.cs
using System.Windows.Forms;
namespace KinectTest{
static class Program{
static void Main(){
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
2、Form1.Designer.cs
namespace KinectTest{
partial class Form1{
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing){
if(disposing && (components != null)){
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
private void InitializeComponent(){
this.components = new System.ComponentModel.Container();
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Text = "Form1";
}
#endregion
}
}
3、Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace KinectTest{
public partial class Form1 : Form{
Hisui.Graphics.GLViewControl _view = new Hisui.Graphics.GLViewControl();
public Form1(){
InitializeComponent();
_view.Dock = DockStyle.Fill;
this.Controls.Add(_view);
Hisui.SI.DocumentViews.AddView(_view);
Hisui.SI.Tasks.Add(Hisui.SI.DocumentViews);
Hisui.SI.View.SceneGraph.WorldScenes.Add(new Kinect_Opengl());
}
}
}
4、Kinect_Opengl.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Kinect;
using Hisui.OpenGL;
namespace KinectTest{
public class Kinect_Opengl : Hisui.Graphics.IScene{
public class Point{
public float X, Y, Z;
public Point(float _x, float _y, float _z){
X = _x;
Y = _y;
Z = _z;
}
}
public Kinect_Opengl(){
if(KinectSensor.KinectSensors.Count == 0){
throw new Exception("Connect Kinect.");
}
StartKinect(KinectSensor.KinectSensors[0]);
}
private void StartKinect(KinectSensor kinect){
kinect.SkeletonStream.Enable();
kinect.Start();
}
private void StopKinect(KinectSensor kinect){
if(kinect != null){
if(kinect.IsRunning){
kinect.Stop();
kinect.Dispose();
}
}
}
public void Draw(Hisui.Graphics.ISceneContext sc){
Point p = GetPoint();
using(var scope = sc.Push()){
scope.Lighting = false;
if(p != null){
GL.glPointSize(3 * (4.0f - p.Z));
GL.glBegin(GL.GL_POINTS);
GL.glColor3d(0.0f, 1.0f, 0.0f);
GL.glVertex3d(p.X, p.Y, 0.0f);
GL.glEnd();
}
else{
GL.glPointSize(10);
GL.glBegin(GL.GL_POINTS);
GL.glColor3d(1.0f, 0.0f, 0.0f);
GL.glVertex3d(0.0f, 0.0f, 0.0f);
GL.glEnd();
}
}
Hisui.SI.View.Invalidate();
}
Point GetPoint(){
KinectSensor kinect = KinectSensor.KinectSensors[0];
using(SkeletonFrame skeletonFrame = kinect.SkeletonStream.OpenNextFrame(100)){
if(skeletonFrame == null){
return null;
}
Skeleton[] skeletons = new Skeleton[skeletonFrame.SkeletonArrayLength];
skeletonFrame.CopySkeletonDataTo(skeletons);
foreach(Skeleton skeleton in skeletons){
if(skeleton.TrackingState == SkeletonTrackingState.Tracked){
foreach(Joint joint in skeleton.Joints){
if(joint.JointType == JointType.Head){
if(joint.TrackingState == JointTrackingState.Tracked){
return new Point(joint.Position.X, joint.Position.Y, joint.Position.Y);
}
}
}
}
}
}
return null;
}
}
}