资源描述
介绍
Java 3D is an addition to Java for displaying three-dimensional graphics. Programs written in Java 3D can be run on several different types of computer and over the internet.
Java 3D是为Java显示三维图形的附加部分。以Java 3D写成的程序可以在若干不同类型的计算机或经由互联网运行。
The Java 3D class library provides a simpler interface than most other graphics libraries, but has enough capabilities to produce good games and animation. Java 3D builds on existing technology such as DirectX and OpenGL so the programs do not run as slowly as you might expect. Also, Java 3D can incorporate objects created by 3D modeling packages like TrueSpace and VRML models.
Java 3D类库提供了比大多数其他图形库都更为简单的接口,但也足以生成很好的游戏和动画。Java 3D建立在现有的技术,如DirectX和OpenGL之上,因此程序运行起来并非如你所预期的那样慢。除此之外,Java 3D还可以与3D模型化包,如TrueSpace和VRML模型创建的对象实现协作。
This tutorial is an introduction to Java 3D. Examples lead you through the basic methods for producing 3D images and animation. You do not need to have any knowledge of 3D graphics or Java 3D to learn from this tutorial, but it will help if you have a basic understanding of the Java programming language. Programming in three dimensions can seem complicated because of the amount of jargon and the mathematics involved, this tutorial will keep things as simple as possible.
本教程是对Java 3D的介绍。包括了引导你完成产生3D图形和动画的基本方法的实例。学习本教程,你不需要具有任何3D图形或Java3D的知识,不过对于Java编程语言的基本认知将有助于你的学习。由于大量术语和算术运算的存在,三维编程可能看上去很复杂,但本教程将尽量让所有的事情保持简单。
Please help to improve this tutorial for future readers by reporting any mistakes you find or suggesting improvements to editor@java3d.org.
为了帮助改进本教程以便于将来的读者的学习,请在你发现任何问题或想到了改进的建议的时候发信给editor@java3d.org。
Installing and Running Java 3D
安装和运行Java 3D
The software you need to use Java 3D is available free from Sun Microsystems at
你要使用到的Java 3D软件可以免费从Sun Microsystems获得,网址是:
Sun often releases new versions so it is better to look at their site than rely on this document to find what you need. You will have to register as a member of "Java Developer Connection" to download some of the files.
Sun经常会发布新的版本,因此最好是能够找一找他们的网站而非依赖于这篇文档去获得你需要的东西。为了下载某些文件,你需要注册为“Java开发者连接”的成员。
At time of writing the newest version of Java itself (6.2) was at and the current version of the Java 3D extension (1.5.2) was at
编写教程的时候,Java本身的最新版本(6.2)在 3D扩展的当前版本在
Once you have installed Java and Java 3D you can compile programs using the command:
一旦完成了Java和Java 3D的安装,你就可以使用以下命令编译程序:
javac FileName.java
javac FileName.java
And run them using:
并使用如下命令运行它们:
java FileName
java FileName
The FileName should always be the same as the name of the class defined in that file. In some versions of Java 3D you may get a message about a null graphics configuration, but you can just ignore this.
FileName应该永远与定义在文件中的类名相同。在Java3D的某些版本中,你或许会得到一个空图形配置的消息,不过你可以忽略它。
开始 -你的第一个程序
The following program shows you the basic steps needed to display 3D objects.
下列程序说明了显示3D对象的基本步骤。
1. Create a virtual universe to contain your scene.
1.创建包含你的场景的虚拟环境(virtual universe)。
2. Create a data structure to contain a group of objects.
2.创建包含了一组对象的数据结构。
3. Add an object to the group
3.向组内添加对象
4. Position the viewer so that they are looking at the object
4.摆放可以观察对象的视角
5. Add the group of objects to the universe
5.把这组对象添加到环境中
Look at the Hello3d() constructor and you will see the five lines that perform each of these steps. The program displays a glowing cube, the viewer is looking directly at the red face of the cube, so what you actually see is a red square on a black background.
看一下Hello3d()构造器,你会发现其中的五行代码分别处理了以上的步骤。程序显示了一个灼热的立方体,而视角正直直地对着立方体红色的面上,因此你实际看到的是背景上的红色正方形。
import com.sun.j3d.utils.universe.SimpleUniverse;
import com.sun.j3d.utils.geometry.ColorCube;
import javax.media.j3d.BranchGroup;
public class Hello3d {
public Hello3d()
{
SimpleUniverse universe = new SimpleUniverse();
BranchGroup group = new BranchGroup();
group.addChild(new ColorCube(0.3));
universe.getViewingPlatform().setNominalViewingTransform();
universe.addBranchGraph(group);
}
public static void main( String[] args ) {
new Hello3d();
}
} // end of class Hello3d
import com.sun.j3d.utils.universe.SimpleUniverse;
import com.sun.j3d.utils.geometry.ColorCube;
import javax.media.j3d.BranchGroup;
public class Hello3d {
public Hello3d()
{
SimpleUniverse universe = new SimpleUniverse();
BranchGroup group = new BranchGroup();
group.addChild(new ColorCube(0.3));
universe.getViewingPlatform().setNominalViewingTransform();
universe.addBranchGraph(group);
}
public static void main( String[] args ) {
new Hello3d();
}
} // Hello3d类结束
The import statements at the beginning of this program use various parts of Java 3D, so compiling and running this program is a good test that you have installed Java 3D correctly.
程序最初的引用语句使用了Java 3D的多个部分,因此编译和执行这个程序是对你正确安装了Java 3D的有效测试。
点亮世界
OK, the first example was a good start, but was it 3D? If you don't think a square qualifies as three-dimensional, you are going to need to add some lights to your universe. The way the light falls on an object provides us with the shading that helps us see shapes in three dimensions
很好,第一个例子是个良好的开始,但它是3D的吗?如果你不能认同一个正方形是三维的,那么你就需要为你的世界来点儿光。光线照射在对象上的方式能够提供给我们阴影以帮助我们看到三维的形状。
The next example illustrates how to display a ball lit by a red light:
下一个例子演示了如何显示一个被红光照耀的球:
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.*;
public class Ball {
public Ball() {
// Create the universe
SimpleUniverse universe = new SimpleUniverse();
// Create a structure to contain objects
BranchGroup group = new BranchGroup();
// Create a ball and add it to the group of objects
Sphere sphere = new Sphere(0.5f);
group.addChild(sphere);
// Create a red light that shines for 100m from the origin
Color3f light1Color = new Color3f(1.8f, 0.1f, 0.1f);
BoundingSphere bounds =
new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
Vector3f light1Direction = new Vector3f(4.0f, -7.0f, -12.0f);
DirectionalLight light1 =
new DirectionalLight(light1Color, light1Direction);
light1.setInfluencingBounds(bounds);
group.addChild(light1);
// look towards the ball
universe.getViewingPlatform().setNominalViewingTransform();
// add the group of objects to the Universe
universe.addBranchGraph(group);
}
public static void main(String[] args)
{
new Ball();
}
}
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.*;
public class Ball {
public Ball() {
// Create the universe
SimpleUniverse universe = new SimpleUniverse();
// Create a structure to contain objects
BranchGroup group = new BranchGroup();
// Create a ball and add it to the group of objects
Sphere sphere = new Sphere(0.5f);
group.addChild(sphere);
// Create a red light that shines for 100m from the origin
Color3f light1Color = new Color3f(1.8f, 0.1f, 0.1f);
BoundingSphere bounds =
new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
Vector3f light1Direction = new Vector3f(4.0f, -7.0f, -12.0f);
DirectionalLight light1 =
new DirectionalLight(light1Color, light1Direction);
light1.setInfluencingBounds(bounds);
group.addChild(light1);
// look towards the ball
universe.getViewingPlatform().setNominalViewingTransform();
// add the group of objects to the Universe
universe.addBranchGraph(group);
}
public static void main(String[] args)
{
new Ball();
}
}
The sphere we created is white (the default), it appears red because of the colored light. Since it is a DirectionalLight, we also have to specify how far the light shines and in what direction. In the example, the light shines for 100 meters from the origin and the direction is to the right, down and into the screen (this is defined by the vector: 4.0 right, -7.0 down, and -12.0 into the screen).
我们创建的球体(缺省)是白色的,因光照呈现为红色。由于是方向性光源,我们还必须指定光源的距离及方向。例子中,光源在距离原点100米的地方,方向朝右下,并进入屏幕(这由向量:4.0右,-7.0下,以及-12.0进入屏幕,实现定义)。
You can also create an AmbientLight which will produce a directionless light, or a SpotLight if you want to focus on a particular part of your scene. A combination of a strong directional light and a weaker ambient light gives a natural-looking appearance to your scene. Java 3D lights do not produce shadows.
你还可以创建一个环境光源以产生无方向性的光,或者点光源,如果你想对你场景中的特定区域聚焦。由强方向性光源和较弱的环境光源组合在一起可以为你的场景提供一种自然的感观。Java 3D的灯光不能产生阴影。
Positioning the Objects
摆放对象
So far, the examples have created objects in the same place, the center of the universe. In Java 3D, locations are described by using x, y, z coordinates. Increasing coordinates go along the x-axis to the right, along the y-axis upwards, and along the z-axis out of the screen. In the picture, x, y and z are represented by spheres, cones and cylinders.
目前为止,例子都是在同样的地方(场景的中央)创建对象。Java 3D中,位置由x,y,z坐标描述,增加坐标值对于x轴来说是向右移动,y轴是向上,而z轴是向屏幕之外。图中,x,y和z被分别表示为球,圆锥以及圆柱体。
This is called a "right-handed" coordinate system because the thumb and first two fingers of your right hand can be used to represent the three directions. All the distances are measured in meters.
这被称为“右手”坐标系统,因为你右手的大拇指、食指和中指可以被用来表示三个方向。所有的距离都用“米”(meter)来度量。
To place your objects in the scene, you start at point (0,0,0), and then move the objects wherever you want. Moving the objects is called a "transformation", so the classes you use are: TransformGroup and Transform3D. You add both the object and the Transform3D to a TransformGroup before adding the TransformGroup to the rest of your scene.
为了把你的对象摆放到场景中,你开始于点(0, 0, 0),然后把对象移动到你想要的地方。对象的移动被称作“变换”,因此你用到的类包括:TransformGroup和Transform3D。你把对象和Transform3D都添加到TransformGroup,然后把TransformGroup添加到余下的场景中。
…(表格)…
1.创建变换,变换组以及对象
Transform = new Transform3D();
transformGroup tg = new TransformGroup();
Cone cone = new Cone(0.5f, 0.5f);
2.指定对象的位置
Vector3f vector = new Vector3f(-.2f,.1f , -.4f);
3.设置变换为移动(平移)对象到那个位置
Transform.setTranslation(vector);
4.添加变换到变换组
tg.setTransform(transform);
5.添加对象到变换组
tg.addChild(cone);
This may seem complicated, but the transform groups enable you to collect objects together and move them as one unit. For example, a table could be made up of cylinders for legs and a box for the top. If you add all the parts of the table to a single transform group, you can move the whole table with one translation.
这看上去有够复杂,但变换组能够让你把对象聚在一起并且以一体的方式移动它们。例如,桌子是由圆柱体的腿和长方体的顶组成的。如果你把桌子的所有部分都加到单一的变换组,那么你就可以用一个变换移动整张桌子。
The Transform3D class can do much more than specifying the co-ordinates of the object. The functions include setScale to change the size of an object and rotX, rotY and rotZ for rotating an object around each axis (counter clockwise).
Transform3D类的功能远非仅能指定对象的坐标。还包括setScale,改变对象的尺寸以及rotX,rotY还有rotZ使物体绕各个轴(逆时针)转动。
This example displays the different objects on each axis.
下面的例子在每根轴上显示了不同的对象。
…(代码)…
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.*;
public class Position
{
public Position()
{
SimpleUniverse universe = new SimpleUniverse();
BranchGroup group = new BranchGroup();
// X axis made of spheres
for (float x = -1.0f; x <= 1.0f; x = x + 0.1f) {
Sphere sphere = new Sphere(0.05f);
TransformGroup tg = new TransformGroup();
Transform3D transform = new Transform3D();
Vector3f vector = new Vector3f(x, .0f, .0f);
transform.setTranslation(vector);
tg.setTransform(transform);
tg.addChild(sphere);
group.addChild(tg);
}
// Y axis made of cones
for (float y = -1.0f; y <= 1.0f; y = y + 0.1f) {
TransformGroup tg = new TransformGroup();
Transform3D transform = new Transform3D();
Cone cone = new Cone(0.05f, 0.1f);
Vector3f vector = new Vector3f(.0f, y, .0f);
transform.setTranslation(vector);
tg.setTransform(transform);
tg.addChild(cone);
group.addChild(tg);
}
// Z axis made of cylinders
for (float z = -1.0f; z <= 1.0f; z = z + 0.1f) {
TransformGroup tg = new TransformGroup();
Transform3D transform = new Transform3D();
Cylinder cylinder = new Cylinder(0.05f, 0.1f);
Vector3f vector = new Vector3f(.0f, .0f, z);
transform.setTranslation(vector);
tg.setTransform(transform);
tg.addChild(cylinder);
group.addChild(tg);
}
Color3f light1Color = new Color3f(.1f, 1.4f, .1f); // green light
BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);
Vector3f light1Direction = new Vector3f(4.0f, -7.0f, -12.0f);
DirectionalLight light1 = new DirectionalLight(light1Color, light1Direction);
light1.setInfluencingBounds(bounds);
group.addChild(light1);
universe.getViewingPlatform().setNominalViewingTransform();
// add the group of objects to the Universe
universe.addBranchGraph(group);
}
public static void main(String[] args)
{
new Position();
}
}
表现为王
There are many ways to change the way that objects in your scene look. You can change their color, how much light they reflect. You can paint them with two-dimensional images, or add rough textures to their surfaces. The Appearance class contains the functions for making these changes. This section shows you how to use these functions.
你的场景中对象的外观能够通过多种方式加以改变。你可以改变它们的颜色,反光强度。你可以把二维的图像画在对象上,或者在它们的表面添加粗糙的纹理。Appearance类包含了完成这些改变的函数。本节将展示给你这些函数的使用方式。
The simplest way of setting the appearance is by specifying only the color and the shading method. This works for setting an object to being a simple color, but to make an object look realistic, you need to specify how an object appears under lights. You do this by creating a Material.
表现设定的最简方式是仅指定颜色和阴影的方法。对于简单的为对象上色来说这样就行了,但要想让对象看起来更真实,你需要指定对象在光照下的效果。你可以通过创建材质(Material)实现。
1. Create an object
Sphere sphere = new Sphere();
2. Create an appearance
Appearance ap = new Appearance();
3. Create a color
Color3f col = new Color3f(0.0f, 0.0f, 1.0f);
4. C
展开阅读全文