I have a problem in my Android code and can't find a logical solution for it.
My code implements the subdivision by Charles Loop. I have a Half-Edge data-structure running for all Edges and Faces of the Object. But all these things are running as planned. My problem is to display the points and the connection to that points. I define my vertices and my indices to draw an object in my Halfedge
class. After all Halfedges are implemented, I want to draw my Base Object before i do any subdivision.
public HalfEdge()
{
Vector<Vector3f> vertices = new Vector<>();
int[] indices =
{
9, 10, 8,
12, 9, 11,
9, 1, 10,
10, 0, 8,
8, 3, 9,
12, 1, 9,
9, 3, 11,
11, 2, 12
};
vertices.add(new Vector3f(-0.5f, -0.5f, 0.5f));
vertices.add(new Vector3f(0.5f, -0.5f, 0.5f));
vertices.add(new Vector3f(0.5f, 0.5f, 0.5f));
vertices.add(new Vector3f(-0.5f, 0.5f, 0.5f));
vertices.add(new Vector3f(0.5f, -0.5f, -0.5f));
vertices.add(new Vector3f(-0.5f, -0.5f, -0.5f));
vertices.add(new Vector3f(-0.5f, 0.5f, -0.5f));
vertices.add(new Vector3f(0.5f, 0.5f, -0.5f));
vertices.add(new Vector3f(-0.5f, 0.0f, 0.5f));
vertices.add(new Vector3f(0.0f, 0.0f, 0.5f));
vertices.add(new Vector3f(0.0f, -0.5f, 0.5f));
vertices.add(new Vector3f(0.0f, 0.5f, 0.5f));
vertices.add(new Vector3f(0.5f, 0.0f, 0.5f));
vertices.add(new Vector3f(0.5f, 0.0f, 0.0f));
vertices.add(new Vector3f(0.5f, -0.5f, 0.0f));
vertices.add(new Vector3f(0.5f, 0.5f, 0.0f));
vertices.add(new Vector3f(0.5f, 0.0f, -0.5f));
vertices.add(new Vector3f(0.0f, 0.0f, -0.5f));
vertices.add(new Vector3f(0.0f, -0.5f, -0.5f));
vertices.add(new Vector3f(0.0f, 0.5f, -0.5f));
vertices.add(new Vector3f(-0.5f, 0.0f, -0.5f));
vertices.add(new Vector3f(-0.5f, 0.0f, 0.0f));
vertices.add(new Vector3f(-0.5f, -0.5f, 0.0f));
vertices.add(new Vector3f(-0.5f, 0.5f, 0.0f));
vertices.add(new Vector3f(0.0f, 0.5f, 0.0f));
vertices.add(new Vector3f(0.0f, -0.5f, 0.0f));
initHEDS(vertices,indices);
updateMesh();
}
This is an Example from the Front Face of the Cube. updateMesh() is filling my Vertex- and IndexBuffer.
public float[] getVertices()
{
float[] floatVertex = new float[v.size() * 3];
for(int i = 0; i < v.size(); i++)
{
floatVertex[i*3] = v.elementAt(i).xyz.x;
floatVertex[i*3+1] = v.elementAt(i).xyz.y;
floatVertex[i*3+2] = v.elementAt(i).xyz.z;
}
return floatVertex;
}
public short[] getOldIndices()
{
short[] shortIndex = new short[f.size() * 3];
int count = 0;
for(int j = 0; j < f.size(); j++)
{
Edge indexer = f.elementAt(j).edge.previous;
Edge temp;
if(indexer.vertex.interpolated == true)
{
indexer = indexer.previous;
temp = indexer;
}
else
{
temp = indexer;
}
do {
shortIndex[count] = (short)v.indexOf(temp.vertex);
count++;
temp = temp.next;
if(temp.vertex.interpolated == true)
{
temp = temp.next;
}
}while(temp != indexer);
}
return shortIndex;
}
But this ist so important cause the Outcome is right and both Buffers getting the Right Vertices/Indices.
protected void setVertices(float[] vertices)
{
ByteBuffer vb = ByteBuffer.allocateDirect(vertices.length * 4);
vb.order(ByteOrder.nativeOrder());
vertexBuffer = vb.asFloatBuffer();
vertexBuffer.put(vertices);
vertexBuffer.position(0);
vertCount = vertices.length;
}
protected void setIndices(short[] indices)
{
ByteBuffer ib = ByteBuffer.allocateDirect(indices.length * 2);
ib.order(ByteOrder.nativeOrder());
indexBuffer = ib.asShortBuffer();
indexBuffer.put(indices);
indexBuffer.position(0);
indexCount = indices.length;
}
The thing is that I draw 8 faces now. The problem is that only 7/8 are correctly displayed and the last face wont draw the last connection. Since I use GL_LINE_LOOP
the first and the last index will connect at the end. But this doesn't happen. I can't explain this to myself after hours of work.
Here is my Draw method:
public void draw(GL10 gl)
{
gl.glEnable(GL10.GL_CULL_FACE);
gl.glCullFace(GL10.GL_BACK);
gl.glFrontFace(GL10.GL_CW);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glVertexPointer(3,GL10.GL_FLOAT,0,vertexBuffer);
gl.glColor4f(1.0f,1.0f,1.0f,1.0f);
if( colorBuffer != null)
{
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
gl.glColorPointer(4, GL10.GL_FLOAT, 0, colorBuffer);
}
gl.glDrawElements(GL10.GL_LINE_LOOP, indexCount , GL10.GL_UNSIGNED_SHORT, indexBuffer);
gl.glEnable(GL10.GL_POINT_SMOOTH);
gl.glPointSize(30.0f);
gl.glColor4f(0.0f,1.0f,0.0f,1.0f);
gl.glDrawArrays(GL10.GL_POINTS, 0 ,vertCount / 3);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
}
It would be cool if somebody could explain this to me. Why is the last connection not drawn ?! If this is fixed my display will work right for all Loop-Subdivision steps. Here are some screenshots to show the actual problem:
https://i.stack.imgur.com/vxdSF.png
You can see the front face of my cube and the index of the vertices.
https://i.stack.imgur.com/kN9pZ.png
Here you see the problem that the last face in my iteration are not connecting.
I hope somebody could help me! :) I can update this thread if you guys have questions etc.