0
0

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 1 year has passed since last update.

cscの作法 その371

Last updated at Posted at 2023-06-21

概要

cscの作法、調べてみた。
opentkのgamewindow使ってみた。
assimp使ってみた。

写真

image.png

サンプルコード

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Reflection;
using Assimp;
using Assimp.Configs;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;

namespace App
{
	public class SimpleOpenGLSample: GameWindow {
		private Scene m_model;
		private float m_angle;
		private int m_displayList;
		public SimpleOpenGLSample(): base() {
			Title = "test";
			String fileName = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "sphere.obj");
			AssimpContext importer = new AssimpContext();
			m_model = importer.ImportFile(fileName, PostProcessPreset.TargetRealTimeMaximumQuality);
		}
		protected override void OnUpdateFrame(FrameEventArgs e) {
			base.OnUpdateFrame(e);
			m_angle += 25f * (float) e.Time;
			if (m_angle > 360)
			{
				m_angle = 0.0f;
			}
			if (Keyboard[OpenTK.Input.Key.Escape])
			{
				this.Exit();
			}
		}
		protected override void OnRenderFrame(FrameEventArgs e) {
			base.OnRenderFrame(e);
			GL.ClearColor(Color.CornflowerBlue);
			GL.Clear(ClearBufferMask.ColorBufferBit);
				for (int i = 0; i < m_model.RootNode.ChildCount; i++)
				{
					Render(m_model, m_model.RootNode.Children[i]);
				}
			SwapBuffers();
		}
		private void Render(Scene scene, Node node) {
			if (node.HasMeshes)
			{
				foreach (int index in node.MeshIndices)
				{
					Mesh mesh = scene.Meshes[index];
					foreach (Face face in mesh.Faces)
					{
						BeginMode faceMode;
						switch (face.IndexCount)
						{
						case 1:
							faceMode = BeginMode.Points;
						break;
						case 2:
							faceMode = BeginMode.Lines;
						break;
						case 3:
							faceMode = BeginMode.Triangles;
						break;
						default:
							faceMode = BeginMode.Polygon;
						break;
						}
						GL.Begin(faceMode);
						for (int i = 0; i < face.IndexCount; i++)
						{
							int indice = face.Indices[i];
							GL.Color3(Color.White);
							Vector3 pos = FromVector(mesh.Vertices[indice]);
							GL.Vertex3(pos);
						}
						GL.End();
					}
				}
			}
		}
		private Vector3 FromVector(Vector3D vec) {
			Vector3 v;
			v.X = vec.X;
			v.Y = vec.Y;
			v.Z = vec.Z;
			return v;
		}
	}
	class Program {
		static void Main(string[] args) {
			SimpleOpenGLSample sample = new SimpleOpenGLSample();
			sample.Run();
		}
	}
}






コンパイル手順

>set PATH=C:\Windows\Microsoft.NET\Framework\v4.0.30319;%PATH%


>csc ass3.cs /reference:OpenTK.dll /reference:AssimpNet.dll


以上。

0
0
0

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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?