Processing Model-Scaling Model

Source: Internet
Author: User
Problem

When loading a model from a disk, it is often too large or too small. You want to scale the model to the defined size.

Solution

First, you need to define a global ball for the model. The previous tutorial has explained this. Now that you know the ball, you can know the current size of the model. From this dimension, you can see how much or how small the model is to be enlarged. You can also store this scaling operation in the root bone matrix, so that the scaling will be applied to all bone matrices in the model (see tutorial 4-9 ).

Working Principle

Generally, the model you use is made by different tools or downloaded from the Internet. You cannot specify the model size. So it would be great if you could scale the model to the desired size.

The followingCodeCalculate how much the model needs to be scaled, which needs to be stored in the global sphere in the model tag attribute (see the previous tutorial ).

 
Private matrix [] autoscale (model, float requestedsize) {boundingsphere bsphere = (boundingsphere) model. tag; float originalsize = bsphere. radius * 2; float scalingfactor = requestedsize/originalsize; model. root. transform = model. root. transform * matrix. createscale (scalingfactor); matrix [] modeltransforms = new matrix [model. bones. count]; model. copyabsolutebonetransformsto (modeltransforms); Return modeltransforms ;}

You need to pass the model to this method to specify the final model size. This method gets the global ball of a model at the beginning. You need to multiply the obtained radius by 2 to get the ball size, which corresponds to the initial size of the model. Next, you divide the requestedsize by the value to obtain the scale of the model. You can use this scaling factor to create a matrix and use this matrix as the world matrix for model creation. In this way, your model will be scaled to the desired size.

Scale on Root bone

Build a good model to link all sub-grids to their root bone. This means that when you scale the root bone matrix (see tutorial 4-9), all modelmeshes are automatically scaled. You can multiply the root matrix by the zoom matrix and store the result matrix in the model based on the previously calculated scalingfactor. Because you have changed the content of the bone matrix, you must extract the new version of the modeltransforms matrix.

Note:A better way is to use a variable that can be configured in the custom model processor. See tutorial 4-12 to learn how to extend the default model processor.

Code

In the loadcontent method, you need to use two lines of code to load and scale the model:

 
Mymodel = xnautils. loadmodelwithboundingsphere (ref modeltransforms, "tank", content); modeltransforms = autoscale (mymodel, 10.0f );

When creating a model, you can use common code because the scaling information is already stored in the root bone of the model. In this way, each time you call model. when the copyabsolutebonetransformsto method is used, all the result matrices contain the scaling operation, so that all the parts of the model are scaled before being drawn to the screen.

 matrix worldmatrix = matrix. identity; mymodel. copyabsolutebonetransformsto (modeltransforms); foreach (modelmesh in mymodel. meshes) {foreach (basiceffect effect in mesh. effects) {effect. enabledefalightlighting (); effect. world = modeltransforms [mesh. parentbone. index] * worldmatrix; effect. view = fpscam. viewmatrix; effect. projection = fpscam. projectionmatrix;} mesh. draw () ;}

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.