Thread: OpenGl Help

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    4

    OpenGl Help

    Heya,

    I have been reading the tutorial(http://www.cprogramming.com/tutorial/gl6.html) for animating a Robot in OpenGl and I am I complete noob when it comes to OpenGl only been learning for about a month. Basically I am trying to animate a dog to walk around however my code seems to be a lot different to the way you have drawn and animated your Robot. Anyway I was wondering if someone could give me a hand with this. Basically I have the rough outline of my dog and now I need to animate it to walk around the environment. How do I go about doing this with the code I have already written. My code seems different to how a lot of people would do it but it does the trick.

    Here is my code:

    Code:
    
    #include <GL/glut.h>
    #include <GL/glu.h>
    
    using namespace std;
    
    const float BODY_LENGTH = 0.5;
    const float NECK_LENGTH = 0.5;
    const float HEAD_LENGTH = 0.45;
    const float FRONTLEG_LENGTH = 0.5;
    const float FRONTLEGL_LENGTH = 0.5;
    const float BOTTOMLEG_LENGTH = 0.9;
    const float BOTTOMLEGL_LENGTH = 0.9;
    const float BACKLEG_LENGTH = 0.5;
    const float BACKLEGL_LENGTH = 0.5;
    const float BOTTOMBACKLEG_LENGTH = 0.9;
    const float BOTTOMBACKLEGL_LENGTH = 0.9;
    const float RIGHTFRONTFOOT_LENGTH = 0.2;
    const float LEFTFRONTFOOT_LENGTH = 0.2;
    const float RIGHTBACKFOOT_LENGTH = 0.2;
    const float LEFTBACKFOOT_LENGTH = 0.2;
    
    void leftbackfoot()
    {
    
    
    glPushMatrix();
     
    glTranslatef(-0.3,(LEFTBACKFOOT_LENGTH*-6.0),-0.4);
    glScalef(0.5,LEFTBACKFOOT_LENGTH,0.3);
    
    
    glutWireCube(1.0);
    
    glPopMatrix();
    
     }
    
    
    void rightbackfoot()
    {
    
     
    
    glPushMatrix();
     
    glTranslatef(-0.3,(RIGHTBACKFOOT_LENGTH*-6.0),0.4);
    glScalef(0.5,RIGHTBACKFOOT_LENGTH,0.3);
    
    
    glutWireCube(1.0);
    
    glPopMatrix();
    
     }
    
     
    
     
    
     void leftfrontfoot()
    {
    
     
    
    glPushMatrix();
     
    glTranslatef(0.7,(LEFTFRONTFOOT_LENGTH*-6.0),-0.4);
    glScalef(0.5,LEFTFRONTFOOT_LENGTH,0.3);
    
    
    glutWireCube(1.0);
    
    glPopMatrix();
    
     }
    
     
    
     
    
     
    
    
     void rightfrontfoot()
    {
    
     
    
    glPushMatrix();
     
    glTranslatef(0.7,(RIGHTFRONTFOOT_LENGTH*-6.0),0.4);
    glScalef(0.5,RIGHTFRONTFOOT_LENGTH,0.3);
    
    
    glutWireCube(1.0);
    
    glPopMatrix();
    
     }
     
    
    void bottombacklegl()
    {
    
    
    glPushMatrix();
    
    glTranslatef(-0.51,(BOTTOMBACKLEGL_LENGTH*-0.8),-0.4);
    glScalef(0.15,BOTTOMBACKLEGL_LENGTH,0.15);
    
    glutWireCube(1.0);
    
    glPopMatrix();
    
     }
    
    
    void bottombackleg()
    {
    
    
    glPushMatrix();
    
    glTranslatef(-0.51,(BOTTOMBACKLEG_LENGTH*-0.8),0.4);
    glScalef(0.15,BOTTOMBACKLEG_LENGTH,0.15);
    
    glutWireCube(1.0);
    
    glPopMatrix();
    
     }
    
     
    
    
    void backlegl()
    {
    
    
    glPushMatrix();
    
     
    
    glTranslatef(-0.51,(BACKLEGL_LENGTH*-0.3),-0.4);
    glScalef(0.15,BACKLEGL_LENGTH,0.15);
    glRotatef(-10,0.0,0.0,1.0);
    
    glutWireCube(1.0);
    
    glPopMatrix();
    
     }
    
     
    
     
    
    void backleg()
    {
    
    
    glPushMatrix();
    
     
    
    glTranslatef(-0.5,(BACKLEG_LENGTH*-0.3),0.4);
    glScalef(0.15,BACKLEG_LENGTH,0.15);
    glRotatef(-10,0.0,0.0,1.0);
    
    glutWireCube(1.0);
    
    glPopMatrix();
    
     }
    
     
    
    void bottomlegl()
    {
       
    glPushMatrix();
    
    glTranslatef(0.51,(BOTTOMLEGL_LENGTH*-0.8),-0.4);
    glScalef(0.15,BOTTOMLEGL_LENGTH,0.15);
    
    glutWireCube(1.0);
    
    glPopMatrix();
    
     }
    
     
    
     
    
    void bottomleg()
    {
       
    glPushMatrix();
    
    glTranslatef(0.51,(BOTTOMLEG_LENGTH*-0.8),0.4);
    glScalef(0.15,BOTTOMLEG_LENGTH,0.15);
    
    glutWireCube(1.0);
    
    glPopMatrix();
    
     }
    
     
    
     
    
    void frontlegl()
    {
    
    //glRotatef(-20,0.0,1.0,0.0);   
    //glRotatef(90,0.0,0.0,1.0);   
    
    
    glPushMatrix();
    
    glTranslatef(0.5,(FRONTLEGL_LENGTH*-0.3),-0.4);
    glScalef(0.15,FRONTLEGL_LENGTH,0.15);
    glRotatef(-10,0.0,0.0,1.0);
    
    glutWireCube(1.0);
    
    glPopMatrix();
    
     }
    
    void frontleg()
    {
    
      
    glPushMatrix();
    
     
    
    glTranslatef(0.5,(FRONTLEG_LENGTH*-0.3),0.4);
    glScalef(0.15,FRONTLEG_LENGTH,0.15);
    glRotatef(-10,0.0,0.0,1.0);
    
    glutWireCube(1.0);
    
    glPopMatrix();
    
     }
     
    
     
    
    void body()
    {
         
    glPushMatrix();
    glTranslatef(0.0,(BODY_LENGTH*0.5),0.0);
    glScalef(1.5,BODY_LENGTH,1.0);
    
    glutWireCube(1.0);
    glPopMatrix();
    
    
    }
    
    void neck()
    {
    
     
    
    glPushMatrix();
    
    glTranslatef(0.5,(NECK_LENGTH*1.3),0.0);
    glScalef(0.5,NECK_LENGTH,0.3);
    glRotatef(-15,0.0,0.0,1.0);   
    
    glutWireCube(1.0);
    
    
    glPopMatrix();
    
     }
     
     
     void head()
    {
    
     
    
    glPushMatrix();
     
    glTranslatef(0.68,(HEAD_LENGTH*2.4),0.0);
    glScalef(0.8,HEAD_LENGTH,0.4);
    glRotatef(-10,0.0,0.0,1.0);  
    
    glutWireCube(1.0);
    
    glPopMatrix();
    
     }
     
     
     
     
    
    using namespace std;
    
    void display(){
         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
         glMatrixMode(GL_MODELVIEW);
         glLoadIdentity();
         //gluLookAt(1.0,0.0,0.0,  0.0,0.0,0.0,  0.0,1.0,0.0); 
        gluLookAt(1.0,1.0,2.0,  0.0,0.0,0.0,  0.0,1.0,0.0); 
         body();
         neck();
         head();
         frontleg();
         frontlegl();
         bottomleg();
         bottomlegl();
         backleg();
         backlegl();
         bottombackleg();
         bottombacklegl();
         rightfrontfoot();
         leftfrontfoot();
         rightbackfoot();
         leftbackfoot();
         glutSwapBuffers();
    }
    
    void init(){
         glClearColor(0.0,0.0,0.0,0.0);
         glEnable(GL_DEPTH_TEST); 
         glColor3f(1.0,1.0,1.0);
         glMatrixMode(GL_PROJECTION);
         glLoadIdentity();
           //glFrustum(-1.0,1.0, -1.0,1.0, 1.0,10.0);  
         glOrtho(-5.0,5.0, -5.0,5.0, -1.0,10.0);
    }
      
    int main(int argc, char** argv) {
        glutInit(&argc, argv);
        glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
        glutInitWindowSize(500,500);
        glutInitWindowPosition(0,0);
        glutCreateWindow("Transformations");
        glutDisplayFunc(display);
        init();
        glutMainLoop();
    }

  2. #2
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    Shouldn't you put stuff like Neck, Head, etc. into classes? Not that I know, but that's kinda what I've understood for Classes so far :P
    Currently research OpenGL

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    85
    Quote Originally Posted by Akkernight
    Shouldn't you put stuff like Neck, Head, etc. into classes? Not that I know, but that's kinda what I've understood for Classes so far :P
    You could put the dog in a class and make the function to display it dog.display()

    Also animation gets complicated. You have to translate each body part for each frame. It's insane to do it by hand. You'd need to write your code so that it could display animated models from blender or anim8 or wings 3d, and then to make the model actually move in open gl, you'd need to translate it forward each frame while at the same translating the movements for each individual body part.

  4. #4
    Registered User
    Join Date
    Jun 2008
    Posts
    266
    Actually, the dog itself would make a better class.

    Sorry for repeating. A10's post wasn't there before I began typing.

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    4
    Well the thing is I need to animate it through OpenGL for an assignment.

  6. #6
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    For personal learning.. So, is it smarter to make all those body parts into Classes, and have those classes in a header?
    Currently research OpenGL

  7. #7
    Registered User
    Join Date
    Nov 2008
    Posts
    4
    Lol I need to animate it. My code works just fine I just want to know how you go about animating it. I'd rather keep the code the way it is because it works fine like that.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, what do you mean by animate it? Do you want each part to move separately, or the whole thing to move around the screen, or both, or neither, or what?

  9. #9
    Registered User
    Join Date
    Nov 2008
    Posts
    4
    I've now changed the code to make the pair of legs move. I need to get it to walk around the environment so I need to get it's legs to move and move the whole of the dog at the same time to make it move around realisticly.
    My current code looks like this:

    Code:
    
    #include <GL/glut.h>
    #include <GL/glu.h>
    
    using namespace std;
    
    float legsright = 30.0f;
    float legsleft = 30.f;
    
    const float BODY_LENGTH = 0.5;
    const float NECK_LENGTH = 0.5;
    const float HEAD_LENGTH = 0.45;
    const float FRONTLEG_LENGTH = 0.5;
    const float FRONTLEGL_LENGTH = 0.5;
    const float BOTTOMLEG_LENGTH = 0.9;
    const float BOTTOMLEGL_LENGTH = 0.9;
    const float BACKLEG_LENGTH = 0.5;
    const float BACKLEGL_LENGTH = 0.5;
    const float BOTTOMBACKLEG_LENGTH = 0.9;
    const float BOTTOMBACKLEGL_LENGTH = 0.9;
    const float RIGHTFRONTFOOT_LENGTH = 0.2;
    const float LEFTFRONTFOOT_LENGTH = 0.2;
    const float RIGHTBACKFOOT_LENGTH = 0.2;
    const float LEFTBACKFOOT_LENGTH = 0.2;
    
    
    
    
    
    
    void leftbackfoot()
    {
    glPushMatrix();
    glTranslatef(-0.3,(LEFTBACKFOOT_LENGTH*-6.0),-0.4);
    glScalef(0.5,LEFTBACKFOOT_LENGTH,0.3);
    glutWireCube(1.0);
    glPopMatrix();
    
    }
    
    
    void rightbackfoot()
    {
    glPushMatrix();
    glTranslatef(-0.3,(RIGHTBACKFOOT_LENGTH*-6.0),0.4);
    glScalef(0.5,RIGHTBACKFOOT_LENGTH,0.3);
    glutWireCube(1.0);
    glPopMatrix();
    
     }
    
     
    
     
    
     void leftfrontfoot()
    {
    glPushMatrix();
    glTranslatef(0.7,(LEFTFRONTFOOT_LENGTH*-6.0),-0.4);
    glScalef(0.5,LEFTFRONTFOOT_LENGTH,0.3);
    glutWireCube(1.0);
    glPopMatrix();
    
     }
    
     
    
     
    
     
    
    
     void rightfrontfoot()
    {
    glPushMatrix(); 
    glTranslatef(0.7,(RIGHTFRONTFOOT_LENGTH*-6.0),0.4);
    glScalef(0.5,RIGHTFRONTFOOT_LENGTH,0.3);
    glutWireCube(1.0);
    glPopMatrix();
    }
     
    
    void bottombacklegl()
    {
    
    
    glPushMatrix();
    glTranslatef(-0.51,(BOTTOMBACKLEGL_LENGTH*-0.8),-0.4);
    glScalef(0.15,BOTTOMBACKLEGL_LENGTH,0.15);
    
    glutWireCube(1.0);
    
    glPopMatrix();
    
     }
    
    
    void bottombackleg()
    {
    
    glPushMatrix();
    
    glTranslatef(-0.51,(BOTTOMBACKLEG_LENGTH*-0.8),0.4);
    glScalef(0.15,BOTTOMBACKLEG_LENGTH,0.15);
    
    glutWireCube(1.0);
    
    glPopMatrix();
    
     }
    
     
    
    
    void backlegl()
    {
    
    
    glPushMatrix();
    
     
    
    glTranslatef(-0.51,(BACKLEGL_LENGTH*-0.3),-0.4);
    glScalef(0.15,BACKLEGL_LENGTH,0.15);
    glRotatef(-10,0.0,0.0,1.0);
    
    glutWireCube(1.0);
    
    glPopMatrix();
    
     }
    
     
    
     
    
    void backleg()
    {
    
    
    glPushMatrix();
    
     
    
    glTranslatef(-0.5,(BACKLEG_LENGTH*-0.3),0.4);
    glScalef(0.15,BACKLEG_LENGTH,0.15);
    glRotatef(-10,0.0,0.0,1.0);
    
    glutWireCube(1.0);
    
    glPopMatrix();
    
     }
    
     
    
    void bottomlegl()
    {
       
    glPushMatrix();
    
    glTranslatef(0.51,(BOTTOMLEGL_LENGTH*-0.8),-0.4);
    glScalef(0.15,BOTTOMLEGL_LENGTH,0.15);
    
    glutWireCube(1.0);
    
    glPopMatrix();
    
     }
    
     
    
     
    
    void bottomleg()
    {
      
    glPushMatrix();
    glTranslatef(0.51,(BOTTOMLEG_LENGTH*-0.8),0.4);
    glScalef(0.15,BOTTOMLEG_LENGTH,0.15);
    
    glutWireCube(1.0);
    
    glPopMatrix();
    
     }
    
     
    
     
    
    void frontlegl()
    {
    
    //glRotatef(-20,0.0,1.0,0.0);   
    //glRotatef(90,0.0,0.0,1.0);   
    
    
    glPushMatrix();
    
    glTranslatef(0.5,(FRONTLEGL_LENGTH*-0.3),-0.4);
    glScalef(0.15,FRONTLEGL_LENGTH,0.15);
    glRotatef(-10,0.0,0.0,1.0);
    
    glutWireCube(1.0);
    
    glPopMatrix();
    
     }
    
    void frontleg()
    {
    
    
      
    glPushMatrix();
    
    
    
    glTranslatef(0.5,(FRONTLEG_LENGTH*-0.3),0.4);
    glScalef(0.15,FRONTLEG_LENGTH,0.15);
    glRotatef(-10,0.0,0.0,1.0);
    
    glutWireCube(1.0);
    
    glPopMatrix();
    
     }
     
    
     
    
    void body()
    {
         
    glPushMatrix();
    glTranslatef(0.0,(BODY_LENGTH*0.5),0.0);
    glScalef(1.5,BODY_LENGTH,1.0);
    
    glutWireCube(1.0);
    glPopMatrix();
    
    
    }
    
    void neck()
    {
    
     
    
    glPushMatrix();
    
    glTranslatef(0.5,(NECK_LENGTH*1.3),0.0);
    glScalef(0.5,NECK_LENGTH,0.3);
    glRotatef(-15,0.0,0.0,1.0);   
    
    glutWireCube(1.0);
    
    
    glPopMatrix();
    
     }
     
     
     void head()
    {
    
     
    
    glPushMatrix();
     
    glTranslatef(0.68,(HEAD_LENGTH*2.4),0.0);
    glScalef(0.8,HEAD_LENGTH,0.4);
    glRotatef(-10,0.0,0.0,1.0);  
    
    glutWireCube(1.0);
    
    glPopMatrix();
    
     }
     
     
     
     
    
    using namespace std;
    
    void display(){
         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
         glMatrixMode(GL_MODELVIEW);
         glLoadIdentity();
         //gluLookAt(1.0,0.0,0.0,  0.0,0.0,0.0,  0.0,1.0,0.0); 
        gluLookAt(1.0,1.0,2.0,  0.0,0.0,0.0,  0.0,1.0,0.0); 
         body();
         neck();
         head();
         
         //dog's right front leg
         glPushMatrix();
         glRotatef(legsright,0.0,0.0,-1.0);
         frontleg();//thigh
         bottomleg();//shin
         rightfrontfoot();//foot
         glPopMatrix();
         
         
         //dog's right back leg
         glPushMatrix();
        glRotatef(legsright,0.0,0.0,-1.0); //dogs right front leg and back leg are connected with this same rotate 
         backleg();
         bottombackleg();
         rightbackfoot();
         glPopMatrix();
         
        
         
    
    // dog's left front leg
         glPushMatrix();
         glRotatef(legsleft,0.0,0.0,-1.0);
         frontlegl();//thigh
         bottomlegl();//shin
         leftfrontfoot();//foot
         glPopMatrix();
        
        
      // need a second rotate for the left back and front legs remeber to make an update at the bottom a varible ect
         glPushMatrix();
         glRotatef(legsleft,0.0,0.0,-1.0);
         backlegl();
         bottombacklegl();
         leftbackfoot();
         glPopMatrix();
         
         glutSwapBuffers();
    }
    
    void update(int value) {
    	legsright += 2.0f;
    	if (legsright > 30) 
    	{ 
    	    legsright -= 30;
    	}
    	
    	glutPostRedisplay(); 
    	glutTimerFunc(30,update,0); // change speed
    	
    }
    
    
    void update2(int value) {
    	legsleft += 2.0f;
    	if (legsleft > 30) 
    	{ 
    	    legsleft -= 30;
    	}
    	
    	glutPostRedisplay(); 
    	glutTimerFunc(30,update2,0); // change speed
    	
    }
    
    
    
    void init(){
         glClearColor(0.0,0.0,0.0,0.0);
         glEnable(GL_DEPTH_TEST); 
         glColor3f(1.0,1.0,1.0);
         glMatrixMode(GL_PROJECTION);
         glLoadIdentity(); 
         glOrtho(-5.0,5.0, -5.0,5.0, -1.0,10.0);
    }
      
    int main(int argc, char** argv) {
        glutInit(&argc, argv);
        glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
        glutInitWindowSize(500,500);
        glutInitWindowPosition(0,0);
        glutCreateWindow("Transformations");
        glutDisplayFunc(display);
        init();
        glutTimerFunc(30,update,0);
        glutTimerFunc(25, update2, 0);
        glutMainLoop();
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linking OpenGL in Dev-C++
    By linkofazeroth in forum Game Programming
    Replies: 4
    Last Post: 09-13-2005, 10:17 AM
  2. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 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. OpenGL and Windows
    By sean345 in forum Game Programming
    Replies: 5
    Last Post: 06-24-2002, 10:14 PM
  5. opengl code not working
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 02-14-2002, 10:01 PM