Author Topic: Object3D possiblities  (Read 3741 times)

Offline miron123

  • byte
  • *
  • Posts: 16
    • View Profile
Object3D possiblities
« on: January 10, 2014, 09:54:48 pm »
Hello
I'm working now on a 3D perspectiv of a roadnetwork. I have Geodatas where and how each road segment has to be drawn. My problem at the moment is the following:
I'm trying to create a Object3D object for each road segment but I don t know how to initialize this object correctly. Cause the roadsegments are polygons and i tried to initialize the
object with all points of the polygon, an uvs array, a  normals array and an empty texture, (TextureManager.TEXTURE_NOTFOUND ). But when I try to render the world(road segment included)
nothing is shown. Of course i changed the camera position and I always look at the object but there just isn't anything.
My question now is: if its better to draw polylines and create a custom polygon or have i done something wrong with the object3d?
Hopefully my english is understandable.

Code snip:
public static void drawRoadSegment(RoadSegment seg) {

      float[] tmp = new float[((seg.getBounds().xpoints.length) * 3)];

      int[] x = seg.getBounds().xpoints;
      int[] y = seg.getBounds().ypoints;
      Vector<Float> t = new Vector<Float>();

      for (int i = 0; i < seg.getBounds().xpoints.length; i++) {
         t.add((float) x);
         t.add((float) y);
         t.add(0.0f);
      }
      for (int i = 0; i < t.size(); i++) {
         tmp = t.elementAt(i);
      }

      Object3D obj = new Object3D(tmp, null, null, TextureManager.TEXTURE_NOTFOUND); //in this case I tried to use null for uvs and normals but I tried it also with initialized arrays
      obj.setVisibility(true);
      obj.build();

      box = Primitives.getBox(13f, 2f);
      box.setTexture("box");
      box.setEnvmapped(Object3D.ENVMAP_ENABLED);
      box.build();

      world.addObject(box);
      world.getCamera().lookAt(box.getTransformedCenter());
    }
   }

Please help

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Object3D possiblities
« Reply #1 on: January 10, 2014, 10:09:14 pm »
The bulk-constructor of Object3D isn't as easy to handle as the normal ones, because it's much less forgiving in case of an error. You really have to be sure that your input is correct or otherwise, you'll get *nothing* in return. I advise to use the normal constructors instead and add your geometry by using addTriangle() instead. However, this may or may not be the reason of your problem. Maybe your polygon order is just wrong. Try a Object3D.setCulling(false); on your road and see if that helps.

Offline miron123

  • byte
  • *
  • Posts: 16
    • View Profile
Re: Object3D possiblities
« Reply #2 on: January 15, 2014, 05:44:24 pm »
Hello again.
This last week i tried to create a custom Object3D with addTriangle() but it didn't work as I expected.
I have a 2D polygon of a road segment which contains two int arrays:

int xPoly[] = { 468, 551, 551, 606, 606, 676, 678, 609, 609, 608, 608,
               608, 608, 607, 607, 607, 607, 606, 606, 605, 605, 551, 551, 468 };
int yPoly[] = { 424, 419, 419, 426, 426, 358, 362, 429, 429, 430, 430,
               430, 430, 430, 430, 430, 430, 430, 430, 430, 430, 423, 423, 428 };

Now my question is, how to create a Object3D based on these two arrays. Can I use the constructor: public Object3D(float[] coordinates, float[] uvs, int[] indices, int textureId). If that's possible, can someone post a example how to use this constructor in my case?

Thanks in advance

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Object3D possiblities
« Reply #3 on: January 15, 2014, 08:41:55 pm »
You would have to merge them and add a z-component to it. Just like you would with addTriangle. I strongly suggest to use addTriangle instead of the bulk constructor. You just have to make some reasonable triangles out of this point cloud. What kind of format is this? If that defines 2d triangles, then...how...?

Offline miron123

  • byte
  • *
  • Posts: 16
    • View Profile
Re: Object3D possiblities
« Reply #4 on: January 16, 2014, 09:46:34 am »
Here is a picture of how the polygon looks like:
https://www.dropbox.com/sh/hyvk3wha4akoxok/0kpYjcglKa

I just want to display the same polygon in my jpct world. I don t necessarily need to add a z value, cause the roadsegments has no real hight.
I drew the polygon like this: g2d.draw(lane);

Before that i created the Polygon with 2 for loops:

Polygon lane = new Polygon();
for (int i = 0; i < leftEdge.size() - 1; i++) {
   int x11 = (int) leftEdge.get(i).x;
   int y11 = (int) leftEdge.get(i).y;
   int x21 = (int) leftEdge.get(i + 1).x;
   int y21 = (int) leftEdge.get(i + 1).y;
   lane.addPoint(x11, y11);
   lane.addPoint(x21, y21);
}
for (int i = rightEdge.size() - 1; i > 0; i--) {
   int x12 = (int) rightEdge.get(i).x;
   int y12 = (int) rightEdge.get(i).y;
   int x22 = (int) rightEdge.get(i - 1).x;
   int y22 = (int) rightEdge.get(i - 1).y;
   lane.addPoint(x12, y12);
   lane.addPoint(x22, y22);
}

