Merge Object3D with extends Object3D

Started by roninjnj, August 11, 2008, 11:06:54 PM

Previous topic - Next topic

roninjnj

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)

EgonOlsen

#1
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)

roninjnj

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!!

paulscode

#3
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:

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:

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.

fireside

#4
I don't know anything about this, but could you use cloneMesh() for this type of thing?
click here->Fireside 7 Games<-

EgonOlsen

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... ???

roninjnj

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!!

;-)

roninjnj

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! ;-)



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);
}
}
}



EgonOlsen

#8
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).

roninjnj