Author Topic: Merge Object3D with extends Object3D  (Read 5805 times)

Offline roninjnj

  • byte
  • *
  • Posts: 20
    • View Profile
Merge Object3D with extends Object3D
« on: August 11, 2008, 11:06:54 pm »
Hi!!

I have a new question! :D

I have a Terrain class defined as "Public class Terrain extends Objec3D" with constructor:

public TerrainObject3D(int numCellX, int numCellY, Object3D firstCell){
       
       super(firstCell);   
       cellsX = numCellX;
       cellsY = numCellY;
}

I need add more Object3Ds to this class to make a terrain tile style formed with individuals Objects3D merges.

If i do " Object3D.merge( <<Terrain instance >>, <<Object3D instance>>)" i recive a error! (Obviously)

there is another way to do it with Terrain class?? how works merge method??

Another question: If i merge two objects with some vertexs in same positions (x,y,z). Are it´s convertes as only one vertex, or all vertexs are stores in the returning Object3D?

Thanks very much!!!

(Sorry my english)

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Merge Object3D with extends Object3D
« Reply #1 on: August 11, 2008, 11:31:30 pm »
I'm not sure, if merging is really what you want. Why don't you create the terrain as a whole? Merging an instance of a derived class with an instance of a base class (or another derived instance) won't work, because you'll get an Object3D in return which doesn't hold your own class' additional attributes. What merging bascially does, is to create an Object3D that can hold the data of both merge participants and then copy the data from both into the new object and return that. It's doesn't take care of vertex sharing (to answer your second question).
Adding a merge-method to your terrain class that takes care of the additional attributes and that returns a terrain instance instead of Object3D should be possible somehow, but doesn't sound very reasonable to me. I can think of three other options instead:

1.) Don't merge. Maybe it's not needed anyway. Use composition instead of inheritance for your terrain class
2.) Create Object3Ds with your terrain information, merge them, create your terrain instance from that merged object
3.) Create the terrain as whole without any merging involved (<- best option IMHO)
« Last Edit: August 11, 2008, 11:34:01 pm by EgonOlsen »

Offline roninjnj

  • byte
  • *
  • Posts: 20
    • View Profile
Re: Merge Object3D with extends Object3D
« Reply #2 on: August 12, 2008, 12:11:32 am »
When you talk about option number three, do you talk about make a only one 3ds model for whole terrain (bad for tile style)?

I can do with JPCT a unique object to make terrain adding one to one all vertexs with "addVertex" method? It sound crazy?

I have a map created with a semi-random function. Every cell or tile in map refers a unique model (small mountain, flat terrain, water, ect...). The "idea" was copy every one of this models and merge all them in map in a determinated position (cpoy of model "m" on position x,y world).

If i don´t merge all them objects as only one, lights do a bad effect on every edge model for every tile. I need make a unique object with all them models, and then lights go fine.

That´s my problem :S   otherwise, options three sound good.

Thanks very much!!

Offline paulscode

  • double
  • *****
  • Posts: 863
    • View Profile
    • PaulsCode.Com
Re: Merge Object3D with extends Object3D
« Reply #3 on: August 12, 2008, 01:00:30 am »
I was thinking about Egon's suggestion of composition instead of inheritance, and thought I would add my two cents worth in case you were not familiar with those terms.  I came up with an example.

First make the Object3D a member of the TerainObject3D class:

Code: [Select]
public class TerrainObject3D
{
    public Object3D myObject3D;
    public int cellsX, cellsY;

    // additional varriables here

    public TerrainObject3D( int numCellX, int numCellY, Object3D firstCell )
    {
        myObject3D = new Object3D( firstCell );
        cellsX = numCellX;
        cellsY = numCellY;
    }

    // additional methods here
}

Then additional Object3D's could be merged with the member Object3D rather than with the TerrainObject3D itself.  For example:

Code: [Select]
Object3D.merge( myTerrain.myObject3D, anotherObject3D );
After reading you last post about the map being created with a semi-random function, as well as your lighting issue, I think this might be your best option.
« Last Edit: August 12, 2008, 01:03:28 am by paulscode »

Offline fireside

  • double
  • *****
  • Posts: 607
    • View Profile
