Thread: rotating composite objects with OpenGL

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595

    rotating composite objects with OpenGL

    I've embarked on a self-led journey into OpenGL (like many others I presume). At the moment I can work through the tutorials I have found and can do some simple stuff independently. But I'd like to do more. One thing I'd like to do is attempt to rotate a composite object. I searched the board but came up with no solutions.

    Here's a simple scenario. Say I have a bar made up of two cubes. One cube has alternating white and black faces and the other has alternating red and blue faces. Each cube has an X, Y, and Z axis and independently could spin on each axis. When connected together into a bar however the two cubes can only spin on their X axis. The question comes in if I want to animate the process of flipping the bar so the white/black cube is on the right hand side of the bar instead of the left. Is it possible to rotate the entire bar about it's Y axis (which would be the interface between the two cubes if the cubes were of the same size)? Or do you have to do the math and flip each component cube separately?

    A similar situation would arise if you were to draw a snowman's face using a sphere for the head, a cone for the nose, and two smaller spheres for the eyes. How could you animate the snowman so the whole head (face in total, nose, and eyes) all rotate together?

    A slightly more advanced scenario would be this. Say you have two bars that interlock perpendicular to each other at their middle third. Can you rotate each bar about the common midpoint independent of the other bar? If so, how do you do that, in general terms (although any detail or referrals would be gladly accepted as well).

    Thank you.
    Last edited by elad; 01-20-2003 at 10:05 AM.

  2. #2
    Registered User
    Join Date
    Feb 2002
    Posts
    93

    hmm..

    Hmmm.. i'm new to OpenGL as well.. it sounds like you are looking for heirarchies.(i can't spell)... like a arm ... with shoulder/elbow/wrist/fingers that would move together or independently... why not just use a couple of MATRIXS to solve your problem?

    Code:
    pushMatrix();
      rotatef(bardegrees, 0, 1, 0);
      pushMatrix();
        translatef(-1, 0, 0); 
        rotatef(box1x, 1, 0, 0);
        rotatef(box1y, 0, 1, 0);
        rotatef(box1z, 0, 0, 1);
        drawbox(); // Draw first box here.
      popMatrix();
      pushMatrix();
        translatef(1, 0, 0);
        rotatef(box2x, 1, 0, 0);
        rotatef(box2y, 0, 1, 0);
        rotatef(box2z, 0, 0, 1);
        drawbox(); // Draw second box here.
      popMatrix();
    popMatrix();
    increase bardegrees to rotate both boxes (the bar)around y-axis..
    increase box1x, box1y, box1z to rotate box1 independently
    increase box2x, box2y, box2z to rotate box2 independently

    P.S. if you want an easy challenge on learning heirarchies building a solar system is a good idea.. sun with planets revolving around at different periods and moons revolving around planets.. i found it a very useful thing to do to learn this stuff..
    Last edited by tegwin; 01-20-2003 at 11:10 AM.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Thanks for the ideas tegwin, but things are still a bit fuzzy. Comparing the solar system idea with the rotating bar I think the analagy would be to somehow flip the solarsystem relative to the viewer such that the souht pole becomes the north pole and visa versa.

    Without animating the flip in the bar I can clearly just change the color of the sides of the cubes without changing their relative position and end up with the same result, but that isn't a very visually pleasing solution. I can flip a rectangle (bar) and I can flip a cube, but I'm not sure I can flip a bar made of two cubes. I'll try the code you suggested as a trial and see what happens and I'll try looking in the "Red Book"'s section on viewing and review the use of Matrix stacks again, but at the moment it just doesn't ring true.

    Any seconds to tegwins suggestion or any other suggestions?

    Thanks.

  4. #4
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    if you render both boxes from the same (translated) position, then increment the rotation before rendering in your draw loop, the boxes will rotate together on the same axis. is that what your trying to do?

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    that sounds pretty close, given that I'm still new to the terminology used. I'm writing this from memory from work to see if it looks like the correct approach. Any recommendations welcome. Thanks.
    Code:
    static GLfloat angle = 0.0; //maintain value of angle from one function call to another
    
    void drawScene()
    {
      while(angle <= 180)//cause scene to rotate 180 degrees then stop
      {
        glLoadIdentity();//start with original view each change in angle
        glRotatef(angle, 0.0f, 0.0f, 1.0f); //rotate scene about z axis
        
        //draw two square panels 0.5/side centered at (0,0,0)
        glBegin(GL_QUADS)
           //first panel--expand to cube next version
           glColor3f(1.0f, 0.0f, 0.0f); //use red color
           glVertex3f(-0.5f, 0.25f; 0.0f);//upper left corner
           glVertex3f(-0.5f, -0.25f, 0.0f);//lower left corner
           glVertex3f(0.0f, -0.25f, 0.0f);//lower right corner
           glVertex3f(0.0f, 0.25f, 0.0f); //uppre right corner
    
           //second panel--expand to cube next version
           glColor3f(0.0f, 0.0f, 0.1f);//use blue color
           glVertex3f(0.0f, 0.25f, 0.0f);//upper left corner
           glVertex3f(0.0f, -0.25f, 0.0f);//lower left corner
           glVertex3f(0.5f, -0.25f, 0.0f);//lower right corner
           glVertex3g(0.5f, 0.25f, 0.0f);//upper right corner
         glEnd();
    
         angle += 0.1f;
      }
    }
    Last edited by elad; 01-21-2003 at 09:31 AM.

  6. #6
    Registered User
    Join Date
    Feb 2002
    Posts
    93

    Hope this helps

    Is this what you are looking for? I wrote a small little program that has two cubes side by side that rotate independently but can also rotate together.. this what your looking for? I included an .exe & a .cpp file that shows relevant code..


    Keys to work program...

    Left & Right (on keyboard) rotates the cubes.
    Up & Down (on keyboard) rotates the bar.

    i just wrote this program quick so i didn't make the cubes the colors you were looking for ... and i didn't implement rotation about the x & z axis for the cubes cause i'm too lazy .... but hopefully this is what you were looking for and you can finish the rest...
    Last edited by tegwin; 01-21-2003 at 12:15 PM.

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Wonderful! That is exactly my idea. I had managed to get almost that far on my own (with the stuff posted earlier today), but my cube colors weren't stable. I suspect using the Matrix stack more appropriately will fix that. Thanks so much.

    Just a few questions, if you don't mind, as I'm not familiar with everything in the .cpp file you included.

    I suspect glColor3ub() is a variant of glColor3f() but is there a place to document the numeric values. I can just goof around and see what I come up with, but documentation is always nice.

    Second, what is g_Camera.Look() and what is the purpose? I've not seen anything like it in the tutorials I've seen so far. Is it a variant of gluLookAt().

    Third, I tried incorporating the code in your .cpp into my OpenGL framework (I'm using the framework from the tutorial on NeHe). I can get the two cubes to appear, but when pressing up/down keys the cubes go into an orbit, eventually going out of the window. Same thing happens with my own code, so I think the "problem" is in the way I'm using the framework somehow. When trying to incorporate the code in the GLUT framework I've been using from Lighthouse3D the window flashes open, I see the two cubes, and then it closes so fast that I can't even try to push the keys and there is just a black window. Would you be willing to post your framework so I could look it over?

    Thanks again.
    Last edited by elad; 01-22-2003 at 12:00 AM.

  8. #8
    Registered User
    Join Date
    Feb 2002
    Posts
    93

    sure

    glColor3ub() stands for GL-Color-3 values of unsigned byte type: which just means the three values must range between 0 - 255 (which is the range for RGB coloring)
    glColor3f() stands for GL-Color-3 values of float type: which accomplishes the same thing as above except you would have to define your colors ranging from 0.0-1.0 (i'm a graphics guy and i just prefer RGB mode.. plus i think it runs alittle faster than using float numbers as they require more storage)

    g_Camera.Look() oops sorry.. didn't really read through my code before i posted it... this is a custom made function... g_Camera is a object of a class called Camera which holds data on the camera's position, view, upvector, etc.. It is good practice not to call things like gluLookAt() directly from the main code... (something about bad practice if your building a game engine) g_Camera.Look() just calls the function Look() which belongs to the Camera class and it just sets up the camera (gluLookAt()) is located in the Look() function..

    As for the code.. i dont' have it on this computer right now.. but i will gladly upload it when i can... (approx: 1 hr from this post)

  9. #9
    Registered User
    Join Date
    Feb 2002
    Posts
    93

    Talking Here is the code

    Here is the code that you wanted.. These are my personal templates i've been writing.. not quite finished yet so it isn't all documented/commented but it should be well enough for you to follow.. quite similar to the openGL templates on NeHe except I created a class for Vectors and moved all implementation into a separate file.. Only file you really need to look in is Main.cpp since i believe that is all I added for the spinning cubes/bar.

    Zip contains:
    Visual C++ 6.0 project files
    Camera/Vector3/Main/Init header files
    Camera/Vector3/Main/Init .cpp files

    if u don't have VC++ just put all files in same directory... add all .cpp files to a project and compile..should work fine...


  10. #10
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Haven't looked at the files in detail but I'm sure there will be a thing or two in them I can use.

    Thank you very much.

    P.S. I think I got the porting to my framework worked out. Thanks again.

  11. #11
    Registered User
    Join Date
    Oct 2008
    Posts
    2
    I need help in my assignment. I m stuck at rotating the whole scene about the origin. I m very interesting in your code. Do you mind to explain more on your camera.cpp and camera.h? Such as how you update the camera view?

    If I comment out g_camera.LookAt(), the code won't work. Can you explain the theory behind your g_camera.LookAt() please? I need help very urgent in solving this matter.

  12. #12
    Registered User
    Join Date
    Oct 2008
    Posts
    2
    Or is it possible to convert your code to make it works without the g_camera.LookAt()? What are the function called by g_camera.LookAt()? Does it update it's position everytime i press my Up & Down key?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM
  2. Rotating In OpenGL
    By SubLogic in forum Game Programming
    Replies: 2
    Last Post: 04-05-2003, 08:56 PM
  3. OpenGL .dll vs video card dll
    By Silvercord in forum Game Programming
    Replies: 14
    Last Post: 02-12-2003, 07:57 PM
  4. 3D Objects In OpenGL
    By kas2002 in forum Game Programming
    Replies: 5
    Last Post: 08-06-2002, 12:15 PM
  5. opengl code not working
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 02-14-2002, 10:01 PM