using System;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.DirectX;
using System.ComponentModel;
using Microsoft.DirectX.Direct3D;
using System.IO;
namespace sampleAnimation
{
public class CreateDevice : Form
{
private Device device = null;
private AnimationRootFrame rootframe;
private Vector3 objectCenter;
private float objectRadius;
private float elapsedTime;
private VertexBuffer bufferfloor = null;
private Texture tex = null;
private Material materialfloor ;
private System.ComponentModel.Container components = null;
public CreateDevice()
{
this.components = new System.ComponentModel.Container();
this.ClientSize = new System.Drawing.Size(500, 500);
this.Text = "Sample Animation";
this.Setstyle(Controlstyles.AllPaintingInWmPaint | Controlstyles.Opaque, true);
}
public bool InitializeGraphics()
{
// Die Hardware initialisieren und ein paar Parameter setzen
PresentParameters presentParams = new PresentParameters();
presentParams.Windowed = true;
presentParams.SwapEffect = SwapEffect.Discard;
presentParams.AutoDepthStencilFormat = DepthFormat.D16;
presentParams.EnableAutoDepthStencil = true;
bool canDoHardwareSkinning = true;
Caps hardware = Manager.GetDeviceCaps(0, DeviceType.Hardware);
if (hardware.MaxVertexBlendMatrices >= 4 )
//if (hardware .MaxVertexBlendMatrixIndex >=12)
{
CreateFlags flags = CreateFlags.SoftwareVertexProcessing;
if (hardware.DeviceCaps.SupportsHardwareTransformAndLight)
flags = CreateFlags.HardwareVertexProcessing;
if (hardware.DeviceCaps.SupportsPureDevice)
flags |= CreateFlags.PureDevice;
device = new Device(0, DeviceType.Hardware, this, flags,
presentParams);
bufferfloor = new VertexBuffer(typeof(CustomVertex.PositionTextured),
6, device,0, CustomVertex.PositionTextured.Format, Pool.Default);
bufferfloor.Created += new EventHandler(this.OnVertexBufferCreate);
OnVertexBufferCreate(bufferfloor, null);
loadtexturefloor();
}
else
{
canDoHardwareSkinning = false;
device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);
}
CreateAnimation(@".\manwork.x", presentParams);
device.DeviceReset += new EventHandler(OnDevicesReset);
OnDevicesReset(device, null);
return canDoHardwareSkinning;
}
/// <summary>
/// สร้างพื้นหลัง
private void loadtexturefloor()
{
materialfloor = new Material();
materialfloor.Diffuse = Color.White;
materialfloor.Ambient = Color.White;
device.Material = materialfloor;
tex = TextureLoader.FromFile(device, "ground.bmp");
}
private void OnVertexBufferCreate(object sender, EventArgs e)
{
VertexBuffer buffer = (VertexBuffer)sender;
CustomVertex.PositionTextured[] verts =new CustomVertex.PositionTextured[6];
verts [0] = new CustomVertex.PositionTextured (-1.0f,1.0f,1.0f,0.0f,0.0f);
verts[1]= new CustomVertex.PositionTextured (-1.0f,-1.0f,1.0f,0.0f,1.0f);
verts [2] =new CustomVertex.PositionTextured (1.0f,1.0f,1.0f,1.0f,1.0f);
verts [3]= new CustomVertex.PositionTextured (-1.0f,-1.0f,1.0f,0.0f,1.0f);
verts [4]= new CustomVertex.PositionTextured (1.0f,-1.0f,1.0f,1.0f,1.0f);
verts[5] = new CustomVertex.PositionTextured(1.0f, 1.0f, 1.0f, 1.0f, 0.0f);
bufferfloor.SetData(verts, 0, LockFlags.None);
}
private void DrawBox(float x, float y, float z, Texture t)
{
device.Transform.World = Matrix.Translation(x, y, z);
device.SetTexture(0,t);
device.DrawPrimitives(PrimitiveType.TriangleList, 0, 2);
}
/// </summary>
private void OnDevicesReset(object sender, EventArgs s)
{
Device dev = (Device)sender;
Vector3 vEye = new Vector3(0.9f,0f, 0.8f * objectRadius);
Vector3 vUp = new Vector3(0, 1, 0);
dev.Transform.View = Matrix.LookAtLH( vEye ,objectCenter, vUp);
dev.Transform.View = Matrix.Translation(0f, -300f, 500f);
float aspectRatio = (float)dev.PresentationParameters.BackBufferWidth
/ (float)dev.PresentationParameters.BackBufferHeight;
dev.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4,
aspectRatio, objectRadius / 64.0f, objectRadius * 200.0f);
dev.Lights[0].Type = LightType.Directional;
dev.Lights[0].Direction = new Vector3(0.0f, 0.0f, 1.0f);
dev.Lights[0].Diffuse = Color.White;
//dev.Lights[0].Commit();
dev.Lights[0].Enabled = true;
}
private void CreateAnimation(string file, PresentParameters presentParams)
{
AllocatehierarchyDerived alloc = new AllocatehierarchyDerived(this);
rootframe = Mesh.LoadHierarchyFromFile(file, MeshFlags.Managed, device, alloc, null);
objectRadius = Frame.CalculateBoundingSphere(rootframe.FrameHierarchy, out objectCenter);//
SetupBoneMatrices((FrameDerived)rootframe.FrameHierarchy);
DXUtil.Timer(DirectXTimer.Start);
}
private void SetupBoneMatrices(FrameDerived frame)
{
if (frame.MeshContainer != null)
{
SetupBoneMatrices((MeshContainerDerived)frame.MeshContainer);
}
if (frame.FrameSibling != null)
{
SetupBoneMatrices((FrameDerived)frame.FrameSibling);
}
if (frame.FrameFirstChild != null)
{
SetupBoneMatrices((FrameDerived)frame.FrameFirstChild);
}
}
private void SetupBoneMatrices(MeshContainerDerived mesh)
{
if (mesh.SkinInformation != null)
{
int numBones = mesh.SkinInformation.NumberBones;
FrameDerived[] frmaeMatrices = new FrameDerived[numBones];
for (int i = 0; i < numBones; i++)
{
FrameDerived frame = (FrameDerived)Frame.Find(rootframe.FrameHierarchy, mesh.SkinInformation.GetBoneName(i));
if (frame == null)
throw new ArgumentException();
frmaeMatrices = frame;
}
mesh.setFrames(frmaeMatrices);
}
}
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
PrecessNextFrame();
device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.CornflowerBlue, 1.0f, 0);
device.BeginScene();
///
device.SetStreamSource(0, bufferfloor, 0);
device.VertexFormat = CustomVertex.PositionTextured.Format;
DrawBox(0.0f, 0.0f, 0.0f, tex );
///
DrawFrame((FrameDerived)rootframe.FrameHierarchy);
device.EndScene();
device.Present();
this.Invalidate();
}
public void GeneraeSkinnedMesh(MeshContainerDerived mesh)
{
if (mesh.SkinInformation == null)
throw new ArgumentException();
int numInfl = 0;
BoneCombination[] bones;
MeshData m = mesh.MeshData ;
m.Mesh = mesh.SkinInformation.ConvertToBlendedMesh(m.Mesh, MeshFlags.Managed
| MeshFlags.OptimizeVertexCache, mesh.GetAdjacencyStream(), out numInfl, out bones);
mesh.NuberInfluences = numInfl;
mesh.setBones(bones);
mesh.NumberAttributes = bones.Length;
mesh.MeshData = m;
}
private void PrecessNextFrame()
{
elapsedTime = DXUtil.Timer(DirectXTimer.GetElapsedTime);
Matrix worldMatrix = Matrix.Translation(objectCenter);
device.Transform.World = worldMatrix;
if (rootframe.AnimationController != null)
rootframe.AnimationController.AdvanceTime(elapsedTime, null);
UpdateFrameMatrices((FrameDerived)rootframe.FrameHierarchy, worldMatrix);
}
private void UpdateFrameMatrices(FrameDerived frmae, Matrix parentMatrix)
{
frmae.CombinedTransformationMatrix = frmae.TransformationMatrix * parentMatrix;
if (frmae.FrameSibling != null)
{
UpdateFrameMatrices((FrameDerived)frmae.FrameSibling, parentMatrix);
}
if (frmae.FrameFirstChild != null)
{
UpdateFrameMatrices((FrameDerived)frmae.FrameFirstChild, frmae.CombinedTransformationMatrix);
}
}
private void DrawFrame(FrameDerived frame)
{
MeshContainerDerived mesh = (MeshContainerDerived)frame.MeshContainer;
while (mesh != null)
{
DrawMeshContainer(mesh, frame);
mesh = (MeshContainerDerived)mesh.NextContainer;
}
if (frame.FrameSibling != null)
{
DrawFrame((FrameDerived)frame.FrameSibling);
}
if (frame.FrameFirstChild != null)
{
DrawFrame((FrameDerived)frame.FrameFirstChild);
}
}
private void DrawMeshContainer(MeshContainerDerived mesh, FrameDerived frame)
{
if (mesh.SkinInformation != null)
{
int attribIdPrev = -1;
for (int iattrib = 0; iattrib < mesh.NumberAttributes; iattrib++)
{
int numBlend = 0;
BoneCombination[] bones = mesh.getBones();
for (int i = 0; i < mesh.NuberInfluences; i++)
{
if (bones[iattrib].BoneId != -1)
{
numBlend = i;
}
}
if (device.DeviceCaps.MaxVertexBlendMatrices >= numBlend + 1)
{
Matrix[] offsetmatrices = mesh.Getoffsetmatrices();
FrameDerived[] framematrices = mesh.getFrames();
for (int i = 0; i < mesh.NuberInfluences; i++)
{
int matrixIndex = bones[iattrib].BoneId;
if (matrixIndex != -1)
{
Matrix tempMatrix = offsetmatrices[matrixIndex] *
framematrices[matrixIndex].CombinedTransformationMatrix;
device.Transform.SetWorldMatrixByIndex(i, tempMatrix);
}
}
device.RenderState.VertexBlend = (VertexBlend)numBlend;
if ((attribIdPrev != bones[iattrib].AttributeId) || (attribIdPrev == -1))
{
device.Material = mesh.GetMaterials()[bones[iattrib].AttributeId].Material3D;
device.SetTexture(0, mesh.getTextures()[bones[iattrib].AttributeId]);
attribIdPrev = bones[iattrib].AttributeId;
}
try
{
mesh.MeshData.Mesh.DrawSubset(iattrib);
}
catch(Exception)
{
}
}
}
}
else
{
device.Transform.World = frame.CombinedTransformationMatrix;
ExtendedMaterial[] mtrl = mesh.GetMaterials();
for (int imaterial = 0; imaterial < mtrl.Length; imaterial++)
{
device.Material = mtrl[imaterial].Material3D;
device.SetTexture(0, mesh.getTextures()[imaterial]);
try
{
mesh.MeshData.Mesh.DrawSubset(imaterial);
}
catch (Exception )
{
}
}
}
}
protected override void OnKeyPress(System.Windows.Forms.KeyPressEventArgs e)
{
if ((int)(byte)e.KeyChar == (int)System.Windows.Forms.Keys.Escape)
this.Close(); // Esc wurde gedrckt
}
protected void DisposeGraphics()
{
}
// Einstiegspunkt des Programms
static void Main()
{
using (CreateDevice frm = new CreateDevice())
{
frm.Show();
Console.Write(frm.InitializeGraphics());
if (!frm.InitializeGraphics()) // Direct3D initialisieren
{
// frm.init();
MessageBox.Show("Konnte Hardware nicht initialisieren.");
}
Application.Run(frm);
}
}
}
public class AllocatehierarchyDerived : AllocateHierarchy
{
CreateDevice app = null;
public AllocatehierarchyDerived(CreateDevice parent)
{
app = parent;
}
public override Frame CreateFrame(string name)
{
FrameDerived frame = new FrameDerived();
frame.Name = name;
frame.TransformationMatrix = Matrix.Identity;
frame.CombinedTransformationMatrix = Matrix.Identity;
return frame;
}
public override MeshContainer CreateMeshContainer(string name, MeshData meshData, ExtendedMaterial[] materials, EffectInstance[] effectInstances, GraphicsStream adjacency, SkinInformation skinInfo)
{
if (meshData.Mesh == null)
throw new ArgumentException();
if (meshData.Mesh.VertexFormat == VertexFormats.None)
throw new ArgumentException();
MeshContainerDerived meshs = new MeshContainerDerived();
meshs.Name = name;
int numFaces = meshData.Mesh.NumberFaces;
Device dev = meshData.Mesh.Device;
if ((meshData.Mesh.VertexFormat & VertexFormats.Normal) == 0)
{
Mesh tempMesh = meshData.Mesh.Clone(meshData.Mesh.Options.Value, meshData.Mesh.VertexFormat
| VertexFormats.Normal, dev);
meshData.Mesh = tempMesh;
meshData.Mesh.ComputeNormals();
}
meshs.SetMaterials(materials);
meshs.SetAdjacency(adjacency);
Texture[] meshtextures = new Texture[materials.Length];
for (int i = 0; i < materials.Length; i++)
{
if (materials.TextureFilename != null)
{
meshtextures = TextureLoader.FromFile(dev, @".\"
+ materials.TextureFilename);
}
}
meshs.settextures(meshtextures);
meshs.MeshData = meshData;
if (skinInfo != null)
{
meshs.SkinInformation = skinInfo;
int numbones = skinInfo.NumberBones;
Matrix[] offsetMatrices = new Matrix[numbones];
for (int i = 0; i < numbones; i++)
offsetMatrices = skinInfo.GetBoneOffsetMatrix(i);
meshs.setoffsetMatrices(offsetMatrices);
app.GeneraeSkinnedMesh(meshs );
}
return meshs ;
}
}
public class FrameDerived : Frame
{
private Matrix combined = Matrix.Identity;
public Matrix CombinedTransformationMatrix
{
get { return combined; }
set { combined = value; }
}
}
public class MeshContainerDerived : MeshContainer
{
private Texture[] meshtextures = null;
private int numattr = 0;
private int numInfl = 0;
private BoneCombination[] bones;
private FrameDerived[] framematrices;
private Matrix[] offsetMatrices;
public Texture[] getTextures()
{
return meshtextures;
}
public void settextures(Texture[] textures)
{
meshtextures = textures;
}
public BoneCombination[] getBones()
{
return bones;
}
public void setBones(BoneCombination[] b)
{
bones = b;
}
public FrameDerived[] getFrames()
{
return framematrices;
}
public void setFrames(FrameDerived[] frames)
{
framematrices = frames;
}
public Matrix[] Getoffsetmatrices()
{
return offsetMatrices;
}
public void setoffsetMatrices(Matrix[] matrices)
{
offsetMatrices = matrices;
}
public int NumberAttributes
{
get { return numattr; }
set { numattr = value; }
}
public int NuberInfluences
{
get { return numInfl; }
set { numInfl = value; }
}
}
}
Error C# and DirectX
I Create Animation (.x) combine background Or similar "MultiAnimation" C++
but Error at line device.DrawPrimitives(PrimitiveType.TriangleList, 0, 2);
what is problem?
help me please
thank you
(Edit by jpetrie: Fixing code tags.)
[Edited by - jpetrie on July 30, 2008 2:19:25 AM]
I don't see the code where you create your actual VertexBuffer but it should look something like this
Also I would try rendering your Quad without a VertexBuffer just to see that it's rendering okay. So to do that try
Also just check that your writing to the vertex buffer correctly, setting the stream source and vertex format.
Also just note for the future when you post large code snippets use the [ source ] and [ /source ] around your code (without the spaces).
bufferfloor = new VertexBuffer(device, 2 * 6 * CustomVertex.PositionTextured.StrideSize, Usage.Dynamic | Usage.WriteOnly, CustomVertex.PositionTextured.Format, Pool.Default);
Also I would try rendering your Quad without a VertexBuffer just to see that it's rendering okay. So to do that try
device.DrawUserPrimitives(PrimitiveType.TriangleList, 2, verts);
Also just check that your writing to the vertex buffer correctly, setting the stream source and vertex format.
GraphicsStream gs = bufferfloor.Lock(0, 0, LockFlags.Discard);gs.Write(verts);bufferfloor.Unlock();device.VertexFormat = CustomVertex.PositionTextured.Format;device.SetStreamSource(0, bufferfloor, 0, CustomVertex.PositionTextured.StrideSize);device.DrawPrimitives(PrimitiveType.TriangleList, 0, 2);
Also just note for the future when you post large code snippets use the [ source ] and [ /source ] around your code (without the spaces).
I test code at "Headkaze" answer but when run have Error at line
"device.DrawPrimitives(PrimitiveType.TriangleList, 0, 2);" as before
please help me again
thank you
(Edit by jpetrie: Fixing code tags.)
[Edited by - jpetrie on July 30, 2008 2:58:45 AM]
"device.DrawPrimitives(PrimitiveType.TriangleList, 0, 2);" as before
please help me again
thank you
using System;using System.Drawing;using System.Windows.Forms;using Microsoft.DirectX;using System.ComponentModel;using Microsoft.DirectX.Direct3D;using System.IO;namespace load3d{ public class CreateDevice : Form { private Device device = null; private AnimationRootFrame rootframe; private Vector3 objectCenter; private float objectRadius; private float elapsedTime; private int i = 1; private VertexBuffer bufferfloor = null; private Texture tex = null; private Material materialfloor ; private System.ComponentModel.Container components = null; public CreateDevice() { this.components = new System.ComponentModel.Container(); this.ClientSize = new System.Drawing.Size(800, 800); this.Text = "DirectX Sample Animation"; this.Setstyle(Controlstyles.AllPaintingInWmPaint | Controlstyles.Opaque, true); } public bool InitializeGraphics() { PresentParameters presentParams = new PresentParameters(); presentParams.Windowed = true; presentParams.SwapEffect = SwapEffect.Discard; presentParams.AutoDepthStencilFormat = DepthFormat.D16; presentParams.EnableAutoDepthStencil = true; bool canDoHardwareSkinning = true; Caps hardware = Manager.GetDeviceCaps(0, DeviceType.Hardware); if (hardware.MaxVertexBlendMatrices >= 4 ) { CreateFlags flags = CreateFlags.SoftwareVertexProcessing; if (hardware.DeviceCaps.SupportsHardwareTransformAndLight) flags = CreateFlags.HardwareVertexProcessing; if (hardware.DeviceCaps.SupportsPureDevice) flags |= CreateFlags.PureDevice; device = new Device(0, DeviceType.Hardware, this, flags, presentParams); } else { canDoHardwareSkinning = false; device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams); } CreateAnimation(@".\manwork.x", presentParams); device.DeviceReset += new EventHandler(OnDevicesReset); OnDevicesReset(device, null); return canDoHardwareSkinning; } public void WasteVideoMemory() { VertexBuffer waste = new VertexBuffer(device, 5000, 0, VertexFormats.None, Pool.Default); } private void loadtexturefloor() { materialfloor = new Material(); materialfloor.Diffuse = Color.White; materialfloor.Ambient = Color.White; device.Material = materialfloor; tex = TextureLoader.FromFile(device, "skybox_back.bmp"); } private void SetupCamera() { device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, this.Width / this.Height, 1.0f, 100.0f); device.Transform.View = Matrix.LookAtLH(new Vector3(0, 0,18f), new Vector3(), new Vector3(0, 1, 0)); device.Lights[0].Enabled = true; device.RenderState.Ambient = Color.White ; } private void DrawBox(float yaw,float pitch,float roll, float x, float y, float z, Texture t,CustomVertex.PositionTextured [] vr) { device.Transform.World = Matrix .RotationYawPitchRoll (yaw ,pitch ,roll) * Matrix.Translation(x, y, z); device.SetTexture(0, t); } private void CreateVertexBuffer() { bufferfloor = new VertexBuffer(device, 2 * 6 * CustomVertex.PositionTextured.StrideSize, Usage.Dynamic | Usage.WriteOnly, CustomVertex.PositionTextured.Format, Pool.Default); } protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) { Console.Write("OnPaint\n"); PrecessNextFrame(); device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Blue , 1.0f, 0); SetupCamera(); device.BeginScene(); CreateVertexBuffer(); GraphicsStream gs = bufferfloor.Lock(0, 0, LockFlags.Discard); CustomVertex.PositionTextured[] verts = new CustomVertex.PositionTextured[6]; verts[0] = new CustomVertex.PositionTextured(-10.0f, 10.0f, 1.0f, 0.0f, 0.0f); verts[1] = new CustomVertex.PositionTextured(-10.0f, -10.0f, 1.0f, 0.0f, 1.0f); verts[2] = new CustomVertex.PositionTextured(10.0f, 10.0f, 1.0f, 1.0f, 0.0f); verts[3] = new CustomVertex.PositionTextured(-10.0f, -10.0f, 1.0f, 0.0f, 1.0f); verts[4] = new CustomVertex.PositionTextured(10.0f, -10.0f, 1.0f, 1.0f, 1.0f); verts[5] = new CustomVertex.PositionTextured(10.0f, 10.0f, 1.0f, 1.0f, 0.0f); gs.Write(verts); bufferfloor.Unlock(); device.VertexFormat = CustomVertex.PositionTextured.Format; device.SetStreamSource(0, bufferfloor, 0, CustomVertex.PositionTextured.StrideSize); DrawBox(0,0,0,0.0f, 0.0f, -5.0f, tex,verts); device.DrawUserPrimitives(PrimitiveType.TriangleList, 0, verts ); DrawFrame((FrameDerived)rootframe.FrameHierarchy); device.EndScene(); device.Present(); this.Invalidate(); i++; } private void OnDevicesReset(object sender, EventArgs s) { Device dev = (Device)sender; Vector3 vEye = new Vector3(0.9f,0f, 0.8f * objectRadius); Vector3 vUp = new Vector3(0, 1, 0); dev.Transform.View = Matrix.LookAtLH( vEye ,objectCenter, vUp); dev.Transform.View = Matrix.Translation(0f, -300f, 500f); float aspectRatio = (float)dev.PresentationParameters.BackBufferWidth / (float)dev.PresentationParameters.BackBufferHeight; dev.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, aspectRatio, objectRadius / 64.0f, objectRadius * 200.0f); dev.Lights[0].Type = LightType.Directional; dev.Lights[0].Direction = new Vector3(0.0f, 0.0f, 1.0f); dev.Lights[0].Diffuse = Color.White; dev.Lights[0].Enabled = true; } private void CreateAnimation(string file, PresentParameters presentParams) { AllocatehierarchyDerived alloc = new AllocatehierarchyDerived(this); rootframe = Mesh.LoadHierarchyFromFile(file, MeshFlags.Managed, device, alloc, null); objectRadius = Frame.CalculateBoundingSphere(rootframe.FrameHierarchy, out objectCenter); SetupBoneMatrices((FrameDerived)rootframe.FrameHierarchy); DXUtil.Timer(DirectXTimer.Start); } private void SetupBoneMatrices(FrameDerived frame) { if (frame.MeshContainer != null) { SetupBoneMatrices((MeshContainerDerived)frame.MeshContainer); } if (frame.FrameSibling != null) { SetupBoneMatrices((FrameDerived)frame.FrameSibling); } if (frame.FrameFirstChild != null) { SetupBoneMatrices((FrameDerived)frame.FrameFirstChild); } } private void SetupBoneMatrices(MeshContainerDerived mesh) { if (mesh.SkinInformation != null) { int numBones = mesh.SkinInformation.NumberBones; FrameDerived[] frmaeMatrices = new FrameDerived[numBones]; for (int i = 0; i < numBones; i++) { FrameDerived frame = (FrameDerived)Frame.Find(rootframe.FrameHierarchy, mesh.SkinInformation.GetBoneName(i)); if (frame == null) throw new ArgumentException(); frmaeMatrices = frame; } mesh.setFrames(frmaeMatrices); } } public void GeneraeSkinnedMesh(MeshContainerDerived mesh) { if (mesh.SkinInformation == null) throw new ArgumentException(); int numInfl = 0; BoneCombination[] bones; MeshData m = mesh.MeshData ; m.Mesh = mesh.SkinInformation.ConvertToBlendedMesh(m.Mesh, MeshFlags.Managed | MeshFlags.OptimizeVertexCache, mesh.GetAdjacencyStream(), out numInfl, out bones); mesh.NuberInfluences = numInfl; mesh.setBones(bones); mesh.NumberAttributes = bones.Length; mesh.MeshData = m; } private void PrecessNextFrame() { elapsedTime = DXUtil.Timer(DirectXTimer.GetElapsedTime); Matrix worldMatrix = Matrix.Translation(objectCenter); device.Transform.World = worldMatrix; if (rootframe.AnimationController != null) rootframe.AnimationController.AdvanceTime(elapsedTime, null); UpdateFrameMatrices((FrameDerived)rootframe.FrameHierarchy, worldMatrix); } private void UpdateFrameMatrices(FrameDerived frmae, Matrix parentMatrix) { frmae.CombinedTransformationMatrix = frmae.TransformationMatrix * parentMatrix; if (frmae.FrameSibling != null) { UpdateFrameMatrices((FrameDerived)frmae.FrameSibling, parentMatrix); } if (frmae.FrameFirstChild != null) { UpdateFrameMatrices((FrameDerived)frmae.FrameFirstChild, frmae.CombinedTransformationMatrix); } } private void DrawFrame(FrameDerived frame) { MeshContainerDerived mesh = (MeshContainerDerived)frame.MeshContainer; while (mesh != null) { DrawMeshContainer(mesh, frame); mesh = (MeshContainerDerived)mesh.NextContainer; } if (frame.FrameSibling != null) { DrawFrame((FrameDerived)frame.FrameSibling); } if (frame.FrameFirstChild != null) { DrawFrame((FrameDerived)frame.FrameFirstChild); } } private void DrawMeshContainer(MeshContainerDerived mesh, FrameDerived frame) { if (mesh.SkinInformation != null) { int attribIdPrev = -1; for (int iattrib = 0; iattrib < mesh.NumberAttributes; iattrib++) { int numBlend = 0; BoneCombination[] bones = mesh.getBones(); for (int i = 0; i < mesh.NuberInfluences; i++) { if (bones[iattrib].BoneId != -1) { numBlend = i; } } if (device.DeviceCaps.MaxVertexBlendMatrices >= numBlend + 1) { Matrix[] offsetmatrices = mesh.Getoffsetmatrices(); FrameDerived[] framematrices = mesh.getFrames(); for (int i = 0; i < mesh.NuberInfluences; i++) { int matrixIndex = bones[iattrib].BoneId; if (matrixIndex != -1) { Matrix tempMatrix = offsetmatrices[matrixIndex] * framematrices[matrixIndex].CombinedTransformationMatrix; device.Transform.SetWorldMatrixByIndex(i, tempMatrix); } } device.RenderState.VertexBlend = (VertexBlend)numBlend; if ((attribIdPrev != bones[iattrib].AttributeId) || (attribIdPrev == -1)) { device.Material = mesh.GetMaterials()[bones[iattrib].AttributeId].Material3D; device.SetTexture(0, mesh.getTextures()[bones[iattrib].AttributeId]); attribIdPrev = bones[iattrib].AttributeId; } try { mesh.MeshData.Mesh.DrawSubset(iattrib); } catch(Exception) { } } } } else { device.Transform.World = frame.CombinedTransformationMatrix; ExtendedMaterial[] mtrl = mesh.GetMaterials(); for (int imaterial = 0; imaterial < mtrl.Length; imaterial++) { device.Material = mtrl[imaterial].Material3D; device.SetTexture(0, mesh.getTextures()[imaterial]); try { mesh.MeshData.Mesh.DrawSubset(imaterial); } catch (Exception ) { } } } } protected override void OnKeyPress(System.Windows.Forms.KeyPressEventArgs e) { if ((int)(byte)e.KeyChar == (int)System.Windows.Forms.Keys.Escape) this.Close(); } protected void DisposeGraphics() { } static void Main() { using (CreateDevice frm = new CreateDevice()) { frm.Show(); frm.InitializeGraphics (); Application.Run(frm); } } } public class AllocatehierarchyDerived : AllocateHierarchy { CreateDevice app = null; public AllocatehierarchyDerived(CreateDevice parent) { app = parent; } public override Frame CreateFrame(string name) { FrameDerived frame = new FrameDerived(); frame.Name = name; frame.TransformationMatrix = Matrix.Identity; frame.CombinedTransformationMatrix = Matrix.Identity; return frame;//ส่งให้ funcation Loadhierarchyfromfils } public override MeshContainer CreateMeshContainer(string name, MeshData meshData, ExtendedMaterial[] materials, EffectInstance[] effectInstances, GraphicsStream adjacency, SkinInformation skinInfo) { if (meshData.Mesh == null) throw new ArgumentException(); if (meshData.Mesh.VertexFormat == VertexFormats.None) throw new ArgumentException(); MeshContainerDerived meshs = new MeshContainerDerived(); meshs.Name = name; int numFaces = meshData.Mesh.NumberFaces; Device dev = meshData.Mesh.Device; if ((meshData.Mesh.VertexFormat & VertexFormats.Normal) == 0) { Mesh tempMesh = meshData.Mesh.Clone(meshData.Mesh.Options.Value, meshData.Mesh.VertexFormat | VertexFormats.Normal, dev); meshData.Mesh = tempMesh; meshData.Mesh.ComputeNormals(); } meshs.SetMaterials(materials); meshs.SetAdjacency(adjacency); Texture[] meshtextures = new Texture[materials.Length]; for (int i = 0; i < materials.Length; i++) { if (materials.TextureFilename != null) { meshtextures = TextureLoader.FromFile(dev, @".\" + materials.TextureFilename); } } meshs.settextures(meshtextures); meshs.MeshData = meshData; if (skinInfo != null) { meshs.SkinInformation = skinInfo; int numbones = skinInfo.NumberBones; Matrix[] offsetMatrices = new Matrix[numbones]; for (int i = 0; i < numbones; i++) offsetMatrices = skinInfo.GetBoneOffsetMatrix(i); meshs.setoffsetMatrices(offsetMatrices); app.GeneraeSkinnedMesh(meshs ); } return meshs ; } } public class FrameDerived : Frame { private Matrix combined = Matrix.Identity; public Matrix CombinedTransformationMatrix { get { return combined; } set { combined = value; } } } public class MeshContainerDerived : MeshContainer { private Texture[] meshtextures = null; private int numattr = 0; private int numInfl = 0; private BoneCombination[] bones; private FrameDerived[] framematrices; private Matrix[] offsetMatrices; public Texture[] getTextures() { return meshtextures; } public void settextures(Texture[] textures) { meshtextures = textures; } public BoneCombination[] getBones() { return bones; } public void setBones(BoneCombination[] b) { bones = b; } public FrameDerived[] getFrames() { return framematrices; } public void setFrames(FrameDerived[] frames) { framematrices = frames; } public Matrix[] Getoffsetmatrices() { return offsetMatrices; } public void setoffsetMatrices(Matrix[] matrices) { offsetMatrices = matrices; } public int NumberAttributes { get { return numattr; } set { numattr = value; } } public int NuberInfluences { get { return numInfl; } set { numInfl = value; } } } }
(Edit by jpetrie: Fixing code tags.)
[Edited by - jpetrie on July 30, 2008 2:58:45 AM]
Quote:Original post by s_madooI don't know, you tell us. What error do you get? What do the Debug runtimes tell you?
I Create Animation (.x) combine background Or similar "MultiAnimation" C++
but Error at line device.DrawPrimitives(PrimitiveType.TriangleList, 0, 2);
what is problem?
I am sory
I start create game . I want modify game have animation(".x") and have background(".bmp").but I modify have Errr line "device.DrawPrimitives(PrimitiveType.TriangleList, 0, 2);".
What error code cause?
you help Edit bug
Please Help me again
I start create game . I want modify game have animation(".x") and have background(".bmp").but I modify have Errr line "device.DrawPrimitives(PrimitiveType.TriangleList, 0, 2);".
What error code cause?
you help Edit bug
Please Help me again
How exactly is anyone supposed to help you do anything when you've basically posted a huge wall of code and said "plz halp!".
Here's a few hints:
#1: Do some homework, google around, search for the error, oh, and for FUNTS sake, FIND OUT WHERE THE ERROR IS IN YOUR CODE.
#2: When you ask for help, post ONLY the relevant sections, and then state the errors you're getting. Be sure to read and understand the error first.
#3: Use [ source ] blocks for large chunks of code.
[edit]
Someone flagged this funting post because apparently I used the funting word funt one too funting many times, so I've edited out the funting word from the post for your funting enjoyment.
[/edit]
[Edited by - Washu on July 30, 2008 2:36:16 AM]
Here's a few hints:
#1: Do some homework, google around, search for the error, oh, and for FUNTS sake, FIND OUT WHERE THE ERROR IS IN YOUR CODE.
#2: When you ask for help, post ONLY the relevant sections, and then state the errors you're getting. Be sure to read and understand the error first.
#3: Use [ source ] blocks for large chunks of code.
[edit]
Someone flagged this funting post because apparently I used the funting word funt one too funting many times, so I've edited out the funting word from the post for your funting enjoyment.
[/edit]
[Edited by - Washu on July 30, 2008 2:36:16 AM]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement