Author Topic: addTriangle issue  (Read 1077 times)

Offline cnasich

  • byte
  • *
  • Posts: 8
    • View Profile
addTriangle issue
« on: March 19, 2014, 09:20:20 pm »
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.
Code: [Select]
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.
Code: [Select]
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?


Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11748
    • View Profile
    • http://www.jpct.net
Re: addTriangle issue
« Reply #1 on: March 20, 2014, 09:32:32 am »
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.

Offline cnasich

  • byte
  • *
  • Posts: 8
    • View Profile
Re: addTriangle issue
« Reply #2 on: March 20, 2014, 03:53:23 pm »
I did that and not get the expected result :(
So I will continue adding triangles to world albeit is slower.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11748
    • View Profile
    • http://www.jpct.net
Re: addTriangle issue
« Reply #3 on: March 20, 2014, 04:16:50 pm »
'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?

Offline cnasich

  • byte
  • *
  • Posts: 8
    • View Profile
Re: addTriangle issue
« Reply #4 on: March 20, 2014, 04:25:27 pm »
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.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11748
    • View Profile
    • http://www.jpct.net
Re: addTriangle issue
« Reply #5 on: March 20, 2014, 05:41:54 pm »
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.

Offline cnasich

  • byte
  • *
  • Posts: 8
    • View Profile
Re: addTriangle issue
« Reply #6 on: March 21, 2014, 12:51:23 am »
No..I called after ;)
But happen a strange behavior: the normals change.

Here create triangles in a visible position:
Code: [Select]
//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:
Code: [Select]
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()
Code: [Select]
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.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11748
    • View Profile
    • http://www.jpct.net
Re: addTriangle issue
« Reply #7 on: March 21, 2014, 07:22:48 am »
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.

Offline cnasich

  • byte
  • *
  • Posts: 8
    • View Profile
Re: addTriangle issue
« Reply #8 on: March 21, 2014, 06:34:42 pm »
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