advertisement
javaboutique
Search Tips
Articles  |   Tutorials  |   Reviews  |   Tools  |   by Category  |   by Date  |   by Name  |   Submit  |   Source  |   Forums  |  
javaboutique
Browse DevX


Partners & Affiliates











advertisement

Tutorials : Processing: Open Source Language Brings You Closer to Web 2.0 :

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 ball—without 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 lines—not 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 endeavor—Java 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 /

How to Add Java Applets to Your Site

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.

 DevX Skillbuilding from IBM developerWorks
 RIA Run Contest: Build Next-Gen Apps in Microsoft Silverlight 2
 Avaya DevConnect Center
 Intel Go Parallel Portal
 Internet.com eBook Library
 Microsoft RIA Development Center
 Destination .NET
XML error: not well-formed (invalid token) at line 53
advertisement
Receive Articles via our XML/RSS feed
Receive Articles via our XML/RSS feed

JavaBytes
Internet Cyclone
This powerful, easy-to-use, internet optimizer is for Windows 95, 98, ME, NT, 2000 and XP. It's designed to automatically optimize your Windows settings, boosting your Internet connection up to 200%.

RIM Ups Ante With Mobile Software Push
Novell Readies Silverlight Clone for Linux
Yahoo Pitches The 'Next Generation of Search'
Alfresco's Latest ECM: Prying Open a Sector?
SaaS Tool Offers Custom Database Development
Microsoft’s Automated Agent: Can We Talk?
Borland Finally Sells CodeGear
Red Hat Heads for the JON 2.0
Out with the Old, in with the New at JavaOne
Trolltech Expands WebKit Footprint

Create Secure Java Applications Productively, Part 1: Use Rational Application Developer and Data Studio
.NET Building Blocks: Custom User Control Fundamentals
Secure Internet File-Sharing with PHP, MySQL, and JavaScript
Getting Started with TBB on Windows
Moving to VoIP: Should You Go It Alone?
Introduction to the WPF Command Framework
7.0, Microsoft's Lucky Version?
Will Hyper-V Make VMware This Decade's Netscape?
Eliminate Fragmentation Frustration with Netbiscuits
Taming Trees: Building Branching Structures

Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Microsoft Article: Will Hyper-V Make VMware This Decade's Netscape?
Microsoft Article: 7.0, Microsoft's Lucky Version?
Microsoft Article: Hyper-V--The Killer Feature in Windows Server 2008
Avaya Article: How to Feed Data into the Avaya Event Processor
Microsoft Article: Install What You Need with Windows Server 2008
HP eBook: Putting the Green into IT
Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
Avaya Article: Setting Up a SIP A/S Development Environment
IBM Article: How Cool Is Your Data Center?
Microsoft Article: Managing Virtual Machines with Microsoft System Center
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Video: Are Multi-core Processors Here to Stay?
On-Demand Webcast: Five Virtualization Trends to Watch
HP Video: Page Cost Calculator
Intel Video: APIs for Parallel Programming
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Sun Download: Solaris 8 Migration Assistant
Sybase Download: SQL Anywhere Developer Edition
Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
Red Gate Download: SQL Compare Pro 6
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
IBM Article: Collaborating in the High-Performance Workplace
HP Demo: StorageWorks EVA4400
Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES