Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
3D Computer Graphics Programming
Tasks
1. 3D Computer Graphics
Appendix
1. Processing
2. Java
COMP3419 Graphics and Multimedia   IMAGE-Lab (WEEK08)
1
TASK-1. Getting Familiar with 3D Graphics
1. Use primitive 3D objects, such as cube, sphere, etc.
2. Use lighting
3. Position, rotate and transform the objects
4. Use textures
Tip: You can use world32k.jpg as your texture.
The appendix for Processing is given first, because creating 3d graphics is very easy in
Processing. Most of the technical details are hidden behind the scene. Java 3D requires
deeper level of understanding about the theories. I would recommend using Processing 2
at first during the lab time to get familiar with the concepts and then dive in Java 3D
afterwards.
TASK-2. Simple 3D Object Animation with Transformation
After  you  are  comfortable  with  the  transformations,  we  are  ready  to  make  some
interesting animations.
1. Make a 3D ball
2. Apply texture on it
3. Appy spotlight from a fixed postion
4. Rotate the ball consistently according to Y axis with a fixed angle of 2*PI/16 at
each time
5. At the same time rotate the ball consistently according to Z axis with a fixed angle
of 2*PI/32 at each time.
TASK-3*. Earth and Moon!
Make two balls A (moon) and B (earth) rotate according to the Y axis at the same time.
Ball A travels in a circle around ball B with a predefined radius. You can try to play with
the travelling trajectory with more complex math.
COMP3419 Graphics and Multimedia   IMAGE-Lab (WEEK08)
2
Appendix – Processing
 
