I’m trying to make a custom mesh and used
custom mesh tutorial
as starting point. In print out it looks correct, but when I try to render my mesh it’s all messed up.
import com.jme3.math.Vector3f;
import com.jme3.scene.Mesh;
import com.jme3.math.FastMath;
import com.jme3.scene.VertexBuffer;
import com.jme3.util.BufferUtils;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author Asteroth
*/
public class Cone extends Mesh{
private Vector3f [] vertices;
private float [] textureCoords;
private int [] indexes;
private float height, radius;
/*
TODO - add parameter for multiple rings along the height and noising.
*/
public Cone(int verticeCount, float heightin, float radin)
{
this.radius = radin;
this.height = heightin;
vertices = new Vector3f[verticeCount+1];
textureCoords = new float[verticeCount+1];
float stepsize = FastMath.TWO_PI/(float)verticeCount;
System.err.println(String.format("computed stepsize = %f", FastMath.RAD_TO_DEG*stepsize));
Vector3f basepos = new Vector3f(0, 0, -height/2);
vertices[0] = basepos;
for(int i = 1; i < verticeCount+1; i++)
{
float angle = (i-1) * stepsize;
vertices[i] = new Vector3f(FastMath.cos(angle), FastMath.sin(angle), height/2).mult(radius);
textureCoords[i] = (float)i /verticeCount;
}
List<Integer> indexHelper = new ArrayList<Integer>();
int current = 1;
for(int i = 0; i<vertices.length*3; i++)
{
if(i%3==0)
{
indexHelper.add(0);
current --;
}
else
{
current ++;
indexHelper.add(current);
}
}
indexes = new int[indexHelper.size()];
/*
why is there no built in method to turn list<Integer> to int array...*/
for(int i = 0; i< indexHelper.size(); i++)
{
indexes[i] = indexHelper.get(i);
}
setBuffer(VertexBuffer.Type.Position, 3, BufferUtils.createFloatBuffer(vertices));
setBuffer(VertexBuffer.Type.TexCoord, 2, BufferUtils.createFloatBuffer(textureCoords));
setBuffer(VertexBuffer.Type.Index, 3, BufferUtils.createIntBuffer(indexes)); //EDIT - was missing indexing, still missing some triangles thogh
updateBound();
setStatic();
}
public void print()
{
System.out.println(String.format("printing cone of %s height with %s vertices", height, vertices.length));
for(int i = 0; i < vertices.length; i++)
{
System.out.println("["+i+"] " + vertices[i]);
}
System.out.println("--------------------------------------------%\nVertex connections:");
for(int i = 0; i<indexes.length; i++)
{
if(i%3==0)
System.out.println();
System.out.print(indexes[i] + ", ");
}
}
}
For example, when I use
Cone island = new Cone(8, 3f, 1f);
island.print();
Geometry testCone = new Geometry("island test", island);
Material testMat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
testMat.getAdditionalRenderState().setWireframe(true);
testMat.setColor("Color", ColorRGBA.Red);
testCone.setMaterial(testMat);
testCone.setLocalTranslation(0, 0, 3f);
rootNode.attachChild(testCone);
I see from the print that the connection by index should be correct, yet I only get 3 triangles, with weird angles and wrong dimensions. What did I mess up?
EDIT - one missing line found, still some indexing problems though - last triangle is missing