国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

DirectX9 3D 快速上手 7

2024-07-21 02:24:03
字體:
來源:轉載
供稿:網友
國內最大的酷站演示中心!




這里我想繼續寫點和mesh有關的東西,畢竟我們可能還需要對它有很多別的要求。在3d游戲的實際運用中,一般來說都是運用低多邊形模型,簡稱低模。這樣才能有更加好的速度來運行游戲,恰好dx中有提供給我們這樣的函數讓我們來控制讀入的mesh的復雜程度。



public void weldvertices (



microsoft.directx.direct3d.weldepsilonsflags flags ,//標志



    microsoft.directx.direct3d.weldepsilons epsilons ,



    microsoft.directx.direct3d.graphicsstream adjacencyin ,



    microsoft.directx.direct3d.graphicsstream adjacencyout ,



    out int[] faceremap ,



    microsoft.directx.direct3d.graphicsstream vertexremap )



這個方法能實現簡化模型的目的,前2個參數用來確定怎么簡化模型,



第一個標志一共包括以下幾個:



member



value



description



donotsplit



8



instructs the weld to allow vertices to be modified only, not removed. this flag is valid only if weldpartialmatches is set. it is useful to modify vertices so that they are equal, but not to allow vertices to be removed.



只有當weldpartialmatches參數指定時才能生效,不允許分離定點



donotremovevertices



4



instructs the weld to allow vertices to be modified only, not removed. this flag is valid only if weldpartialmatches is set. it is useful to modify vertices to be equal, but not to allow vertices to be removed.



只有當weldpartialmatches參數指定時才能生效,不能移除定點,只能修改



weldpartialmatches



2



if a given vertex component is within epsilon, instructs the weld to modify partially matched vertices so that both components are equal. if all components are equal, one of the vertices is removed.



修改符合在weldepsilons結構中給定的頂點的條件



weldall



1



welds all vertices that are marked by adjacency as being overlapping.



焊接所有的adjacency指定的定點



例如我們可以這樣簡單的調用這個方法



mesh.weldvertices(weldepsilonsflags.weldall, new weldepsilons(), null, null);

當然 前提是你必須在這之前對變量mesh進行付值。

到程序里面看看,效果還是不錯的,已經簡化了很多頂點。


 


或許你還覺得不夠簡化,沒關系這里我們還可以把復雜的模型拆成小塊,用下面這個函數:

public static microsoft.directx.direct3d.mesh[]
split
(



mesh mesh , //要拆分的mesh



int[] adjacencyin ,



system.int32 maxsize ,// 拆分后新的mesh的最大頂點數量



meshflags options , //標志



    out graphicsstream adjacencyarrayout , //這三個out 參數返回新mesh的一些信息



    out graphicsstream faceremaparrayout ,



    out graphicsstream vertremaparrayout )




 


我們也可以使用簡單的重載版本的函數。



在拆分前,我們需要建立保存拆分后的各個mesh的容器,數組不錯。



我們這么寫:



mesh  splitmeshes[]=meshes = mesh.split(mesh, null, 500, mesh.options.value);




 


這樣我們就把mesh根據頂點的樹木分成了很多個部分



在我們這個事例程序里面,通過控制對數組的遍歷來實現對所有拆分出來的mesh的遍歷。



完整代碼如下:



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 chapter7code



{



     /// <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;




 


        // split information



        private mesh[] meshes = null;



        private bool drawsplit = false;



        private bool drawallsplit = false;



        private int index = 0;



        private int lastindextick = system.environment.tickcount;




 


        /// <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");




 


            meshes = mesh.split(mesh, null, 10, mesh.options.value);



        }




 


        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[i] = mtrl[i].material3d;



                    if ((mtrl[i].texturefilename != null) && (mtrl[i].texturefilename != string.empty))



                    {



                        // we have a texture, try to load it



                        meshtextures[i] = textureloader.fromfile(device, @"../../" + mtrl[i].texturefilename);



                    }



                }



            }



        }




 


        private void setupcamera()



        {



            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.fillmode = fillmode.wireframe;



            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].update();



            device.lights[0].enabled = true;




 


        }




 


        protected override void onpaint(system.windows.forms.painteventargs e)



        {



            device.clear(clearflags.target | clearflags.zbuffer, color.black, 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.01f;




 


        if ((system.environment.tickcount - lastindextick) > 500)



        {



            index++;



            if (index >= meshes.length)



                index = 0;




 


            lastindextick = system.environment.tickcount;



        }



        device.transform.world = matrix.rotationyawpitchroll(yaw, pitch, roll) * matrix.translation(x, y, z);



        for (int i = 0; i < meshmaterials.length; i++)



        {



            device.material = meshmaterials[i];



            device.settexture(0, meshtextures[i]);



            if (drawsplit)



            {



                if (drawallsplit)



                {



                    foreach(mesh m in meshes)



                        m.drawsubset(i);



                }



                else



                {



                    meshes[index].drawsubset(i);



                }



            }



            else



            {



                mesh.drawsubset(i);



            }



        }



        }



        protected override void onkeypress(keypresseventargs e)



        {



            if (e.keychar == ' ')



            {



                drawsplit = !drawsplit;



            }



            else if (e.keychar == 'm')



            {



                drawallsplit = !drawallsplit;



            }



        }




 


        /// <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);



            }



        }



     }



}



當然我們這里并不能任意的控制模型的細節程度,明天要期中答辯,突然發現自己的周記沒寫完,所以今天先草草結束,細節控制的下一次我們再講吧。





 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 瓦房店市| 出国| 卢龙县| 扎赉特旗| 巫溪县| 五莲县| 青阳县| 资讯 | 元谋县| 洪泽县| 珲春市| 洛浦县| 余姚市| 新津县| 兴化市| 公主岭市| 若尔盖县| 黑河市| 合作市| 灵台县| 靖远县| 凤城市| 甘洛县| 苗栗县| 永定县| 甘泉县| 华坪县| 温泉县| 通道| 安康市| 新郑市| 济宁市| 定结县| 绥阳县| 邵东县| 双城市| 新密市| 子长县| 开原市| 抚顺县| 德庆县|