To  generate  3D  graphics  in  Processing,  you  should  know  about  how  to  do  2D
transformations  and  3D  transformation.  Please  read  this  great  article  carefully  to
understand what a Transformation Matrix is and how to use it:
http://processing.org/learning/transform2d/
The wiki page of Transformation Matrix may also help:
http://en.wikipedia.org/wiki/Transformation_matrix
After that, start the tutorials at  http://processing.org/tutorials/p3d/.  Notice that many of
the examples are animated and/or interactive.
1. To generate primitive 3D objects  Form - Primitives 3D
This corresponds to Java Task-1 and Task-2.
2. To use light  Lights – all
This corresponds to Java Task-3.
3. To position and rotate the objects  Transform – all
This corresponds to Java Task-4.
4. To use textures to render the objects  Textures – all
This corresponds to Java Task-5 and Task-6.
The examples are very useful,  and you are going to use these examples  for the next
tutorial as well.
COMP3419 Graphics and Multimedia   IMAGE-Lab (WEEK08)
3
Appendix - Java
Computer Graphics Programming with Java 3D 
The Java 3D API is an interface for writing programs to display and interact with 3D
graphics,  and enables the creation of 3D graphics  applications  and Internet-based 3D
applets. It provides a collection of high-level constructs for creating and manipulating 3D
geometry and building the structures used in rendering that geometry. With Java 3D, you
can efficiently define and render very large virtual worlds. 
The Java 3D API is a scene graph-based 3D API for the Java platform. The programmer
works with high-level constructs for creating and manipulating 3D geometric  objects.
These geometric objects reside in a virtual universe, which is then rendered. Java 3D runs
on top of either OpenGL or DirectX. Compared to other solutions, Java 3D is not only a
wrapper  around  these  graphics  APIs,  but  an  interface  that  encapsulates  the  graphics
programming using a real, object-oriented concept. The Java 3D API provides a simpler
interface than most other graphics libraries, but has enough capabilities for creation of
imagery, visualizations, animations, and interactive 3D graphics application programs.
INSTALL FOR YOUR OWN PC
To install Java 3D at your own computer (NOT the lab pc), visit this page:
http://www.oracle.com/technetwork/java/javase/tech/index-jsp-138252.html
PROCEDURES
When doing the following tasks, if there are libraries missing, import the  j3dcore.jar,
j3dutils.jar, and vecmath.jar into your project (The lab pc should have been set up
already): 
Right click project Properties  Java Build Path  Libraries  Add external Jars 
Find XXX.jar open ok
TASK-1. Getting Started with JA  VA   3D
In this task, you are asked to run your first Java 3D program (HelloCube1.java) which
can be downloaded from the resource website. This simple example shows you the basic
steps needed to display 3D objects: (1) Create a virtual universe to contain your scene;
(2) Create a data structure to contain a group of objects; (3) Add an object to the group;
(4) Position the viewer so that they are looking at the object; and (5) Add the group of
objects to the virtual universe.
Look at the HelloCube1() constructor and you will see the five lines that perform each
of these steps. The program displays a simple color-per-vertex cube with a different color
for each face (i.e., a Java 3D class ColorCube). 
Since the viewer is looking directly at the red face of the cube, what you actually see on
the screen is a red square on a black background.
COMP3419 Graphics and Multimedia   IMAGE-Lab (WEEK08)
4
TASK-2. Rotating the Cube
The above example is a good start, but the resultant cube looks like a 2D square. In this
task, a simple rotation of the cube will be made to show more than one face of the cube,
so that a nice 3D view of the cube can be presented. 
R  ead  the  provided  Java  program  “  HelloCube2.java  ”  (revised  from
HelloCube1.java  ),  and  see  how the  task  can  be  done  with  the  combination  of  3D
transformations.  The  first  step  is  to  create  the  desired  transformation  using  a
Transform3D object. Two parameters specify the rotation: the axis of revolution, and the
angle of rotation. The axis is chosen by selecting the proper method. The angle of rotation
is the value that is the argument to the method. Since the angle of rotation is specified in
radians, the value π/4 is 1/8 of a full rotation, or 45 degrees. Quite often a visual object is
translated  and  rotated,  or  rotated  about  two  axes.  In  either  case,  two  different
transformations are specified for a single visual object. The two transformations can be
combined into one transformation matrix and held by a single TransformGroup object. In
this task, the program rotates the cube around both the x and y-axes. Two Transform3D
objects, one for each rotation, are created. The individual rotations are specified for the
two  TransformGroup objects.  Then  the  rotations  are  combined  by  multiplying  the
Transform3D objects.  The combination  of the two transforms is  then loaded into the
TransformGroup object. 
R  un the program and compare the graphics result with the output from Task1. 
C  hange the angles of rotation  at   rotX()   and rotY().
TASK-3. Lighting up the 3D World
Lights  provide  the  sources  for  illuminating  the
graphics  objects  in  a  virtual  world.  Lights  have
color  attributes,  positions,  directions,  and  other
characteristics depending on their specific types. In
our lecture, we have introduced three major types of
lights in graphics systems: directional lights,  point
lights (a.k.a. omni lights), and spotlights. In addition,
there is one more popular 3D light call ambient light
that  is  uniform  in  all  directions  and  locations.  It
generally provides a simplified representation of the
numerous and weak inter-object reflections existing
in the real-world scene. Java 3D offers a family of Light classes for four different types
of lights  described above.  Java 3D lights  do not  produce shadows.  The Light class
hierarchy is shown in the following figure. 
In this task, we will work on how to display a ball lit by a red
directional light. The way the lights falls on a 3D object provides us
with the shading that helps us see shapes in three dimensions. In the
provided Java code “Ball.java”,
the background of scene is set to grey
color,  and  the  sphere  we  created  is
black,  it  appears  red  because  of  the
COMP3419 Graphics and Multimedia   IMAGE-Lab (WEEK08)
5
An ambient light represents weak random
colored light. Since it is a DirectionalLight, we also have to specify how far the light
shines and in what direction. In the program, 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). 
R  un the program and monitor the lighting effect on the sphere;
C  omplete the following questions:
Try using a SpotLight and  a AmbientLight.
TASK-4. Positioning the Objects
So far, in the above tasks, we have created 3D 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. 
R  un the provided program “  Position.java  ”, you will get a 3D scene, in which, x, y
and z are  represented  by spheres,  cones  and cylinders.  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. 
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  translation  which  is  a  kind  of
“transformations”, 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. 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), as shown in TASK-2. 
TASK-5. Setting up the Appearance of 3D Objects – Creating Materials
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. In this task and the following task, we will work on
how to use these functions.
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. 
COMP3419 Graphics and Multimedia   IMAGE-Lab (WEEK08)
6
Materials have five properties that enable you to specify how the object appears. There
are four colors: Ambient, Emissive, Diffuse, and Specular. The fifth property is Shininess,
which you can specify with a number. Each color specifies what light is given off in a
certain situation. 
 Ambient color reflects light that has been scattered so much by the environment that
