2.2.1 A simple 3D projection routine | JAVA 3D Programming | Chapter 2
2.2.1 A simple 3D projection routine
Here is my simple 3D−projection routine. The
projectPoint method takes two Point3d instances, the first is the input
3D−coordinate while the second will be used to store the result of the
projection from 3D to 2D coordinates (the z attribute will be 0). Point3d is
one of the classes defined by Java 3D. Refer to the Java 3D JavaDoc for
details. Essentially, it has three public members, x, y, and z that store the
coordinates in the three axes.
From AwtRenderingEngine.java
private int xScreenCenter = 320/2;
private int yScreenCenter = 240/2;
private Vector3d screenPosition = new Vector3d( 0, 0, 20 );
private Vector3d viewAngle = new Vector3d( 0, 90, 180 );
private static final double DEG_TO_RAD = 0.017453292;
private double modelScale = 10;
CT = Math.cos( DEG_TO_RAD * viewAngle.x );
ST = Math.sin( DEG_TO_RAD * viewAngle.x );
CP = Math.cos( DEG_TO_RAD * viewAngle.y );
SP = Math.sin( DEG_TO_RAD * viewAngle.y );
public void projectPoint( Point3d input, Point3d output )
{
double x = screenPosition.x + input.x * CT − input.y * ST;
double y = screenPosition.y + input.x * ST * SP + input.y * CT * SP
+ input.z * CP;
double temp = viewAngle.z / (screenPosition.z + input.x * ST * CP
+ input.y * CT * CP − input.z * SP );
output.x = xScreenCenter + modelScale * temp * x;
output.y = yScreenCenter − modelScale * temp * y;
output.z = 0;
}
Let’s quickly project some points using this
routine to see if it makes sense. The result of running seven 3D points through
the projectPoint method is listed in table 2.1.
CT: 1
ST: 0
SP: 1
CP: 0
Table 2.1 Sample output from the projectPoint
method to project points from 3D−world coordinates to 2D−screen Coordinates
WX
|
WY
|
WZ
|
SX
|
SY
|
1
|
1
|
0
|
250
|
30
|
-1
|
1
|
0
|
70
|
30
|
1
|
-1
|
0
|
250
|
210
|
-1
|
-1
|
0
|
70
|
210
|
0
|
0
|
0
|
160
|
120
|
1
|
1
|
1
|
255
|
25
|
-1
|
-1
|
1
|
65
|
215
|
Figure
2.3 The positions of some projected points
Plotting these points by hand using a 2D graphics
program (figure 2.3), you can see that they seem to make sense. Projecting the
point 0,0,0 places a point at the center of the screen (160,120). While you
have symmetry about the corners of the cube, increasing the Z−coordinate
appears to move the two opposing corners (1,1,1 and −1,−1,1) closer to the
viewer.
Taking a look at the projectPoint function again,
you can see it uses the following parameters:
- Input point x, y, and z coordinates
- Center of the screen
- Sin and cosine of the viewer’s angle of view
- Distance of the screen from the viewer
- Model scaling factor
This very simple projection function is adequate
for simple 3D projection. As you become more familiar with Java 3D, you will
see that it includes far more powerful projection abilities. These allow you to
render to stereo displays (such as head−mounted displays) or perform parallel
projections. (In parallel projections, parallel lines remain parallel after
projection.)
Comments
Post a Comment