Model Primitive

The model primitive can load and render AGI's MDL models. MDL is the 3D model format used by STK. Models can be downloaded from AGI's website.

Models can be initialized as shown below:

[C#] Copy Code
IAgStkGraphicsSceneManager manager = ((IAgScenario)root.CurrentScenario).SceneManager;
IAgStkGraphicsModelPrimitive model = manager.Initializers.ModelPrimitive.InitializeWithStringUri(modelUri);
Array position = new object[] { 39.88, -75.25, 5000 };
model.SetPositionCartographic("Earth", ref position);

The orientation of the model depends on the model itself. For example, in the hellfire.mdl model, the missile is laid out lengthwise along the positive x axis. To change the orientation, use the Orientation property and provide an IAgOrientation that defines the rotation from the model's local axes to the axes of the reference frame in which the model's position is defined. In this case, the model's position is defined in the central body's fixed frame. A convenient way to compute the orientation is shown below:

[C#] Copy Code
IAgPosition origin = CreatePosition(root, 39.88, -75.25, 0);
IAgCrdnAxesFixed axes = CreateAxes(root, "Earth", origin);
IAgCrdnAxesFindInAxesResult result = root.VgtRoot.WellKnownAxes.Earth.Fixed.FindInAxes(0, (IAgCrdnAxes)axes);
IAgOrientation orientation = result.Orientation;

Array eulerAngles = orientation.QueryEulerAnglesArray(AgEEulerOrientationSequence.e213);
eulerAngles.SetValue((double)eulerAngles.GetValue(0) + Math.PI / 2, 0);
orientation.AssignEulerAngles(AgEEulerOrientationSequence.e213, eulerAngles.GetValue(0), eulerAngles.GetValue(1), eulerAngles.GetValue(2));

model.Orientation = orientation;

A topographic axes is created based on the model's position where the x axis points in the local north direction, the y axis points in the local east direction and the z axis points in the local down direction. An IAgOrientation that defines the rotation from these axes to the fixed frame axes is created. To point the model in the local up direction, the IAgOrientation is modified to rotate an extra 90 degrees around the y axis.

Like orientation, the size of the model depends on the model itself. To change the size of the model, use the Scale property. After a model is initialized, its position can be changed with Position.

See the Video Streams on Models overview for more information.

Model Articulations

The model primitive supports model articulations, which allow different parts of the model to be rotated, scaled, and/or translated. If the model supports articulations, they can be accessed through the ModelPrimitive'sArticulations property.

An example of how to modify a model's articulations is shown below.

The model primitive without modifying the spin ModelTransformation of the propeller's ModelArticulation:

The same model with the spin of the propellers modified to be 135 degrees:

[C#] Copy Code
// Create the model and set the position.
IAgStkGraphicsModelPrimitive model = manager.Initializers.ModelPrimitive.InitializeWithStringUri(modelUri);
Array position = new object[] { 36, -116.75, 25000.0 };
model.SetPositionCartographic("Earth", ref position);

// Modify the roll and yaw of the entire model so that the model appears to be parallel
// with the horizon. This method is only to demonstrate how articulations can be used.
// the preferred method is to set the model's orientation.
model.Articulations.GetByName("Commuter").GetByName("Roll").CurrentValue = 234;
model.Articulations.GetByName("Commuter").GetByName("Yaw").CurrentValue = -25;

// Modify the spin of the propellers.
model.Articulations.GetByName("props").GetByName("Spin").CurrentValue = 135;

The Articulations property of the ModelPrimitive is an instance of ModelArticulationCollection. ModelArticulationCollection contains a collection of ModelArticulations. ModelArticulation contains a collection of ModelTransformations. Both the ModelArticulationCollection and ModelArticulation classes accept an integer index to access its values. Use the ModelTransformation.CurrentValue property to set the value of the transformation as shown in the code above. The Type of transformation can also be retrieved using the ModelTransformation.Type property which returns a value of the ModelTransformationType enumeration.

Within your shader code, you have access to scene data that is useful for shading your model. We refer to these as Preset Uniforms because they are accessed as uniform variables in your vertex or fragment shader. These variables must be declared as uniforms and then can be used. Here is an example using STK Engine's animation time to move a vertex.

[GLSL] Copy Code
uniform float agi_Time;
uniform float fullOffset;

// Vertex Shader that oscillates from position to position+fullOffset along the Z axis.
void main()
{
     // Get fractional part of time
     float t = fract(agi_Time);

     // Move apply modelview and projection
     vec4 pos = gl_ModelViewProjectionMatrix * gl_Vertex;

     // Move vertex a certain percentage of offset
     pos.z += fullOffset * t;

     // Set position
     gl_Position = pos;
}

Preset Uniforms

Preset Uniform Type Description
agi_ActiveLights bool[gl_MaxLights] Light i is active if agi_ActiveLights[i] is true and inactive if false. Light properties can be accessed through gl_LightSources.
agi_CameraPosition vec3 Position of the camera relative to the model's reference frame.
agi_CentralBodyDepthTexture0,
agi_CentralBodyDepthTexture1,
...,
agi_CentralBodyDepthTexture7
sampler2D A texture that contains the depth values after rendering the Central Body. The number binds the texture to a particular texture unit. See example image below.
agi_LightingEnabled float 1 if lighting is enabled and 0 otherwise
agi_LODGeometricToleranceOverDistance float When multiplied by the distance from the vertex or fragment to the viewer, the result is the geometric error in world space.
agi_LODNegativeGeometricToleranceOverDistance float Negative of agi_LODGeometricToleranceOverDistance
agi_ModelViewMatrix mat4 Modelview matrix
agi_ModelViewMatrixRelativeToCameraPosition mat4 Modelview matrix relative to the camera position
agi_ModelViewMatrixRelativeToPrimitiveCenter mat4 Modelview matrix relative to the model's center
agi_ModelViewProjectionMatrix mat4 Modelview projection matrix
agi_ModelViewProjectionMatrixRelativeToCameraPosition mat4 Modelview projection matrix relative to the camera position
agi_ModelViewProjectionMatrixRelativeToPrimitiveCenter mat4 Modelview projection matrix relative to the model's center
agi_OrthographicProjectionMatrix mat4 Orthographic projection matrix
agi_PerspectiveProjectionMatrix mat4 Perspective projection matrix
agi_PrimitiveBoundingSphere vec4 The model's bounding sphere relative to the model's reference frame. The structure is xyz components are the position and w is the radius in meters.
agi_PrimitiveBoundingSphereRelativeToCameraPosition vec4 The model's bounding sphere relative to the camera position. The structure is xyz components are the position and w is the radius in meters.
agi_PrimitiveColor vec4 The color of the primitive
agi_SnapshotPixelScale float Scale for when rendering high resolution snap shots. Otherwise it is 1.
agi_TerrainSilhouetteTexture0,
agi_TerrainSilhouetteTexture1,
...,
agi_TerrainSilhouetteTexture7
sampler2D A texture that contains the silhouette of the terrain. The number binds the texture to a particular texture unit. See example image below.
agi_Time float Number of seconds since the time specified to StartTime property.
agi_Viewport vec4 The viewport in the format (left, bottom, width, height)
Central Body rendered normally Central Body Depth Texture
Example of Terrain Silhouette Texture

Resources
GLSL Specification
GLSL Tutorial
BRDF explanation and examples