• R/O
  • SSH
  • HTTPS

mmdx: Commit


Commit MetaInfo

Revision660 (tree)
Zeit2011-01-10 11:46:21
Autorwilfrem

Log Message

CPUモデル表示に切り替えたところ

Ändern Zusammenfassung

Diff

--- branches/XNA4/MikuMikuDanceXNADemo1/MikuMikuDanceXNADemo1/Game1.cs (revision 659)
+++ branches/XNA4/MikuMikuDanceXNADemo1/MikuMikuDanceXNADemo1/Game1.cs (revision 660)
@@ -102,6 +102,9 @@
102102 }
103103 modelMesh.Draw();
104104 }*/
105+ Viewport viewport = GraphicsDevice.Viewport;
106+ float aspectRatio = (float)viewport.Width / (float)viewport.Height;
107+ model.Draw(Matrix.Identity, Matrix.CreateLookAt(new Vector3(0, 10, 30), new Vector3(0, 10, 0), Vector3.UnitY), Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, aspectRatio, 1, 300));
105108 base.Draw(gameTime);
106109 }
107110 }
--- branches/XNA4/MikuMikuDanceXNA/Model/MMDModel.cs (revision 659)
+++ branches/XNA4/MikuMikuDanceXNA/Model/MMDModel.cs (revision 660)
@@ -3,23 +3,61 @@
33 using System.Linq;
44 using System.Text;
55 using MikuMikuDance.XNA.Motion;
6+using System.Collections.ObjectModel;
7+using Microsoft.Xna.Framework;
8+using Microsoft.Xna.Framework.Graphics;
69
710 namespace MikuMikuDance.XNA.Model
811 {
912 public class MMDModel
1013 {
11- private List<MMDModelPart> modelParts;
12- private SkinningData skinningData;
13- Dictionary<string, MMDMotionData> attachedMotion;
14+ private readonly List<MMDModelPart> modelParts;
15+ private readonly SkinningData skinningData;
16+ readonly Dictionary<string, MMDMotionData> attachedMotion;
1417
18+ /// <summary>
19+ /// このモデルのメッシュパーツ
20+ /// </summary>
21+ public ReadOnlyCollection<MMDModelPart> Parts { get; internal set; }
22+
1523 public MMDModel(List<MMDModelPart> modelParts, SkinningData skinningData, Dictionary<string, MMDMotionData> attachedMotion)
1624 {
17- // TODO: Complete member initialization
1825 this.modelParts = modelParts;
1926 this.skinningData = skinningData;
2027 this.attachedMotion = attachedMotion;
28+
29+ Parts = new ReadOnlyCollection<MMDModelPart>(modelParts);
30+
2131 }
2232
33+ /// <summary>
34+ /// Draws the model using the specified camera matrices and the default
35+ /// lighting model.
36+ /// </summary>
37+ public void Draw(Matrix world, Matrix view, Matrix projection)
38+ {
39+ foreach (var part in modelParts)
40+ {
41+ // use the new effect interfaces so this code will still work
42+ // even if someone changes the effect on the part to a new type.
43+
44+ IEffectMatrices matrices = part.Effect as IEffectMatrices;
45+ if (matrices != null)
46+ {
47+ matrices.World = world;
48+ matrices.View = view;
49+ matrices.Projection = projection;
50+ }
51+
52+ IEffectLights lights = part.Effect as IEffectLights;
53+ if (lights != null)
54+ {
55+ lights.EnableDefaultLighting();
56+ }
57+
58+ part.Draw();
59+ }
60+ }
2361
2462 }
2563 }
--- branches/XNA4/MikuMikuDanceXNA/Model/MMDModelPart.cs (revision 659)
+++ branches/XNA4/MikuMikuDanceXNA/Model/MMDModelPart.cs (revision 660)
@@ -2,24 +2,302 @@
22 using System.Collections.Generic;
33 using System.Linq;
44 using System.Text;
5+using Microsoft.Xna.Framework.Graphics;
6+using Microsoft.Xna.Framework;
57
68 namespace MikuMikuDance.XNA.Model
79 {
8- public class MMDModelPart
10+ /// <summary>
11+ /// モデルパーツ
12+ /// </summary>
13+ public abstract class MMDModelPart
914 {
10- private int triangleCount;
11- private MMDVertex[] cpuVertices;
12- private Microsoft.Xna.Framework.Graphics.IndexBuffer indexBuffer;
15+ protected readonly int triangleCount;
16+ protected readonly int vertexCount;
17+ protected MMDVertex[] vertices;
18+ protected IndexBuffer indexBuffer;
19+ protected DynamicVertexBuffer vertexBuffer;//このフィールドは継承先で代入
20+ public BasicEffect Effect { get; set; }
1321
14- public MMDModelPart(int triangleCount, MMDVertex[] cpuVertices, Microsoft.Xna.Framework.Graphics.IndexBuffer indexBuffer)
22+ public MMDModelPart(int triangleCount, MMDVertex[] vertices, IndexBuffer indexBuffer)
1523 {
16- // TODO: Complete member initialization
1724 this.triangleCount = triangleCount;
18- this.cpuVertices = cpuVertices;
25+ this.vertexCount = vertices.Length;
26+ this.vertices = vertices;
1927 this.indexBuffer = indexBuffer;
28+
2029 }
2130
2231
23- public Microsoft.Xna.Framework.Graphics.BasicEffect Effect { get; set; }
32+
33+ public void Draw()
34+ {
35+ GraphicsDevice graphics = Effect.GraphicsDevice;
36+
37+ //バッファセット
38+ graphics.Indices = indexBuffer;
39+ graphics.SetVertexBuffer(vertexBuffer);
40+
41+ foreach (EffectPass pass in Effect.CurrentTechnique.Passes)
42+ {
43+ pass.Apply();
44+ graphics.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, vertexCount, 0, triangleCount);
45+ }
46+
47+ graphics.Indices = null;
48+ graphics.SetVertexBuffer(null);
49+ }
50+
51+ //ファクトリー関数
52+ internal static MMDModelPart Create(int triangleCount, MMDVertex[] Vertices, IndexBuffer indexBuffer)
53+ {
54+ if (Vertices[0].Normal.HasValue)
55+ {
56+ if (Vertices[0].TextureCoordinate.HasValue)
57+ {
58+ if (Vertices[0].VertexColor.HasValue)
59+ return new MMDModelPartPNmTxVc(triangleCount, Vertices, indexBuffer);
60+ else
61+ return new MMDModelPartPNmTx(triangleCount, Vertices, indexBuffer);
62+ }
63+ else
64+ {
65+ if (Vertices[0].VertexColor.HasValue)
66+ return new MMDModelPartPNmVc(triangleCount, Vertices, indexBuffer);
67+ else
68+ return new MMDModelPartPNm(triangleCount, Vertices, indexBuffer);
69+ }
70+ }
71+ else
72+ {
73+ if (Vertices[0].TextureCoordinate.HasValue)
74+ {
75+ if (Vertices[0].VertexColor.HasValue)
76+ return new MMDModelPartPTxVc(triangleCount, Vertices, indexBuffer);
77+ else
78+ return new MMDModelPartPTx(triangleCount, Vertices, indexBuffer);
79+ }
80+ else
81+ {
82+ if (Vertices[0].VertexColor.HasValue)
83+ return new MMDModelPartPVc(triangleCount, Vertices, indexBuffer);
84+ else
85+ return new MMDModelPartP(triangleCount, Vertices, indexBuffer);
86+ }
87+ }
88+ }
2489 }
90+ /// <summary>
91+ /// モデルパーツ(頂点のみ)
92+ /// </summary>
93+ public class MMDModelPartP : MMDModelPart
94+ {
95+ readonly VertexPosition[] gpuVertices;
96+
97+ public MMDModelPartP(int triangleCount, MMDVertex[] vertices, IndexBuffer indexBuffer)
98+ : base(triangleCount, vertices, indexBuffer)
99+ {
100+ //GPUリソース作成
101+ gpuVertices = new VertexPosition[vertices.Length];
102+ vertexBuffer = new DynamicVertexBuffer(indexBuffer.GraphicsDevice, typeof(VertexPosition), vertices.Length, BufferUsage.WriteOnly);
103+ //初期値代入
104+ for (int i = 0; i < vertices.Length; i++)
105+ {
106+ gpuVertices[i].Position = vertices[i].Position;
107+ }
108+ // put the vertices into our vertex buffer
109+ vertexBuffer.SetData(gpuVertices, 0, vertexCount, SetDataOptions.Discard);
110+ }
111+ }
112+ /// <summary>
113+ /// モデルパーツ(頂点+色)
114+ /// </summary>
115+ public class MMDModelPartPVc : MMDModelPart
116+ {
117+ readonly VertexPositionColor[] gpuVertices;
118+
119+ public MMDModelPartPVc(int triangleCount, MMDVertex[] vertices, IndexBuffer indexBuffer)
120+ : base(triangleCount, vertices, indexBuffer)
121+ {
122+ //GPUリソース作成
123+ gpuVertices = new VertexPositionColor[vertices.Length];
124+ vertexBuffer = new DynamicVertexBuffer(indexBuffer.GraphicsDevice, typeof(VertexPositionColor), vertices.Length, BufferUsage.WriteOnly);
125+ //初期値代入
126+ for (int i = 0; i < vertices.Length; i++)
127+ {
128+ gpuVertices[i].Position = vertices[i].Position;
129+ if (!vertices[i].VertexColor.HasValue)
130+ throw new ArgumentException("vertices.Color is null");
131+ gpuVertices[i].Color = new Color(vertices[i].VertexColor.Value);
132+ }
133+ // put the vertices into our vertex buffer
134+ vertexBuffer.SetData(gpuVertices, 0, vertexCount, SetDataOptions.Discard);
135+ }
136+ }
137+ /// <summary>
138+ /// モデルパーツ(頂点+法線)
139+ /// </summary>
140+ public class MMDModelPartPNm : MMDModelPart
141+ {//スフィアマップだけ貼りつけている物はこれになる……
142+ readonly VertexPositionNormal[] gpuVertices;
143+
144+ public MMDModelPartPNm(int triangleCount, MMDVertex[] vertices, IndexBuffer indexBuffer)
145+ : base(triangleCount, vertices, indexBuffer)
146+ {
147+ //GPUリソース作成
148+ gpuVertices = new VertexPositionNormal[vertices.Length];
149+ vertexBuffer = new DynamicVertexBuffer(indexBuffer.GraphicsDevice, typeof(VertexPositionNormal), vertices.Length, BufferUsage.WriteOnly);
150+ //初期値代入
151+ for (int i = 0; i < vertices.Length; i++)
152+ {
153+ gpuVertices[i].Position = vertices[i].Position;
154+ if (!vertices[i].Normal.HasValue)
155+ throw new ArgumentException("vertices.Normal is null");
156+ gpuVertices[i].Normal = vertices[i].Normal.Value;
157+ }
158+ // put the vertices into our vertex buffer
159+ vertexBuffer.SetData(gpuVertices, 0, vertexCount, SetDataOptions.Discard);
160+ }
161+ }
162+ /// <summary>
163+ /// モデルパーツ(頂点+テクスチャー)
164+ /// </summary>
165+ public class MMDModelPartPTx : MMDModelPart
166+ {
167+ readonly VertexPositionTexture[] gpuVertices;
168+
169+ public MMDModelPartPTx(int triangleCount, MMDVertex[] vertices, IndexBuffer indexBuffer)
170+ : base(triangleCount, vertices, indexBuffer)
171+ {
172+ //GPUリソース作成
173+ gpuVertices = new VertexPositionTexture[vertices.Length];
174+ vertexBuffer = new DynamicVertexBuffer(indexBuffer.GraphicsDevice, typeof(VertexPositionTexture), vertices.Length, BufferUsage.WriteOnly);
175+ //初期値代入
176+ for (int i = 0; i < vertices.Length; i++)
177+ {
178+ gpuVertices[i].Position = vertices[i].Position;
179+ if (!vertices[i].TextureCoordinate.HasValue)
180+ throw new ArgumentException("vertices.TextureCoordinate is null");
181+ gpuVertices[i].TextureCoordinate = vertices[i].TextureCoordinate.Value;
182+ }
183+ // put the vertices into our vertex buffer
184+ vertexBuffer.SetData(gpuVertices, 0, vertexCount, SetDataOptions.Discard);
185+ }
186+ }
187+
188+ /// <summary>
189+ /// モデルパーツ(頂点+法線+色)
190+ /// </summary>
191+ public class MMDModelPartPNmVc : MMDModelPart
192+ {
193+ readonly VertexPositionNormalColor[] gpuVertices;
194+
195+ public MMDModelPartPNmVc(int triangleCount, MMDVertex[] vertices, IndexBuffer indexBuffer)
196+ : base(triangleCount, vertices, indexBuffer)
197+ {
198+ //GPUリソース作成
199+ gpuVertices = new VertexPositionNormalColor[vertices.Length];
200+ vertexBuffer = new DynamicVertexBuffer(indexBuffer.GraphicsDevice, typeof(VertexPositionNormalColor), vertices.Length, BufferUsage.WriteOnly);
201+ //初期値代入
202+ for (int i = 0; i < vertices.Length; i++)
203+ {
204+ gpuVertices[i].Position = vertices[i].Position;
205+ if (!vertices[i].Normal.HasValue)
206+ throw new ArgumentException("vertices.Normal is null");
207+ gpuVertices[i].Normal = vertices[i].Normal.Value;
208+ if (!vertices[i].VertexColor.HasValue)
209+ throw new ArgumentException("vertices.Color is null");
210+ gpuVertices[i].Color = new Color(vertices[i].VertexColor.Value);
211+ }
212+ // put the vertices into our vertex buffer
213+ vertexBuffer.SetData(gpuVertices, 0, vertexCount, SetDataOptions.Discard);
214+ }
215+ }
216+ /// <summary>
217+ /// モデルパーツ(頂点+テクスチャー+カラー)
218+ /// </summary>
219+ public class MMDModelPartPTxVc : MMDModelPart
220+ {
221+ readonly VertexPositionColorTexture[] gpuVertices;
222+
223+ public MMDModelPartPTxVc(int triangleCount, MMDVertex[] vertices, IndexBuffer indexBuffer)
224+ : base(triangleCount, vertices, indexBuffer)
225+ {
226+ //GPUリソース作成
227+ gpuVertices = new VertexPositionColorTexture[vertices.Length];
228+ vertexBuffer = new DynamicVertexBuffer(indexBuffer.GraphicsDevice, typeof(VertexPositionColorTexture), vertices.Length, BufferUsage.WriteOnly);
229+ //初期値代入
230+ for (int i = 0; i < vertices.Length; i++)
231+ {
232+ gpuVertices[i].Position = vertices[i].Position;
233+ if (!vertices[i].TextureCoordinate.HasValue)
234+ throw new ArgumentException("vertices.TextureCoordinate is null");
235+ gpuVertices[i].TextureCoordinate = vertices[i].TextureCoordinate.Value;
236+ if (!vertices[i].VertexColor.HasValue)
237+ throw new ArgumentException("vertices.Color is null");
238+ gpuVertices[i].Color = new Color(vertices[i].VertexColor.Value);
239+ }
240+ // put the vertices into our vertex buffer
241+ vertexBuffer.SetData(gpuVertices, 0, vertexCount, SetDataOptions.Discard);
242+ }
243+ }
244+ /// <summary>
245+ /// モデルパーツ(頂点+法線+テクスチャー)
246+ /// </summary>
247+ public class MMDModelPartPNmTx : MMDModelPart
248+ {
249+ readonly VertexPositionNormalTexture[] gpuVertices;
250+
251+ public MMDModelPartPNmTx(int triangleCount, MMDVertex[] vertices, IndexBuffer indexBuffer)
252+ : base(triangleCount, vertices, indexBuffer)
253+ {
254+ //GPUリソース作成
255+ gpuVertices = new VertexPositionNormalTexture[vertices.Length];
256+ vertexBuffer = new DynamicVertexBuffer(indexBuffer.GraphicsDevice, typeof(VertexPositionNormalTexture), vertices.Length, BufferUsage.WriteOnly);
257+ //初期値代入
258+ for (int i = 0; i < vertices.Length; i++)
259+ {
260+ gpuVertices[i].Position = vertices[i].Position;
261+ if (!vertices[i].Normal.HasValue)
262+ throw new ArgumentException("vertices.Normal is null");
263+ gpuVertices[i].Normal = vertices[i].Normal.Value;
264+ if (!vertices[i].TextureCoordinate.HasValue)
265+ throw new ArgumentException("vertices.TextureCoordinate is null");
266+ gpuVertices[i].TextureCoordinate = vertices[i].TextureCoordinate.Value;
267+ }
268+ // put the vertices into our vertex buffer
269+ vertexBuffer.SetData(gpuVertices, 0, vertexCount, SetDataOptions.Discard);
270+ }
271+ }
272+ /// <summary>
273+ /// モデルパーツ(頂点+法線+テクスチャー+カラー)
274+ /// </summary>
275+ public class MMDModelPartPNmTxVc : MMDModelPart
276+ {
277+ readonly VertexPositionNormalTextureColor[] gpuVertices;
278+
279+ public MMDModelPartPNmTxVc(int triangleCount, MMDVertex[] vertices, IndexBuffer indexBuffer)
280+ : base(triangleCount, vertices, indexBuffer)
281+ {
282+ //GPUリソース作成
283+ gpuVertices = new VertexPositionNormalTextureColor[vertices.Length];
284+ vertexBuffer = new DynamicVertexBuffer(indexBuffer.GraphicsDevice, typeof(VertexPositionNormalTextureColor), vertices.Length, BufferUsage.WriteOnly);
285+ //初期値代入
286+ for (int i = 0; i < vertices.Length; i++)
287+ {
288+ gpuVertices[i].Position = vertices[i].Position;
289+ if (!vertices[i].Normal.HasValue)
290+ throw new ArgumentException("vertices.Normal is null");
291+ gpuVertices[i].Normal = vertices[i].Normal.Value;
292+ if (!vertices[i].VertexColor.HasValue)
293+ throw new ArgumentException("vertices.Color is null");
294+ gpuVertices[i].Color = new Color(vertices[i].VertexColor.Value);
295+ if (!vertices[i].TextureCoordinate.HasValue)
296+ throw new ArgumentException("vertices.TextureCoordinate is null");
297+ gpuVertices[i].TextureCoordinate = vertices[i].TextureCoordinate.Value;
298+ }
299+ // put the vertices into our vertex buffer
300+ vertexBuffer.SetData(gpuVertices, 0, vertexCount, SetDataOptions.Discard);
301+ }
302+ }
25303 }
--- branches/XNA4/MikuMikuDanceXNA/Model/VertexDeclarations.cs (nonexistent)
+++ branches/XNA4/MikuMikuDanceXNA/Model/VertexDeclarations.cs (revision 660)
@@ -0,0 +1,62 @@
1+using System;
2+using System.Collections.Generic;
3+using System.Linq;
4+using System.Text;
5+using Microsoft.Xna.Framework.Graphics;
6+using Microsoft.Xna.Framework;
7+
8+namespace MikuMikuDance.XNA.Model
9+{
10+ struct VertexPosition : IVertexType
11+ {
12+
13+ public Vector3 Position;
14+ public readonly static VertexDeclaration vertexDeclaration = new VertexDeclaration
15+ (
16+ new VertexElement(0, VertexElementFormat.Vector3, VertexElementUsage.Position, 0)
17+ );
18+ VertexDeclaration IVertexType.VertexDeclaration { get { return vertexDeclaration; } }
19+ }
20+ struct VertexPositionNormal : IVertexType
21+ {
22+
23+ public Vector3 Position;
24+ public Vector3 Normal;
25+ public readonly static VertexDeclaration vertexDeclaration = new VertexDeclaration
26+ (
27+ new VertexElement(0, VertexElementFormat.Vector3, VertexElementUsage.Position, 0),
28+ new VertexElement(sizeof(float) * 3, VertexElementFormat.Vector3, VertexElementUsage.Normal, 0)
29+ );
30+ public VertexDeclaration VertexDeclaration { get { return vertexDeclaration; } }
31+ }
32+ struct VertexPositionNormalColor : IVertexType
33+ {
34+
35+ public Vector3 Position;
36+ public Vector3 Normal;
37+ public Color Color;
38+ public readonly static VertexDeclaration vertexDeclaration = new VertexDeclaration
39+ (
40+ new VertexElement(0, VertexElementFormat.Vector3, VertexElementUsage.Position, 0),
41+ new VertexElement(sizeof(float) * 3, VertexElementFormat.Vector3, VertexElementUsage.Normal, 0),
42+ new VertexElement(sizeof(float) * 6, VertexElementFormat.Color, VertexElementUsage.Color, 0)
43+ );
44+ public VertexDeclaration VertexDeclaration { get { return vertexDeclaration; } }
45+ }
46+ struct VertexPositionNormalTextureColor : IVertexType
47+ {
48+
49+ public Vector3 Position;
50+ public Vector3 Normal;
51+ public Vector2 TextureCoordinate;
52+ public Color Color;
53+ public readonly static VertexDeclaration vertexDeclaration = new VertexDeclaration
54+ (
55+ new VertexElement(0, VertexElementFormat.Vector3, VertexElementUsage.Position, 0),
56+ new VertexElement(sizeof(float) * 3, VertexElementFormat.Vector3, VertexElementUsage.Normal, 0),
57+ new VertexElement(sizeof(float) * 6, VertexElementFormat.Vector2, VertexElementUsage.TextureCoordinate, 0),
58+ new VertexElement(sizeof(float) * 8, VertexElementFormat.Color, VertexElementUsage.Color, 0)
59+ );
60+ public VertexDeclaration VertexDeclaration { get { return vertexDeclaration; } }
61+ }
62+}
--- branches/XNA4/MikuMikuDanceXNA/Model/MMDModelPartReader.cs (revision 659)
+++ branches/XNA4/MikuMikuDanceXNA/Model/MMDModelPartReader.cs (revision 660)
@@ -16,11 +16,11 @@
1616 {
1717 //モデルパーツの読み込み
1818 int triangleCount = input.ReadInt32();
19- MMDVertex[] cpuVertices = input.ReadObject<MMDVertex[]>();
19+ MMDVertex[] Vertices = input.ReadObject<MMDVertex[]>();
2020 IndexBuffer indexBuffer = input.ReadObject<IndexBuffer>();
2121
2222 // create the model part from this data
23- MMDModelPart modelPart = new MMDModelPart(triangleCount, cpuVertices, indexBuffer);
23+ MMDModelPart modelPart = MMDModelPart.Create(triangleCount, Vertices, indexBuffer);
2424
2525 // read in the BasicEffect as a shared resource
2626 input.ReadSharedResource<BasicEffect>(fx => modelPart.Effect = fx);
Show on old repository browser