VRML and JAVA - VRML Script Node and JAVA Why would you...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: VRML Script Node and JAVA Why would you want to use Java with your VRML? Java class has more flexibility to define complex behaviors Java has its simple networking classes create multi-user VRML worlds, visualizations of the Internet or visualizations of data warehouses. In Java (or JavaScript) you can manipulate VRML object properties programmatically and even generate new objects Why would you want to use VRML with your Java? 3-D animations with Java requires tremendous effort to build it. VRML: provides a specification of the entire 3D object and its basic properties small file sizes can achieve huge bandwidth savings plus cost savings by using a VRML world animated by a Java applet, rather than 2D GIF or JPEG images animated by a Java world Mechanism for integrating VRML and Java 3 Possible methods: 1. VRML Script Node 1. JavaScript/VRMLscript/ECMAscript - interpreted at runtime 2. Java - precompiled classes 3. both methods (Java/VRML/ECMAScript & Java) are supported in most commercial browsers 2. VRML EAI (External Authoring Interface) 1. Java classes for interacting with the VRML browser from an external Java applet 2. powerful, but requires the right combination of Web browser, VRML browser, and Java 3. VRML implemented (in whole or in part) within another programming environment 1. VRML loader for Java3D 2. VRML classes in Java, C++, and other scene graph libraries 3. usually no execution model -> developer must implement that in the native language Mechanism for integrating VRML and Java We focus on VRML 2.0 Script Nodes and Java classes VRML will describe the environment of the virtual world and Java will describe the change. Example: DEF fighter1 WWWInline { name "fighter1.wrl" } and then that fighter1 you DEF'd becomes an instance of a class that has various methods available. For instance, you might be able to call methods something like: fighter1.addChild(); fighter1.translate(); and so on. Example JAVA & VRML The example VRML file below is taken from ( http://www.vrmlsite.com/mar97/a.cgi/spot3.html) which shows how to create an intelligent agent that tracks your location and follows you. Example is divided into 2 steps: 1) how to create a randomly moving object (representing the agent), 2) how to have that object track your movement through the VRML scene and follow you Step 1 - Random movement of a scene object 1. #VRML V2.0 utf8 2. # an object is floating randomly. 3. # 4. DEF AGENT_TIME TimeSensor{ 5. loop TRUE 6. cycleInterval 0.1 7. } 8. DEF AGENT Transform{ # floating agent 9. children[ 10. Inline{url "ManDetailed.wrl"} # a model of agent. 11. ] 12. } 13. DEF AGENT_SCRIPT Script{ 14. url "FloatingAgent.class" 15. eventIn SFTime interval 16. eventOut SFVec3f setAgentPosition 17. } 18. ROUTE AGENT_TIME.cycleTime TO AGENT_SCRIPT.interval 19. ROUTE AGENT_SCRIPT.setAgentPosition TO AGENT.set_translation Explanation of Code Snippets At line 4 there is a TimeSensor which invokes a Script node named "AGENT_SCRIPT" every 0.1 seconds. Every 0.1 seconds it generates a cycleTime value, containing the current time, which is routed to the Script Node. The associated Java program uses this event to randomly move the Transform node which includes the geometry representing the scene object to be moved. As a result, the object appears to be floating back and forth in a random fashion. Explanation of Code Snippets Line 18 routes the event from the Timesensor to the Script node's eventIn field, interval. This is then passed across to the event handler class "FloatingAgent.class". 1. // an agent is floating randomly. 2. // 3. import java.util.*; 4. import vrml.*; 5. import vrml.node.*; 6. import vrml.field.*; 7. // 8. public class FloatingAgent extends Script{ 9. SFVec3f setAgentPosition; 10. float agentPosition = new float[3]; 11. Random randomNumGenerator = new Random(); 12. 13. public void initialize(){ 14. // get the reference of the event-out 'setAgentPosition'. 15. setAgentPosition = (SFVec3f)getEventOut("setAgentPosition"); 16. // initialize the agent position. 17. agentPosition[0] = 0.0f; 18. agentPosition[1] = 0.0f; 19. agentPosition[2] = 0.0f; 20. } Associated Java Class Associated Java Class 21. public void processEvent(Event e){ 22. if(e.getName().equals("interval") == true){ 23. moveAgent(); 24. } 25. } 26. // generate random float value ranging between -0.1 to 0.1. 27. float generateRandomFloat(){ 28. return(randomNumGenerator.nextFloat() * 0.2f - 0.1f); 29. } 30. // move the agent randomly. 31. void moveAgent(){ 32. agentPosition[0] += generateRandomFloat(); 33. agentPosition[1] += generateRandomFloat(); 34. agentPosition[2] += generateRandomFloat(); 35. 36. // move the agent to the new position. 37. setAgentPosition.setValue(agentPosition); 38. } 39. } Explanation of Code Snippets At line 13 - initialize() method is used to create a reference to the Script node. This field is used to receive the agent's new position when it is calculated. In addition, the agent's initial position is set up at line 18. At line 21 - the processEvent() method shows that the receipt of the TimeSensor event causes the script to generate a random number ranging from -0.1 to 0.1. Explanation of Code Snippets Using this random number, the script moves the Transform that represents the agent by simply setting its position co-ordinates to a random position within a small distance of its current position. At line 41 - This value is written back to the Script node using the setValue method on the setAgentPosition variable Since the setAgentPosition of the Script node is an event out, the value is then routed through to the agent's Transform node's translation field. Step 2 - Following the viewer A very simple extension to the 3 # initial viewpoint random agent example is to have 4 Viewpoint{ the agent follow the user as they 5 position 0 2 0 navigate through the scene. 6} 7 DEF PROX_SENSOR ProximitySensor{ To achieve this, all that needs to 8 size 100 100 100 #always in the sensor field. be added to the VRML file is a 9 } ... means to track the user's position ... and the mechanism to send this 24 DEF PROX_SCRIPT Script{ across to the event handler 25 url "FollowingAgent.class" class. 26 eventIn SFVec3f currentPosition 27 eventOut SFVec3f setAgentPosition 29 } 30 ROUTE PROX_SENSOR.position_changed TO 31 PROX_SCRIPT.currentPosition 32 ROUTE PROX_SCRIPT.setAgentPosition TO AGENT.set_translation Explanation of Code Snippets To track a user in the scene, a proximity sensor can be used (line 7). This is initialized with a bounding box sufficiently large to encompass the whole scene (line 8). Then a route is set up that sends the user's position, as recorded by the proximity sensor, to the script node (line 30 and 31) Explanation of Code Snippets Here is another code fragment which calculates the agents new position. The calculation simply reads the user's current X, Y and Z positions and adds a fixed offset held in agentDefaultPosition. 35 void moveAgent(){ 36 // translate the agent according to your current position. 37 agentPosition[0] = 38 yourPosition[0] + agentDefaultPositionX; 39 agentPosition[1] = 40 yourPosition[1]+ agentDefaultPositionY; 41 agentPosition[2] = 42 yourPosition[2]+ agentDefaultPositionZ; 43 // move the agent to the new position. 44 setAgentPosition.setValue(agentPosition); 45 } VRML 2.0 Script nodes In VRML 2.0, Script nodes allow you to fashion nodes that are described by Java classes using the VRML 2.0 Java API. So that complex animation / behavior can be generated In the Script node you can define the events the node receives and sends (using ROUTE). A Script node can also have fields that act as persistent local variables to the script VRML 2.0 Script nodes Syntax Script { url directOutput FALSE mustEvaluate FALSE #and any number of : eventIn type eventName eventOut type eventName field type fieldName initialValue } The type can be any of the standard VRML data types. The field, eventIn and evenOut syntax of the Script node each define an interface between the Script node and the program script indicated by the url field value. VRML 2.0 Script nodes A script can receive any number of events, the values where these events are stored are eventIn type variables. The name of the variables specify the name of the events the Script can receive. These variables are read-only, i.e. you can't set a value to one of these variables, you can only read its current value. The values associated with these events are stored in eventOut type variables. These variables, as well as fields, are persistent. Example For instance, if you are creating a multi-user Script node, you might have the following properties. 1. eventIn for movements by the user, 2. eventOuts for movements of the current avatars, 3. eventOuts to transition for new avatars entering the world, 4. eventOuts for garbage collection of avatars who have left the world, and a 5. field for the number of avatars currently in the world Example (cont..) When it comes to programming the actual Java class for this Script node: fields and eventOuts correspond to properties of the class, while eventIns correspond to class methods. eventOuts can be referenced using their setValue() method. In addition there is a constructor method for the class. Plus there is also an eventsProcessed method, which will be called after a set of events has been received by the Script node. Java class Java classes for use with the Script node must import the vrml package. They also are extensions of the Script class in the vrml package, as shown below. import vrml; class MultiUser extends Script { ... } The bridge The bridge between the VRML and Java worlds was provided by the special Script node which passed events from the VRML world across to the event handler class (Java). Events generated in the VRML scene, when routed to the eventIn fields of the Script node are delivered into the event handler class of the associated Java program as specified in the Script node. Conversely, within the Java code, an event can be written into the Script node in the VRML scene which can then be routed to other nodes in the scene. Java Class Script vrml.node Class Script java.lang.Object vrml.BaseNode vrml.node.Script public abstract class Script extends BaseNode This is the general Script class, to be subclassed by all scripts. Note that the provided methods allow the script author to explicitly throw tailored exceptions in case something goes wrong in the script. Java Class Script Methods: eventsProcessed() getEventIn (java.lang.String eventInName) getEventOut(java.lang.String eventOutName) getField(java.lang.String fieldName) initialize() processEvent(Event event) processEvents(int count, Event events) shutdown() toString() How does JavaScript interact with VRML? To use JavaScript with VRML, you want to create an HTML file with frames, with one frame -- let's call it vrmlframe -- for your VRML output. Here's what the overall HTML frames file might look like: <HTML> <HEAD> <FRAMESET cols="30%, 70%"> <FRAME NAME="htmlframe" SRC="dummy.html"> <FRAME NAME="vrmlframe" SRC="blank.wrl"> </FRAMESET> </HEAD> </HTML> How does JavaScript interact with VRML? Then your dummy.html will contain the JavaScript to create your world, and maybe some HTML to get things going. A simple function could be as follows: function do_vrml() { vrml = parent.vrmlframe; vrml.document.open( "x-world/x-vrml" ); vrml.document.writeln("#VRML V2.0 ascii"); vrml.document.writeln("Separator { AsciiText { string \"hello VRML world\" } }"); vrml.document.writeln("}"); vrml.document.close(); } Tutorial Question Create a virtual world using VRML. Then demonstrate how object (s) in the virtual world can be manipulated using Java. References Andrian Scott, 1996, The marriage of Java and VRML http://www.vrmlsite.com/sep96/spotlight/javavrml/javavrml.html Rodger Lea, Java and VRML 2.0 Part 2: Putting theory into practice, http://www.vrmlsite.com/mar97/a.cgi/spot3.html Script Node tutorial (with JavaScripts example) http://www.lighthouse3d.com/vrml/tutorial/script.shtml VRML/Java/Javascript FAQ at http://www.aereal.com/faq/ Visit http://www.frontiernet.net/~imaging/avatar_builder.htm for interesting applications of VRML and JAVA ...
View Full Document

This note was uploaded on 02/06/2012 for the course FACULTY OF WXGE6320 taught by Professor Noraini during the Winter '09 term at University of Malaya.

Ask a homework question - tutors are online