ObjectDynamic Class
ObjectDynamic class allows to create a dynamic object, which can be rendered by means of any type of geometry (vertex format for an object is changeable). The class supports instancing as well as point/line/triangle rendering modes. ObjectDynamic class requires a custom shader for rendering, no build-in shaders are available.
The following sample demonstrates the ObjectDynamic class usage:
Creating a Dynamic Object
An example of creating a dynamic object - square:
Do not forget to create .mat file with all the materials for the new dynamic object and add a reference to it in the .world file.
#include <core/unigine.h>
/*
*/
// create a new dynamic object
ObjectDynamic dynamic;
/*
*/
int init() {
// load a library to the world
engine.materials.addWorldLibrary("dynamic.mat");
// add a free-flying camera and set position to it
Player player = new PlayerSpectator();
player.setPosition(Vec3(4.0f,4.0f,4.0f));
player.setDirection(vec3(-1.0f));
engine.game.setPlayer(player);
// add new dynamic object and set its coordinates
dynamic = new ObjectDynamic(1);
dynamic.setMaterial("dynamic_mesh_base","*");
// set vertex format
dynamic.setVertexFormat((
ivec3( 0, OBJECT_DYNAMIC_TYPE_FLOAT, 2),
ivec3( 8, OBJECT_DYNAMIC_TYPE_HALF, 4),
ivec3( 16, OBJECT_DYNAMIC_TYPE_HALF, 4),
ivec3( 24, OBJECT_DYNAMIC_TYPE_HALF, 4),
));
// set vertex adding parameters
void add_vertex(vec3 vertex,vec4 texcoord,vec3 normal,vec4 tangent) {
dynamic.addVertexFloat(0,vertex,2);
dynamic.setVertexHalf(1,texcoord,4);
dynamic.setVertexHalf(2,vec4(normal,vertex.z),4);
dynamic.setVertexHalf(3,tangent,4);
}
// add four vertices of a square
add_vertex(vec3(-1.0f,-1.0f,0.0f),vec4(0.0f,0.0f,0.0f,0.0f),vec3(0.0f,0.0f,1.0f),vec4(1.0f,0.0f,0.0f,1.0f));
add_vertex(vec3( 1.0f,-1.0f,0.0f),vec4(1.0f,0.0f,0.0f,0.0f),vec3(0.0f,0.0f,1.0f),vec4(1.0f,0.0f,0.0f,1.0f));
add_vertex(vec3( 1.0f, 1.0f,0.0f),vec4(1.0f,1.0f,0.0f,0.0f),vec3(0.0f,0.0f,1.0f),vec4(1.0f,0.0f,0.0f,1.0f));
add_vertex(vec3(-1.0f, 1.0f,0.0f),vec4(0.0f,1.0f,0.0f,0.0f),vec3(0.0f,0.0f,1.0f),vec4(1.0f,0.0f,0.0f,1.0f));
// add the indices for the vertices
dynamic.addIndex(0);
dynamic.addIndex(1);
dynamic.addIndex(2);
dynamic.addIndex(2);
dynamic.addIndex(3);
dynamic.addIndex(0);
// set a bounding box for the object
dynamic.setBoundBox(vec3(-1.0f),vec3(1.0f));
return 1;
}
/*
*/
int update() {
// add a rotation to the object (if needed)
dynamic.setWorldTransform(rotateZ(engine.game.getTime() * 32.0f));
return 1;
}
The obtained result in the Editor:
ObjectDynamic Class
This class inherits from ObjectMembers
ObjectDynamic (int dynamic)
Constructor. Creates a new dynamic object. By default, it will be rendered using triangles.Arguments
- int dynamic - Dynamic flag. If set to 1, indicates that the object is going to be changed every frame. This flag optimizes internal render buffer usage and, for example, can be used to create custom particle systems.
void addIndex (int vertex)
Adds an index pointing to the given vertex.Arguments
- int vertex - Index of the vertex in the vertex buffer.
void addLineStrip (int num_vertex)
Adds a line strip to the object.This method does not add the new vertices, but allocates their indices. Vertices should be created with addVertexFloat(), addVertexHalf() or addVertexUChar() methods accordingly to the required vertex type.
Arguments
- int num_vertex - Number of vertices composing the strip.
void addPoints (int num_points)
Adds the points to the object.This method does not add the new vertices, but allocates their indices. Vertices should be created with addVertexFloat(), addVertexHalf() or addVertexUChar() methods accordingly to the required vertex type.
Arguments
- int num_points - Number of points.
void addQuads (int quads)
Adds a given number of quads to the object.This method does not add the new vertices, but allocates their indices. Vertices should be created with addVertexFloat(), addVertexHalf() or addVertexUChar() methods accordingly to the required vertex type.
Arguments
- int quads - Number of quads.
void addSurface (string name)
Adds all the last listed and unsigned vertices and triangles to a new object surface with a specified name.Arguments
- string name - Name of the new surface.
void addTriangleFan (int num_vertex)
Adds a triangle fan to the object.This method does not add the new vertices, but allocates their indices. Vertices should be created with addVertexFloat(), addVertexHalf() or addVertexUChar() methods accordingly to the required vertex type.
Arguments
- int num_vertex - Number of vertices composing the fan.
void addTriangleStrip (int num_vertex)
Adds a triangle strip to the object.This method does not add the new vertices, but allocates their indices. Vertices should be created with addVertexFloat(), addVertexHalf() or addVertexUChar() methods accordingly to the required vertex type.
Arguments
- int num_vertex - Number of vertices composing the strip.
void addTriangles (int triangles)
Adds a given number of triangles to the object.This method does not add the new vertices, but allocates their indices. Vertices should be created with addVertexFloat(), addVertexHalf() or addVertexUChar() methods accordingly to the required vertex type.
Arguments
- int triangles - Number of triangles.
void addVertexFloat (int attribute, float[] coordinates, int size)
Adds a vertex of a float type with the given attribute, coordinates and size to the object.Before adding a vertex, make sure that you set all the attributes for it with the setVertexFormat() method.
Arguments
- int attribute - The number of the attribute, set in the setVertexFormat() method.
- float[] coordinates - Vertex coordinates.
- int size - Size of the vertex, bytes. Can be 1,2,3,4 for this type.
The size can be different with the size, set in the setVertexFormat() method.
void addVertexHalf (int attribute, float[] coordinates, int size)
Adds a vertex of the half-float type with the given attribute, coordinates and size to the object.Before adding a vertex, make sure that you set all the attributes for it with the setVertexFormat() method.
Arguments
- int attribute - The number of the attribute, set in the setVertexFormat() method.
- float[] coordinates - Vertex coordinates.
- int size - Size of the vertex, bytes. Can be 2,4 for this type.
The size can be different with the size, set in the setVertexFormat() method.
void addVertexUChar (int attribute, int coordinates, int size)
Adds a vertex of an unsigned char type with the given attribute, coordinates and size to the object.Before adding a vertex, make sure that you set all the attributes for it with the setVertexFormat() method.
Arguments
- int attribute - The number of the attribute, set in the setVertexFormat() method.
- int coordinates - Vertex coordinates.
- int size - Size of the vertex, bytes. Can be only 4 for this type.
The size can be different with the size, set in the setVertexFormat() method.
void allocateIndices (int num)
Allocate an index buffer for a given number of indices that will be used for an object. With this function, memory can be allocated once rather than in chunks, making the creation faster.Arguments
- int num - The number of indices that will be stored in a buffer.
void allocateVertex (int num)
Allocate a vertex buffer for a given number of vertices that will be used for an object. With this function, memory can be allocated once rather than in chunks, making the creation faster.Arguments
- int num - The number of vertices that will be stored in a buffer.
void clearIndices ()
Clears all vertex indices used by the object.void clearSurfaces ()
Clears all the surface settings.void clearVertex ()
Clears all the current vertex settings.void flush ()
Sends all data to the GPU. This method is called automatically, if the length of either vertex or index buffer changes. If you change the contents of either of the buffers, you should call this method.int getIndex (int index)
Returns a vertex number pointed by a given index.Arguments
- int index - Index in the index buffer.
Return value
Vertex number.int getInstancing ()
Returns a value indicating if the hardware instancing flag is enabled.Return value
1 if the hardware instancing flag is enabled; otherwise, 0.int getNumIndices ()
Returns the number of vertex indices used by the object.Return value
Number of indices.int getNumVertex ()
Returns the number of vertices composing the object.Return value
Number of vertices.int getSurfaceBegin (int surface)
Returns the begin index of the specified object surface.Arguments
- int surface - ID of a target surface.
Return value
The begin index.int getSurfaceEnd (int surface)
Returns the end index of the specified object surface.Arguments
- int surface - ID of a target surface.
Return value
End index ID.int getSurfaceMode (int surface)
Returns primitives used to render the object surface with: triangles (by default), lines or points.Arguments
- int surface - Surface number.
Return value
Surface rendering mode:- OBJECT_DYNAMIC_MODE_POINTS = 0
- OBJECT_DYNAMIC_MODE_LINES
- OBJECT_DYNAMIC_MODE_TRIANGLES
int getVertexSize ()
Returns the size of the current vertex, bytes.Return value
Vertex size.void setBoundBox (vec3 min, vec3 max, int surface)
Sets a bounding box of the specified size for the given dynamic object surface.Arguments
- vec3 min - Bound box minimum.
- vec3 max - Bound box maximum.
- int surface - ID number of the target surface of the object.
void setBoundBox (vec3 min, vec3 max)
Sets a bounding box of the specified size for the dynamic object.Arguments
- vec3 min - Bounding box minimum.
- vec3 max - Bounding box maximum.
void setIndex (int index, int vertex)
Updates a given index in the index buffer.Arguments
- int index - Index in the index buffer.
- int vertex - ID number of the vertex.
void setInstancing (int instancing)
Activates the hardware instancing technique.Arguments
- int instancing - Instancing flag. 1 to enable hardware instancing, 0 to disable it.
void setParameterBool (string name, int value)
Sets boolean shader parameter of the specified value.Arguments
- string name - Name of the parameter.
- int value - Parameter value.
void setParameterFloatArray (string name, int size, int num)
Sets an array of the specified number of float shader parameters.Arguments
- string name - Name of the parameter.
- int size - Parameter size.
- int num - Number of parameters.
void setParameterFloat (string name, float[] value, int size)
Sets float shader parameter of the specified value.Arguments
- string name - Name of the parameter.
- float[] value - Parameter value pointer.
- int size - Parameter size.
void setParameterInt (string name, int value, int size)
Sets integer shader parameter of the specified value.Arguments
- string name - Name of the parameter.
- int value - Parameter value.
- int size - Parameter size.
void setSurfaceBegin (int begin, int surface)
Sets the begin index for the specified object surface.Arguments
- int begin - The index to be set as the begin one for the surface.
- int surface - ID number of the target surface.
void setSurfaceEnd (int begin, int surface)
Sets the end index for the specified object surface.Arguments
- int begin - The index to be set as the end one for the surface.
- int surface - ID number of the target surface.
void setSurfaceMode (int mode, int surface)
Sets primitives to render an object surface with: triangles (by default), lines or points.Arguments
- int mode - Surface rendering mode:
- int surface - Surface number.
void setVertexFloat (int attribute, float[] coordinates, int size)
Updates the last added vertex to the vertex of the float type with the given parameters.Arguments
- int attribute - The number of the attribute, set in the setVertexFormat() method.
- float[] coordinates - Vertex coordinates.
- int size - Size of the vertex: can be 1,2,3,4 for this type.
The size can be different with the size, set in the setVertexFormat() method.
void setVertexFormat (int num_attributes)
Sets the number of the vertex attributes.This method does not create vertices, but their attributes. Vertices should be created with addVertexFloat(), addVertexHalf() or addVertexUChar() methods accordingly to the type specified in this method.
Arguments
- int num_attributes - Number of the vertex attributes, can be up to 16 attributes for one vertex. The numeration starts from 0. Each attribute consists of:
- An offset of the vertex in bytes, depends on the vertex type and size.
- Type of the vertex: OBJECT_DYNAMIC_TYPE_FLOAT, OBJECT_DYNAMIC_TYPE_HALF, OBJECT_DYNAMIC_TYPE_UCHAR
- Size of the vertex: can be 1,2,3,4 for float type; 2,4 for half type; 4 for UChar type
When it goes to shader, 0-attribute always comes with the size of 4, no matter what size is specified in the method. All the other attributes comes with the specified sizes.
The example of setting 3 different vertices attributes:
// vertex format dynamic.setVertexFormat(( ivec3( 0, OBJECT_DYNAMIC_TYPE_FLOAT, 1), ivec3( 12, OBJECT_DYNAMIC_TYPE_HALF, 2), ivec3( 24, OBJECT_DYNAMIC_TYPE_UCHAR, 4), ));
void setVertexHalf (int attribute, float[] coordinates, int size)
Updates the last added vertex to the vertex of the half-float type with the given parameters.Arguments
- int attribute - The number of the attribute, set in the setVertexFormat() method.
- float[] coordinates - Vertex coordinates.
- int size - Size of the vertex: can be 2, 4 for this type.
The size can be different with the size, set in the setVertexFormat() method.
void setVertexUChar (int attribute, int coordinates, int size)
Updates the last added vertex to the vertex of the unsigned char type with the given parameters.Arguments
- int attribute - The number of the attribute, set in the setVertexFormat() method.
- int coordinates - Vertex coordinates.
- int size - Size of the vertex: can be 4 for this type.
The size can be different with the size, set in the setVertexFormat() method.
void updateSurfaceBegin (int surface)
Synchronizes surface begin index.Arguments
- int surface - ID of a target surface.
void updateSurfaceEnd (int surface)
Synchronizes surface end index.Arguments
- int surface - ID of a target surface.
int OBJECT_DYNAMIC_MODE_LINES
Description
Mode to render the lines.int OBJECT_DYNAMIC_MODE_POINTS
Description
Mode to render the points.int OBJECT_DYNAMIC_MODE_TRIANGLES
Description
Mode to render the triangles (to create the objects).int OBJECT_DYNAMIC_TYPE_FLOAT
Description
Float type of the vertices.int OBJECT_DYNAMIC_TYPE_HALF
Description
Half-float type of the vertices.int OBJECT_DYNAMIC_TYPE_UCHAR
Description
Unsigned char type of the vertices.Last update: 2017-07-03
Help improve this article
Was this article helpful?
(or select a word/phrase and press Ctrl+Enter)