views:

199

answers:

2

I am trying to learn how to add several objects and working with those in my 3D-world. I am developing for iPhone. Regarding my question below I couldn't find good information about it while Googling.

I have a function that creates all vertices-arrays with data in order to create a sphere. No problem adding it as the only object. The problem is that the data for the sphere is not aware of any other objects I may have created and added to the world. Of course, I understand that I can merge the arrays with vertices and add it all at the same time and the world will look as I want with my objects in place. But then, how do I do the glTranslatef(,,), glScalef, and glRotatef(,,,)? It will affect the entire world and perhaps I only want to rotate the sphere. How do I change the position of only the vertices of the sphere and nothing else when I want to move the sphere?

So, the question I really want to ask is: What is the strategies/patterns to use when working with several objects in order to be able to work with them as single entities and not working with the world as just one chunk containing a lot of objects? Later on I will need to have a strategy that works with collision detection as well. I just need some help in order to move in the right direction while learning.

+1  A: 

There are basically two patterns: using the matrix stack, and copying the data.

To use the matrix stack, you do something like this:

for each object {
 glPushMatrix
 glTranslate(location of first object)
 /* rotate as desired too */
 draw_one_object();
 glPopMatrix();
}

The same coordinate data can be recycled because it is drawn in different locations.

The alternative (data copying) is to allocate a much larger array, copy your data into it N times, and then add your object offsets to each copy to form unique copies of the original data.

Ben Supnik
Thanks! That's similar to information I found somewhere else, so I guess this is the general way of doing it. One thing I don't understand is this "The same coordinate data can be recycled because it is drawn in different locations.". Could you explain that, please? I am a newbie, so sorry for my "non-understandability"...
Nicsoft
+1  A: 

The "transform" commands (glRotate, glTranslate, etc.) do not permanently affect your coordinate data. They only affect the drawing that is done at one time. For example, if you have a cube around the origin, then...

draw_cube(); // draws the cube around the origin
glTranslate(10,0,0);
draw_cube(); // draws the cube 10 units to the right
glTranslate(2,0,0);
draw_cube(); // draws the cube 12 units to the right
draw_cube(); // draws the cube 12 units to the right again
glTranslate(-12,0,0);
draw_cube(); // draws the cube around the origin (again).

In other words, every time you draw with glDrawElements or glDrawArrays, the coordinates are affected by the TOTAL results of all PREVIOUS matrix routines. But the raw data that you use to draw your elements is unchanged.

(You can think of the matrix commands as changing how the graphics card sees your geometry data. It does not change the geometry data itself.) It is like the difference between actually painting your house red vs. putting on red tinted sunglasses; the matrix routines change how the data is observed by the graphics card, and thus the change is not permanent.

Ben Supnik
Ok, thank you! I'll try it out.
Nicsoft