Package org.xjge.graphics
Class Model
java.lang.Object
org.xjge.graphics.Model
public class Model
extends java.lang.Object
Represents a 3D model. Models may possess animations, multiple meshes,
textures, or a combination of these things. Various limitations are imposed
by the engine to ensure models are loaded with consistency. The Autodesk
.fbx file format is the preferred format of this engine for its compact
size, though other formats should work as well.
- Since:
- 2.0.0
-
Field Summary
Fields Modifier and Type Field Description Color
color
boolean
loopAnimation
static int
MAX_ANIM_SPEED
static int
MAX_BONES
static int
MAX_TEXTURES
static int
MAX_WEIGHTS
java.util.List<Mesh>
meshes
float
opacity
-
Constructor Summary
Constructors Constructor Description Model(java.lang.String filename)
Parses the file provided and generates a 3D model from the data it contains.Model(java.lang.String filename, int aiArgs)
Overloaded version ofModel(java.lang.String)
that permits the use of custom post processing arguments. -
Method Summary
Modifier and Type Method Description void
bindMeshTexture(java.lang.String name)
Alternate version ofbindMeshTexture()
.void
bindMeshTexture(Mesh mesh)
Sets the texture of the specified mesh to the currentGL_TEXTURE_2D
target.void
delocalizeNormal()
Couples the local space of the models mesh normals to that of the current scenes world space.void
freeBuffers()
Convenience method which frees the data buffers allocated by this class.void
freeTextures()
Frees the OpenGL texture objects used by this class.float
getAnimationDuration(java.lang.String name)
Obtains the total duration of the specified animation as it was parsed from the model file.boolean
getAnimationFinished(java.lang.String name)
Checks whether the specified animation has finished playing.int
getAnimationFrameCount(java.lang.String name)
Obtains the number of keyframes the specified animation has.float
getAnimationFrameTime(java.lang.String name)
Obtains a value which indicates how close we are to completing the specified animations currentKeyFrame
.float
getAnimationSeekTime(java.lang.String name)
Obtains the current amount of time that's elapsed in the specified animation.void
listAnimations()
Outputs a list to the console containing information about every animation baked into this model.void
render(GLProgram glProgram, Light[] lights, int shineValue, int depthTexHandle)
Alternate version ofrender()
that uses the default capabilties provided by the engine which includes backface culling and depth testing.void
render(GLProgram glProgram, Light[] lights, GLCaps capabilities, int shineValue, int depthTexHandle)
Renders the 3D model.void
renderShadow(GLProgram depthProgram)
Alternate version ofrenderShadow()
that uses the default capabilties provided by the engine which includes backface culling and depth testing.void
renderShadow(GLProgram depthProgram, GLCaps capabiltites)
Used to cast a shadow using the model matrix of each mesh (and texture if necessary) by passing them as uniforms to the depth program.void
rotateX(float angle)
Rotates the entire 3D model in relation to the worlds x-axis.void
rotateY(float angle)
Rotates the entire 3D model in relation to the worlds y-axis.void
rotateZ(float angle)
Rotates the entire 3D model in relation to the worlds z-axis.void
rotationXYZ(float angleX, float angleY, float angleZ)
Changes the rotation of the entire 3D model relative to each axis.void
scale(float factor)
Scales the entire 3D model by the factor specified.void
setAnimationFrameTime(float frameTime)
Sets the time elapsed between the currentKeyFrame
and the next one in sequence.void
setAnimationSeekTime(float seekTime)
Sets the current seek time of the animation.void
setAnimationSpeed(float speed)
Sets the playback speed of this models current animation.void
setCurrentAnimation(java.lang.String name, int numFrames)
Sets the current animation that will be played by this model.void
translation(float x, float y, float z)
Alternate version oftranslation()
.void
translation(org.joml.Vector3f position)
Translates the entire 3D model to the location specified.void
updateAnimation(double delta)
Steps the seek time of the models current animation forwards.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Field Details
-
MAX_TEXTURES
public static final int MAX_TEXTURES- See Also:
- Constant Field Values
-
MAX_BONES
public static final int MAX_BONES- See Also:
- Constant Field Values
-
MAX_ANIM_SPEED
public static final int MAX_ANIM_SPEED- See Also:
- Constant Field Values
-
MAX_WEIGHTS
public static final int MAX_WEIGHTS- See Also:
- Constant Field Values
-
opacity
public float opacity -
loopAnimation
public boolean loopAnimation -
color
-
meshes
-
-
Constructor Details
-
Model
public Model(java.lang.String filename, int aiArgs)Overloaded version ofModel(java.lang.String)
that permits the use of custom post processing arguments.- Parameters:
filename
- the name of the file to load. Expects the file extension to be included.aiArgs
- the Assimp arguments to use for post processing such asaiProcess_Triangulate
,aiProcess_GenSmoothNormals
,aiProcess_FixInfacingNormals
, etc.
-
Model
public Model(java.lang.String filename)Parses the file provided and generates a 3D model from the data it contains.- Parameters:
filename
- the name of the file to load. Expects the file extension to be included.
-
-
Method Details
-
bindMeshTexture
Sets the texture of the specified mesh to the currentGL_TEXTURE_2D
target. This method is useful when you want to change various texture parameters to better suit the model.NOTE: By default textures are loaded by the model class using mirrored-repeat wrapping and a nearest neighbor filter.
- Parameters:
mesh
- the mesh who's texture we want to set as the current bind target
-
bindMeshTexture
public void bindMeshTexture(java.lang.String name)Alternate version ofbindMeshTexture()
.NOTE: Mesh names are not guaranteed to be unique. As such, you should only use this method if you're certain the name specified matches that of the mesh who's texture you're trying to bind. This method may fail silently.
- Parameters:
name
- the name that corresponds to the mesh we want to set as the bind target
-
delocalizeNormal
public void delocalizeNormal()Couples the local space of the models mesh normals to that of the current scenes world space. Use this to fix the direction of the light source relative to the model whenever it's being illuminated incorrectly. -
translation
public void translation(org.joml.Vector3f position)Translates the entire 3D model to the location specified.- Parameters:
position
- the position to set the model to
-
translation
public void translation(float x, float y, float z)Alternate version oftranslation()
.- Parameters:
x
- the x-coordinate of the game world to place this model aty
- the y-coordinate of the game world to place this model atz
- the z-coordinate of the game world to place this model at
-
rotationXYZ
public void rotationXYZ(float angleX, float angleY, float angleZ)Changes the rotation of the entire 3D model relative to each axis. This method is essentially a combined version ofrotateX()
,rotateY()
, androtateZ()
.- Parameters:
angleX
- the angle to rotate the model along the x-axisangleY
- the angle to rotate the model along the y-axisangleZ
- the angle to rotate the model along the z-axis
-
rotateX
public void rotateX(float angle)Rotates the entire 3D model in relation to the worlds x-axis.- Parameters:
angle
- the angle with which the model will be rotated
-
rotateY
public void rotateY(float angle)Rotates the entire 3D model in relation to the worlds y-axis.- Parameters:
angle
- the angle with which the model will be rotated
-
rotateZ
public void rotateZ(float angle)Rotates the entire 3D model in relation to the worlds z-axis.- Parameters:
angle
- the angle with which the model will be rotated
-
scale
public void scale(float factor)Scales the entire 3D model by the factor specified.- Parameters:
factor
- the factor with which the models size will be multiplied by
-
listAnimations
public void listAnimations()Outputs a list to the console containing information about every animation baked into this model. -
getAnimationDuration
public float getAnimationDuration(java.lang.String name)Obtains the total duration of the specified animation as it was parsed from the model file.- Parameters:
name
- the name of the animation to query- Returns:
- the duration (in seconds) of the animation
-
getAnimationFinished
public boolean getAnimationFinished(java.lang.String name)Checks whether the specified animation has finished playing.- Parameters:
name
- the name of the animation to query- Returns:
- true if the animation has completed a full cycle of all its keyframes
-
getAnimationFrameTime
public float getAnimationFrameTime(java.lang.String name)Obtains a value which indicates how close we are to completing the specified animations currentKeyFrame
.- Parameters:
name
- the name of the animation to query- Returns:
- a non-negative number (between 0 and 1) indicating the progression of the animations current keyframe
-
getAnimationFrameCount
public int getAnimationFrameCount(java.lang.String name)Obtains the number of keyframes the specified animation has.- Parameters:
name
- the name of the animation to query- Returns:
- the number of keyframes parsed from the file
-
getAnimationSeekTime
public float getAnimationSeekTime(java.lang.String name)Obtains the current amount of time that's elapsed in the specified animation.- Parameters:
name
- the name of the animation to query- Returns:
- the seek time of the animation (in seconds)
-
setCurrentAnimation
public void setCurrentAnimation(java.lang.String name, int numFrames)Sets the current animation that will be played by this model. A small transition animation will be generated if the value passed to thenumFrames
argument is greater than zero.- Parameters:
name
- the name of the animation as it appears in the model filenumFrames
- the number of frames to transition between the current animation and the new one
-
setAnimationSpeed
public void setAnimationSpeed(float speed)Sets the playback speed of this models current animation. Subsequent animations will inherit the value specified.NOTE: The total number of keyframes in an animation should be evenly divisible otherwise you'll get some stuttering with values below 1.
- Parameters:
speed
- a non-negative number between 1 and 0. A value of zero will pause the animation at its currentKeyFrame
.
-
setAnimationFrameTime
public void setAnimationFrameTime(float frameTime)Sets the time elapsed between the currentKeyFrame
and the next one in sequence.- Parameters:
frameTime
- a non-negative number (between 0 and 1) indicating the progression of the animations current keyframe
-
setAnimationSeekTime
public void setAnimationSeekTime(float seekTime)Sets the current seek time of the animation. Similar in function to a slider on a video or movie that can be moved back and forth to change the current time/frame/scene.- Parameters:
seekTime
- a value to offset the animations current seek/elapsed time with
-
updateAnimation
public void updateAnimation(double delta)Steps the seek time of the models current animation forwards. A call to this method must be made regularly inside the update loop for models to utilize the engines animation utilities properly.NOTE: The value passed to the delta parameter should be one of
targetDelta
ortrueDelta
provided by the update() method of the Scene class.- Parameters:
delta
- the time (in seconds) it took to complete a single game tick
-
render
public void render(GLProgram glProgram, Light[] lights, GLCaps capabilities, int shineValue, int depthTexHandle)Renders the 3D model. Should be called from within the implementing entitiesrender()
method.- Parameters:
glProgram
- the shader program that will be used to render this modellights
- an array of light source objects inhabiting the current scenecapabilities
- an object that can be used to enable various OpenGL capabilitiesshineValue
- a temporary parameter indicating the shininess of the object until be parse this information from the model file directlydepthTexHandle
- the handle of the texture generated by the current shadow map or -1 if one has not been set
-
render
Alternate version ofrender()
that uses the default capabilties provided by the engine which includes backface culling and depth testing.- Parameters:
glProgram
- the shader program that will be used to render this modellights
- an array of light source objects inhabiting the current sceneshineValue
- a temporary parameter indicating the shininess of the object until be parse this information from the model file directlydepthTexHandle
- the handle of the texture generated by the current shadow map or -1 if one has not been set
-
renderShadow
Alternate version ofrenderShadow()
that uses the default capabilties provided by the engine which includes backface culling and depth testing.- Parameters:
depthProgram
- the shader program provided by the engine that will be used to generate the shadow map texture
-
renderShadow
Used to cast a shadow using the model matrix of each mesh (and texture if necessary) by passing them as uniforms to the depth program.- Parameters:
depthProgram
- the shader program provided by the engine that will be used to generate the shadow map texturecapabiltites
- an object that can be used to enable various OpenGL capabilities- See Also:
renderShadow(org.xjge.graphics.GLProgram)
-
freeBuffers
public void freeBuffers()Convenience method which frees the data buffers allocated by this class. -
freeTextures
public void freeTextures()Frees the OpenGL texture objects used by this class.
-