LoginSignup
1
1

More than 5 years have passed since last update.

vs2010でmanaged directx その2

Last updated at Posted at 2018-08-30

概要

c#で、3Dしたかった。
cameraを、動かす。

写真

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 VertexBuffer buffer = null;
        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);
            buffer = new VertexBuffer(typeof(CustomVertex.PositionColored), 3, device, Usage.Dynamic | Usage.WriteOnly, CustomVertex.PositionColored.Format, Pool.Default);
            CustomVertex.PositionColored[] verts = (CustomVertex.PositionColored[]) buffer.Lock(0, 0);
            verts[0].Position = new Vector3(0.0f, 1.0f, 1.0f);
            verts[0].Color = Color.BlueViolet.ToArgb();
            verts[1].Position = new Vector3(-1.0f, -1.0f, 1.0f);
            verts[1].Color = Color.GreenYellow.ToArgb();
            verts[2].Position = new Vector3(1.0f, -1.0f, 1.0f);
            verts[2].Color = Color.Red.ToArgb();
            buffer.Unlock();                
        }
        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            device.Clear(ClearFlags.Target, Color.Blue, 1.0f, 0);
            Matrix view,
                w;
            w = Matrix.RotationY(Environment.TickCount / 1000.0f);
            view = Matrix.Translation(0, 0, 5);
            view = w * view;
            device.SetTransform(TransformType.View, view);
            device.Transform.Projection = Matrix.PerspectiveFovLH((float) Math.PI / 4, 1.0f, 1.0f, 100.0f);
            device.RenderState.CullMode = Cull.None;
            device.RenderState.Lighting = false;    
            device.BeginScene();
            device.VertexFormat = CustomVertex.PositionColored.Format;
            device.SetStreamSource(0, buffer, 0);
            device.DrawPrimitives(PrimitiveType.TriangleList, 0, 1);
            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