Java 3D API official tutorial [translation 3]

Source: Internet
Author: User
Tags constant math
Java 3D API official tutorial [translation 3]

Branchgroup objects can all be compiled (Compiled ). Compiling a branchgroup can convert a branchgroup object and all its ancestors into a format that the Renderer can render more efficiently. We recommend that you compile a branchgroup object in the last step before it becomes a live object, and it is best to compile and insert all branchgroups in locale only once. The compilation issue is further discussed in sections 1.8 and 1.8.1.

Branchgroup compile () method
Void compile ()
Create and cache a compiled scenario graph to compile the source branchgroup associated with this object.

 

The concept of compilation and transformation is implemented in scenegraphobject. The two methods of the scenegraphobject class related to these concepts are described in the reference box of the scenegraphobject method.

Scenegraphobject method (partially listed)
Scenegraphobject is almost the superclass of every object in the scenario graph created in the group. Including leaf and nodecomponent. Scenegraphobject provides some common methods and attributes for its subclass. Here are two examples. The scenegraphobject method is associated with "Compilation" and describes some basic things in section 1.8.
Boolean iscompiled ()
Returns a Boolean flag, indicating whether the node in the scenario graph has been compiled.
Boolean islive ()
Returns a Boolean tag, indicating whether the node in the scene graph is activated.

 

Note that "Start Renderer" is not available in both basic and simple methods. When a branch graph containing a view instance is activated in the virtual world, the Java 3D Renderer starts to run in an infinite loop. Once started, the Java 3D Renderer implements the following operations in the pseudo code.
While (true) {// always Execute
Process input // process input
If (request to exit) Break // if the program requires
Perform behaviors // execute Interaction
Traverse the scene graph // traverse the scene graph to render each Visual Object
And render visual objects
}
Cleanup and exit // clear and exit

 

The main classes of Java 3D programs generally define a method to build a content branch diagram. In the hellojava3da example, this method is defined and declared as createscenegraph (). All the steps for creating a scenario map by using the simple method are implemented in the hellojava3da constructor class. Step 2: Create a canvas3d object. For details, see lines 4th to 6th of the program. Step 2: Create a simpleuniverse object in Row 3 .. Step 2a: customizes the simpleuniverse object and completes step 17th in line 2. You can call the createscenegraph () method to construct the content branch graph. Step 2: Compile the content subgraph. For details about the code, see Row 3. In the last step, insert the content subgraph to the lacale of simpleuniverse. For details about this operation, see row 5th.
1. Public class hellojava3da extends applet {
2. Public hellojava3da (){
3. setlayout (New borderlayout ());
4. graphicsconfiguration Config =
5. simpleuniverse. getpreferredconfiguration ();
6. canvas3d canvas3d = new canvas3d (config );
7. Add ("center", canvas3d );
8.
9. branchgroup scene = createscenegraph ();
10. Scene. Compile ();
11.
12. // simpleuniverse class is a very convenient tool class
13. simpleuniverse simpleu = new simpleuniverse (canvas3d );
14.
15. // move the ViewPlatform back a little bit,
16. // to make the objects in the scenario visible.
17. simpleu. getviewingplatform (). setnominalviewingtransform ();
18.
19. simpleu. addbranchgraph (scene );
20.} // hellojava3da (constructor) ended
Code snippet 1-1 class hellojava3da
Step 1 of the simple method is to create a content branch graph. The code for creating a content branch chart can be found in code snippet 1-2. This may be a simple content branch chart. The content branch graph created in section 1-2 contains a static graphical object, that is, a color cube. The colored cube is located at the initial position in the virtual world coordinate system. The rendered cube looks like a rectangle according to the position and direction provided by the view. The specific effect is 1-12.
1. Public branchgroup createscenegraph (){
2. // create the root node of the branch subgraph
3. branchgroup objroot = new branchgroup ();
4.
5. // create a simple graphic leaf node and add it to the scene Map
6. // colorcube is also a convenient tool class.
7. objroot. addchild (New colorcube (0.4 ));
8.
9. Return objroot;
10.} // The createscenegraph method for creating the scenario graph of hellojava3da ends.
11.} // end of the hellojava3da class
Code snippet 1-2 createscenegraph Method s used by the hellojava3da class to create a scenario Graph

