# Model Primitive

ModelPrimitive is a primitive that can load and render glTF 2.0, COLLADA and AGI's MDL models. glTF and COLLADA are industry standard formats. MDL is the 3D model format used by STK. We recommend using glTF or COLLADA models. Models can be downloaded from AGI's website.

C#
```ModelPrimitive model = new ModelPrimitive(modelUri);
EarthCentralBody earth = CentralBodiesFacet.GetFromContext().Earth;
model.SetPositionCartographic(earth, position);```

The orientation of the model depends on the model itself. For example, in the hellfire.dae model, the missile is laid out lengthwise along the positive x axis. To change the orientation, use the Orientation property and provide a quaternion 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 quaternion is shown below:

C#
```Cartographic center = new Cartographic(Trig.DegreesToRadians(-75.25), Trig.DegreesToRadians(39.88), 0.0);
PointCartographic origin = new PointCartographic(earth, center);
Axes axes = new AxesNorthEastDown(earth, origin);
AxesEvaluator axesEvaluator = GeometryTransformer.GetAxesTransformation(axes, earth.FixedFrame.Axes);
UnitQuaternion q = axesEvaluator.Evaluate(TimeConstants.J2000);

ElementaryRotation r = new ElementaryRotation(AxisIndicator.Second, Math.PI / 2);
q = q.Multiply(r);

model.Orientation = q;```

A topographic axes is created based on the model's position where the x axis point in the local north direction, the y axis points in the local east direction and the z axis points in the local down direction. A quaternion that defines the rotation from these axes to the fixed frame axes is created. To point the model in the local up direction, the quaternion is rotated 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 using the Position property.

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 Articulations property.

An example of how to modify the ModelTransformation of a model's ModelArticulation is shown below:

C#
```// Create the model and set the position.
ModelPrimitive model = new ModelPrimitive(modelUri);
EarthCentralBody earth = CentralBodiesFacet.GetFromContext().Earth;
model.SetPositionCartographic(earth, 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.

// Modify the spin of the propellers.
The model primitive without modifying the spin.
The model with the spin of the propellers modified to be 135 degrees.

The Articulations property contains an instance of ModelArticulationCollection. ModelArticulationCollection implements IEnumerable<T> and contains a collection of ModelArticulations. ModelArticulation also implements IEnumerable<T> and contains a collection of ModelTransformations. Both the ModelArticulationCollection and ModelArticulation classes accept either an integer or string index to access its values. Use the 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 Type property, which returns a ModelTransformationType.

Collada models also support the ColladaFX framework, which allows users to write GLSL code to calculate vertex positions, pixel colors, etc. This allows for models to exhibit lighting properties of materials such as metal, glass, plastic, etc. ColladaFX also allows changing OpenGL States, use of all supported OpenGL Texture types (1D, 2D, 3D, cubemap, rectangle, shadow), and render to texture for use with multipass rendering techniques.

The following image shows a satellite that uses a metallic BRDF based on the work of Ashikhmin and Shirley. The paper can be found here This example can be seen in the HowTo example under "Primitives" -> "Models" -> "Draw a Collada model with user defined lighting"

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 Insight3D's animation time to move a vertex.

GLSL
```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 CentralBody. The number binds the texture to a particular texture unit. See images 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

Model-View matrix.

agi_ModelViewMatrixRelativeToCameraPosition

mat4

Model-View matrix relative to the camera position.

agi_ModelViewMatrixRelativeToPrimitiveCenter

mat4

Model-View matrix relative to the model's center.

agi_ModelViewProjectionMatrix

mat4

Model-View-Projection matrix.

agi_ModelViewProjectionMatrixRelativeToCameraPosition

mat4

Model-View-Projection matrix relative to the camera position.

agi_ModelViewProjectionMatrixRelativeToPrimitiveCenter

mat4

Model-View-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 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 xyz components are the position and w is the radius in meters.

agi_PrimitiveColor

vec4

The color of the primitive.

agi_SnapshotPixelScale

float

