PDA

View Full Version : Dynamically Adding Shapes/Objects to a Scene



dhin
04-21-2002, 11:59 PM
I ran a search, I've checked every think I can think of and I can't find an answer to this one. Now maybe I've looked in all of the wrong place but if any one can help I would really be thankful.

What I am trying to do is to create a Open GL 3d Modeling Application. It is a class project. I am navigating thought the scene using the glutLookAt() function, and I can add static scenes and move though them easily enough, but when I try to dynamically create object within the scene nothing happens at best, at worst I get errors with in errors and no comprehensible way of sifting though them. I've been using GL4Java (Yes, I know, it wasn't the best choice. I should have gone with C++ but I can't change now.) Is there any advice anyone can give me to add objects Dynamically. If not any ideas on whom to ask or where to look would be helpful too.

JML
04-22-2002, 12:52 AM
One approach is to create an interface/class like 'GLObject' that has at least a 'drawYourself' method.
Also create a manager class to which GLObjects can register themselves. The manager class should take care of the rendering loop and calling each 'drawYourself' method on all registered objects.

This will simplify adding and removing objects when building your scene.

Depending on your needs you can create GLObject implementations that draw a fixed geometric figure, a parametrized figure or even read in a complete 3d mesh file and display it.

You can introduce GLObject draw-levels/types to have the manager decide on the order of drawing (like 'background'/cubemap first, opaque objects next, transparent objects next, console/lensflare/postprocessing last etc.).

HTH

Jean-Marc

dhin
04-22-2002, 02:47 AM
Not to sound stupid (I am still new to OGL,) but is drawYourself a GL standard? I couldn't find an example to go off so I just was wondering. And exactly how would the manager be used. (I know I sound really new a programming at thins point but, I have been working around the clock for the last couple of weeks and implementing new ideas isn't easy to figure out at the moment.)

Would 'GLObject' need to be an abstact class, and the manager an interface, or is it different than that?

JML
04-22-2002, 03:02 AM
Dhin,

Please note, I'm not a Java programmer (just C++ and other OO languages). Hopefully you'll be able to translate the C++ speak to Java.

drawYourself is a made up name. It's not OpenGL.

The manager would be instantiated in the main() function. Then the main() function would instantiate some GLObject objects (or derivations/descendants of it) and register them with the manager: call a self-made method like registerObject( GLObject theObject)
Then the main() function would setup an OpenGL rendering context and hand it to the manager (or would instruct the manager to create the context, whatever suits your view on responsibilities best)
Then the main() function would tell the manager to startRendering().
The startRendering() method would repeatedly do something like:
1. clear screen
2. setup lights and camera
3. draw background objects
4. draw opaque objects
5. draw transparent objects
6. do postprocessing
7. draw screen-overlays like console/fps counter etc.
8. if exit signal received: terminate loop

Depending on your needs, this loop can be different.

This approach is my OO interpretation of the way the GLUT framework operates. In fact I've build a tiny render-engine on top of GLUT that works as described above (the manager hooks into all GLUT callback functions and distributes them to the GLObjects that have indicated to be interested in receiving them).

HTH

Jean-Marc.

dhin
04-22-2002, 03:15 AM
Jean-Marc

Thank you for the, help. I'll see what I can do with it. Admitedly I'm not a true programmer, but I do understand the basics enough to follow what you've given me. Atlest I hope so.

Dhin

dhin
04-22-2002, 04:29 AM
Okay, my brain still isn't comprehending this. The problem now is what exactly is needed in the 'drawYouself' method. Would this be the OGL commands or would it be something else. I just can't think for myself at the moment, I guess I'll need this all spoon feed to me. I normally only create the models, I don't program that often.

mphanke
04-22-2002, 04:59 AM
Dhin,

what you need to do from my point of view is to setup a tree (ever done this??). What you need to do is write a base class "CommonObject" from which all other Objects are derived. I would suggest you put a member String ClassName into your class where you set the type of object when you derive other classes. This Base class should have a Draw-Func (in the above post "DrawYourself" (that's only a name, nuttin' more!)). Then you write a class, let's call it "ObjectList" which has an array of CommonObject and a count. Then you loop through the ObjectList and call for each Object in the array the Draw-Func. That's it! ;-)

Example:



class CommonObject
{
private ClassName;
public void CommonObject(String name)
{ ClassName = Name; }
virtual Draw();
}

class SphereObject extends CommonObject
{
SphereObject(int x, int y, int z, int radius):CommonObject("SphereObject")
{ YOUR CONSTRUCTOR CODE HERE! }

Draw() { YOUR DRAW CODE HERE! }
}
class ObjectList
{
Private CommonObject myObjects[100]; <- static is no good!!! Make it dynamic!
private int Count;

ObjectList() {Count = 0;}
Add(CommonObject Obj)
{
myObjects[Count] = Obj;
Count++;
}

Draw()
{
CLEAR AND SETUP YOUR VIEWPORT HERE!
for(int i=0; i<Count; i++)
myObjects[i]->Draw();
}
}

Some where in your Main-Func you call something like:

ObjectList ObjList = new ObjectList();
SphereObject Sphere = new Sphere( 0, 0, 0, 30 );
ObjList.Add(Sphere);
ObjList.Draw();


Or somehow like that. I don't really like JAVA so I can't say this code is usable, but it should look somehow like that!

Have fun,

Martin

[This message has been edited by mphanke (edited 04-22-2002).]

nexusone
04-22-2002, 08:15 AM
Hi, have you ever used variables? or structures?

examples of added dynamiclly, note this is a very simple example:

int object_count; // number of objects to process when drawing scene.

routine:

addobject(int *object_data_pointer)
{
if (object_data_pointer != NULL) //valid object then process;
{
object_count++; // give our new object a number
object_array[object_count] = object_data_pointer; // add object to our object array.
return(TRUE); // Indicate object added.
}else return(FALSE) // Indicate object not added.
}

draw_scene()
{
int x;
// process objects.
for(x = 0; x < object_count; x++)
{
Draw_object( object_array[x] );
}
}



Originally posted by dhin:
I ran a search, I've checked every think I can think of and I can't find an answer to this one. Now maybe I've looked in all of the wrong place but if any one can help I would really be thankful.

What I am trying to do is to create a Open GL 3d Modeling Application. It is a class project. I am navigating thought the scene using the glutLookAt() function, and I can add static scenes and move though them easily enough, but when I try to dynamically create object within the scene nothing happens at best, at worst I get errors with in errors and no comprehensible way of sifting though them. I've been using GL4Java (Yes, I know, it wasn't the best choice. I should have gone with C++ but I can't change now.) Is there any advice anyone can give me to add objects Dynamically. If not any ideas on whom to ask or where to look would be helpful too.

Miguel_dup1
04-22-2002, 09:28 AM
nex, java does not use structs...

Dhin, vectors are very easy to use in java...
They will allow you to attach new objects to your vector variable...

To keep it as simple as possible without binary trees and quad trees and this and linked lists and this and that, create a vector of objects to be drawn and traverse through it for drawing.
I have never used opengl and java so the code I give you is not an exact guide.

You could have something like this


import java.util.Vector;

puclic class MyObject
{
//your varibles, e.g vector
Vector myObjects;

MyObject()
{
myObjects = new Vector();
}

void AddObject( "Object to add" )
{
myObjects.add( int index, object );
}

public void DrawObjects()
{
//traverse through all of your objects
for( int i; i < numOfObjects; i++ )
{
//draw objects with opengl functions

}
}
}


With this, you can simply add more objects to you vector to then draw them.

Java API is here http://java.sun.com/j2se/1.3/docs/api/index.html

Give it a shot.