The hellojava3da class is inherited from the Applet Class, but this program can run independently as an application because it uses the mainframe class. The Applet Class is the basic class used to write Java 3D programs that are easy to run in a window. Mainframe provides an AWT frame (form) to enable the applet to be embedded and run as an application. The window size of the result program is specified when the mainframe class is used for construction. Code snippet 1-3 illustrates the usage of the mainframe class used in hellojava3da. java.

MainframeConstructor (list part)

Package: COM. Sun. j3d. utils. Applet
The mainframe class converts a applet into an application. The objects inherited from the Applet Class call the mainframe constructor and there is a main () method. Mainframe extends the java. AWT. frame class and implements java. Lang. runnable, java. Applet. appletstub, and Java. Applet. appletcontext.
The mainframe class is copyrighted and created by Jef poskanzer from. Email: jef@acme.com http://www.acme.com/java/
Mainframe (Java. Applet. Applet applet, int width, int height)
Create a mainframe object so that an applet can run independently as an application.
Parameters:
Applet-the constructor inherited from the Applet Class. Mainframe provides an AWT framework for the applet.
Width-pixel value of the form width.
Height-form height pixel value.

 

The first four code segments (1-1, 1-2, 1-3, and 1-4) constitute the complete sample program hellojava3da. Java file. The complete sample program can also be found in the examples/hellojava3d directory of the code release package. Use the javac hellojava3da. Java command to compile the code and use Java hellojava3da to run the code. The running result is 1-12.

In the hellojava3da example, not every line of code is explained. If you read the entire example, the basic concepts for integrating the Java3D program should be clear. The following section describes the classes used in the above program to make up for the defects described below.

 

In the hellojava3da. Java sample program, a unique image object is placed in a unique locale object. This scenario is shown in Figure 1-11. Code snippet 1-4 hellojava3da. java Import Statement 9. import javax. vecmath. *; 8. import javax. media. j3d. *; 7. import COM. sun. j3d. utils. geometry. colorcube; 6. import COM. sun. j3d. utils. universe. *; 5. import COM. sun. j3d. utils. applet. mainframe; 4. import Java. AWT. event. *; 3. import Java. AWT. frame; 2. import Java. AWT. borderlayout; 1. import Java. applet. the most common classes used in Java 3D programming are javax. media. j3d or javax. in vecmath, in this example, colorcub E tool class is included in the package com. Sun. j3d. utils. Geometry. Thus, most Java 3D programs will have the import statement shown in addition to the colorcube class introduced in code segment 1-4. The preceding three code segments (1-1, 1-2, and 1-3) and corresponding import statements form a complete Java3D application. The following import statements are required when compiling the hellojava3da class. Code snippet 1-3 main () method of the hellojava3da class calls mainframe6.} // end of main (hellojava3da method) 5. frame frame = new mainframe (New hellojava3da (), 256,256); 4. public static void main (string [] ARGs) {3.2. // run the program as an application. 1. // The following Code allows the applet results

BranchgroupClass
This type of object is used to build a scenario map. The branchgroup instance is the root node of the subgraph,
The branchgroup object is also the only child object that can be a locale object. The branchgroup object can have multiple children, and the branchgroup object can be group and leaf objects.

BranchgroupDefault constructor
Branchgroup ()
The branchgroup instance is the root node of the scenario graph branch. The branchgroup object is also a unique object that can be inserted into the locale object.

Canvas3dClass
The canvas3d class inherits from the canvas class of the abstract wing Toolkit (AWT) class. in the View branch diagram of the scene chart, at least one canvas3d object must be referenced (Note: there may be multiple canvas3d objects. to simplify the demonstration, when using the simpleuniverse class, in the program, there is only one instance of the canvas3d class ). For more information about the canvas class, see AWT. Appendix B provides a series of references.

Canvas3dClass Constructor
Canvas3d (graphicsconfiguration)
Create and initialize a new canvas3d object. Java 3D can render this object under the specified graphicsconfiguration object. This is an extended class of the AWT canvas class. For more information about graphicsconfiguration objects, see Java 2D standards. This is also part of JDK 1.2 AWT.

Transform3dClass
Transform3d objects are used to perform 3D Geometric Variation ratio translation and rotation. These objects are generally only used to create a transformgroup object. First, the transform3d object may be created based on the combination of some transform3d objects, and then a transformgroup object will be constructed using this transform3d object.

