This package contains the implementation of classes that describe the application states of the traffic 3D visualizer. The “appstate” package consists of the following classes:
1) “TrafficEnvironmentAppState”
2) “TrafficSimulatingAppState”
1. Implementing the “TrafficEnvironmentAppState” class
The “TrafficEnvironmentAppState” class describes the traffic environment application state. Everything that is not simulated, for example, terrain, sky, water, light, etc., is attached to this application state. In addition, the application state class implements the mechanism that allows the lights of the 3D visualization to be turned on and off. There are two lights in the simulation, one direction light, representing the Sun, and another ambient light, representing the natural ambient light. Also, this class implements the ability for showing or hiding the visualized objects attached to its state. To implement this class:
- Inside the package “edu.utdallas.mavs.traffic.visualization.vis3D.appstate”, create a class named “TrafficEnvironmentAppState” that extends the class “EnvironmentAppState” defined in the package “edu.utdallas.mavs.divas.visualization.vis3D.appstate”.
- The class “TrafficEnvironmentAppState” provides concrete implementations of the following abstract methods defined originally in the jMonkey “AbstractAppState” class:
- unloadLights: This method unload the lights from the 3D visualizer.
- loadLights: There are two lights in the simulation, one direction light that represents the Sun, and another ambient light that rep
- stateDetached: This method detaches the sky box, lights, water, and terrain effects from the visualization scene.
- initialize: This method initializes the visualization scene by loading the sky box, lights, water, and terrain effects. In addition,resents the natural ambient light.
The following code provides the full implementation of the class TrafficEnvironmentAppState:
package edu.utdallas.mavs.traffic.visualization.vis3D.appstate; import com.jme3.app.Application; import com.jme3.app.state.AppStateManager; import com.jme3.light.AmbientLight; import com.jme3.light.DirectionalLight; import com.jme3.math.ColorRGBA; import com.jme3.math.Vector3f; import edu.utdallas.mavs.divas.core.config.VisConfig; import edu.utdallas.mavs.divas.visualization.vis3D.appstate.EnvironmentAppState; import edu.utdallas.mavs.divas.visualization.vis3D.vo.effect.Terrain; import edu.utdallas.mavs.divas.visualization.vis3D.vo.effect.Water; import edu.utdallas.mavs.traffic.simulation.config.TrafficVisConfig; import edu.utdallas.mavs.traffic.visualization.vis3D.vo.Skybox; /** * This class describes the traffic environment application state. * * Everything that is not simulated, for example, terrain, sky, water, light, etc., is attached to this application state. * This application state allows the lights of the 3D visualization to be turned on and off, having the effect of showing * or hiding the visualized objects attached to this application state. * * There are two lights in the simulation, one direction light, representing the Sun, and another ambient light, representing the natural ambient light. */ public class TrafficEnvironmentAppState extends EnvironmentAppState { private DirectionalLight sun; private AmbientLight al; boolean waterLoaded = false; boolean terrainLoaded = false; @Override public void update(float tpf) {} @Override public void stateDetached(AppStateManager stateManager) { super.stateDetached(stateManager); Skybox.unloadSky(); if(waterLoaded) { waterLoaded = false; Water.unloadWater(app); } unloadLights(); if(terrainLoaded) { terrainLoaded = false; Terrain.unloadTerrain(); } } @Override public void initialize(AppStateManager stateManager, Application app) { super.initialize(stateManager, app); app.getCamera().setFrustumFar(20000); Skybox.loadSky(); if(VisConfig.getInstance().getCustomProperty(TrafficVisConfig.WATER)) { waterLoaded = true; Water.loadWater(app); } loadLights(); if(VisConfig.getInstance().getCustomProperty(TrafficVisConfig.TERRAIN)) { terrainLoaded = true; Terrain.loadTerrain(); } app.getCamera().setLocation(new Vector3f(30, 30, 30)); app.getCamera().lookAt(new Vector3f(-150, 15, -150), Vector3f.UNIT_Y); } @Override public void unloadLights() { app.getRootNode().removeLight(sun); app.getRootNode().removeLight(al); } @Override public void loadLights() { sun = new DirectionalLight(); sun.setDirection(new Vector3f(-0.577f, -0.577f, -0.577f)); app.getRootNode().addLight(sun); al = new AmbientLight(); // al.setColor(ColorRGBA.White.mult(2.5f)); al.setColor(new ColorRGBA(1, 1, 1, 0)); app.getRootNode().addLight(al); } }
2. Implementing the “TrafficSimulatingAppState” class
This class represents the main application state of the traffic 3D visualizer. It allows users to interact with the simulation by selecting visualized objects (VOs) and triggering events in the simulation. It also allows users to add agents to simulation. To implement this class:
Inside the package “edu.utdallas.mavs.traffic.visualization.vis3D.appstate”, create a class named “TrafficSimulatingAppState” that extends the class “SimulatingAppState” defined in the package “edu.utdallas.mavs.divas.visualization.vis3D.appstate”.
package edu.utdallas.mavs.traffic.visualization.vis3D.appstate; import com.jme3.math.Vector3f; import edu.utdallas.mavs.divas.visualization.vis3D.appstate.SimulatingAppState; import edu.utdallas.mavs.divas.visualization.vis3D.common.InputMode; import edu.utdallas.mavs.divas.visualization.vis3D.dialog.NiftyScreen; import edu.utdallas.mavs.traffic.visualization.vis3D.TrafficApplication; import edu.utdallas.mavs.traffic.visualization.vis3D.dialog.vehicleModification.VehicleModificationDialog; /** * This class represents the main application state of the traffic 3D visualizer. * * It allows users to interact with the visualized simulation, by selecting visualized objects (VOs) and triggering events to the simulation. * It also allows users to add agents to simulation. */ public class TrafficSimulatingAppState extends SimulatingAppState<TrafficApplication> { @Override protected void triggerEvent(Vector3f position) { // specific events here } @Override public void setCursor(InputMode inputMode) { super.setCursor(inputMode); // specific modes here } @Override protected InputMode getMappedEventInputMode(String inputMode) { InputMode mappedInputMode = InputMode.SELECTION; // specific modes here return mappedInputMode; } @Override protected void setupContextObservers() { super.setupContextObservers(); contextObservers.add(new VehicleModificationDialog(NiftyScreen.windowLayerElement)); } }