hello all

for some reason in my project the need of use 3d ojbects has come...
so i was taking some look at opengl tuts.. and so far i learned a few things...
like :

    _glVertex3f  1.0f, 1.0f,-1.0f
    _glVertex3f -1.0f, 1.0f,-1.0f
    _glVertex3f -1.0f, 1.0f, 1.0f
    _glVertex3f  1.0f, 1.0f, 1.0f

and so on...

but i need to instanciate new object on the runtime!
lets say i click a button and should add a new Cube on screen... thats what i need

but i wasnt able to create that handling so far
cos the OpengGL specification (and probably DirectX) has a main Loop that refresh the screen and draw again the cube writen on the code (just like my example), but how can i have multiple cubes,add,remove them ?

how can i create such handling?


Posted on 2006-06-10 16:10:48 by GR33d
Neither the OpenGL nor the DirectX have anything to do with 'loops'. You refresh things when you want. You can do this in response to WM_PAINT, if you wish. There are thousands of methods to draw cubes on the screen - it depends on what you want to do.
Posted on 2006-06-10 16:17:20 by ti_mo_n

i know how to draw cubes and stuff
i just want to know a method to have multi objects on screen
and draw them, with the possiblity of add and remove them thx...
Posted on 2006-06-10 16:26:26 by GR33d
It's time for you to begin learning how to use MATRICES :)
You wish to render N instances of your geometry.
Each instance has unique position and rotation - agreed?

What you do is this:
For each instance, create suitable Translation and Rotation matrices and Multiply (concatenate) those two Matrices, which gives you one 'Combined Transformation Matrix' for each instance.
Now in your Rendering loop, you do this:

For each instance,
-apply the instance's combined matrix as the 'current world transform'
-render the object as you normally would
-reset the World Transform to 'identity matrix'

The last step of setting Identity is important, because if we don't do that, each object will be positioned and oriented relative to the previous object, and not relative to the World Origin !!!
Sometimes we WANT this, but not when we're just learning how to use Matrices properly :P

I hope this small contribution helps you to better understand how to correctly instance your objects with arbitrary position and rotation :)
If you have further questions, feel free to ask :)
Posted on 2006-06-10 23:06:55 by Homer
In other words: either you draw multiple predefines cubes, using 1 'camera',  OR    you draw 1 predefined cube, move the camera, draw the same cube again (it'll appear next to the cube already rendered), and so on.

By 'predefined cube' I mean a cube which vertices are already prepared when the renderer is running. You can have 10 sets of vertices for 10 cubes and render them all at once (more memory, but faster), or you can have 1 set of vetices and then: render, change 'camera', render, 'change camera', ... , ... , perform 'buffer flip' (make the rendered screen visible).

Learing about matrices may be a harder approach, but it's essential if you want to play with 3D graphics, so recommend you doing it.
Posted on 2006-06-11 07:48:46 by ti_mo_n
We're not moving the Camera at all.
This is important, so listen up :)

There's three different Matrices we can screw with inside opengl and direct3d.
1 - View Matrix
2 - Projection Matrix
3 - World Matrix

The Camera is (usually) implemented using the View and Projection matrices.. specifically, the Projection matrix (normally only set up once then forget it) provides the 'perspective' (makes stuff smaller when its further away)..ie it controls the 'camera lensing', while the View matrix determines where the Camera is, and which way it is pointing.

Now we get to the third (and last) matrix we get to mess around with.
The World matrix can be thought of as 'an imaginary 3D pen' which we can use to draw each item in the world at whatever position and rotation we want.
In fact, its not the objects we draw that are being rotated and translated, THE WORLD IS BEING ROTATED AND TRANSLATED - then we draw our object(s), then we reset the 'imaginary pen' back to its initial state (starting point at world origin) with that Identity matrix I mentioned, before proceeding to the next object(s).
Let's go into a little more detail about this concept of 'shifting the world' because it can confuse the heck out of newbies...

Forgetting about rotation for a moment, if we wanted to draw an object at (10,10,10) , we have to put the values (-10,-10,-10) into a translation matrix, apply the matrix, draw our object..
Why are we using negative values? Because if we want our object at X=10, we need to shift THE WORLD by -10 to achieve that (and the rotations work the same way - we rotate by MINUS the desired rotations for a specific object instance).

So, we're not manipulating the Camera, we're not even manipulating the objects we render - we're manipulating the entire universe !!!
This seems crazy until you grasp that 'the stuff in the World is not being manipulated' and go back to my initial description of 'an imaginary 3D pen' - then everything becomes sane again :)

Welcome to the Matrix  8)

PostScript : It's important to note that we should ALWAYS set the World Matrix back to Identity after all our rendering is complete and before we display the Frame we just rendered, otherwise it WILL mess with the Camera, because the whole world will be drawn using the most recently rendered object as the world origin  :shock:
Posted on 2006-06-11 09:08:39 by Homer
Homer: You're right, of course. By writing 'camera' I meant some general 'concept' of a camera - not the view matrix (which is often compared to a camera). I understand how the 3D stuff works - we move the universe ;)
Posted on 2006-06-11 16:14:34 by ti_mo_n