LoginSignup
1
1

More than 5 years have passed since last update.

vs2010でmanaged directx その5

Last updated at Posted at 2018-08-30

概要

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 WindowsFormsApplication4
{
    public partial class Form1 : Form
    {
        private Device device = null;
        private VertexBuffer _vertexBuffer = null;
        private IndexBuffer _indexBuffer = null;
        private static Int16[] _vertexIndices = new Int16[] { 
            2, 0, 1, 
            1, 3, 2, 
            4, 0, 2, 
            2, 6, 4,
            5, 1, 0, 
            0, 4, 5, 
            7, 3, 1, 
            1, 5, 7, 
            6, 2, 3, 
            3, 7, 6, 
            4, 6, 7, 
            7, 5, 4 
        };
        private float cheta = 270.0f;
        private float chi = 0.0f;
        private Point oPoint = Point.Empty;
        public Form1()
        {
            InitializeComponent();
            this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            PresentParameters presentParams = new PresentParameters();
            presentParams.Windowed = true;
            presentParams.SwapEffect = SwapEffect.Discard;
            device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);
            _vertexBuffer = new VertexBuffer(typeof(CustomVertex.PositionColored), 8, device, Usage.None, CustomVertex.PositionColored.Format, Pool.Managed);
            CustomVertex.PositionColored[] vertices = new CustomVertex.PositionColored[8];
            vertices[0] = new CustomVertex.PositionColored(-2.0f, 2.0f, 2.0f, Color.Yellow.ToArgb());
            vertices[1] = new CustomVertex.PositionColored(2.0f, 2.0f, 2.0f, Color.Gray.ToArgb());
            vertices[2] = new CustomVertex.PositionColored(-2.0f, 2.0f, -2.0f, Color.Purple.ToArgb());
            vertices[3] = new CustomVertex.PositionColored(2.0f, 2.0f, -2.0f, Color.Red.ToArgb());
            vertices[4] = new CustomVertex.PositionColored(-2.0f, -2.0f, 2.0f, Color.SkyBlue.ToArgb());
            vertices[5] = new CustomVertex.PositionColored(2.0f, -2.0f, 2.0f, Color.Orange.ToArgb());
            vertices[6] = new CustomVertex.PositionColored(-2.0f, -2.0f, -2.0f, Color.Green.ToArgb());
            vertices[7] = new CustomVertex.PositionColored(2.0f, -2.0f, -2.0f, Color.Blue.ToArgb());
            using (GraphicsStream data = _vertexBuffer.Lock(0, 0, LockFlags.None))
            {
                data.Write(vertices);
                _vertexBuffer.Unlock();
            }
            _indexBuffer = new IndexBuffer(device, 12 * 3 * 2, Usage.WriteOnly, Pool.Managed, true);
            using (GraphicsStream data = _indexBuffer.Lock(0, 0, LockFlags.None))
            {
                data.Write(_vertexIndices);
                this._indexBuffer.Unlock();
            }
        }
        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            device.Clear(ClearFlags.Target, Color.Blue, 1.0f, 0);
            float radius = 10.0f;
            float theta = Geometry.DegreeToRadian(cheta);
            float phi = Geometry.DegreeToRadian(chi);
            Vector3 camera = new Vector3((float)(radius * Math.Cos(theta) * Math.Cos(phi)), (float)(radius * Math.Sin(phi)), (float)(radius * Math.Sin(theta) * Math.Cos(phi)));
            device.Transform.View = Matrix.LookAtLH(camera, 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, 100.0f);
            device.RenderState.Lighting = false;
            device.BeginScene();
            device.SetStreamSource(0, _vertexBuffer, 0);
            device.VertexFormat = CustomVertex.PositionColored.Format;
            device.Indices = _indexBuffer;
            device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 8, 0, 12);
            device.EndScene();
            device.Present();
            this.Invalidate();
        }
        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                cheta -= e.Location.X - oPoint.X;
                chi += e.Location.Y - oPoint.Y;
                if (chi >= 90.0f)
                {
                    chi = 89.9999f;
                }
                else if (chi <= -90.0f)
                {
                    chi = -89.9999f;
                }
            }
            oPoint = e.Location;
        }
    }
}



以上。

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