3Д БУМ

3Д принтеры и всё что с ними связано

Making It Move: Animation

Now that you have a nice, textured model, you can start writing the code to animate it onscreen. Animation of a keyframed model such as an MD2 essentially involves drawing the frames one after another.

You can take the approach of simply drawing the next frame in the series, but this will produce less than satisfactory results. This method will result in jerky animation, and will run at different speeds on different computers; something you definitely do not want.

The solution to this issue is to interpolate between the frames with respect to time. In order to have smooth animations, you will automati­cally create new frames that represent the model’s position at any given time. This approach has many advantages. It provides smooth transitions between frames. You can control the speed of the anima­tion, and the frames will pass at the same rate on all machines. Last of all, it is predictable. You can count on it being at frame x during time y; even if the system gets hung up, the program will skip to wherever it should be at the time it starts again.

To do anything with time, you need a timer. The CTimer class is pro­vided just for that, you can find out how it works by examining timer. h. The function you are most interested in here is CTimer::GetMS(). This function returns the number of milliseconds that have elapsed since the function was called.

If you know how many milliseconds have passed since the last frame, and how long each frame should last in milliseconds, you can “create” a new frame that is partway between two frames of the MD2 model.

First, you need the time it should take between frames (in ms). The Animate() function of CMd2 takes care of that. A parameter is passed to the function that tells the function how fast to animate, in frames per second. Using this it is a simple matter of obtaining the number of milliseconds for each frame. The timer takes care of the time elapsed between frames.

Using these values, you can calculate an interpolation value. This will let you calculate the vertices to display. Use the following formula:

Vo+M-Vo )f

In this equation, vo is the vertex position of the last previous frame, v: is the vertex position of the next frame, and t is the interpolation value.

The interpolation value must be between 0 and 1. If it isn’t, it means that you have to skip frames. The next part does just that, it keeps adding one to the frame number, and decreasing the interpolation value by 1.0, until it is between 1 and 0. Once this is done, you will know what frame the value falls between. If the last frame is 19, the next frame will be 20, and if the interpolation value is 0.871, you are essentially calculating frame 19.871.

To accomplish this, you obviously need somewhere to store the inter­polated vertices. The code provided simply uses an array of CMd2Vertices with the same number of slots as each frame. This pro­vides a place to put every transformed vertex.

Next you must actually calculate the intermediate vertices. Using linear interpolation to calculate the new vertices can be done using the previous formula.

Now that you have an array of vertices, they can be rendered as a regular frame, as you did earlier in the chapter.

As you look at the code, you will notice that there are static variables that hold information such as the current frame and the last interpola­tion value. The values of these variables are stored between calls to the function, eliminating the need to store them in the class or in global variables. This function is called every time the main game or render­ing loop is executed.

One last shot of the model, in mid-animation, is shown in Figure 3.7.

Для любых предложений по сайту: [email protected]