Logo
News Software Inside Support
Software
Simlets Tutorials Controller and Message Passing Tutorial 2
Separating Model from View
Simlets Tutorials Controller and Message Passing

Overview

In the MVC or Model-View-Controller paradigm, the job of managing the data, displaying the data and handling user or system inputs are divided into three components called the Model, View and Controller, respectively. This tutorial will focus on the Model and View components and the MVC paradigms' impact on developing in the Simulation Container.

This tutorial is based on NeHe Lesson 19. Refer to that lesson for original code, background and motivation on OpenGL.

Setup

Download, setup and install the Simulation Container. Download and extract the tutorials. Set tutorial2 as the active simulation.

Usage in the Simulation Container

Start the Simulation running. Notice the large particle explosion followed by the gradual downward flow of particles.

Open ParticleView.java in an editor. Find the line (about line 142) that reads:

gl.glColor4f(particle.r, particle.g, particle.b, particle.life);

and change it to read:

gl.glColor4f(1.0f, 0.0f, 0.0f, particle.life);

Make sure that the file has been saved and recompile the class. Be sure that there are no compilation errors.

Note the state of the running simulation and refresh the simulation. Notice that the positions of the particles (the Model) did not change -- only the look of the particles (the View) changed from multi-colored to all red. This highlights one of the most important aspects of the Simulation Container: the ability to change one simlet in the simulation without affecting the others.

If the simulation stopped running or the color of the particles did not change then it is likely that there was a error while compiling. Ensure that the class was compiled and that there are no errors. Refer to the trouble shooting section for more information.

Next, recompile ParticleModel and refresh the simulation. (No code change is needed. Simply add a new blank line to the file and recompile.) Notice that the particle explosion happened just as when you first started the Simulation. This is because the initGL() method of ParticleModel resets the position of all of the particles. When the simlet is redeployed the initGL() method is called.

Code Structure

There are 4 classes in this simulation: Particle, ParticleCache, ParticleModel and ParticleView. The first three classes, Particle, ParticleCache and ParticleModel make up the Model or data of the simulation. The remaining class, ParticleView, is the View. The breakdown of components is seen below:

Particle Model-View

Let us look at the function of each of these classes in turn:

  • Particle is a simple structure-like object that contains the position, velocity, color, and life-time attributes.
  • The Particles are held by the ParticleCache. The ParticleCache provides a convenient way to access the particles.
  • The ParticleModel initializes and updates the Particle.
  • The ParticleView draws each Particle in the ParticleCache to the display.

ParticleModel and ParticleView are each simlets.

Pipeline Overview

Look at the pipeline section of the simulation deployment descriptor (found in the simulation.xml file). Notice the order in which the simlets are processed. The full simlet life-cycle including processing is shown below:

Particle Pipeline

Pipelines have the same life-cycle methods as simlets. In fact the only difference between pipelines and simlets is that pipelines can contain other pipelines or simlets. This is seen in the process() method of the pipeline. process() executes the process() methods of the simlets.

ParticleModel has only initialization and processing. ParticleView has initialization and rendering (the process() method of ParticleView is found in GLStandaloneSimlet).

The flow for the simulation is:

Initialization:

  • All particles are created and initialized in ParticleModel. Because ParticleModel has the lowest load-on-startup value in the deployment descriptor it is initialized first.
  • GLStandaloneSimlet creates and initializes the GL contexts.
  • ParticleView loads the particle texture is loaded and the establishes the initial GL state.

Processing:

  • Because ParticleView is listed first in the pipeline (in the deployment descriptor ) it is processed first. GLStandaloneSimlet sets the viewport and establishes the projection matrix.
  • ParticleView draws all Particles.
  • GLStandaloneSimlet swaps the buffers and the objects are shown on the screen.
  • ParticleModel updates the position, velocity and life-time of each Particle.

Destruction:

  • GLStandaloneSimlet destroys the GL context. Neither ParticleModel nor ParticleView have explicit destroy() methods.

Conclusion

As you can literally see, the separation of View (rendering) from Model (the position, velocity and color) adds extraordinary power and flexibility to the usefulness of the Simulation Container. Added to this is the simplification, modularity and encapsulization inherent to simlets shown by changing the particles appearance independently from the particles motion.

Simlets Tutorials Controller and Message Passing Simlets Tutorials Controller and Message Passing
Copyright 2002-2005, Reality Interactive, Inc.