Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

OpenGL coding

Options
  • 05-05-2004 1:09pm
    #1
    Registered Users Posts: 658 ✭✭✭


    yo!

    I'm needing to create a few animated 3D shapes using openGL. I've scoured the net for days and I can't find anything remotely helpful. All the sites claiming to be simple to follow are ridiculously cryptic.

    I'm a complete noob when it comes to openGL and I need to be able to create shapes as soon as possible.

    Any help is much appreciated,
    Cheers

    Ps i'm using C++ and don't know how to tie the two together


Comments

  • Moderators, Music Moderators Posts: 1,481 Mod ✭✭✭✭satchmo


    I've always found the Lighthouse3D GLUT tutorial to be very easy to follow. GLUT is a wrapper toolkit for creating GL-compatible windows and handling input. It replaces any platform-specific code you might need to write and generally makes it very easy to get an OpenGL app up and running quickly.

    As for the actual OpenGL part, you might want to check out opengltutorial.co.uk. I haven't looked at it in detail since it just started up recently, and only the first two tutorials are up at the moment. But it seems like as good a starting place as any, and you might pick up a thing or two from it.

    For some basic working code you could also check out gametutorials.com to give you an idea of how things are put together.

    Basically, read read read! If you don't understand one webpage, go off and read some others on the same subject. Then come back to the first one, you might find you understand something that you didnt previously. I find this is the best way to learn things, since you'll almost never be able to learn everything from just one page in isolation.

    And don't forget you can always ask questions here! As long as they're specific enough, there'll always be someone who can answer them.


  • Registered Users Posts: 658 ✭✭✭Chunks


    Cheers m8, appreciate it. I actually found OpenGltutorials.co.uk after my post, it seems to be the most helpful site out there.
    I might be harassing you further in the near future if thats ok.

    Thanks


  • Registered Users Posts: 658 ✭✭✭Chunks


    Here's another quick question for you..........


    ...........you know the way in openGL you can use pre-made stuff from the GLUT library (for example, glutWireCube(2.0)), well.........I was wondering if there is also a pre-made cylinder, i.e; glutCylinder(int x, int y);

    Am I right in thinking this?


    The reason I'm asking and not experimenting for myself is because I can't seem to get openGL working on my machine. I keep getting an error saying '#include<GL/glut.h>' doesn't exist/cannot be accessed. Despite the fact that glut.h i in the same folder as the source code!! So i've been limited to working it out on paper for the time being.:(

    Cheers


  • Moderators, Music Moderators Posts: 1,481 Mod ✭✭✭✭satchmo


    No there's no GLUT cylinder I'm afraid. It wouldn't be too hard to build one procedurally out of triangles though.

    When you see a file included like #include <GL\gl.h>, it usually means it's being included from VC++'s 'Include' folder, ie "C:\Program Files\Microsoft Visual Studio\VC98\Include\GL". This is because when you use angle brackets, the file is looked for where the compiler is configured to look for the standard header files. VC++'s Include directory is one of these places (where you'll find all your favourite header files like iostream.h and windows.h), and others can be specified in 'Project->Settings->C++->Preprocessor->Additional include directories'.

    When you use inverted commas, like #include "foobar.h", it means you want the file to be included from the same directory that the file being compiled is in. If it isn't found in this directory, it will proceed to search the standard include directories as above. So if you want to include glut.h from your project's folder, you should use #include "glut.h".


  • Registered Users Posts: 658 ✭✭✭Chunks


    i figured it out a few minutes ago. I have code for a cylinder and I ned to use it a lot. The main problem is placing them on the screen, very awkward and time-consuming. Any suggestions?

    Cheers m8


  • Advertisement
  • Moderators, Music Moderators Posts: 1,481 Mod ✭✭✭✭satchmo


    That's quite a vague question, I'm not sure what you mean. It shouldn't be too hard, just a couple of glTranslates and glRotates should be all you need. Often what I do for just prototyping stuff without getting too involved with the camera system is this:
    glTranslatef(0,0,-zoom);
    glRotatef(camPitch,1,0,0);
    glRotatef(camSpin,0,1,0);
    DrawYourGeometry();
    
    If you let the mouse X-axis change camSpin and the Y-axis change camPitch, this gives you a kind of orbital camera which will orbit around whatever geometry you're drawing. This is simpler than writing a whole fps-style camera class with strafing etc., when all you really need is an easy way to view and rotate objects. Plus, you never end up with just a blank screen because you've put the camera in the wrong place.


  • Registered Users Posts: 658 ✭✭✭Chunks


    Originally posted by satchmo
    That's quite a vague question, I'm not sure what you mean.
    sorry Satchmo, i was in a bit of a hurry when I was writing that last post. Whatf I really meant to say was......... I have the code for a cylinder, but, it seems to be very tempramental and extremely hard and awkward to use several times (i need to draw several fingers, cos i'm trying to make a hand, and so use several cylinders with spheres in between as knuckles.).

    The code for a cylinder is as follows:

    //Cylinder
    glTranslatef (0.0f,0.0f,-1.5f);
    glRotatef (120,1.0,0.0,1.0);
    GLUquadricObj *quadric;
    quadric = gluNewQuadric();
    gluQuadricDrawStyle(quadric, GLU_FILL);
    glColor3f(1, 1, 0);
    gluCylinder(quadric, 1.0, 1.25, 3.0, 32, 32);

    What do you think? Is it a bit too complicated? Is there an easier way to create a
    cylinder??

    Also, is this possible? I want to create a method to draw cylinders instead of using all the code over and over. I'm getting errors saying that ''glTranslate and glRotate are undeclared identifiers''


    //create a cylinder to be used several times
    void cylinder(double v,double w, double x, int y, int z, double a, double b, double c, int d, double e, double f, double g)
    {
    glTranslate (a, b, c);
    glRotate(d, e, f, g);
    GLUquadricObj *quadric;
    quadric = gluNewQuadric();
    gluQuadricDrawStyle(quadric, GLU_FILL);
    glColor3f(1,1,0);
    gluCylinder(quadric, v, w, x, y, z);
    }


    // the method for displaying the shapes
    void display(void)
    {
    glClear (GL_COLOR_BUFFER_BIT);
    glColor3f (1.0f, 0.0f, 0.0f);

    glLoadIdentity ();

    gluLookAt (0.0, 0.0, 0.5,
    0.0, 0.0, 0.0
    0.0, 1.0, 0.0);

    if (rotationOn == true)
    {
    rot+=1.0f;
    }

    glTranslatef (xAxis, yAxis, zAxis);
    glRotatef (rot, xRotate, yRotate, zRotate);


    cylinder(0.0, 0.0, -1.5, 120, 1.0, 0.0, 1.0, 1.0, 1.25, 3.0, 32, 32);

    glFlush ();
    }






    Cheers m8, i really appreciate all the help, I'll buy you a pint if I ever run into you


  • Moderators, Music Moderators Posts: 1,481 Mod ✭✭✭✭satchmo


    Oh yeah I'd forgotten about quadrics, that'll do fine. Those errors are because you forgot the 'f' (for float) at the end of glTranslatef and glRotatef. It looks fine otherwise (except for the space after the first glTranslatef in cylinder()!). Although I'd suggest moving any transformations to outside the cylinder function and leave it just drawing and nothing else. It would reduce the amount of parameters, and what if you wanted to do a scale after the translate & rotate?


  • Registered Users Posts: 658 ✭✭✭Chunks


    Originally posted by satchmo
    Although I'd suggest moving any transformations to outside the cylinder function and leave it just drawing and nothing else. It would reduce the amount of parameters, and what if you wanted to do a scale after the translate & rotate?

    I'm not too sure what you mean when you say that, can you dumb it down for my idiot brain?

    I tried that new method and when i put in the parameters into cylinder() nothing came up on the screen........hmm.......weird.

    These were the values i putinto the method:

    cylinder(0.25, 0.25, -1.5, 32, 32, -0.5, -0.5, 0.0, 120, 1.0, 0.0, 1.0);



    And here's the method again:

    //create a cylinder to be used several times
    void cylinder(double v,double w, double x, int y, int z, double a, double b, double c, int d, double e, double f, double g)
    {
    glTranslatef(a, b, c);
    glRotatef(d, e, f, g);
    GLUquadricObj *quadric;
    quadric = gluNewQuadric();
    gluQuadricDrawStyle(quadric, GLU_FILL);
    glColor3f(1,1,0);
    gluCylinder(quadric, v, w, x, y, z);
    }

    I can't see why the method isn't showing the newcylinder on screen


  • Moderators, Music Moderators Posts: 1,481 Mod ✭✭✭✭satchmo


    All I meant was that it's good to seperate the code that creates & draws objects from the code that transforms (ie translates, rotates & scales) that object. If you wanted to do a scale after the translate & rotate in the cylinder function, you wouldn't be able to because you've already included the translate and rotate inside the function. Functions with as many parameters as that are just a pain in the ass to use.

    When stuff doesnt appear on the screen, its usually becase
    a) you have the camera pointed in the wrong direction,
    b) the camera's in the right direction but the object's in the wrong place, or
    c) the lighting is screwing things up

    I'd say in your case it's probably the camera pointing in the wrong direction, get out a pencil and some paper and work it out by hand!


  • Advertisement
  • Registered Users Posts: 658 ✭✭✭Chunks


    Satchmo, i don't know what i'd do with out you mate!! You are my new God!
    All clear for th moment but i'm sure i'll run into some more problems soon enough....And that's where you come in!!!:D


  • Registered Users Posts: 1,287 ✭✭✭joe_chicken


    gamedev.nehe.net.... its got a pretty long tutorial for all sorts of compilers!

    p.s. satchmo's right thats some pretty messy code, too many parameters


  • Registered Users Posts: 1,419 ✭✭✭nadir


    yea ive been playing with opengl recently again, I want to get texture mapping working, I understand the proper way to do it is to set coordinate offsets of the polygons, asigning points on the pixmap that correspond to your wierframe, the obvious way to do this is to find a format that outputs texture co-ordinates to the models. However I have been unable to find a suitable format. Im into opensource stuff, and i want a plaintext format. So if anyone knows of one please let me know. :)


  • Moderators, Music Moderators Posts: 1,481 Mod ✭✭✭✭satchmo


    Yeah that's how it works - each vertex on the model gets assigned texture coordinates between [0,1], and these coordinates get interpolated along the face that connects the vertices. Assigning them isn't an easy job, and it takes a good artist to do proper texture-mapping on a complex model. The guys over at Game Editing might be able to help you there, although it seems kinda quiet at the moment. Most likely they'll tell you to get gmax or Milkshape, which are both good free model editors.

    As for the plaintext object format, .obj is probably your best bet. You can find the file format description over at wotsit.org, and most modellers will import/export obj files.


  • Registered Users Posts: 1,419 ✭✭✭nadir


    nice ok, im using wings and blender atm, i can use a plugin for obj, I must check out the desription and key of that format


  • Registered Users Posts: 658 ✭✭✭Chunks


    Satchmo I need your help m8!! Can I pm you with my code? I'm having probs with the co-ords and degree of angles.

    Cheers


  • Moderators, Music Moderators Posts: 1,481 Mod ✭✭✭✭satchmo


    Post it up here so everyone can have a look, i'm not the only one around that knows OpenGL! Plus, others can learn from your mistakes right?!


  • Registered Users Posts: 658 ✭✭✭Chunks


    ok, here it is:


    // a rotating wdesign depending on keyboard presses


    #include <glut.h>
    #include <stdlib.h>

    // set the axes and rotation angles as global values to avoid annoying time-consuming changes later
    GLfloat xAxis = 0;
    GLfloat yAxis = 0;
    GLfloat zAxis = 0;

    GLfloat xRotate = 0;
    GLfloat yRotate = 0;
    GLfloat zRotate = 0;

    GLfloat fingAngle = 90;
    // boolean value to determine if rotation is on or off
    // angle of rotation is initially set to 0
    bool rotationOn = false;
    GLfloat rot = 0;

    // set the background color and state that the shading will be smooth
    void init(void)
    {
    glClearColor (0.0, 0.0, 0.0, 0.0);
    glShadeModel (GL_SMOOTH);//for 3D shapes

    }


    //create a cylinder to be used several times
    void cylinder(double v,double w, double x, int y, int z)
    {
    GLUquadricObj *quadric;
    quadric = gluNewQuadric();
    gluQuadricDrawStyle(quadric, GLU_FILL);
    gluCylinder(quadric, v, w, x, y, z);
    }


    // the method for displaying the cube
    void display(void)
    {
    glClear (GL_COLOR_BUFFER_BIT);

    glLoadIdentity ();

    gluLookAt (10.0, 0.0, 0.0,
    0.0, 0.0, 0.0,
    1.0, 0.5, 0.0);

    if (rotationOn == true)
    {
    rot+=1.0f;
    }

    glTranslatef (xAxis, yAxis, zAxis);
    glRotatef (rot, xRotate, yRotate, zRotate);

    //=================================create the right middle finger===============================

    // bottom part
    glPushMatrix();
    glColor3f(1.0, 1.0, 0.0);
    glTranslatef(0.14, -0.14, 0.0);
    glRotatef(90, 1.0, 1.0, 0.0);

    cylinder(0.35, 0.35, 1.1, 32, 32);
    glPopMatrix();

    // middle joint
    glPushMatrix();
    glColor3f(1.0, 1.0, 0.0);
    glTranslatef(0.0, 0.0, 0.0);
    glRotatef(0, 0.0, 0.0, 0.0);

    glutSolidSphere(0.38, 20, 20);
    glPopMatrix();

    // middle part
    glPushMatrix();
    glColor3f(1.0, 1.0, 0.0);
    glTranslatef(-0.67, 0.67, 0.0);
    glRotatef(90, 1.0, 1.0, 0.0);

    cylinder(0.35, 0.35, 0.8, 32, 32);
    glPopMatrix();

    // top joint
    glPushMatrix();
    glColor3f(1.0, 1.0, 0.0);
    glTranslatef(-0.70, 0.70, 0.0);
    glRotatef(0, 0.0, 0.0, 0.0);

    glutSolidSphere(0.35, 32, 32);
    glPopMatrix();

    // top part
    glPushMatrix();
    glColor3f(1.0, 1.0, 0.0);
    glTranslatef(-1.22, 1.22, 0.0);
    glRotatef(90, 1.0, 1.0, 0.0);

    cylinder(0.34, 0.34, 0.6, 32, 32);
    glPopMatrix();

    // tip middle finger
    glPushMatrix();
    glColor3f(1.0, 1.0, 0.0);
    glTranslatef(-1.22, 1.22, 0.0);
    glRotatef(0, 0.0, 0.0, 0.0);

    glutSolidSphere(0.34, 32, 32);
    glPopMatrix();



    //=================================create the right index finger===============================


    // bottom part
    glPushMatrix();
    glColor3f(1.0, 0.0, 0.0);
    glTranslatef(0.23, -0.23, 0.75);
    glRotatef(90, 1.0, 1.0, 0.0);

    cylinder(0.37, 0.37, 1.0, 32, 32);
    glPopMatrix();

    // middle joint
    glPushMatrix();
    glColor3f(1.0, 0.0, 0.0);
    glTranslatef(0.21, -0.21, 0.75);
    glRotatef(0, 0.0, 0.0, 0.0);

    glutSolidSphere(0.39, 20, 20);
    glPopMatrix();

    // middle index finger
    glPushMatrix();
    glColor3f(1.0, 0.0, 0.0);
    glTranslatef(-0.52, 0.52, 0.75);
    glRotatef(90, 1.0, 1.0, 0.0);

    cylinder(0.35, 0.37, 0.80, 32, 32);
    glPopMatrix();

    // top joint
    glPushMatrix();
    glColor3f(1.0, 0.0, 0.0);
    glTranslatef(-0.52, 0.52, 0.75);
    glRotatef(0, 0.0, 0.0, 0.0);

    glutSolidSphere(0.33, 32, 32);
    glPopMatrix();

    // top index finger
    glPushMatrix();
    glColor3f(1.0, 0.0, 0.0);
    glTranslatef(-1.02, 1.02, 0.75);
    glRotatef(90, 1.0, 1.0, 0.0);

    cylinder(0.32, 0.33, 0.6, 32, 32);
    glPopMatrix();

    // tip index finger
    glPushMatrix();
    glColor3f(1.0, 0.0, 0.0);
    glTranslatef(-1.02, 1.02, 0.75);
    glRotatef(0, 0.0, 0.0, 0.0);

    glutSolidSphere(0.32, 32, 32);
    glPopMatrix();


    //=================================create the right ring finger===============================

    // bottom part
    glPushMatrix();
    glColor3f(0.0, 1.0, 0.0);
    glTranslatef(0.29, -0.29, -0.72);
    glRotatef(90, 1.0, 1.0, 0.0);

    cylinder(0.32, 0.32, 1.0, 32, 32);
    glPopMatrix();

    // middle joint
    glPushMatrix();
    glColor3f(0.0, 1.0, 0.0);
    glTranslatef(0.26, -0.26, -0.72);
    glRotatef(0, 0.0, 0.0, 0.0);

    glutSolidSphere(0.34, 32, 32);
    glPopMatrix();

    // middle part
    glPushMatrix();
    glColor3f(0.0, 1.0, 0.0);
    glTranslatef(-0.40, 0.40, -0.72);
    glRotatef(90, 1.0, 1.0, 0.0);

    cylinder(0.31, 0.34, 0.8, 32, 32);
    glPopMatrix();

    // top joint
    glPushMatrix();
    glColor3f(0.0, 1.0, 0.0);
    glTranslatef(-0.40, 0.40, -0.72);
    glRotatef(0, 0.0, 0.0, 0.0);

    glutSolidSphere(0.31, 32, 32);
    glPopMatrix();

    // top part
    glPushMatrix();
    glColor3f(0.0, 1.0, 0.0);
    glTranslatef(-0.84, 0.84, -0.72);
    glRotatef(90, 1.0, 1.0, 0.0);

    cylinder(0.33, 0.33, 0.6, 32, 32);
    glPopMatrix();

    // tip of finger
    glPushMatrix();
    glColor3f(0.0, 1.0, 0.0);
    glTranslatef(-0.84, 0.84, -0.72);
    glRotatef(0, 0.0, 0.0, 0.0);

    glutSolidSphere(0.32, 32, 32);
    glPopMatrix();

    //=================================create the right pinky finger===============================

    // bottom part
    glPushMatrix();
    glColor3f(0.0, 0.0, 1.0);
    glTranslatef(0.5, -0.5, -1.40);
    glRotatef(90, 1.0, 1.0, 0.0);

    cylinder(0.31, 0.30, 0.9, 32, 32);
    glPopMatrix();

    // middle joint
    glPushMatrix();
    glColor3f(0.0, 0.0, 1.0);
    glTranslatef(0.5, -0.5, -1.40);
    glRotatef(0, 0.0, 0.0, 0.0);

    glutSolidSphere(0.31, 32, 32);
    glPopMatrix();

    // middle part
    glPushMatrix();
    glColor3f(0.0, 0.0, 1.0);
    glTranslatef(-0.0, 0.0, -1.40);
    glRotatef(90, 1.0, 1.0, 0.0);

    cylinder(0.30, 0.31, 0.6, 32, 32);
    glPopMatrix();

    // top joint
    glPushMatrix();
    glColor3f(0.0, 0.0, 1.0);
    glTranslatef(-0.0, 0.0, -1.40);
    glRotatef(0, 0.0, 0.0, 0.0);

    glutSolidSphere(0.31, 32, 32);
    glPopMatrix();

    // top part
    glPushMatrix();
    glColor3f(0.0, 0.0, 1.0);
    glTranslatef(-0.40, 0.40, -1.40);
    glRotatef(90, 1.0, 1.0, 0.0);

    cylinder(0.27, 0.31, 0.4, 32, 32);
    glPopMatrix();

    // tip of finger
    glPushMatrix();
    glColor3f(0.0, 0.0, 1.0);
    glTranslatef(-0.40, 0.40, -1.40);
    glRotatef(0, 0.0, 0.0, 0.0);

    glutSolidSphere(0.27, 32, 32);
    glPopMatrix();

    //=================================create the right thumb===============================

    // foundation of thumb
    //code here

    // bottom part
    glPushMatrix();
    glColor3f(0.7, 0.7, 1.0);
    glTranslatef(1.50, -1.50, 2.0);
    glRotatef(120, 1.0, 1.0, 0.0);

    cylinder(0.37, 0.39, 1.0, 32, 32);
    glPopMatrix();

    glFlush ();

    }


    void keyboard(unsigned char key, int x, int y)
    {
    switch (key) {
    case 27:
    exit(0);
    break;

    // Moving the cube around a 3D space
    case 'm':
    xAxis += 0.5;
    yAxis += 0.5;
    glutPostRedisplay();
    break;

    case 'j':
    fingAngle+= 10;
    glutPostRedisplay();
    break;

    case 'd':
    zAxis+=0.5; // X axis
    glutPostRedisplay();
    break;
    case 'a':
    zAxis-=0.5; // X axis
    glutPostRedisplay();
    break;

    case 'w':
    yAxis+=0.5; // Y axis
    glutPostRedisplay();
    break;
    case 's':
    yAxis-=0.5; // Y axis
    glutPostRedisplay();
    break;

    case 'z':
    xAxis+=0.5; // Z axis
    glutPostRedisplay();
    break;
    case 'x':
    xAxis-=0.5; // Z axis
    glutPostRedisplay();
    break;

    // Rotating the Cube in a 3D space
    case 'i': // X axis
    yRotate = 0.0;
    zRotate = 0.0;
    xRotate = 1.0;
    rotationOn = (!rotationOn);
    glutPostRedisplay();
    break;
    case 'o': // Y axis
    zRotate = 0.0;
    xRotate = 0.0;
    yRotate = 1.0;
    rotationOn = (!rotationOn);
    glutPostRedisplay();
    break;
    case 'p': // Z axis
    xRotate = 0.0;
    yRotate = 0.0;
    zRotate = 10000.0;
    rotationOn = (!rotationOn);
    glutPostRedisplay();
    break;
    }
    }


    void reshape (int w, int h)
    {
    glViewport (0, 0, (GLsizei) w, (GLsizei) h);

    glMatrixMode (GL_PROJECTION);
    glLoadIdentity ();
    glFrustum (-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);

    glMatrixMode (GL_MODELVIEW);
    }


    int main(int argc, char** argv)
    {
    glutInit(&argc, argv);
    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize (500, 500);
    glutInitWindowPosition (100, 100);

    glutCreateWindow (argv[0]);

    init ();

    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutKeyboardFunc(keyboard);

    glutMainLoop();
    return 0;
    }






    The main problem i have is moving the top parts of the fingers. when i change the degree to more or less than 90degrees, it goes in the wrong direction!!!! Any suggestions?

    Cheers


  • Moderators, Music Moderators Posts: 1,481 Mod ✭✭✭✭satchmo


    Wah okay I didn't think you'd post ALL your code!....

    The main problem I can see is that you're getting a little confused with the glRotatef(angle, x, y, z) function. The angle determines the amount of rotation in degrees alright, but the x,y,z parts specify an axis to rotate around. Unless you want to use arbitrary rotation axes, I'd advise you to stick to just rotating around the primary x, y or z axis, one at a time. So if you want to rotate 90 degrees around the Y axis, it would be glRotatef(90, 0,1,0).

    In a couple of places I see you doing things like glRotatef(90, 1,1,0) - this will rotate around an axis that orthogonal to the z axis, and at 45 degrees to the x and y axes, if you get me. It's easier to explain with a somewhat crude ASCII diagram....
          y            .    <- your rotation axis
          |         .
          |      .
          |   .
          |.
          +-----------x
         /
        /
       /
      z
    
    So the dotted line is the one you're rotating around. In other words, the last 3 parameters specify the 3d vector that you want to rotate around. Now this might well be what you're trying to do, but I'm guessing your intention was to rotate 90 degrees around the X axis AND 90 degrees around the Y axis. To do this you need to do two seperate rotates - glRotatef(90,1,0,0); glRotatef(90,0,1,0); I never really do rotations around axes other than X, Y or Z individually, because they never seem to turn out the way you think they will, and any arbitrary rotations can be decomposed into multiple individual rotations around these axes. It makes for code that's easier to read, and it's much easier to visualise in your head (or using 3-fingers-in-the-air method) rather than a pencil and some graph paper.

    Besides all that, you also seem to be missing the point of the matrix stack. Articulated bodies like the hand you're trying to model is exactly what the matrix stack is for. when you say glPushMatrix(), it's like saying "save my current position and rotation". And when you glPopMatrix(), it's saying "go back to the last saved position and rotation".
    What you're doing at the moment is analagous to:
    Go to the bottom, Move up one unit, Draw the bottom part
    Go to the bottom, Move up two units, Draw the middle part
    Go to the bottom, Move up three units, Draw the top part

    in other words,
    glPushMatrix(); glTranslatef(0,1,0); drawBottom(); glPopMatrix();
    glPushMatrix(); glTranslatef(0,2,0); drawMiddle(); glPopMatrix();
    glPushMatrix(); glTranslatef(0,3,0); drawTop(); glPopMatrix();

    What you should be doing is:
    Go to the bottom, Draw the bottom part
    Move up one unit, Draw the middle part
    Move up one unit, Draw the top part

    in other words,
    glPushMatrix(); glTranslatef(0,1,0); drawBottom();
    glPushMatrix(); glTranslatef(0,1,0); drawMiddle();
    glPushMatrix(); glTranslatef(0,1,0); drawTop();
    glPopMatrix();glPopMatrix();glPopMatrix();

    This way, if you decide to move up ten units at the beginning, then that change will carry all the way through. The way your doing it at the moment, you'd have to change each individual part to move up an extra ten units. This isn't a very good explanation, but there's not enough room for a proper one here really (and I've rambled on enough already). Check out these slides for a more in-depth look at it.

    Phew! I didn't mean to go on this long, but that should give you a bit of a better idea.

    -satch


  • Registered Users Posts: 658 ✭✭✭Chunks


    you're an absolute legend Satchmo!!!!! Cheers for all the help.

    I'll buy you a beer in O'Reillys on saturday for this!!


  • Advertisement
  • Moderators, Music Moderators Posts: 1,481 Mod ✭✭✭✭satchmo


    Heheh thanks, but I'm not going to be able to make it on Saturday unfortunately.

    I'm just after properly reading the slides that I linked to in that last post, and that explanation is almost worse than mine!! Your best bet is to google some combination of the terms opengl, hierarchy, matrix stack, transformation and glPushMatrix, and hope something useful comes up :p


Advertisement