A 3D Example cont'd.
All 3D development environments like OPEN GL, DirectX, and Java 3D, have to implement these concepts one way or another. Processing does a really good job of simplifying them as much as possible and at getting a lot of the preparatory stuff out of the way.
You can now see the idea behind the project in Processing terms: the sphere object rotates around the wall (a box object), while the camera moves in response to keyboard controls. a/d/w/x change the x/y coordinates and S/s change the z coordinate of the camera.
The rotation of the ball (as well as the placement of any object in the 3D space) is accomplished by the translate statement. The parameters of the translate function are added to the current position (0, 0, 0 initially) and this becomes the new current position. Calling a graphic object constructor at this point (for example, sphere; there are two 3D primitives in Processing, sphere and box, and quite a few 2D primitives, such as point, line, arc, rectangle, triangle, etc.) will draw the respective shape at the current position.
The application loop starts by setting the position of the camera and defining two spotlights.
camera( camX, camY, camZ, centerX, centerY, centerZ, 0.0, 1.0, 0.0 );
spotLight( 255, 0, 0, 200, 400, 200, 0, -1, 0, PI/2, 2 );
spotLight( 0, 255, 0, 200, 0, 200, 0, 1, 0, PI/2, 200 );
As you can see, besides the six parameters already discussed, camera takes three more, which indicate the upward facing axis.
The spotLight statement has to be included in the loop to persist between iterations. The first three parameters define the color (in RGB or hue values format), the next three the coordinates define the light, the next three, the direction of the light, and the last two, the angle of the cone and the center bias of the cone.
Processing has a few more light types: ambient, directional, and point.
The remainder of the code is not difficult to understand. First, the current drawing point is set to the center of the room:
translate( centerX, centerY, centerZ );
Next, the ball is drawn. Use fill to set the color to a combination of red and green with full opacity. noStroke and sphereDetail render the ballwithout wireframes and at a level of detail that is not too code-intensive. Next, you compute the coordinates of the ball (as it rotates in the y/z plane around the wall) and the drawing point is set to these coordinates. Notice that translate, which sets the current drawing point (or, more precisely, sets the displacement amount), has an additive effect: the new coordinates are computed relative to the old coordinates. Finally, the angle of the ball (used to calculate the next position) is incremented by a PI/50 step:
fill( 255, 255, 0, 255 );
noStroke();
z1 = z * cos( angle ) - y * sin( angle );
y1 = y * cos( angle ) + z * sin( angle );
sphereDetail( 20 );
translate( 0, y1, z1 );
sphere( 35 );
angle += PI/50;
Next, draw the wall by resetting the current drawing point to the center of the space:
stroke( 1 );
fill( 255, 255, 0, 100 );
translate( 0, -y1, -z1 );
box( plankSide, plankDepth, plankSide );
The final section of the code deals with the text, which is displayed at a fixed position:
fill( 0, 0, 0 );
translate( centerX / 2, -1, 0 );
rotateY( PI );
text("X " + nfc( camX , 2) + " Y " + nfc( camY, 2) +
" Z " + nfc( camZ, 2 ), 0, 0);
All in 127 linesnot bad. On my test workstation, the performance is smooth and adequate for Web execution.
A Good Compromise
The obvious comparisons that should be made are between Java 3D and Processing. Developing a similar application in Java 3D would be a more complex endeavorJava 3D uses a tree structure to represent a 3D universe. Populating a tree is the equivalent of using the setup function in Processing; it's more versatile, but it's also more complex. Each object in the universe has to be added to the structure. Java 3D and Processing both offer the same types of lights. Java 3D offers more 3D primitives such as cylinders and cones, and supports custom 3D shapes; Processing has a third-party library that allows it to use Alias .obj files. An enthusiastic user community has developed libraries for camera detection, fog generation, sound processing, database interaction, and even GUI widgets.
Although Processing does not offer all the features available in Java 3D, it significantly reduces the complexity of 3D application development, while being surprisingly feature-rich. Its runtime performance is more than adequate. Java developers looking for a 3D toolkit for the Web (or even for the desktop) should consider giving Processing a try.
Related Resources
Razvan Petrescu has eight years experience in technology as a systems analyst, architect, developer, and systems administrator.
| Home / Articles
/ Processing: Open Source Language Brings You Closer to Web 2.0 / 1 / 2 / 3 / |
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|