Opengl walking leg animation
Hey Peeps
I have a problem with designing a walking leg animation using opengl (in C language). Im fairly new to programming and this is my first animation. I've managed to get so far (not sure if its correct though), but im having trouble working out how to actually get the legs walking in a forward direction with the correct leg motion.
I know I have to use the idle function at some point and probably some 'If' statements. I just can't seem to get it right though.
Any help you could offer would be highly appreciated. The code so far is below.
Cheers
Code:
#include "stdafx.h"
#include <stdlib.h>
#include <GL/glut.h>
#include <math.h>
#include <stdio.h>
//GLOBALS for communication
static float G_theta[3];
static float G_body_rot=0, G_up_leg_angle=0, G_low_leg_angle=-45.0, G_footr_angle=-45.0;
static float G_zoom=0.3;
//PROTOTYPES
void draw_scene(float body_rot, float up_leg_angle, float low_leg_angle, float footr_angle);
void view_control();
void draw_floor();
void draw_body(float up_leg_angle, float low_leg_angle, float footr_angle);
void draw_leg(float low_leg_angle, float footr_angle,float length_hip, float length_upper_leg, float length_lower_leg, float length_foot);
void draw_below_knee(float low_leg_ang, float length_upper_leg, float length_lower_leg, float length_foot);
void draw_foot(float length_foot);
void MyIdle(void)
//=============================================
// DRAW CUBOID ROUTINES
//=============================================
//Cube points and colours
float vertices[][3] =
{
{-0.5,-0.5,-0.5},{0.5,-0.5,-0.5},
{0.5,0.5,-0.5}, {-0.5,0.5,-0.5}, {-0.5,-0.5,0.5},
{0.5,-0.5,0.5}, {0.5,0.5,0.5}, {-0.5,0.5,0.5}
};
float colors[][3] = {{0.0,0.5,0.5},{1.0,0.0,0.0},
{1.0,1.0,0.0}, {0.0,1.0,0.0}, {0.0,0.0,1.0},
{1.0,0.0,1.0}, {1.0,1.0,1.0}, {0.0,1.0,1.0}};
void polygon(int a, int b, int c , int d)
{
// draw a polygon using colour of first vertex
glBegin(GL_POLYGON);
glColor3fv(colors[a]);
glVertex3fv(vertices[a]);
glVertex3fv(vertices[b]);
glVertex3fv(vertices[c]);
glVertex3fv(vertices[d]);
glEnd();
}
void cube(void)
{
//Draw unit cube centred on the origin
/* map vertices to faces */
polygon(0,3,2,1);
polygon(2,3,7,6);
polygon(4,7,3,0);
polygon(1,2,6,5);
polygon(7,4,5,6);
polygon(5,4,0,1);
}
void draw_cuboid(float x, float y, float z)
{
glPushMatrix();
//size cuboid
glScalef(x,y,z);
// move base up so that bottom face is at origin
glTranslatef(0,0.5,0.0);
cube();
glPopMatrix();
}
//=============================================
// KEYBOARD CALLBACK ROUTINE
//=============================================
void keyboard(unsigned char key, int x, int y)
{
printf("Keyboard call back: key=%c, x=%d, y=%d\n", key, x, y);
switch(key)
{
//walk legs
case '1':
G_
//=============================================
//Draw_Scene
//=============================================
void draw_scene (float body_rot, float up_leg_angle, float low_leg_angle, float foot_angle);
{
glPushMatrix();
draw_floor();
//rotate complete body about y-axis
glRotatef(body_rot, 0.0,1.0,00);
//Draw body
draw_body (float up_leg_angle, float low_leg_angle, float footr_angle);
glPopMatrix();
}
//=============================================
//Draw_Floor
//=============================================
void draw_floor()
{
glPushMatrix();
glBegin(GL_POLYGON);
glColor3f(.75,.75,.75);
glVertex3f(-10,0,10);
glVertex3f(-10,0,-10);
glVertex3f(10,0,-10);
glVertex3f(10,0,10);
glEnd();
glPopMatrix();
}
//=============================================//Draw_Body
//=============================================
void draw_body(float up_leg_angle, float low_leg_angle, float footr_angle);
{
float length_upper_leg=5.5 //length of top part of leg
float length_lower_leg=5.5 //length of bottom part of leg
float length_foot=1 //length of foot
float length_hip=1.5//length of hip
glPushMatrix();
draw_hip();
//draw box for Hips
glPushMatrix();
draw_cuboid(2.0,length_hip,3.5);
//Change frame of reference to the base of the box
glTranslatef(0.0,length_hip/2,-10)
glPopMatrix();
//rotate upper leg around z-axis
glRotatef(up_leg_angle, 0.0,0.0,1.0);
//Draw leg
void draw_leg(float low_leg_angle, float footr_angle,float length_hip, float length_up_leg, float length_low_leg, float length_footr);
glPopMatrix();
}
//=============================================
//Leg_Displacement
//=============================================
//=============================================
//Draw_Leg
//=============================================
void draw_leg(float low_leg_angle, float footr_angle,float length_hip, float length_upper_leg, float length_lower_leg, float length_foot);
{
glPushMatrix();
draw_upper_leg();
glPushMatrix();
draw_cuboid(2.0,length_upper_leg,1.5);
//change frame of reference to the end of the 2nd joint/base of upper leg
glTranslatef(0.0,length_upper_leg,0);
glPopMatrix();
//rotate lower leg around z-axis
glRotatef(low_leg_angle, 0.0,00,1.0);
//Draw below the knee
void draw_below_knee(float footr_angle,float length_leg, float length_);
glPopMatrix();
}
//=============================================
//Draw_Below_Knee
//=============================================
void draw_below_knee(float footr_angle, float length_lower_leg, float length_foot);
{
glPushMatrix();
draw_lower_leg();
glPushMatrix();
draw_cuboid(2.0,length_lower_leg,1.5);
//change frame of reference to the end of the 3rd joint/base of lower leg
glTranslatef(0.0,length_lower_leg,0);
glPopMatrix();
//rotate foot around z-axis
glRotatef(footr_angle,0.0,0.0,1.0);
draw_foot();
glPopMatrix();
}
//=============================================
//Draw_Foot
//=============================================
void draw_foot(float length_foot);
{
glPushMatrix();
draw_cuboid(3.5,length_foot,1.5);
glPopMatrix();
//=============================================
//Idle_Function
//=============================================
void MyIdle(void)
{
/* Some code to modify the variables defining next frame */
....
};