LoginSignup
1
1

More than 5 years have passed since last update.

vs2010でmanaged directx その13

Posted at

概要

c#で、3Dやりたかった。
Xファイルを読んでみた。

写真

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 WindowsFormsApplication9
{
    public partial class Form1 : Form
    {
        private Device device = null;
        private Mesh mesh = null;
        private Material[] meshMaterials;
        private Texture[] meshTextures;

        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);
            ExtendedMaterial[] materials = null;
            mesh = Mesh.FromFile("Dolphin1.x", MeshFlags.SystemMemory, device, out materials);
            if (meshTextures == null)
            {
                meshTextures = new Texture[materials.Length];
                meshMaterials = new Material[materials.Length];
                for (int i = 0; i < materials.Length; i++)
                {
                    meshMaterials[i] = materials[i].Material3D;
                    meshMaterials[i].Ambient = meshMaterials[i].Diffuse;
                    if (materials[i].TextureFilename != null)
                    {
                        meshTextures[i] = TextureLoader.FromFile(device, materials[i].TextureFilename);
                    }
                }
            }
        }
        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            device.Transform.View = Matrix.LookAtLH(new Vector3(0.0f, 1.0f, 1000.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, 10000.0f);
            device.Lights[0].Type = LightType.Directional;
            device.Lights[0].Diffuse = Color.White;
            device.Lights[0].Ambient = Color.Red;
            device.Lights[0].Enabled = true;
            device.Clear(ClearFlags.Target, Color.DarkBlue, 1.0f, 0);
            device.RenderState.Lighting = true;
            Matrix rotateMatrix = Matrix.RotationY(Environment.TickCount / 5000.0f);
            device.BeginScene();          
            device.SetTransform(TransformType.World, rotateMatrix * Matrix.Translation(new Vector3(0.0f, 0.0f, 0.0f)));
            for (int i = 0; i < meshMaterials.Length; i++)
            {
                device.Material = meshMaterials[i];
                device.SetTexture(0, meshTextures[i]);
                mesh.DrawSubset(i);
            }
            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