Scale when rendering high-resolution snapshots. 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 image below.

agi_Time

float

Number of seconds since the animation start time.

agi_Viewport

vec4

The viewport in the format (left, bottom, width, height).

Central Body Rendered Normally
Central Body Depth Texture
Terrain Silhouette Texture

#### Supported OpenGL States

OpenGL State

Notes

alpha_func

glAlphaFunc

alpha_test_enable

glEnable/glDisable with GL_ALPHA_TEST

blend_color

glBlendColor

blend_enable

glEnable/glDisable with GL_BLEND

blend_equation

glBlendEquation

blend_equation_separate

glBlendEquationSeparate

Requires OpenGL 2.0.

blend_func

glBlendFunc

blend_func_separate

glBlendFuncSeparate

Requires OpenGL 1.4.

cull_face

glCullFace

cull_face_enable

glEnable/glDisable with GL_CULL_FACE

depth_clamp_enable

glEnable/glDisable with GL_DEPTH_CLAMP_NV

Requires GL 3.2 or GL_ARB_depth_clamps.

depth_func

glDepthFunc

depth_range

glDepthRange

depth_test_enable

glEnable/glDisable with GL_DEPTH_TEST

fog_color

glFogfv with GL_FOG_COLOR

fog_density

glFogf with GL_FOG_DENSITY

fog_enable

glEnable/glDisable with GL_FOG

fog_end

glFogf with GL_FOG_END

fog_mode

glFogi with GL_FOG_MODE

fog_start

glFogf with GL_FOG_START

front_face

glFrontFace

line_smooth_enable

glEnable/glDisable with GL_LINE_SMOOTH

line_stipple

glLineStipple

line_stipple_enable

glEnable/glDisable with GL_LINE_STIPPLE

line_width

glLineWidth

point_size

glPointSize

point_smooth_enable

glEnable/glDisable with GL_POINT_SMOOTH

polygon_mode

glPolygonMode

Only FRONT_AND_BACK.

polygon_offset

glPolygonOffset

polygon_offset_fill_enable

glEnable/glDisable with GL_POLYGON_OFFSET_FILL

scissor

glScissor

scissor_test_enable

glEnable/glDisable with GL_SCISSOR_TEST

stencil_func

glStencilFunc

stencil_func_separate

glStencilFuncSeparate

Requires OpenGL 2.0.

Requires OpenGL 2.0.

stencil_op

glStencilOp

stencil_op_separate

glStencilOpSeparate

Requires OpenGL 2.0.

stencil_test_enable

glEnable/glDisable with GL_STENCIL_TEST

texture1D

glBindTexture with GL_TEXTURE_1D

texture2D

glBindTexture with GL_TEXTURE_2D

texture3D

glBindTexture with GL_TEXTURE_3D

textureCUBE

glBindTexture with GL_TEXTURE_CUBE_MAP

Requires OpenGL 1.3.

textureDEPTH

glBindTexture with GL_TEXTURE_2D

Used for depth textures.

textureRECT

glBindTexture with GL_TEXTURE_RECTANGLE_ARB

#### Unsupported OpenGL States

Notes

auto_normal_enable

clip_plane

Can be emulated in the fragment shader. Removed from OpenGL 3.2 core.

clip_plane_enable

Can be emulated in the fragment shader. Removed from OpenGL 3.2 core.

color_logic_op_enable

Rarely used.

color_material

Can be emulated in the fragment shader by implementing a BRDF. Removed from OpenGL 3.2 core.

color_material_enable

Can be emulated in the fragment shader by implementing a BRDF. Removed from OpenGL 3.2 core.

depth_bounds

Only affects performance, not the visual output. Not in core GL, requires EXT_depth_bounds_test.

depth_bounds_enable

Only affects performance, not the visual output. Not in core GL, requires EXT_depth_bounds_test.

dither_enable

Rarely used.

light_ambient

Can be emulated in fragment shader. Fixed function lighting was removed from OpenGL 3.2 core.

light_constant_attenuation

