LoginSignup
1
1

More than 5 years have passed since last update.

vs2010でmanaged directx その7

Last updated at Posted at 2018-09-01

概要

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 WindowsFormsApplication5
{
    public partial class Form1 : Form
    {
        private Device device = null;
        private Mesh _boxMesh = null;
        private Mesh _sphereMesh = null;
        private Mesh _teapotMesh = null;
        private Texture _texture;
        private VertexBuffer _vertexBuffer;
        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.SoftwareVertexProcessing, pp);
            _boxMesh = Mesh.Box(device, 2.0f, 1.0f, 0.5f);
            _sphereMesh = Mesh.Sphere(device, 0.8f, 8, 6);
            _teapotMesh = Mesh.Teapot(device);
            _texture = TextureLoader.FromFile(device, Application.StartupPath + @"\earth2k.jpg");
            _vertexBuffer = new VertexBuffer(typeof(CustomVertex.PositionTextured), 4, device, 0, CustomVertex.PositionTextured.Format, Pool.Managed);
            CustomVertex.PositionTextured[] verts = new CustomVertex.PositionTextured[4];
            verts[0].Position = new Vector3(3, 3, 0);
            verts[0].Tu = 0;
            verts[0].Tv = 0;
            verts[1].Position = new Vector3(-3, 3, 0);
            verts[1].Tu = 1;
            verts[1].Tv = 0;
            verts[2].Position = new Vector3(3, -3, 0);
            verts[2].Tu = 0;
            verts[2].Tv = 1;
            verts[3].Position = new Vector3(-3, -3, 0);
            verts[3].Tu = 1;
            verts[3].Tv = 1;
            GraphicsStream stm = _vertexBuffer.Lock(0, 0, 0);
            stm.Write(verts);
            _vertexBuffer.Unlock();


        }
        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, 0.1f, 100.0f);
            device.Clear(ClearFlags.Target, Color.DarkBlue, 1.0f, 0);
            device.SetTexture(0, _texture);
            device.RenderState.Lighting = false;
            device.RenderState.CullMode = Cull.None;
            Matrix rotateMatrix = Matrix.RotationY(Environment.TickCount / 1000.0f);
            device.BeginScene();
            device.Transform.World = Matrix.Identity;
            device.SetStreamSource(0, _vertexBuffer, 0);
            device.VertexFormat = CustomVertex.PositionTextured.Format;
            device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2);
            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(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.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