I have an array pointing to a class that stores info regarding spheres…I use GLUsphere to generate the sphere for each member of the array. Also stored in this class is the relative location in X,Y,Z coordinates. I figured that I should use GLtranslatef to position the spheres…but it seems that every time I call GLtranslate it translate all the spheres. How do I create multiple spheres and translate them to the right location, and anchor them down?
Easiest is probably just to translate, draw a sphere, translate back, etc:
glTranslatef( [first location] );
gluSphere( … );
glTranslatef( -[first location] );
glTranslatef( [second location] );
gluSphere( … );
glTranslatef( -[second location] );
I suppose you could combine those two consecutive glTranslatef’s into one:
glTranslatef( [second location] - [first location] );
Hmmmm…that makes my head hurt…basically your code says:
Plot a sphere #1 at the location #1. Move it to the origin. Now move it to location #2. Now sphere #2 is plotted at the origin. Doesn’t that invert what I’m trying to do? Or maybe it plots the correct picture, but I lose the references. what was once know as sphere #1 is passed down until it occupys location #x. That won’t help too much (unless I change references)…for the sake of the program sphere #1 needs to be in location #1. In addition, is your code iterative? meaning is it just [location 3] - [location 2] or is it [location 3] - [location 2] - [location 1]. Nevertheless I will try your code. If anyone else has any other suggestions I would be most appreciative.
It may be helpful if you think of the translate functions as moving the origin. Then think of gluSphere as only plotting at the current location of the origin. Then the code says move to location1, drop a sphere, move back to where you started. Now move to location2, drop the second sphere, move back to where you started. And so on until you’ve plotted all your spheres.
When I mentioned combining the consectutive translate functions, that’s like saying “instead of going back to the origin, I’m just going to move to the next location, relative to where I am now.”
I could be misunderstanding your problem.
[This message has been edited by Rob (edited 09-09-2000).]
The best way to do this is to push and pop the modelview matrix. Then you don’t have to bother translating the matrix back again.
//… and so on
Easier to read the code, and it’s not slower, if anything, it’s faster.
Let’s say you have three spheres. The center of each sphere is xi, yi, zi. Try this:
glTranslatef(x1, y1, z1);
/* Draw sphere /
glTranslatef(x2, y2, z2);
/ Draw sphere /
glTranslatef(x3, y3, z3);
/ Draw sphere */
I doubt you want to load the identity each time since that will undo any other transformations which you might have wanted. I think Bob’s suggestion is the best. I just assumed that all that matrix pushing and popping would be slower than just doing the translations, but I honestly don’t know.
I would do it like bob. It’s the easiest and most correct approach.
Rob: I don’t know how fast a matrix multiplication is, the number of adds/multiplications needed. But when pushing a matrix, you only need to copy 64 (16*sizeof(float)) bytes to the top of the stack and make that matrix current. And when popping, you don’t have to do anything except change the stackpointer to the matrix below.
Even if the HWT&L engine accelerates the matrix multiplication, I really doubt it’s faster than a memcopy.
Good points, Bob. It’d be interesting to run a couple tests and compare what kind of difference it makes. But probably not quite interesting enough for me to take the time to do it .
Yeah, test is the best way to know for sure
Anyways, the difference in speed is very small. The whole story wether using push/pop or backwards translation is a matter of clean and understandable code. Honestly, there are other things that needs to be optimized before starting optimizing these kind of things
thanks for all the help my friends…I posted this topic because I was using Bob’s code originally. Several spheres should appear on the screen…instead only one does. For some reason I have the wild idea that I should delete the quadric object (sphere) after I plot it…is this correct?
It just seems that every time I plot a new sphere, it overwrites the last one. I’m also using using the:
PUSH MATRIX…POP MATRIX
from a class function…each class has defined in it its own quadricobject…although every instance of the class has the same name for the quadric object, could this be a reason?
i should also point out that even though the quadric object has the same name…it is a private member of that class (remebmer I’m new to openGL – I don’t know if class opengl objects maintain private/public status – please let me know if you know the answer to this).
thanks everyone…I got it!!