Examples‎ > ‎

3D scene description language

by Luis Quesada, M.Sc.


We present an example language that allows the specification and rendering of complex 3D objects using the reference resolution capabilities of ModelCC.

First, we will outline the features we wish to include in our 3D object specification language.
Then, we will provide the full language specification for ModelCC by defining an abstract syntax model, which will be annotated to specify the desired ASM-CSM mapping.

Language Specification 
Our 3D object specification language is designed to support the following features:
  • A special section, denoted by the "scene" keyword, delimits the statements that will be used for rendering the scene.
  • The definition of custom objects, which are identified by an object name. As references can be lazily resolved, recursion is allowed.
  • Scoped statements, delimited by "{" and "}'", that allow the specification of lists of statements that will run sequentially in a new OpenGL scope (that is, emulating a "glPushMatrix" before executing the statements and a "glPopMatrix" after executing the statements).
  • Composite statements, delimited by "[" and "]", that allow the specification of lists of statements that will run sequentially, but without creating a new OpenGL scope.
  • Repeated statements that allow the repetition of a statement, a group of statements, or a block of statements, a specific number of times.
  • Object statements, which draws either basic objects (e.g. a cube) or user-defined objects. Draw statements allow the specification of a numeric parameter. The "next" keyword is replaced in runtime by the current parameter decreased by one, and draw statements will not run when the parameter is 0.
  • State-machine OpenGL-like scale transformation statements, which support the specification of a combination of x, y, and z values in any order, or a single scaling factor that will be applied to the three axes.
  • State-machine OpenGL-like rotate transformation statements, which support the specification of the angle and a combination of x, y, and z axis values in any order.
  • State-machine OpenGL-like translate transformation statements, that support the specification of a combination of x, y, and z values in any order.
  • State-machine color transformation statements, which support the specification of a combination of red, green, blue, and alpha values in any order, and allow either absolute (by default) or relative color adjustments.
Example input and output:  
  •    Input:

define MengerSlice {
  scale x 0.3333333 y 0.3333333 % divide into 3x3
  { translate x -1 y -1 draw MengerSlice next }
  { translate x 1 y -1 draw MengerSlice next }
  { translate x -1 y 1 draw MengerSlice next }
  { translate x 1 y 1 draw MengerSlice next }
  color relative red -0.2 green -0.2 blue -0.2
  { translate x 0 y -1 draw MengerSlice next }
  { translate x -1 y 0 draw MengerSlice next }
  { translate x 1 y 0 draw MengerSlice next }
  { translate x 0 y 1 draw MengerSlice next }
} last {
  draw cube % a $0$-Menger sponge slice is filled.
}
scene {
  scale x 4 y 4 z 0.15
  color red 1 green 0.6 blue 0.3
  { draw MengerSlice 1 }
  { translate x 1.1 draw MengerSlice 2 }
  { translate x 2.2 draw MengerSlice 3 }
  { translate x 3.3 draw MengerSlice 4 }
}
   
            Output:


  • Input:
scene {
  color red 0 green 0 blue 0
  repeat 10 times [
    {
      repeat 10 times [
        {
          repeat 10 times [
            draw cube
            translate x 1.8
            color relative red 0.1
          ]
        }
        translate y 1.8
        color relative green 0.1
      ]
    }
    translate z 1.8
    color relative blue 0.1
  ]
}

            Output:


  • Input:
define snail [
  color red 1 green 0.6 blue 0.2
  draw tail next
]
define tail [
  {
    scale 0.6
    draw cube
  }
  {
    color blue 0.3
    scale 0.1
    translate y 2.5
    repeat 11 times [
      translate y 1
      color relative blue +0.01 alpha -0.1
      draw cube
    ]
  }
  rotate angle 1 z 1
  translate x 0.3
  rotate z 1 angle 3
  scale 0.995
  color relative red -0.003 green -0.001 blue -0.001
  draw tail next
]
scene {
  draw snail 700
}

            Output:




ModelCC Implementation

In ModelCC, the abstract syntax model is designed first and then it is mapped to a concrete syntax model by imposing constraints by means of metadata annotations on the abstract syntax model.

The resulting model can be processed by ModelCC to generate the corresponding parser.
The following UML class diagram presents our annotated 3D object specification language model.
      
       

The actual code needed to generate and invoke the parser in ModelCC is the following.

ModelReader reader = new JavaModelReader(Scene.class);       // Prepare the Java model reader
Model model = reader.read();                                 // Read the model
Parser<Scene> parser = ParserGenerator.create(model);        // Generate the parser
Scene scene = parser.parse(new FileInputStream("mss.txt"));  // Parse the input file and
                                                             // instantiate the corresponding scene 
scene.draw();                                                // Draw the scene

ModelCC reference support can be observed in the Definition, ObjectName, and DefinedObject classes. The name member of the Definition class is annotated with @ID, which means that a Definition instance can be identified by an ObjectName. Then, the ref member of a DefinedObject is annotated with @Reference, which means that, in textual form, a DefinedObject can refer to a Definition by its ObjectName. ModelCC reference resolution allows references to be resolved during the parsing process and makes the implementation of a traditional symbol table unnecessary.

It should be noted that certain constraints cannot be expressed in the abstract syntax model. However, these constraints can be expressed as custom constraints using the @Constraint annotation. In our example, some statements corresponding to elements in our model, such as draw statements and repeat statements, will not accept real values as parameters. These custom semantic constraints are implemented in the checkArguments() methods of the language elements classes corresponding to those statements.


Download

Binaries (ZIP file, 3.6MB)

Source code (ZIP file, 5.9MB)