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?

cscの作法 その592

Posted at

概要

cscの作法、調べてみた。
wpf3d、やってみた。
練習問題やってみた。

練習問題

wpf3dで、球を表示せよ。

写真

image.png

サンプルコード


using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Media3D;

class Main3d: Window {
	public Main3d() {
		var stacks = 12;
		var slices = 16;
		var pos = new Point3DCollection();
		var idx = new Int32Collection();
		for (var st = 0; st <= stacks; st++) 
		{
			var a = Math.PI * st / stacks;
			var y = Math.Cos(a);
			var r = Math.Sin(a);
			for (var sl = 0; sl <= slices; sl++) 
			{
				var b = 2 * Math.PI * sl / slices + Math.PI;
				var z = Math.Cos(b);
				var x = Math.Sin(b);
				pos.Add(new Point3D(x * r, y, z * r));
			}
		}
		for (var st = 0; st < stacks; st++) 
		{
			var h0 = (slices + 1) * st;
			var h1 = (slices + 1) * (st + 1);
			for (var sl = 0; sl < slices; sl++) 
			{
				var i0 = h0 + sl;
				var i1 = h1 + sl;
				var i2 = h0 + sl + 1;
				var i3 = h1 + sl + 1;
				idx.Add(i0);
				idx.Add(i1);
				idx.Add(i2);
				idx.Add(i3);
				idx.Add(i2);
				idx.Add(i1);
			}
		}
		var model = new GeometryModel3D();
		model.Geometry = new MeshGeometry3D() {
			Positions = pos,
			TriangleIndices = idx,
		};
		model.Material = new DiffuseMaterial(Brushes.LightGreen);
		var light = new DirectionalLight();
		light.Direction = new Vector3D(-1, -2, -3);
		var cam = new PerspectiveCamera();
		cam.Position = new Point3D(0, 0, 5);
		var vp3d = new Viewport3D();
		vp3d.Camera = cam;
		vp3d.Children.Add(new ModelVisual3D() { 
			Content = model 
		});
		vp3d.Children.Add(new ModelVisual3D() { 
			Content = light 
		});
		Width = 400;
		Height = 400;
		Content = vp3d;
	}
	[STAThread]
	static void Main() {
		var wnd = new Main3d();
		wnd.ShowDialog();
	}
}


以上。

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?