游戏开发论坛

 找回密码
 立即注册
搜索
查看: 8676|回复: 3

开源:在程序中加载解析3DS文件,显示三维模型(c# MDX),

[复制链接]

1

主题

2

帖子

0

积分

新手上路

Rank: 1

积分
0
发表于 2007-5-23 16:10:00 | 显示全部楼层 |阅读模式
把3ds转化为x文件后有很多问题(试了好多转换工具都不行,如果你有好工具别忘了告诉我),因此决心直接解析3DS文件然后用mesh显示。

解析3DS文件网上有好多代码,但我就只找到一个是C#写的,C#的太多资源仍然稀缺啊
http://www.nabble.com/3DS-File-loader-t1663534.html

它是用OpenGL显示的,我还得把它移植到MDX上来。
(C++写的解析显示3DS的代码,目前我只找到了几个opengl的,没找到directx的,如果你知道不要忘了告诉我啊)
还有许多细节上的问题…………

附件是全部的源代码。
当前的显示还有一些问题,有一些三角形显示不出来,又有一些三角形是多余的,可是我的顶点和索引解析不会有问题呀,郁闷,期待高手帮忙解决此问题。

附件中的代码仅Form1.cs是我写的,其他的是来自(http://www.nabble.com/3DS-File-loader-t1663534.html),仅有少许修改。

Form1.cs

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace ThreeD
{
        /// <summary>
        /// Summary description for Form1.
        /// </summary>
        public class Form1 : System.Windows.Forms.Form
        {
        private Device device = null;
        private Mesh mesh = null;
        private Material[] meshMaterials;
        private Texture[] meshTextures;

                /// <summary>
                /// Required designer variable.
                /// </summary>
                private System.ComponentModel.Container components = null;
        private float angle = 0.0f;

                public Form1()
                {
                        //
                        // Required for Windows Form Designer support
                        //
                        InitializeComponent();

            this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);
                }

        /// <summary>
        /// We will initialize our graphics device here
        /// </summary>
        public void InitializeGraphics()
        {
            // Set our presentation parameters
            PresentParameters presentParams = new PresentParameters();

            presentParams.Windowed = true;
            presentParams.SwapEffect = SwapEffect.Discard;
            presentParams.AutoDepthStencilFormat = DepthFormat.D16;
            presentParams.EnableAutoDepthStencil = true;

            // Create our device
            device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);

            // Load our mesh
            //LoadMesh(@"..\..\tiny.x");
            //LoadD3S(@"E:\myprog\MeshFile\tpot.3ds");
            LoadD3S(@"E:\myprog\box.3ds");
        }

        private void LoadMesh(string file)
        {
            ExtendedMaterial[] mtrl;

            // Load our mesh
            mesh = Mesh.FromFile(file, MeshFlags.Managed, device, out mtrl);

            // If we have any materials, store them
            if ((mtrl != null) && (mtrl.Length > 0))
            {
                meshMaterials = new Material[mtrl.Length];
                meshTextures = new Texture[mtrl.Length];

                // Store each material and texture
                for (int i = 0; i < mtrl.Length; i++)
                {
                    meshMaterials = mtrl.Material3D;
                    if ((mtrl.TextureFilename != null) && (mtrl.TextureFilename != string.Empty))
                    {
                        // We have a texture, try to load it
                        meshTextures = TextureLoader.FromFile(device, @"..\..\" + mtrl.TextureFilename);
                    }
                }
            }
        }
        private void LoadD3S(string file)
        {
            
            /*
             * mesh = Mesh.FromFile(file, MeshFlags.Managed, device, out mtrl);

            // If we have any materials, store them
            if ((mtrl != null) && (mtrl.Length > 0))
            {
                meshMaterials = new Material[mtrl.Length];
                meshTextures = new Texture[mtrl.Length];

                // Store each material and texture
                for (int i = 0; i < mtrl.Length; i++)
                {
                    meshMaterials = mtrl.Material3D;
                    if ((mtrl.TextureFilename != null) && (mtrl.TextureFilename != string.Empty))
                    {
                        // We have a texture, try to load it
                        meshTextures = TextureLoader.FromFile(device, @"..\..\" + mtrl.TextureFilename);
                    }
                }
            }*/

            ExtendedMaterial[] mtrl;
            Model model = new ThreeDSFile(file).ThreeDSModel;

            Entity e = model.Entities[0];
            int numberVerts = e.vertices.Length;   
            mesh = new Mesh(numberVerts * 3, numberVerts, MeshFlags.Managed,
                     CustomVertex.PositionNormal.Format, device);


            using (IndexBuffer ib = mesh.IndexBuffer)
            {
                ib.SetData(e.indices, 0, LockFlags.None);
            }
            using (VertexBuffer vb = mesh.VertexBuffer)
            {
                GraphicsStream data = vb.Lock(0, 0, LockFlags.None);
                //for(int i2=0;i2<e.vertices.Length;i2++)
                for(int i2=0;i2<numberVerts;i2++)
                {
                    data.Write
                        (new CustomVertex.PositionNormal
                        (
                        (float)e.vertices[i2].X,(float)e.vertices[i2].Y, (float)e.vertices[i2].Z,
                        (float)e.normals[i2].X, (float)e.normals[i2].Y, (float)e.normals[i2].Z
                        )
                        );

                }
                vb.Unlock();
            }

            
            meshMaterials = new Material[1];
            meshTextures = new Texture[1];
            /*meshMaterials[0].Ambient = e.material.Ambient;
            meshMaterials[0].Diffuse = e.material.Diffuse;
            meshMaterials[0].Emissive = e.material.Shininess;
            meshMaterials[0].Specular = e.material.Specular;
             */
            meshMaterials[0].Ambient = Color.FromArgb((int)e.material.Ambient[0], (int)e.material.Ambient[1], (int)e.material.Ambient[2]);
            meshMaterials[0].Diffuse = Color.FromArgb((int)e.material.Diffuse[0], (int)e.material.Diffuse[1], (int)e.material.Diffuse[2]);
            //meshMaterials[0].Emissive = e.material.Shininess;
            meshMaterials[0].Specular = Color.FromArgb((int)e.material.Specular[0], (int)e.material.Specular[1], (int)e.material.Specular[2]);
            
            
            // Load our mesh
            /*Model model = new ThreeDSFile(file).ThreeDSModel;
            for (int i = 0; i < model.Entities.Count; i++)
            {
                Entity e = model.Entities;
                for(int i2=0;i2<e.indices.Length;i2++)
                {
                    e.indices[i2].vertex1;
                }
            };
            model.Entities.
             * */

            

            
        }

        private void SetupCamera()
        {
            device.RenderState.CullMode = Cull.None;
            device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, this.Width / this.Height, 1.0f, 10000.0f);
            device.Transform.View = Matrix.LookAtLH(new Vector3(0,0, 580.0f), new Vector3(), new Vector3(0,1,0));
            //device.RenderState.Ambient = Color.DarkBlue;
            device.Lights[0].Type = LightType.Directional;
            device.Lights[0].Diffuse = Color.White;
            device.Lights[0].Direction = new Vector3(0, -1, -1);
            //device.Lights[0].Commit();
            device.Lights[0].Enabled = true;

        }

        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.CornflowerBlue, 1.0f, 0);

            SetupCamera();

            device.BeginScene();

            // Draw our Mesh
            DrawMesh(angle / (float)Math.PI, angle / (float)Math.PI * 2.0f, angle / (float)Math.PI / 4.0f, 0.0f, 0.0f, 0.0f);

            device.EndScene();

            device.Present();

            this.Invalidate();
        }

        private void DrawMesh(float yaw, float pitch, float roll, float x, float y, float z)
        {
            angle += 0.1f;

            device.Transform.World = Matrix.RotationYawPitchRoll(yaw, pitch, roll) * Matrix.Translation(x, y, z);
            
            for (int i = 0; i < meshMaterials.Length; i++)
            {
                Material boxMaterial = new Material();
                boxMaterial.Ambient = Color.White;
                boxMaterial.Diffuse = Color.White;
                device.Material = boxMaterial;
                //device.Material = meshMaterials;
                //device.SetTexture(0, meshTextures);
                mesh.DrawSubset(i);
            }
        }

        /// <summary>
                /// Clean up any resources being used.
                /// </summary>
                protected override void Dispose( bool disposing )
                {
                        if( disposing )
                        {
                                if (components != null)
                                {
                                        components.Dispose();
                                }
                        }
                        base.Dispose( disposing );
                }

                #region Windows Form Designer generated code
                /// <summary>
                /// Required method for Designer support - do not modify
                /// the contents of this method with the code editor.
                /// </summary>
                private void InitializeComponent()
                {
                        this.components = new System.ComponentModel.Container();
                        this.Size = new Size(800,600);
                        this.Text = "Form1";
                }
                #endregion

                /// <summary>
                /// The main entry point for the application.
                /// </summary>
        static void Main()
        {
            using (Form1 frm = new Form1())
            {
                // Show our form and initialize our graphics engine
                frm.Show();
                frm.InitializeGraphics();
                Application.Run(frm);
            }
        }
        }
}

sf_2007523161027.rar

45.73 KB, 下载次数:

1

主题

2

帖子

0

积分

新手上路

Rank: 1

积分
0
 楼主| 发表于 2007-5-23 16:12:00 | 显示全部楼层

Re:开源:在程序中加载解析3DS文件,显示三维模型(c# MDX

// Load our mesh
            //LoadMesh(@"..\..\tiny.x");
            //LoadD3S(@"E:\myprog\MeshFile\tpot.3ds");
            LoadD3S(@"E:\myprog\box.3ds");
象这样的地方要替换成你自己的文件路径

0

主题

11

帖子

11

积分

新手上路

Rank: 1

积分
11
发表于 2007-12-20 12:52:00 | 显示全部楼层

Re:开源:在程序中加载解析3DS文件,显示三维模型(c# MDX

推荐使用转换工具Deep Exploration.到网上搜个最新的破解版就好了.

2

主题

5

帖子

9

积分

新手上路

Rank: 1

积分
9
发表于 2009-11-3 23:01:00 | 显示全部楼层

Re:开源:在程序中加载解析3DS文件,显示三维模型(c# MDX

怎么我DEVICE.
.不出东西出来丫?
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2025-6-18 08:36

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表