Re: Merge Object3D with extends Object3D
« Reply #4 on: August 12, 2008, 05:38:49 am »
I don't know anything about this, but could you use cloneMesh() for this type of thing?
« Last Edit: August 12, 2008, 05:40:35 am by fireside »
click here->Fireside 7 Games<-

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Merge Object3D with extends Object3D
« Reply #5 on: August 12, 2008, 07:56:56 am »
If lighting is the problem, merging most likely won't fix it. jPCT calculates normals based on adjacent polygons/vertices. If you translate and merge a kind of 3d-tile, it's very unlikely that they match in a way that the normal calculation will consider their vertices as shared. I don't have a clever solution for this ATM apart from not to do it this way, which may not be an option....you can of course manipulate the normals afterwards with an IVertexController...if some vertices share almost the same space but their normals are very different, average them and write them back for both...maybe something like that would work good enough... ???

Offline roninjnj

  • byte
  • *
  • Posts: 20
    • View Profile
Re: Merge Object3D with extends Object3D
« Reply #6 on: August 12, 2008, 03:10:22 pm »
Mmmm I was thinking in composition to solver my problem. But i think that make manually a object using addTriangle is a good solution. I was reading the post "Terrain support" and I think that is possible make something like that using vertexs sets to simulate tile style insead of heighmaps, and creating a unique Objec3D using this vertexs infomation. Whitout merging and without class composition.

I will post the results.

Thanks to all!!

;-)

Offline roninjnj

  • byte
  • *
  • Posts: 20
    • View Profile
Re: Merge Object3D with extends Object3D
« Reply #7 on: August 12, 2008, 06:11:57 pm »
Hi!! I have finish the TerrainObject3D class.

I can load 3ds models and adds all his triangles, textures ID and U,V vertexs positions to TerrainObject3D one to one.



I have reduced polygons count in 5000!! and lights go fine! :D

I leave here the code, to help more people. ;-)

One more questions:  ¿What is a Octree? ¿what can it do?

Thansk very very much! ;-)

Code: [Select]

package engine3D;

import com.threed.jpct.*;

public class TerrainObject3D{

private Object3D terrain;

public TerrainObject3D(int maxPol){

terrain = new Object3D(maxPol);
}
public void addNewTerrainCell(Object3D obj, int posX, int posY){

PolygonManager polMan = obj.getPolygonManager();
int maxPolyID = polMan.getMaxPolygonID();
System.out.println("MaxPolyID = "+ Integer.toString(maxPolyID));
for (int poly = 0; poly < maxPolyID; poly++)
{
//Cada polígono tiene 3 vértices.
SimpleVector vect1 = polMan.getTransformedVertex( poly, 0);
SimpleVector vect2 = polMan.getTransformedVertex( poly, 1);
SimpleVector vect3 = polMan.getTransformedVertex( poly, 2);
//Cada vértice de cada polígono tiene asociado un par de valores U,V para la textura.
SimpleVector uv1 = polMan.getTextureUV(poly , 0);
SimpleVector uv2 = polMan.getTextureUV(poly , 1);
SimpleVector uv3 = polMan.getTextureUV(poly , 2);
//Cada polígono tiene una textura asociada, la optenemos.
int texID = polMan.getPolygonTexture(poly);
//Vemos que no haya un error en la textura.
if (texID == -1)
{
texID = 0;
}
vect1.set(vect1.x+posX,vect1.y, vect1.z+posY);
vect2.set(vect2.x+posX,vect2.y, vect2.z+posY);
vect3.set(vect3.x+posX,vect3.y, vect3.z+posY);
//Lo unimos al objeto terrain.
terrain.addTriangle(vect1, uv1.x, uv1.y, vect2, uv2.x, uv2.y, vect3, uv3.x, uv3.y, texID);
}
}
}


Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Merge Object3D with extends Object3D
« Reply #8 on: August 12, 2008, 07:17:47 pm »
Well done. I'm glad that you've finally found a working solution to your problem.

An octree is a tree structure used for spatial subdivision of an Object3D. It can significantly speed up rendering (in cases where not the whole mesh is visible...in your posted screenshot, it won't help) and collision detection (has to be enabled on the OcTree; helps in all cases even when the whole mesh is visible).
« Last Edit: August 12, 2008, 08:17:24 pm by EgonOlsen »

Offline roninjnj

  • byte
  • *
  • Posts: 20
    • View Profile
Re: Merge Object3D with extends Object3D
« Reply #9 on: August 12, 2008, 07:29:52 pm »
Understood!! ;-)

Thanks! :D