the direction is impossible to determine. This is created by an AmbientLight in Java
3D.
 Emissive color is given off even in darkness. You could use this for a neon sign or a
glow-in-the-dark object
 Diffuse color reflects light that comes from one direction, so it's brighter if it comes
squarely down on a surface than if it barely glances off the surface. This is used with
a DirectionalLight.
 Specular light comes from a particular direction, and it tends to bounce off the surface
in a preferred direction. Shiny metal or plastic have a high specular component. The
amount of specular light that reaches the viewer depends on the location of the viewer
and the angle of the light bouncing off the object.
Changing the shininess factor affects  not just how shiny the object  is,  but whether  it
shines with a small glint in one area, or a larger area with less of a gleaming look. For
most objects you can use one color for both Ambient and Diffuse components, and black
for Emissive (most things don’t glow in the dark). If it’s a shiny object, you would use a
lighter color for Specular reflections. 
Create a material for a red billiard ball by modifying the “Ball.java” from TASK-3, with
the following setting: new Material(red, black, red, white, 70f).
TASK-6. Setting up the Appearance of 3D Objects – Texture Mapping with Images
Materials  make change to  the appearance  of a  whole shape,  but sometimes  even the
shiniest  objects  can  seem dull.  By  adding  texture  you  can  produce  more  interesting
effects like marbling or wrapping a two-dimensional image around your object.
The  TextureLoader class  enables  you  to  load  an  image  to  use  as  a  texture.  The
dimensions of your image must be powers of two, for example 128 pixels by 256. When
you load the texture you can also specify how you want to use the image. For example,
RGB to use the color of the image or LUMINANCE to see the image in black and white.
After the texture is loaded, you can change the TextureAttributes to say whether you
want the image to replace the object underneath or modulate the underlying color. You
can also apply it as a decal or blend the image with the color of your choice. 
If you are using a simple object like a sphere then you will also have to enable texturing 
by setting the “primitive flags”. These can be set to: Primitive.GENERATE_NORMALS + 
Primitive.GENERATE_TEXTURE_COORDS when you create the object.
R  un the program “  PictureBall.java  ” with the sample image as a texture map;
TASK-7. Java 3D and the User Interface
COMP3419 Graphics and Multimedia   IMAGE-Lab (WEEK08)
7
Most real-life applications use a mixture of 3D and 2D elements. In this task, we will 
work on how to combine your Java 3D with the rest of your program. Each area where 
3D graphics can be painted is called a Canvas3D.  This is a rectangle that contains a view
of the objects in your universe. You place the canvas inside a frame, then you create a 
universe to be displayed in the canvas. The provided program “CanvasDemo.java” in this
task shows how to create a canvas in a frame with labels at the top and bottom. The 
program can be run as either an applet or an application. 
R  un the program with your own labels;
Add a picture of your own (as a background image) onto your 3D scene.           
              
TASK-optional. Java 3D  to BufferedImage
Unfortunately, it is complicated.  It is much easier to get the generated 3D scene as 2D 
image in Processing.
At http://stackoverflow.com/questions/7074869/java3d-object-exported-as-
bufferedimage, it suggests to use Robot to capture the screen, which happens to be easiest
way!
Otherwise, you should look at http://stackoverflow.com/questions/1326827/render-to-
bufferedimage-in-java3d, which pointed out two java source codes to help you. To 
understand them, you have to read the first a few paragraphs at 
http://docs.oracle.com/cd/E17802_01/j2se/javase/technologies/desktop/java3d/forDevelo
pers/J3D_1_3_API/j3dapi/javax/media/j3d/Canvas3D.html to understand what are On-
screen Rendering and Off-screen Rendering.
REFERENCES
1. Getting Started with the Java 3D API, D.J. Bouvier, Sun Microsystems.
2. Java 3D Programming, D. Selman, Manning.
3. The Joy of Java 3D, G. Hopkins.
4. 3D User Interfaces with Java 3D, J. Barrilleaux.
5. Essential Java 3D Fast: Developing 3D Graphics Applications in Java, I. Palmer, Springer.
6. Java 3D API Jump Start, A.E. Walsh, D. Gehringer, Sun Microsystems.
COMP3419 Graphics and Multimedia   IMAGE-Lab (WEEK08)
8