PDA

View Full Version : Avoid importing the same mesh into the scene?



diskhub
05-09-2011, 07:38 AM
Hi all,
I am very new to opengl.
I managed to import an object but i wish to reuse that MEsh for other instance object in my game... (As there are many of the same geometry mesh in the scene).

How can i do so without keep importing the same mesh into the scene?

ZbuffeR
05-09-2011, 08:13 AM
Import it only once.
In fact I don't see why you would do differently.

Or maybe I misunderstood your question ?

diskhub
05-09-2011, 08:21 AM
I have multiple objects of the same geometry.
And What i did now is that for each objs, when i am in the display() function, i call a loop which loops thru each of the objects and load the geometry.

I find it resources wasteful because the geometry are the same... except that the rotation and translation are different.

Hence, i wish some experts to advice me how i can deal with this?

McLeary
05-09-2011, 08:22 AM
Import it only once.
In fact I don't see why you would do differently.

Or maybe I misunderstood your question ?

And draw multiple times. There's no secret about it.

diskhub
05-09-2011, 08:23 AM
I can't store the geometry in a buffer or something and retrieve it and somehow link that geometry object with each of the class objects I have created??

The Little Body
05-09-2011, 12:10 PM
Diskhub,

You can store the common part using glNewList/glEndList and use glCallList after the rotation/translation Display List (http://www.songho.ca/opengl/gl_displaylist.html)

diskhub
05-09-2011, 06:37 PM
how can i do that?
Currently, i have the following a class called Teaport (for example):


class Teaport{
..
void objload(String name, float size, GL2 gl){
model = new OBJModel(name, size, gl, false);
}

void draw(GLAutoDrawable drawable)
{
System.out.println(model.getDisplayList());
GL2 gl = drawable.getGL().getGL2();
gl.glPushMatrix();
gl.glTranslatef(25, 5, 25);
gl.glRotatef(-90.0f+spin, 1.0f, 1.0f, 1.0f);
model.draw(gl);
gl.glPopMatrix();
spin+=2;
}
..
}


the model.draw method will draw the teaport in the scene.
I have also used glNewList/glEndList and glCallList in my draw method in the objloader.

Now the problem is:
I wish to have mulitple teaport in the scene.
Which means i have to initialise my teaport object like the following:


for(int i=0; i<8; i++){
Teapot temp = new Teaport();
arraylist.add(temp);
}


but i do not wish to do this for my draw and load:


for(int i=0; i<8; i++){
arraylist.get(i).objload('..');
}


how can i achieve this?
Sorry, I am still quite lost.

Alfonse Reinheart
05-09-2011, 07:04 PM
I wish to have mulitple teaport in the scene.
Which means i have to initialise my teaport object like the following:

"have to"? It's your code; you don't "have to" anything.

This is a trivial software engineering problem. Your problem is that your "Teapot" object represents not only a mesh, but the position and orientation of that mesh in the world. Separate these two concepts.

You need a "mesh" object that represents a particular set of vertex data. And you need an object that represents the position and orientation of something in the world.


but i do not wish to do this for my draw and load:

Then don't. Make a separate function that loads a model one time. Then give references to that model to the objects that need them. Your "Teapot" constructor should be given a model; it should neither own or load it.

diskhub
05-09-2011, 07:45 PM
thanks for enlightening me!
I will give it a try :D