The rightEdge and leftEdge Vector contains the roadsegment points.



Offline miron123

  • byte
  • *
  • Posts: 16
    • View Profile
Re: Object3D possiblities
« Reply #5 on: January 16, 2014, 02:52:07 pm »
Today I tried it again but it doesn t look right. It doesn t look like the 2d right polygon.
My code today:

Code: [Select]
public static void drawRoadSegment(RoadSegment seg, Matrix matrix) {
for (LaneSegment ls : seg.getLaneSegments()) {
List leftEdge = ls.getLeftEdge();
List rightEdge = ls.getRightEdge();
leftEdge = matrix.multiply(leftEdge);
rightEdge = matrix.multiply(rightEdge);

Vector<Point> tmp = new Vector<Point>();
for (int i = 0; i < leftEdge.size() - 1; i++) {
int x11 = (int) leftEdge.get(i).x;
int y11 = (int) leftEdge.get(i).y;
int x21 = (int) leftEdge.get(i + 1).x;
int y21 = (int) leftEdge.get(i + 1).y;
tmp.add(new Point(x11, y11));
tmp.add(new Point(x21, y21));
}
for (int i = rightEdge.size() - 1; i > 0; i--) {
int x12 = (int) rightEdge.get(i).x;
int y12 = (int) rightEdge.get(i).y;
int x22 = (int) rightEdge.get(i - 1).x;
int y22 = (int) rightEdge.get(i - 1).y;
tmp.add(new Point(x12, y12));
tmp.add(new Point(x22, y22));
}
                        //the tmp vector contains the correct x/y points for the polygon

int j = tmp.size() - 1;
                        //loop to create the correct triangles
for (int i = 0; i < tmp.size() - 1; i++) {
if (j != i + 1) {
SimpleVector s1 = new SimpleVector(tmp.get(i).getX(), tmp
.get(i).getY(), 0);
SimpleVector s2 = new SimpleVector(tmp.get(i + 1).getX(),
tmp.get(i + 1).getY(), 0);
SimpleVector s3 = new SimpleVector(tmp.get(j).getX(), tmp
.get(j).getY(), 0);

Object3D obj = new Object3D(tmp.size() - 1);
obj.addTriangle(s1, s2, s3);
obj.build();
world.addObject(obj);
j--;
}
}
}
}

But it displays something, but just not entirly correct.
Now I would like to know if my logic is correct or if i made a mistake.

Here: https://www.dropbox.com/sh/hyvk3wha4akoxok/0kpYjcglKa
are two screen shots, one 2d and one 3d;

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Object3D possiblities
« Reply #6 on: January 16, 2014, 08:18:54 pm »
If your logic would be correct, it would work... ;) Your results looks like a mirrored version with some flaws. I'm not sure what xxxEdge and LaneSegment and all this stuff contains in which format...
You seem to add two points for each segment and then build a triangle out of the first two point from the left and one from the right!? Then you create one Object per triangle!? Apart from the fact that is very inefficient (use one object to hold all the triangles), aren't you wrapping around, i.e. if j becomes lower than i, aren't you creating new triangles with the same coordinates as before?

One idea: Scrap this, take out a piece of paper, write down some coordinates in 2D and try to solve it on paper. This usually helps me a lot, if i'm stuck on something like this.

Offline miron123

  • byte
  • *
  • Posts: 16
    • View Profile
Re: Object3D possiblities
« Reply #7 on: January 16, 2014, 11:21:37 pm »
Explanation:
RoadSegment is one road of my road network,
The lanesegments are part of these roadsegment -> so every roadsegment can contain one or multiple lanesegments,
The vector contains all points of my lanesegment, first index is the first point of the polygon, then the second, when i
come to half of the vector the points are the mirrored points from before. So each part of the lanesegment is an rectangle.

I tried to construct a for loop which moves through my vector and creates a triangle. At the end it should contain vector.length / 2 triangles +
the mirrored version of these triangles. Because of that I skipped the case when i and j are equal. Now it should create multiple rectangle based
on triangles and the correct position of the vector points.

Concerning the pen and paper advice:
I already did that and i thought my logic was correct. Here is a quick png which I used for my case:
https://www.dropbox.com/sh/hyvk3wha4akoxok/0kpYjcglKa

I hope you can help :)

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Object3D possiblities
« Reply #8 on: January 17, 2014, 06:13:25 pm »
I can't point my finger at it, but it seems to me something goes wrong with your logic once i is larger than tmp.size/2. Have you created a test case that verifies that your loop processes the right indices?