Load Model in xNa

Source: Internet
Author: User

Generally, for a 3D presentation system, the models are completed in advance in software such as 3dmax, and then loaded to the system for presentation. XNa supports many formats of models, but it is connected to fbx files with 3dmax.

The underlying d3d of xNa, so the rendering essence of the model is also d3d rendering. In the new d3d version, similar to the C-language moral GPU language has emerged. Of course, this is mainly due to the development of graphics technology.

Currently, there are three main GPU languages: glsl for OpenGL, CG for NVIDIA, and HLSL for Microsoft Based on d3d. Among them, CG and HLSL are a product of NVIDIA's cooperation with Microsoft, but finally, for some reason, they part ways. NVIDIA is CG and Microsoft is HLSL, but their usage and syntax are similar.CodeCan run.

If we use xNa, it is better to support HLSL like other Microsoft technologies. I haven't found an example of xNa supporting GC. I don't think so. Because I have read an articleArticleComparing these three GPU languages, he said that he has no reason not to use GC. CG supports OpenGL and d3d at the same time. Since GC is so good, all the xNa code I have read uses HLSL, which may indicate that xNa does not support GC.

The essence of d3d rendering is to select and render the triangle planes one by one. More and more triangle planes constitute the triangle,CubeBody, cylinder, sphere, and more complex terrain, trees, people, animals, etc. The final rendering code is the GPU code, and then some effects such as illumination and texture are made when these triangles are selected, finally presenting a vivid 3D scene.

XNa provides the model class and common 3D rendering class. The 3D rendering class encapsulates GPU Code, allowing developers to quickly and conveniently render models rather than write GPU code. However, xNa provides limited functions. If you want to achieve refraction, reflection, and highlight effects, you need to write GPU Code independently.

Next, let's take a look at the definition in xNa.

The Code is as follows:

Model mymodel = This . Content. Load <model> ( @"  Content/models/Tank  " );
Foreach (Modelmesh mymesh In Mymodel. Meshes)
{
Matrix myworld = mymesh. parentbone. Transform * matrix. createscale ( 0.01f );
Foreach (Modelmeshpart mymodelmeshpart In Mymesh. meshparts)
{
Basiceffect mybasiceffect = mymodelmeshpart. Effect As Basiceffect;
Mybasiceffect. World = myworld;
Mybasiceffect. view = This . Activesence. Camera. view;
Mybasiceffect. Projection = This . Activesence. Camera. projection;
Mybasiceffect. textureenabled = True ;
Mybasiceffect. currenttechnique. Passes [ 0 ]. Apply ();
This . Graphicsdevice. Indices = mymodelmeshpart. indexbuffer;
This . Graphicsdevice. setvertexbuffer (mymodelmeshpart. vertexbuffer );
This . Graphicsdevice. drawindexedprimitives (primitivetype. trianglelist
, Mymodelmeshpart. vertexoffset, 0 , Mymodelmeshpart. numvertices
, Mymodelmeshpart. startindex, mymodelmeshpart. primitivecount );
}
}

 

Here, model is the class defined for the model in xNa. Basiceffect is a rendering class customized by xNa. It encapsulates General GPU rendering.

The running effect is as follows:

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.