Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (408)
games submitted by our members
Games in WIP (293)
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 1206 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
« League of Dukes »

JGO Kernel


Medals: 39
Projects: 12


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!
Play the free demo of Revenge of the Titans!
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.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Browse for soundtracks for your game!

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

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (113 views)
2013-05-17 21:29:12

alaslipknot (123 views)
2013-05-16 21:24:48

gouessej (152 views)
2013-05-16 00:53:38

gouessej (145 views)
2013-05-16 00:17:58

theagentd (158 views)
2013-05-15 15:01:13

theagentd (142 views)
2013-05-15 15:00:54

StreetDoggy (186 views)
2013-05-14 15:56:26

kutucuk (210 views)
2013-05-12 17:10:36

kutucuk (210 views)
2013-05-12 15:36:09

UnluckyDevil (215 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
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!
Page created in 0.204 seconds with 20 queries.