Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (536)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Having multiples of the same mesh (3D)  (Read 1501 times)
0 Members and 1 Guest are viewing this topic.
Offline ahottev

Senior Newbie





« Posted 2010-11-23 00:33:44 »

So i'm loading an .OBJ mesh (using the loader that comes with java) each time i need it but i'm sure there's a more efficient way of doing things, especially since i'll be needing a lot of the same mesh.

What's the more efficient way of doing this? Btw, i'm using Java3D. And to put into context, i'm building a 3D strategy game, the mesh in question being a "forest". So the only thing that would need to be different is the X,Y and Z position, (and maybe the texture and the animation, but im not there yet).

I thought of using the same solution i found with my textures, ie load them once at the first creation of the object as a static variable and re-using them over and over, but it doesn't seem to work with 3D mesh, it gives me errors when i try to attach it to the scene node. I don't have the code right now but if it can help, i'll post something tomorrow.

Thanks!
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #1 - Posted 2010-11-23 02:52:34 »

Load it once, store it in memory, then draw the same model multiple times at different positions.

Not sure how Java3D works, but I'm pretty sure it's got a transformation matrix just like OpenGL. So, to draw the trees elsewhere, just translate the context around each time you draw.

Pseudocode:
1  
2  
3  
4  
5  
6  
for (int i = 0; i < trees.size(); i++)
{
    glLoadIdentity(); //Put the Matrix back to 0
   glTranslatef(trees.get(i).x, trees.get(i).y, trees.get(i).z);
    trees.get(i).getOBJ().render();
}

See my work:
OTC Software
Offline lhkbob

JGO Knight


Medals: 32



« Reply #2 - Posted 2010-11-23 05:09:19 »

In Java3D the geometry is loaded as some form of a node in the scene graph. A node can only have one parent, which is why you can never add the instance again without causing exceptions.  You need to extract the actually geometry information from the node and pass that geometry to the new nodes.  Something like that, it's been far too long since I've used Java3D and I don't actually like it very much.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline tom
« Reply #3 - Posted 2010-11-23 09:01:32 »

You can use a Link + SharedGroup. The SharedGroup contains the .OBJ mesh that you loaded once. Then you can have multiple Links pointing to the same SharedGroup.

Or you can use cloneTree to shallow copies of the loaded mesh. This is what I do. Link + SharedGroups makes a lot of things like picking more complicated.

Offline ahottev

Senior Newbie





« Reply #4 - Posted 2010-11-23 14:41:11 »

Or you can use cloneTree to shallow copies of the loaded mesh. This is what I do. Link + SharedGroups makes a lot of things like picking more complicated.

How do you use cloneTree exactly?

this is what i get: javax.media.j3d.RestrictedAccessException: Node: Cannot clone a live or compiled scenegraph

Does it change anything if i need to clone it at runtime?
Offline lhkbob

JGO Knight


Medals: 32



« Reply #5 - Posted 2010-11-23 15:29:24 »

Are you trying to clone it after you've started rendering it?  You should try cloning a copy right after you've loaded the .OBJ file.

Offline tom
« Reply #6 - Posted 2010-11-23 15:35:07 »

I keep a template model that I don't add to the scenegraph. When I need an instance I use cloneTree on the template and add the clone to the scenegraph.

Offline ahottev

Senior Newbie





« Reply #7 - Posted 2010-11-23 16:11:30 »

Ok, so this is simple:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
private void load3DAssets(){
      forestTemplateBG = new BranchGroup();
      String filename = "props\\forest.obj";
      ObjectFile f = new ObjectFile(ObjectFile.RESIZE);
       forestMesh = null;
      try {
         forestMesh = f.load(filename);
          } catch errors...
         
   }


But then, i'm not sure how to cloneTree it ?

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
private void addTree(float posx, float posy, float posz){               
                 
      TransformGroup tg = new TransformGroup();
     
      tg.addChild(forestTemplateBG.cloneTree());                   <-------????
      tg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
      Transform3D tr = new Transform3D();    
      tr.rotX( Math.PI / 2.0);  

      Vector3f translation = new Vector3f(posx, posy, posz + 0.1f);
      tr.setTranslation(translation);
      Vector3d scale = new Vector3d(0.5d, 0.5d, 0.5d);
      tr.setScale(scale);
     
      tg.setTransform(tr);
             
       forestBG.addChild(tg);
       forestBG.setPickable(false);
       forest.add( forestBG );
       
   }


"forest" at the end is an ArrayList of BranchGroup that i use to add to the scene with:

sceneBG.addChild( forest.get( forest.size()-1 ) );

each time i'm adding a forest to the game. (Working on the level editor right now.)

It might be clumsy but, but i'm still learning Wink

Offline tom
« Reply #8 - Posted 2010-11-23 16:22:42 »

The cloning code looks correct. However I don't see you adding frestMesh to forestTemplateBG.

Offline ahottev

Senior Newbie





« Reply #9 - Posted 2010-11-23 16:25:38 »

The cloning code looks correct. However I don't see you adding frestMesh to forestTemplateBG.

omg.... Thanks a lot!  It's working fine now Wink)

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Riven (14 views)
2014-07-29 18:09:19

Riven (10 views)
2014-07-29 18:08:52

Dwinin (10 views)
2014-07-29 10:59:34

E.R. Fleming (28 views)
2014-07-29 03:07:13

E.R. Fleming (10 views)
2014-07-29 03:06:25

pw (40 views)
2014-07-24 01:59:36

Riven (39 views)
2014-07-23 21:16:32

Riven (27 views)
2014-07-23 21:07:15

Riven (28 views)
2014-07-23 20:56:16

ctomni231 (59 views)
2014-07-18 06:55:21
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!