Trending
Opinion: How will Project 2025 impact game developers?
The Heritage Foundation's manifesto for the possible next administration could do great harm to many, including large portions of the game development community.
Third Entry of LibGDX in Depth series
Before diving I request you to download LibGDX source code and view each of the class as i mention them. It is very difficult to follow otherwise.
The important variables in Mesh class are
static final Map> meshes = new HashMap>(); final VertexData vertices; final IndexData indices; The first variable meshes (Map) is for managed mesh. The second and third variable are the ones which store all the data related to the mesh.
Mesh class contains many constructors. The most generic one is
public Mesh (VertexDataType type, boolean isStatic, int maxVertices, int maxIndices, VertexAttribute... attributes) { if (type == VertexDataType.VertexBufferObject) { vertices = new VertexBufferObject(isStatic, maxVertices, attributes); indices = new IndexBufferObject(isStatic, maxIndices); isVertexArray = false; } else if (type == VertexDataType.VertexBufferObjectSubData) { vertices = new VertexBufferObjectSubData(isStatic, maxVertices, attributes); indices = new IndexBufferObjectSubData(isStatic, maxIndices); isVertexArray = false; } else { vertices = new VertexArray(maxVertices, attributes); indices = new IndexArray(maxIndices); isVertexArray = true; } addManagedMesh(Gdx.app, this); }
We'll concentrate on VertexDataType.VertexBufferObject as the other two types are nearly similar and easier.
The constructor creates an instance of VertexBufferObject class and IndexBufferObject class, which are derived from VertexData class and IndexData class respectively.
The VertexBufferObject class is responsible for setting the vertices,binding the vertices with shader program and eventually unbind and disposing the vertex data.
The data for vertices consists of two parts mainly, one is the actual array of data and the second is the types of attributes included in the array.
Since the vertices data is an interleaved array, it is vital to tell OpenGL about the offset,number of components,etc of each attribute.
The important variables in VertexBufferObject class are
final VertexAttributes attributes; final FloatBuffer buffer;
The entire vertex data is stored as a FloatBuffer.VertexAttribute helper class contains Constants and functions which will take care of informing OpenGL which type of data is present in which part of the array.
Before going further deep we'll analyze the code in first entry with what is learned so far
mesh = new Mesh(true, 3, 3, VertexAttribute.Position(), VertexAttribute.ColorUnpacked()); 1) In this constructor I'm not setting any VertexDataType 2) I'm setting is static as true, so the LibGDX will use GL_STATIC_DRAW in glBufferData function call. 3) I'm telling i have 3 vertices with 3 index. 4) Next I'm passing that in the interleaved data per vertex first i've passed position data (i.e., VertexAttribute.Position() ), then i'm passing color data (VertexAttribute.ColorUnpacked()) To handle different type of vertex attributes, LibGDX uses a the Iterable class named VetexAttributes (note the's') which contains an array of VertexAttribute. Each vertex attribute class contains variable which uniquely defines a type of vertex attribute /** the attribute {@link Usage} **/ public final int usage; /** the number of components this attribute has **/ public final int numComponents; /** the offset of this attribute in bytes, don't change this! **/ public int offset; /** the alias for the attribute used in a {@link ShaderProgram} **/ public String alias; /** optional unit/index specifier, used for texture coordinates and bone weights **/ public int unit; private final int usageIndex;
For Example when I call VertexAtrribute.position()
public static VertexAttribute Position () {
return new VertexAttribute(Usage.Position, 3, ShaderProgram.POSITION_ATTRIBUTE);
}
it returns a new VertexAttribute class instance with usage as position, contains 3 components (X,Y,Z) and is to be passed on to the shader variable "a_position"(ShaderProgram.POSITION_ATTRIBUTE);
Similarly for VertexAttribute.ColorUnpacked()
public static VertexAttribute ColorUnpacked () {
return new VertexAttribute(Usage.Color, 4, ShaderProgram.COLOR_ATTRIBUTE);
}
it returns new VertexAttribute class instance with usage as Color containing 4 components (RGBA) and is passed on to shader variable "a_color" (ShaderProgram.COLOR_ATTRIBUTE);
Now that we have told how our data will look like to LibGDX, I can pass the data.
mesh.setVertices(new float[] {-0.5f, -0.5f, 0, 1, 0.4f, 1, 1, 0.5f, -0.5f, 0, 1, 0.2f, 0f, 1, 0.5f, 0.5f, 0, 1, 1, 1,
1});
setVertices function copies the above float array values to the FloatBuffer inside VertexBufferObject class.
Now the Mesh->VertexBufferObject instance contains details about the position of vertices and color of vertices in FloatBuffer and details of attributes passed as
an VertexAttribute array inside VertexAttributes instance.
Now the only thing remaining is to Bind the values to the location and render .
I'm sure you're confused as I am now. So I'll discuss bind and render in next entry.
I'm following
Read more about:
BlogsYou May Also Like