Jump to content
  • Advertisement
Sign in to follow this  
s_madoo

help me direct x and C#

This topic is 3623 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 at ---> device.DrawPrimitives(PrimitiveType.TriangleList, 0, 2); help me please thank you

Share this post


Link to post
Share on other sites
Advertisement
1. What's your question? What's the error you're getting? What have you done to address the problem? I hate to sound like a jerk, but it's very hard for anybody to help you if you just post a bunch of code and tell us to fix it. We need information in order to help you.

2. Please use the source tags or the code tags when posting code. You can see how to use these in the FAQ.

3. Questions specific to DirectX are best asked in the DirectX Forum.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!