• R/O
  • SSH
  • HTTPS

mmdx: Commit


Commit MetaInfo

Revision729 (tree)
Zeit2011-04-25 01:32:55
Autorwilfrem

Log Message

SlimMMDXの描画システムをとりあえず構築。次からデモを作ってデバッグ

Ändern Zusammenfassung

Diff

--- branches/XNA4/MikuMikuDanceCore/Model/IMMDModelPart.cs (revision 728)
+++ branches/XNA4/MikuMikuDanceCore/Model/IMMDModelPart.cs (revision 729)
@@ -18,10 +18,10 @@
1818 /// <param name="world">ワールドマトリクス</param>
1919 void SetParams(ref Matrix world);
2020 /// <summary>
21- /// ボーンの設定
21+ /// スキン行列の設定
2222 /// </summary>
23- /// <param name="bones">ボーン配列</param>
24- void SetBone(Matrix[] bones);
23+ /// <param name="bones">スキン行列配列</param>
24+ void SetSkinMatrix(Matrix[] skinTransforms);
2525
2626 /// <summary>
2727 /// モデルパーツの描画
--- branches/XNA4/MikuMikuDanceCore/Model/MMDModel.cs (revision 728)
+++ branches/XNA4/MikuMikuDanceCore/Model/MMDModel.cs (revision 729)
@@ -111,7 +111,7 @@
111111 }
112112 protected virtual void SetBone(Matrix[] skinTransforms)
113113 {
114- System.Threading.Tasks.Parallel.ForEach(Parts, (part) => part.SetBone(skinTransforms));
114+ System.Threading.Tasks.Parallel.ForEach(Parts, (part) => part.SetSkinMatrix(skinTransforms));
115115 /*foreach (var part in Parts)
116116 part.SetBone(BoneManager.SkinTransforms);*/
117117 }
--- branches/XNA4/MikuMikuDanceCore/Model/SkinningHelpers.cs (revision 728)
+++ branches/XNA4/MikuMikuDanceCore/Model/SkinningHelpers.cs (revision 729)
@@ -11,7 +11,7 @@
1111
1212 namespace MikuMikuDance.Core.Model
1313 {
14- static class SkinningHelpers
14+ public static class SkinningHelpers
1515 {
1616 #if SlimDX
1717 public static void SkinVertex(Matrix[] bones, MMDVertex vertin, out Vector4 outPosition)
--- branches/XNA4/SlimMMDX/Model/MMDModelPartFactory.cs (revision 728)
+++ branches/XNA4/SlimMMDX/Model/MMDModelPartFactory.cs (revision 729)
@@ -83,11 +83,11 @@
8383 {
8484 effect = Effect.FromFile(SlimMMDXCore.Instance.Device, file,
8585 #if DEBUG
86- ShaderFlags.OptimizationLevel0 | ShaderFlags.Debug
86+ ShaderFlags.OptimizationLevel0 | ShaderFlags.Debug
8787 #else
8888 ShaderFlags.OptimizationLevel3
8989 #endif
90- );
90+);
9191 }
9292 }
9393 if (effect == null)
@@ -94,11 +94,11 @@
9494 {
9595 effect = Effect.FromMemory(SlimMMDXCore.Instance.Device, MMDXResource.MMDWinEffect,
9696 #if DEBUG
97- ShaderFlags.OptimizationLevel0 | ShaderFlags.Debug
97+ ShaderFlags.OptimizationLevel0 | ShaderFlags.Debug
9898 #else
9999 ShaderFlags.OptimizationLevel3
100100 #endif
101- );
101+);
102102 }
103103 //マテリアル設定
104104 effect.SetValue<Vector3>("DiffuseColor", MMDXMath.ToVector3(material.DiffuseColor));
@@ -107,7 +107,7 @@
107107 effect.SetValue("SpecularColor", MMDXMath.ToVector3(material.SpecularColor));
108108 effect.SetValue("SpecularPower", material.Specularity);
109109 int shaderIndex;
110- //テクスチャ設定
110+ //テクスチャ設定
111111 if (!string.IsNullOrEmpty(material.TextureFileName))
112112 {
113113 //テクスチャを読み込んではめ込み
@@ -155,8 +155,8 @@
155155 //シェーダインデックス設定
156156 effect.SetValue("ShaderIndex", shaderIndex);
157157 //インデックスバッファ(頂点は組む必要ない。頂点ストリームはセットしたままにしてインデックスバッファだけ変えてドローするといい)
158- IndexBuffer indexbuffer = new IndexBuffer(SlimMMDXCore.Instance.Device, (int)(sizeof(ushort) * material.FaceVertCount), Usage.None, Pool.Default, true);
159- result.Add(new MMDModelPart(effect, indexbuffer));
158+ IndexBuffer indexbuffer = new IndexBuffer(SlimMMDXCore.Instance.Device, (int)(sizeof(ushort) * material.FaceVertCount), Usage.None, Pool.Managed, true);
159+ result.Add(new MMDModelPart((int)material.FaceVertCount, effect, indexbuffer));
160160 }
161161 return result;
162162 }
--- branches/XNA4/SlimMMDX/Model/SlimMMDModel.cs (revision 728)
+++ branches/XNA4/SlimMMDX/Model/SlimMMDModel.cs (revision 729)
@@ -5,6 +5,9 @@
55 using MikuMikuDance.Core.Model;
66 using MikuMikuDance.Core.Motion;
77 using MikuMikuDance.Core.Model.Physics;
8+using SlimDX.Direct3D9;
9+using System.Runtime.InteropServices;
10+using SlimDX;
811
912 namespace MikuMikuDance.SlimDX.Model
1013 {
@@ -14,6 +17,9 @@
1417 public class SlimMMDModel : MMDModel
1518 {
1619 MMDVertexNmTx[] m_vertex;
20+ VertexPNmTx[] verticesSource;
21+ VertexBuffer vertexBuffer;
22+
1723 /// <summary>
1824 /// コンストラクタ
1925 /// </summary>
@@ -26,7 +32,18 @@
2632 : base(modelParts, boneManager, attachedMotion, rigids, joints)
2733 {
2834 m_vertex = vertex;
29-
35+ //データのコピー
36+ verticesSource = new VertexPNmTx[m_vertex.LongLength];
37+ for (long i = 0; i < m_vertex.LongLength; ++i)
38+ {
39+ verticesSource[i].Position = new Vector4(m_vertex[i].Position, 0);
40+ verticesSource[i].Normal = m_vertex[i].Normal;
41+ verticesSource[i].Texture = m_vertex[i].TextureCoordinate;
42+ }
43+ vertexBuffer = new VertexBuffer(SlimMMDXCore.Instance.Device, m_vertex.Length * Marshal.SizeOf(typeof(VertexPNmTx)), Usage.Dynamic, VertexPNmTx.Format, Pool.Managed);
44+ DataStream stream = vertexBuffer.Lock(0, 0, LockFlags.None);
45+ stream.WriteRange(verticesSource);
46+ vertexBuffer.Unlock();
3047 }
3148
3249 /// <summary>
@@ -34,10 +51,21 @@
3451 /// </summary>
3552 protected override void BeforeDraw()
3653 {
54+ //頂点バッファの更新
55+ DataStream stream = vertexBuffer.Lock(0, 0, LockFlags.None);
56+ stream.WriteRange(verticesSource);
57+ vertexBuffer.Unlock();
3758 //頂点バッファのセット処理
38-
59+ SlimMMDXCore.Instance.Device.SetStreamSource(0, vertexBuffer, 0, Marshal.SizeOf(typeof(VertexPNmTx)));
60+ SlimMMDXCore.Instance.Device.VertexFormat = VertexPNmTx.Format;
3961 base.BeforeDraw();
40- throw new NotImplementedException();
4162 }
63+
64+ protected override void SetBone(Matrix[] skinTransforms)
65+ {
66+ System.Threading.Tasks.Parallel.For(0, m_vertex.Length,
67+ (i) => SkinningHelpers.SkinVertex(skinTransforms, m_vertex[i], out verticesSource[i].Position));
68+ //base.SetBone(skinTransforms);(こっちでは呼ばない
69+ }
4270 }
4371 }
--- branches/XNA4/SlimMMDX/Model/VertexPNmTx.cs (nonexistent)
+++ branches/XNA4/SlimMMDX/Model/VertexPNmTx.cs (revision 729)
@@ -0,0 +1,19 @@
1+using System;
2+using System.Collections.Generic;
3+using System.Linq;
4+using System.Text;
5+using System.Runtime.InteropServices;
6+using SlimDX.Direct3D9;
7+using SlimDX;
8+
9+namespace MikuMikuDance.SlimDX.Model
10+{
11+ [StructLayout(LayoutKind.Sequential)]
12+ struct VertexPNmTx
13+ {
14+ public const VertexFormat Format = VertexFormat.Position | VertexFormat.Normal | VertexFormat.Texture0;
15+ public Vector4 Position;
16+ public Vector3 Normal;
17+ public Vector2 Texture;
18+ }
19+}
--- branches/XNA4/SlimMMDX/Model/MMDModelPart.cs (revision 728)
+++ branches/XNA4/SlimMMDX/Model/MMDModelPart.cs (revision 729)
@@ -4,35 +4,57 @@
44 using System.Text;
55 using MikuMikuDance.Core.Model;
66 using SlimDX;
7+using SlimDX.Direct3D9;
78
89 namespace MikuMikuDance.SlimDX.Model
910 {
1011 public class MMDModelPart : IMMDModelPart
1112 {
12- private global::SlimDX.Direct3D9.Effect effect;
13- private global::SlimDX.Direct3D9.IndexBuffer indexbuffer;
14-
15- public MMDModelPart(global::SlimDX.Direct3D9.Effect effect, global::SlimDX.Direct3D9.IndexBuffer indexbuffer)
13+ protected Effect effect;
14+ protected IndexBuffer indexbuffer;
15+ protected int vertexCount;
16+ public MMDModelPart(int vertexCount, Effect effect, IndexBuffer indexbuffer)
1617 {
17- // TODO: Complete member initialization
18+ this.vertexCount = vertexCount;
1819 this.effect = effect;
1920 this.indexbuffer = indexbuffer;
2021 }
2122 #region IMMDModelPart メンバー
2223
24+ /// <summary>
25+ /// エフェクトにマトリックスを適用
26+ /// </summary>
27+ /// <param name="world">ワールド</param>
2328 public void SetParams(ref Matrix world)
2429 {
25- throw new NotImplementedException();
26- }
30+ Matrix view, projection;
31+ //カメラ情報の取得
32+ Viewport viewport = effect.Device.Viewport;
33+ float aspectRatio = (float)viewport.Width / (float)viewport.Height;
34+ SlimMMDXCore.Instance.Camera.GetCameraParam(aspectRatio, out view, out projection);
2735
28- public void SetBone(Matrix[] bones)
29- {
30- throw new NotImplementedException();
36+ //マトリクス処理
37+ effect.SetValue("World", world);
38+ effect.SetValue("View", view);
39+ effect.SetValue("Projection", projection);
40+ effect.SetValue("EyePosition", SlimMMDXCore.Instance.Camera.Position);
41+ //ライティング処理
42+ Vector3 color, dir;
43+ SlimMMDXCore.Instance.Light.GetLightParam(out color, out dir);
44+ effect.SetValue("AmbientLightColor", color);
45+ effect.SetValue("DirLight0Direction", dir);
46+ effect.Technique = "MMDEffect";
3147 }
3248
49+ public void SetSkinMatrix(Matrix[] bones) { }//こちらではなにもしない
50+
3351 public void Draw()
3452 {
35- throw new NotImplementedException();
53+ effect.Begin();
54+ effect.BeginPass(0);
55+ SlimMMDXCore.Instance.Device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, vertexCount, 0, vertexCount / 3);
56+ effect.EndPass();
57+ effect.End();
3658 }
3759
3860 #endregion
Show on old repository browser