Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: Multiple Sphere's

  1. #1
    Junior Member Newbie
    Join Date
    Sep 2000
    Posts
    5

    Multiple Sphere's

    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?

  2. #2
    Intern Contributor
    Join Date
    Feb 2000
    Posts
    91

    Re: Multiple Sphere's

    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] );

  3. #3
    Junior Member Newbie
    Join Date
    Sep 2000
    Posts
    5

    Re: Multiple Sphere's

    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.

  4. #4
    Intern Contributor
    Join Date
    Feb 2000
    Posts
    91

    Re: Multiple Sphere's

    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).]

  5. #5
    Senior Member OpenGL Guru
    Join Date
    Feb 2000
    Location
    Sweden
    Posts
    3,115

    Re: Multiple Sphere's

    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.
    Code :
    glPushMatrix();
        glTranslatef(position1);
        gluSphere();
    glPopMatrix();
    glPushMatrix();
        glTranslatef(position2);
        gluSphere();
    glPopMatrix();
    //... and so on
    Easier to read the code, and it's not slower, if anything, it's faster.

  6. #6
    Junior Member Newbie
    Join Date
    Sep 2000
    Posts
    25

    Re: Multiple Sphere's

    Let's say you have three spheres. The center of each sphere is xi, yi, zi. Try this:

    glLoadIdentity();
    glTranslatef(x1, y1, z1);
    /* Draw sphere */
    glLoadIdentity();
    glTranslatef(x2, y2, z2);
    /* Draw sphere */
    glLoadIdentity();
    glTranslatef(x3, y3, z3);
    /* Draw sphere */

  7. #7
    Intern Contributor
    Join Date
    Feb 2000
    Posts
    91

    Re: Multiple Sphere's

    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.

  8. #8
    Intern Contributor
    Join Date
    Jun 2000
    Location
    Nęstved,Denmark
    Posts
    51

    Re: Multiple Sphere's

    I would do it like bob. It's the easiest and most correct approach.
    Live long and prosper!!
    Mac, amiga and linux suck!!

  9. #9
    Senior Member OpenGL Guru
    Join Date
    Feb 2000
    Location
    Sweden
    Posts
    3,115

    Re: Multiple Sphere's

    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.

  10. #10
    Intern Contributor
    Join Date
    Feb 2000
    Posts
    91

    Re: Multiple Sphere's

    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 .

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •