Xna load .x file




















If you do this you can use relative paths to access the content. On Xbox and Windows Phone you can only use the content pipeline.

Everything gets serialized to XNB files and only the those are available on those machines. I don't think there is a special case for audio there and besides there is no way for the users to mod the files on Windows Phone and Xbox anyway. However on the PC you can use all the. NET classes and more. So you can just load raw xml files using the classes from the System. XML namespaces.

For example by using XmlDocument. When the user get into the game next time check if exist that copy or modification, use it else load the default raw data through content load. Sign up to join this community. The best answers are voted up and rise to the top. Stack Overflow for Teams — Collaborate and share knowledge with a private group.

Create a free Team What is Teams? Learn more. How do I use Content. Load with raw XML files? Ask Question. Asked 9 years, 2 months ago.

Active 8 years, 8 months ago. Viewed 3k times. The Material class holds a colour and the Bitmap texture. Wait a minute! How are we going to draw the model? We could use the Model Object and add a Draw method. But doing this will cause some problems down the road: when we get into mesh animation, we will calculate a new set of vertices from the original mesh vertices at each animation frame.

If we want to display the same model but with different poses say like in a mob of monsters , we would have to calculate the model mesh for each monster. Instead we will create an Object3D class which will be used to perform all calculations on a model mesh. This Object3D class will be initiated with a Model class and will contain the methods to draw and calculate the bounding box of the mesh.

Why don't we have a Load method within the Model object? There is a simple answer. There are many 3D-model file formats. We would need to implement a load method for each existing file format plus a function to get the correct loading function from the file extension.

This would transform our code into some ungainly spaghetti. We will use a loading interface for ease of implementing future loading functionality. From this loading interface, we will derive our X File loading class. Implementation At last! We can now begin coding. But before diving into the implementation of our design, I will quickly describe the framework into which our code will be embedded. You can find the framework in the file Sample0. Quick description of the framework file Sample0.

All the screen manipulations are encapsulated within a Screen object. This Screen object is also responsible for Font manipulation and texture loading and registration under OpenGL. The Sample0 example shows how the OpenGL logo texture is loaded and selected before being displayed on the screen.

There is also a timer class used to calculate the elapsed time since the last call. This class is based on the Win32 function GetTickCount. Code modification Let's first look at the code in the file Sample1. Parsing the file file Sample1. This is a template interface with two protected methods to help users to convert text to floating point numbers and to remove all occurrences of a character from a string.

This is what happens in pseudo-code: Open the file Check the file Header Grab a reference to the Model Object to fill in Enter the main processing loop: While we have not reached the end of file Read the block name ProcessBlock If the block name is recognised, process it. Else avoid the block AvoidTemplate The file Header is checked by comparing the value read from the file with macros defined in the file XfileStructs.

These macros are important since they can also be used to process binary files. If a valid character is detected, this function reads in the string until it finds a space, then calls the utility function Block. AvoidTemplate: This function will avoid all the data enclosed between a matched pair of braces. It consumes an opening brace character then checks each successive character. If it finds another opening brace character, this function will call itself recursively.

This function returns whenever it finds a closing brace. These utility functions are invaluable to process a text X File since they help us narrow down the blocks we want to process. If a block is contained within another one like in the Frame structure, it will suffice to duplicate the processing loop inside a specialised processing function for the frame structure see the function ProcessBone void.

We found a mesh! Now we have to process it. This is the task of the specialised function ProcessMesh. Here is what happens in pseudo-code: Create an empty Mesh object and initialise it. Read in the name of the mesh. If there is no name, assign a name to that mesh. Read in the number of vertices. Load the list of vertices into the Mesh object. Read in the number of faces Load the list of faces into the Mesh object.

Enter the mesh processing loop: While we have not reached the end of the block Read the block name ProcessBlock If the block name is recognised, process it. Why do we need a mesh name? The X file format either declares the mesh or only references the mesh name within the block that is concerned by it. To be able to trace what happens and check that the mesh is correctly associated, we need a unique mesh name. If there are no names, we need to create a unique name this is done by the utility function SetUID.

Then we enter a loop to process all embedded blocks. The Texture Coordinates block is very simple to process: we read in the number of texture coordinates, and then we load in the list of texture coordinates. The block is processed.

