JMF Technology for playing animations using Java 3D technology

Source: Internet
Author: User

The playback of animation fragments in Java 3D scenes brings opportunities for further development for richer and more vivid 3D content. Animations can display more realistic backgrounds, such as moving clouds, busy city streets, or scenes outside the window. Animation can make the help information more vivid, and can also be used for scenario transition in game design.

This article describes how to implement a Java 3D animation screen. This section explains how to use the Java media framework (JMF) to develop this program, especially the JMF performance package Windows 2.1.1e. Other tools I use include J2SE 5.0 and Java 3D 1.3.2. In the second part, I will discuss how to use Quicktime for Java to implement another version of the animation program.

Figure 1 shows two screen snapshots in the JMF Movie3D application at different times: the one on the right is the view that the screen looks at from the back.


Figure 1. Two views in the Movie3D Application
This application mainly includes:

· Integration of JMF and Java 3D. An application can have multiple screens of any size. Since a screen is a subset of the Java 3D Shape3D class, it can be easily integrated into different scenarios.

· The program execution process adopts the "Model-View-controller" design mode. The screen is a view element described by the JMFMovieScreen class. Animation is the model part and managed by the JMFSnapper class. A Java 3D Behavior class TimeBehavior is a controller that controls the periodic update of an animation. All JMF code is in the JMFSnapper class, which makes it easier to test any changes. In the second part of this article, a QuickTime for Java version called QTSnapper is used instead of JMFSnapper.

· Fully utilizes Java 3D performance techniques to accelerate the coloring process. As a result, an animation can be played at 25 frames per second without any difficulty.

· Finally, I will discuss the problems I encountered when using JMF for development. Due to these problems, my favorite solution cannot work properly-JMF indeed has the potential to become a great API, however, there are still many inferior coding parts behind this greatness that need to be further improved.

1. I am sitting on a mountain

In fact, I did not sit on any mountain, but on a chair in a cold office without an automatic thermostat. What I really want to say is that this article is based on a lot of background knowledge about Java 3D and JMF.

In this article, I will explain the JMF technology I used to extract animation frames from a video, instead of discussing technologies such as streaming media, capturing, or code conversion.

   2. Two profile maps in program implementation

The JMFSnapper class is responsible for loading and playing the animation, and the animation is played cyclically until it is stopped by sending a command.

The animation screen is created by JMFMovieScreen, which manages a Java 3D quadrilateral on the floor of the checkers disk.

Do you want to observe these classes intuitively? See the Application Scenario diagram (this scenario diagram shows how Java 3D nodes are linked together in a scenario ).


Figure 2. Movie3D scene
Many of the details in Figure 2 are ignored, but this figure is surprisingly similar to the example Checkers3D in chapter 1 of KGPJ. Only animation-related nodes are new things.
Because JMFMovieScreen and TimeBehavior objects are all nodes in the scene graph, they are all displayed in triangles. The JMFSnapper object is not part of the graph, but is called by JMFMovieScreen.

Every 40 milliseconds, The TimeBehavior object calls the nextFrame () method of JMFMovieScreen, and nextFrame () calls the getFrame () method of JMFSnapper in sequence to retrieve the current frame in the playing animation, the frame is then placed on the Quadrilateral managed by JMFMovieScreen.

TimeBehavior is a subclass of Java 3D Behavior class and a Java 3D Implementation Method of timer. It is similar to the TimeBehavior class in the 3D sprite example in chapter 1 of KGPJ.

Another way to gain insight into the implementation of this application is to look at the UML class diagram described in 3, which only shows the public methods of the class.


Figure 3. Movie3D class chart
Movie3D subclass Jframe, and WrapMovie3D is the subclass result of Jpanel. WrapMovie3D creates the scene diagram in figure 2 and colors it on the JPanel of the application. It uses the CheckerFloor class and the ColouredTiles class to construct the floor effect of the checkers disk.

JMFMovieScreen creates an animation screen, adds it to the scene, and creates a JMFSnapper object to start playing the animation. TimeBehavior calls the nextFrame () method in JMFMovieScreen every 40 milliseconds, while nextFrame () calls the getFrame () method in JMFSnapper to retrieve the current frame.

All the code for this example and an earlier version of this article can be found at KGPJ website.

3. Watch the animation

The animation, animation screen, and TimeBehavior object used to update the screen are all created by the addMovieScreen () method in WrapMovie3D:

// Global variable
Private BranchGroup sceneBG;
Private JMFMovieScreen MS; // animation Screen
Private TimeBehavior timer; // update the screen
Private void addMovieScreen (String fnm)
{
// Place an fnm animation on the animation Screen
MS = new JMFMovieScreen (new Point3f (1.5f, 0,-1), 2.0f, fnm );
SceneBG. addChild (MS );

// Create a timer object for the animating Animation
Timer = new TimeBehavior (40, MS );
// Update an animation every 40 ms (25 frames/second)
Timer. setSchedulingBounds (bounds );
SceneBG. addChild (timer );
}

The two Java 3D Methods addChild () call link the JMFMovieScreen node and TimeBehavior node to the scenario diagram. The setSchedulingBounds () method is used to activate the TimeBehavior node (that is, start timing the node ).

4. generate an animation Screen

JMFMovieScreen is a subclass of Java 3D Shape3D class, so you must specify a geometric shape and appearance for it.

The ry is a quadrilateral where each side is proportional to the size of the animated image. However, the larger width and height of the Quadrilateral are used as parameters for JMFMovieScreen constructors. The quadrilateral is vertical and oriented toward the positive Z axis. It can be placed anywhere on the floor.

The quadrilateral has two sides, so that the animation can be viewed from both sides of the screen. Texture is smooth by using bilinear interpolation algorithm. Therefore, when you watch an animation from the near corner, the pixel effect is greatly reduced.

Most of the functions here are copied from the ImageCsSeries class in the first person shooting procedure in Chapter 24th of KGPJ. The ImageCsSeries class is used to display a series of GIF images on a quadrilateral. To save space, I will only describe the differences between JMFMovieScreen and ImageCsSeries.

   Efficient image coloring

Frames extracted from an animation are converted to textures and placed on the Quadrilateral. The implementation method is divided into two steps: first, the given BufferedImage is passed to a Java 3D ImageComponent2D object, then it is passed to a Java 3D Texture2D.

The quadrilateral texture is updated quickly: up to 25 frames can be updated per second, and the texture needs to be changed 25 times. Therefore, efficient texture is very important. This may be achieved by specifying certain formats to BufferedImage and ImageComponent2D objects.

The ImageComponent2D object is used by JMFMovieScreen. Its declaration is as follows:

ImageComponent2D ic = new ImageComponent2D(
 ImageComponent2D.FORMAT_RGB,
 FORMAT_SIZE, FORMAT_SIZE, true, true);

The last two parameters in the constructor indicate that they use the "by reference" and "Y-up" methods. Because Java 3D will avoid copying images from application space to image memory, the method mentioned here will reduce the memory requirements for storing texture images.

In Windows, Java 3D uses OpenGL as the basic coloring engine. The ImageComponent2D format should be ImageComponent2D. FORMAT_RGB (as described above), and BufferedImage format should be BufferedImage. TYPE_3BYTE_BGR. In JMFSnapper, The BufferedImage format is fixed.

For more information about this and other performance techniques, see j3d.org.
Link a texture to a quadrilateral

A common method to tile a texture image to a quadrilateral is to link the lower left corner of the texture to the lower left corner of the Quadrilateral, and specify other links in the opposite clockwise direction. This method is shown in Figure 4.


Figure 4. Standard link between texture and quadrilateral
The range of texture coordinates is 0-1, along the X and Y axes and up the Y axis. For example, if the coordinate () is used in the lower left corner of the texture, the upper right corner is ).

When the "Y-up" method is used, the texture coordinates of the Y axis are reversed, pointing to the bottom. That is to say, coordinates () correspond to the upper left corner of the texture, and () correspond to the lower right corner of the texture.

In the "Y-up" mode, texture coordinates must be assigned to different points of the quadrilateral to obtain the same direction of the image. This new configuration is shown in Figure 5.


Figure 5. Link between the texture and the Quadrilateral when the "Y-up" method is used  
In JMFMovieScreen, the code that links the Quadrilateral vertex to the texture coordinate is as follows:

TexCoord2f q = new TexCoord2f ();

Q. set (0.0f, 0.0f );
Plane. setTextureCoordinate (0, 3, q );
// Texture coordinate (0, 0) --> top left vertex of the Quadrilateral (p3)
Q. set (1.0f, 0.0f );
Plane. setTextureCoordinate (0, 2, q );
// (1, 0) --> upper right (p2)

Q. set (1.0f, 1.0f );
Plane. setTextureCoordinate (0, 1, q );
// (1, 1) --> lower right (p1)

Q. set (0.0f, 1.0f );
Plane. setTextureCoordinate (0, 0, q );
// (0, 1) --> lower left (p0)

The plane object here represents the Quadrilateral.

   Update Images

As mentioned above, a TimeBehavior object is created to call the nextFrame () method of JMFMovieScreen every 40 milliseconds. While ne

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.