2

so, I have created a mesh consisting of 2 triangles into my game. Now I want to turn this mesh into a model so it can be rendered with the ModelBatch. If I just render it seperatly it gives me weird depth issues with the models:

Example

If I render multiple different models together it does not give me the error (The two kinds of trees). So If i render it as a model the problem will hopefully go away.

genpfault
  • 51,148
  • 11
  • 85
  • 139
Grobbed
  • 317
  • 2
  • 12

1 Answers1

1

To use a mesh in the modelBatch as you said, you have to pass it in a model. To do so you have to create a mesh with the desired vertices and indices.

Vertices : are the points which compose your 3D/2D shape (here it's your plane compose by two triangle).

Indices : the order of the vertices that must be triangulated, indicies must be "redundant" to close and triangulate the path.

For example here is an image to better explanation :

enter image description here

Then you must specify attributes of position, uv and normals. Attributes are used to give information to shader (mesh must be rendered with a shader). For every vertex the shader has the information given by those attributes.

Position : are 3D points of the vertices in space.

UV : are used for texture mapping and are mendatory (more information here).

Normals : are normalized vectors, the normals of face or vertex (in your case I would assume it's only pointing to the north pole for every vertices).

To create your mesh :

Mesh mesh = new Mesh(true, vertices.length, indices.length,
                    new VertexAttribute(Usage.Position, 3, ShaderProgram.POSITION_ATTRIBUTE),
                    new VertexAttribute(Usage.TextureCoordinates, 2, ShaderProgram.TEXCOORD_ATTRIBUTE),
                    new VertexAttribute(Usage.Normal, 3, ShaderProgram.NORMAL_ATTRIBUTE));

mesh.setVertices(vertices);
mesh.setIndices(indices);

Mesh require boolean if the mesh will be dynamic or not, vertices length, indices length and some vertexAttributes which give attributes to the shader. Default shader of model are using ShaderProgram.POSITION_ATTRIBUTE, ShaderProgram.TEXCOORD_ATTRIBUTE & ShaderProgram.NORMAL_ATTRIBUTE which you may tune for using your own shaders.

Then you add the vertices and the indices. For your case vertices and indices are :

float vertices[] = {-1, -1, 0, // bottom left corner
                          -1,  1, 0, // top left corner
                           1,  1, 0, // top right corner
                           1, -1, 0}; // bottom right corner
    
short indices[] = {0,1,2, // first triangle (bottom left - top left - top right)
                         0,2,3}; // second triangle (bottom left - top right - bottom right)

After creating your mesh you need to give it to the modelBuilder as following

ModelBuilder modelBuilder = new ModelBuilder(); // creates model build
modelBuilder.begin();// begin it
        
modelBuilder.part("plane", mesh, 
                GL20.GL_TRIANGLES,  
                0, 
                mesh.getNumIndices(), 
                new Material()); // creates your mesh
        
model = modelBuilder.end(); // makes the model
instance = new ModelInstance(model);

//...
//render it in render loop
modelBatch.begin(camera);
modelBatch.render(instance, environment);
modelBatch.end();

Following mainly :

Hope it will help you and others that are in search for the same process.

Nain0nain
  • 21
  • 5