The Mesh Normal Vectors block isn't any more difficult. We read in the number of vectors, and then we load in the list of vectors. Next we load in the list of vector indices per face: this gives us the vertex normals for each face allowing for correct illumination of the model.

Material list blocks are a little trickier. Here is the pseudo-code: Read in the number of Materials used by that mesh. Read in the material index for each face Enter the material list processing loop: While we have not reached the end of the block Read the block name ProcessBlock If the block name is recognised, process it.

Else avoid the block AvoidTemplate All that is left is to process each Material description block within the Material list. Here we go: Read in the face colour. Read in the emissive power. Read in the specular colour. Read in the emissive colour. Enter the material description processing loop While we have not reached the end of the block Read the block name ProcessBlock If the block is TextureFileName, we read in the bitmap name.

Else avoid the block AvoidTemplate Once the material is processed, we add it to the mesh's list of materials. And that's it! All the meshes are loaded into the model object. We can't yet display the meshes loaded within the model. First, we want to concatenate the meshes because: This simplifies the mesh maintenance only one material list, one normal vectors list and one list of vertices and faces. This simplifies the drawing step further down the line: we remove a loop through a list of meshes and we only draw a single vertex array.

If you look closely at the Mesh block parsing code, you see at the beginning the initialisation of a series of values for the mesh: these values are the sum of the previous meshes indexes number of vertices, number of faces, These values will be used: To deduce the final size of the concatenated mesh. To increment all index references by their starting values to have a correctly displayed mesh.

Now let's have a look at the pseudo-code: Create a new mesh and retrieve its dimension from the last mesh in the list. Check the new mesh dimensions and resolve all discrepancies.

Create all the new mesh arrays. Process each mesh from the model list For each mesh increment the index references. Copy each mesh data into the new mesh. Move each mesh material into the new mesh material list.

Delete the model mesh list. Add to the model mesh list the new concatenated one. When we calculate the new mesh dimensions, we need to take care of differences between mesh descriptions. One mesh may use textures and thus have texture coordinates while another may just be coloured and have no texture coordinates.

To solve that problem, we duplicate the vertex array size to initialise the texture coordinates array. If we didn't do that, the face list would be divided between indexed faces with colour information and indexed faces with texture coordinates. Now that we have concatenated our meshes, there is one step left: we need to create subsets. Let me explain: we have created a mesh with multiple materials. We want to divide our array of faces into a list of faces for each material used.

The aim is to have only one call to set a material before drawing our mesh subset. The code is very straightforward: For each Material in the material list Count the number of occurrences of that material in the FaceMaterial list Initialise the subset For each occurrence of that material in the face material list copy the face data to the subset Add the subset to the list of subsets 1.

Displaying the result At last, we have parsed our X File, we have concatenated our Meshes and our Model Object is ready for display.

Only one part of our design is left for implementation: the Object3D class that will be in charge of all the calculations based on the original mesh. Let's look at the file Sample1. If the Model was successfully loaded, we concatenate the meshes. We load up into OpenGL all bitmap textures declared within the Meshes material list. Now we enter the meat of our subject: we initialise an instance of Object3D with our loaded Model. This initialisation keeps a pointer to the Model, gets a pointer to the first mesh of the Model Mesh list and initialises an empty vertex array with the same size as the Model Vertex Array.

Last but not least we compute the bounding box coordinates and deduce the centre of the bounding sphere. Let's display our Object3D! First, we calculate our camera position with the centre of the bounding sphere, and then we call the draw method with our Screen object as a parameter. This draw method will parse the mesh material and subset lists. It will set up each material and draw the corresponding subset until there are no more materials to process.

In the Idle function, we clear the Object3D vertex array and call back the Update function. What is the class that one assumes that do that? Thanks again. Check out the Mesh SimpleMesh? Heads up on using the. Texture2D [] Textures remove the Can put the source code? So interesting.

One more thing. Working with 2D Sprites, anyone knows how I check collision between two or more sprites? There ara ways to inspect individual pixels on screen? And how I can draw into the screen directly, pixel by pixel?

Sunday, September 3, AM.



0コメント

  • 1000 / 1000