Transform3dDefault constructor
A common transform object is represented as a 4x4 Double Precision Floating Point matrix. This mathematical representation is based on rows. A transform3d object is not used in the scenario diagram, but is used to specify the graphic transformation of the transformgroup object.
Transform3d ()
Construct a transform3d object. The unit matrix is used by default (transformation is not concerned ).

A transform3d object can represent translation, rotation, scaling, and even the combination of these transformations. When a rotation transformation is specified, the angle is expressed in radians. The radian of a fully rotated circle is 2 pi. One way to set the angle is to use the constant math. pi, another calculation method is to set directly. Here we will talk about the radians of common angles: 45 degrees is 0.785, 90 degrees is 1.57, and 180 degrees is 3.14 (about Pi ).

Transform3dClass Method (list part)
Transform3d objects are used to represent geometric deformation, such as rotation, translation, and scaling.
Transform3d is one of the few classes that are not directly used in any scenario diagram. The transformation defined by the transform3d object is used to create the transformgroup object in the scenario diagram.
Void rotx (double angle)
Sets the orientation of the X axis of the image to rotate along the clockwise direction at the specified angle. The angle value is specified in radians.
Void roty (double angle)
Sets the orientation of the Y axis of the image to rotate along the clockwise direction at the specified angle. The angle value is specified in radians.
Void Rotz (double angle)
Sets the orientation of the zaxis of the image to rotate along the clockwise direction at the specified angle. The angle value is specified in radians.
Void set (vector3f translate)
Use the input vector3f parameter to set the image's translation value. If the input deformation is a matrix of units, set the actual deformation value at other times.

Transformgroup class
As a subclass of the group class. The transformgroup class instance is used to create a set of scene charts with their subnodes. Transformgroup objects store geometric deformation such as translation and rotation. Generally, a deformation is constructed using a transform3d object that is not in the scene graph. The transformgroup object stores the deformation information in the scenario diagram.

RansformgroupConstructor
Transformgroup ()
Construct and initialize a transformgroup object using the matrix of units. (No deformation occurs)
Transformgroup (transform3d T1)
Construct and initialize a transformgroup object by passing in the transform3d object parameter.
Parameters:
T1-transform3d object

 

 

 

 

The deformation information contained in the transform3d object is copied to transformgroup when the transformgroup object is created or the settransform () method is called.

TransformgroupSettransform ()Method
Void settransform (transform3d T1)
Use the input deformation parameter to set the deformation value of the transformgroup object.
Parameters:
T1-The transform3d object to be copied.

Vector3fClass
Vector3f is a mathematical class in the javax. vecmath package. It is used to set a vector with a floating point number. Vector objects are often used to set the image on-demand. Vector3f objects are not directly used in the scenario diagram. It is often used to specify deformation, representation, normal vectors, and some other applications.

Vector3fConstructor
A vector of three elements. The three elements represent the X, Y, and zcoordinates with a single-precision floating point number.
Vector3f ()
Construct and initialize a vector3f with (0, 0, 0.
Vector3f (float X, float y, float Z)
Specify the coordinates of X, Y, and Z axes to construct and initialize a vector3f.

Colorcube
Colorcube is a tool class in the package com. Sun. j3d. utils. geometry. Defines a cube image and its different colors, and puts the position in the middle of the initial position. The default colorcube object is a cube with a length, width, and height of 2 meters. If a cube is in the original position without rotation (such as in the hellojava3da example), in the nominal view position, the red area is visible, and the other colors are: blue, foreign red, yellow, green, and blue.

ColorcubeConstructor
Package: COM. Sun. j3d. utils. Geometry
A colorcube is a simple visual cube with different colors on all sides.
Colorcube extends the shape3d class. Therefore, it is a leaf node. Colorcube can be easily placed in the virtual world.
Colorcube ()
Construct a colored cube with the default size. By default, the center of each angle to the corresponding coordinate is 1 meters. Therefore, the cube is in the center of the view, and the length, width, and height are 2 meters.
Colorcube (double scale)
Construct a colored cube and scale the specified parameters. By default, each side is 2 meters, and this constructor constructs a colorcube with the vertex at (scale, scale, scale) and (-scale,-scale,-scale.

 

Related Article

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.