Can be emulated in fragment shader. Fixed function lighting was removed from OpenGL 3.2 core.

light_diffuse

Can be emulated in fragment shader. Fixed function lighting was removed from OpenGL 3.2 core.

light_enable

Can be emulated in fragment shader. Fixed function lighting was removed from OpenGL 3.2 core.

light_linear_attenuation

Can be emulated in fragment shader. Fixed function lighting was removed from OpenGL 3.2 core.

light_model_ambient

Can be emulated in fragment shader. Fixed function lighting was removed from OpenGL 3.2 core.

light_model_color_control

Can be emulated in fragment shader. Fixed function lighting was removed from OpenGL 3.2 core.

light_model_local_viewer_enable

Can be emulated in fragment shader. Fixed function lighting was removed from OpenGL 3.2 core.

light_model_two_side_enable

Can be emulated in fragment shader. Fixed function lighting was removed from OpenGL 3.2 core.

light_position

Can be emulated in fragment shader. Fixed function lighting was removed from OpenGL 3.2 core.

Can be emulated in fragment shader. Fixed function lighting was removed from OpenGL 3.2 core.

light_specular

Can be emulated in fragment shader. Fixed function lighting was removed from OpenGL 3.2 core.

light_spot_cutoff

Can be emulated in fragment shader. Fixed function lighting was removed from OpenGL 3.2 core.

light_spot_direction

Can be emulated in fragment shader. Fixed function lighting was removed from OpenGL 3.2 core.

light_spot_exponent

Can be emulated in fragment shader. Fixed function lighting was removed from OpenGL 3.2 core.

lighting_enable

Can be emulated in fragment shader. Fixed function lighting was removed from OpenGL 3.2 core.

logic_op

Rarely used.

logic_op_enable

Rarely used.

material_ambient

Can be emulated in the fragment shader by implementing a BRDF. Removed from OpenGL 3.2 core.

material_diffuse

Can be emulated in the fragment shader by implementing a BRDF. Removed from OpenGL 3.2 core.

material_emission

Can be emulated in the fragment shader by implementing a BRDF. Removed from OpenGL 3.2 core.

material_shininess

Can be emulated in the fragment shader by implementing a BRDF. Removed from OpenGL 3.2 core.

material_specular

Can be emulated in the fragment shader by implementing a BRDF. Removed from OpenGL 3.2 core.

model_view_matrix

Can be emulated in the shader. Removed from OpenGL 3.2 core.

multisample_enable

Requires an OpenGL context be created with multisample buffers, which is currently not supported in Insight3D.

normalize_enable

This is always enabled.

point_distance_attenuation

point_size_enable

point_size_max

point_size_min

polygon_offset_line_enable

polygon_offset_point_enable

polygon_smooth_enable

projection_matrix

Can be emulated in the shader. Removed from OpenGL 3.2 core.

sample_alpha_to_coverage_enable

Requires an OpenGL context be created with multisample buffers, which is currently not supported in Insight3D.

sample_alpha_to_one_enable

Requires an OpenGL context be created with multisample buffers, which is currently not supported in Insight3D.

sample_coverage

sample_coverage_enable

texture_env_color

Can be emulated in the fragment shader. Removed from OpenGL 3.2 core.

texture_env_color

Can be emulated in the fragment shader. Removed from OpenGL 3.2 core.

texture_pipeline

GLES profile only. GLSL is currently the only supported profile.

texture1D_enable

This isn't required when using a texture in a shader. Removed from OpenGL 3.2 core.

texture2D_enable

This isn't required when using a texture in a shader. Removed from OpenGL 3.2 core.

texture3D_enable

This isn't required when using a texture in a shader. Removed from OpenGL 3.2 core.

textureCUBE_enable

This isn't required when using a texture in a shader. Removed from OpenGL 3.2 core.

textureDEPTH_enable

This isn't required when using a texture in a shader. Removed from OpenGL 3.2 core.

textureRECT_enable

This isn't required when using a texture in a shader. Removed from OpenGL 3.2 core.