LoginSignup
1
1

More than 5 years have passed since last update.

vs2010でmanaged directx その6

Last updated at Posted at 2018-08-31

概要

c#で、3Dやりたかった。
プリミティブを表示してみた。

写真

image

サンプルコード

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;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        private Device device = null; 
        private Mesh _boxMesh = null;
        private Mesh _cylinderMesh = null;
        private Mesh _polygonMesh = null;
        private Mesh _sphereMesh = null;
        private Mesh _teapotMesh = null;
        private Mesh _textMesh = null;
        private Mesh _torusMesh = null;
        public Form1()
        {
            InitializeComponent();
            this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            PresentParameters pp = new PresentParameters();
            pp.Windowed = true;
            pp.SwapEffect = SwapEffect.Discard;
            device = new Device(0, DeviceType.Hardware, this, CreateFlags.HardwareVertexProcessing, pp);
            _boxMesh = Mesh.Box(device, 2.0f, 1.0f, 0.5f);
            _cylinderMesh = Mesh.Cylinder(device, 0.5f, 0.8f, 1.5f, 8, 1);
            _polygonMesh = Mesh.Polygon(device, 1.0f, 5);
            _sphereMesh = Mesh.Sphere(device, 0.8f, 8, 6);
            _teapotMesh = Mesh.Teapot(device); 
            _torusMesh = Mesh.Torus(device, 0.1f, 0.6f, 6, 12);
        }
        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            device.Transform.View = Matrix.LookAtLH(new Vector3(0.0f, 1.0f, 10.0f), new Vector3(0.0f, 0.0f, 0.0f), new Vector3(0.0f, 1.0f, 0.0f));
            device.Transform.Projection = Matrix.PerspectiveFovLH((float)(Math.PI / 4), 1.0f, 1.0f, 1000.0f);
            device.Lights[0].Direction = Vector3.Normalize(new Vector3(-1, -2, -3));
            device.Lights[0].Type = LightType.Directional;
            device.Lights[0].Diffuse = Color.White;
            device.Lights[0].Ambient = Color.FromArgb(255, 40, 40, 40);
            device.Lights[0].Enabled = true;
            device.Lights[0].Update();
            device.Clear(ClearFlags.Target, Color.DarkBlue, 1.0f, 0);
            device.BeginScene();
            device.SetTransform(TransformType.World, Matrix.Identity);
            device.RenderState.Lighting = true;
            Material mtrl = new Material();
            mtrl.Diffuse = Color.White;
            mtrl.Ambient = Color.FromArgb(255, 128, 128, 128);
            device.Material = mtrl;
            Matrix rotateMatrix = Matrix.RotationY(Environment.TickCount / 1000.0f);
            device.SetTransform(TransformType.World, rotateMatrix * Matrix.Translation(new Vector3(0.0f, 2.0f, 0.0f)));
            _boxMesh.DrawSubset(0);
            device.SetTransform(TransformType.World, rotateMatrix * Matrix.Translation(new Vector3(-2.5f, 0.8f, 0.0f)));
            _cylinderMesh.DrawSubset(0);
            device.SetTransform(TransformType.World, rotateMatrix * Matrix.Translation(new Vector3(-2.5f, -0.8f, 0.0f)));
            _polygonMesh.DrawSubset(0);
            device.SetTransform(TransformType.World, rotateMatrix * Matrix.Translation(new Vector3(0.0f, -2.0f, 0.0f)));
            _sphereMesh.DrawSubset(0);
            device.SetTransform(TransformType.World, rotateMatrix * Matrix.Translation(new Vector3(0.0f, 0.0f, 0.0f)));
            _teapotMesh.DrawSubset(0);
            device.SetTransform(TransformType.World, rotateMatrix * Matrix.Translation(new Vector3(2.5f, -0.8f, 0.0f)));
            device.SetTransform(TransformType.World, rotateMatrix * Matrix.Translation(new Vector3(2.5f, 0.8f, 0.0f)));
            _torusMesh.DrawSubset(0);
            device.EndScene();
            device.Present();
            this.Invalidate();
        }
    }
}

以上。

1
1
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
1
1