addTriangle issue

Started by cnasich, March 19, 2014, 09:20:20 PM

Previous topic - Next topic

cnasich

For a test case, I generated an object adding triangles with addTriangles in two ways, always before compile and build:

1- Positioning each pair of triangle in differents positions.

track = new Object3D(100000);
Log.i("Generating triangles", "Start populating");
SimpleVector v1 = new SimpleVector(), v2 = new SimpleVector(), v3 = new SimpleVector(), v4 = new SimpleVector();
v1.set(-working_width / 2, -TRACK_FLOOR_DISTANCE, 1);
v2.set( working_width / 2, -TRACK_FLOOR_DISTANCE, 1);
v3.set( working_width / 2, -TRACK_FLOOR_DISTANCE, 0);
v4.set(-working_width / 2, -TRACK_FLOOR_DISTANCE, 0);
for (i = 0; i < 499; i++) {
track.addTriangle(v1, 0, 1, v2, 1, 1, v3, 1, 0, TextureManager.getInstance().getTextureID("track"));
track.addTriangle(v1, 0, 1, v3, 1, 0, v4, 0, 0, TextureManager.getInstance().getTextureID("track"));
if (i % 1000 == 0) {
Log.i("Generating triangles", "n = " + String.valueOf(2 * i));
}
}
Log.i("Generating triangles", "End generation");


2- Positioning each pair of triangle in the same position.

track = new Object3D(100000);
Log.i("Generating triangles", "Start populating");
SimpleVector v1 = new SimpleVector(), v2 = new SimpleVector(), v3 = new SimpleVector(), v4 = new SimpleVector();
for (i = 0; i < 499; i++) {
v1.set(-working_width / 2, -TRACK_FLOOR_DISTANCE, -i * 0.0001f);
v2.set( working_width / 2, -TRACK_FLOOR_DISTANCE, -i * 0.0001f);
v3.set( working_width / 2, -TRACK_FLOOR_DISTANCE, -i * 0.0001f - 0.0001f);
v4.set(-working_width / 2, -TRACK_FLOOR_DISTANCE, -i * 0.0001f - 0.0001f);
track.addTriangle(v1, 0, 1, v3, 1, 0, v2, 1, 1, TextureManager.getInstance().getTextureID("track"));
track.addTriangle(v3, 1, 0, v1, 0, 1, v4, 0, 0, TextureManager.getInstance().getTextureID("track"));
if (i % 1000 == 0) {
Log.i("Generating triangles", "n = " + String.valueOf(2 * i));
}
}
Log.i("Generating triangles", "End generation");


When get the mesh throw a VertexController, the mesh array get different sizes depending how was positioned the triangles.
With the option 1, I get 4 elements (wrong). With option 2, I get 998 elements (which is correct size). Why? Is an issue or I are wrong?


EgonOlsen

That's a feature. You can disable it by calling Object3D.disableVertexSharing() before adding triangles. However, it's very likely that the object compiler will collapse these vertices again, so it might be better not to do it this way, but offset the vertices slightly from each other.

cnasich

I did that and not get the expected result :(
So I will continue adding triangles to world albeit is slower.

EgonOlsen

'Did not get the expected result' is a bit vague. What is the expected result and in which way did you fail to achieve it?

cnasich

As you say I try calling Object3D.disableVertexSharing() when add many triangles, but I get the same results: add 998 triangles in the same place result in 4 triangles in the mesh. When add 998 in different places result in 998 triangles in the mesh.

EgonOlsen

And called that before actually adding the triangles? Anyway...the compilation process will collapse them anyway. Just and them on slightly different places far out of sight.

cnasich

No..I called after ;)
But happen a strange behavior: the normals change.

Here create triangles in a visible position:

//Generate a track for testing propose
Log.i("Generating triangles", "Reserving space");
track = new Object3D(20000);
track.disableVertexSharing();
Log.i("Generating triangles", "Start populating");

SimpleVector v1 = new SimpleVector(), v2 = new SimpleVector(), v3 = new SimpleVector(), v4 = new SimpleVector();

for (i = 0; i < 10000; i++) {
v1.set(-working_width / 2, TRACK_FLOOR_DISTANCE, -i * 0.05f);
v2.set( working_width / 2, TRACK_FLOOR_DISTANCE, -i * 0.05f);
v3.set( working_width / 2, TRACK_FLOOR_DISTANCE, -i * 0.05f - 0.05f);
v4.set(-working_width / 2, TRACK_FLOOR_DISTANCE, -i * 0.05f - 0.05f);
track.addTriangle(v3, 1, 0, v1, 0, 1, v4, 0, 0, TextureManager.getInstance().getTextureID("track"));
track.addTriangle(v1, 0, 1, v3, 1, 0, v2, 1, 1, TextureManager.getInstance().getTextureID("track"));

if (i % 1000 == 0) {
Log.i("Generating triangles", "n = " + String.valueOf(i));
}
}
Log.i("Generating triangles", "End generation");

//setup track
track.setTransparency((int)(2^12-1));
track.setTransparencyMode(Object3D.TRANSPARENCY_MODE_DEFAULT);
track.getMesh().setVertexController(RepositionVertexes_, IVertexController.PRESERVE_SOURCE_MESH);
track.compile(true, true);
track.build();


And get this
https://www.dropbox.com/s/oqcrosjce0daotr/Screenshot_2014-03-20-19-47-11.png

The VertexController code is:

public void apply() {
SimpleVector[] dstMesh=this.getDestinationMesh();

v0.set(v1);
v1.set(v2);
if (right_triangle) {
v2.set(width / 2, 0, 0);
v2.rotateY(direction_angle);
v2.set(v2.calcAdd(t2));
dstMesh[i] = new SimpleVector(v0);
++i;
dstMesh[i] = new SimpleVector(v1);
++i;
dstMesh[i] = new SimpleVector(v2);
++i;
}
else {
v2.set(-width / 2, 0, 0);
v2.rotateY(direction_angle);
v2.set(v2.calcAdd(t2));
dstMesh[i] = new SimpleVector(v0);
++i;
dstMesh[i] = new SimpleVector(v1);
++i;
dstMesh[i] = new SimpleVector(v2);
++i;
}

right_triangle = !right_triangle;
}


Then in OnDrawFrame I call track.getMesh().applyVertexController()

track.getMesh().applyVertexController();
track.touch();


And get this
https://www.dropbox.com/s/1dz205vkcywynsb/Screenshot_2014-03-20-19-47-24.png

I not changed the normals, but seems to be changed.

EgonOlsen

In the vertex controller, you have to copy the normals are well. That would explain a lighting issue, but not missing polygons, because face culling isn't based on vertex but on face normals and those are computed at the fly. You have to make sure that you don't change the winding order of the polygons in the controller or the face normals direction will also change. However, i can't spot this in your code...but then again, i don't fully understand what it is supposed to do.
You can try to disable culling on the object. If the polygons return (albeit with bad lighting), it's that issue. If they don't return, it has to be something else.

cnasich

What I try to do is:
- Create a mesh of triangles (for now in sight) to have all Property Types calculated.
- With a VertexController, move the vertices of each triangle to trace the work done by the machine.

I do not try to keep the order of the vertices of the triangles because I assume that moving the vertices, the other properties of a triangle should not change. I also tried setting the normal (0, 1, 0) in the VertexController but nothing changes.

Here I show a sketch of what I hope will happen:
https://www.dropbox.com/s/o3md6eu9372mg54/Concept1.gif