I started 2days ago learning opengl. I built a simple robot with 8 parts.Now I want that my robot is moving his hands and legs as if he was walking. I want do with glRotate. But I don’t know how I should start. I tried it but the body part(hand) got through rotated. Do you have some advice or something else? I want to keep it simple.
#include <iostream>
#include <GL/freeglut.h>
#include "Wuerfel.h"
static float fRHand = 0., fRUnterHand = 0; //right upper hand and right under hand
static float fX = 1, fY = 1, fZ = 1;
void Init()
{
glClearColor(0, 0, 0, 1);
glEnable(GL_DEPTH_TEST);
}
void RenderScene()
{
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
gluLookAt(0,0., 3 // Betrachterposition
, 0., 0., 0.,//Blickziel
0, 0.1, 0.); // Oben-Betrachter
//KOPF/Head
glTranslatef(0.0, 0.5, 0.0);
glColor3f(0.8, 0.5, 0.2);
Wuerfel(0.3);
glPopMatrix();
//KÖRPER/Body
glPushMatrix();
glScalef(1.3, 2., 2.);
glTranslatef(0.0, -0.25, 0.0);
Wuerfel(0.4);
glPopMatrix();
//LINKES BEIN /left Leg
glPushMatrix();
glScalef(0.5, 2., 0.3);
glTranslatef(-0.4, -0.6, 0.0);
Wuerfel(0.2);
glPopMatrix();
//RECHTES BEIN/right leg
glPushMatrix();
glScalef(0.5, 2., 0.3);
glTranslatef(0.4, -0.6, 0.0);
Wuerfel(0.2);
glPopMatrix();
//LINKER OBERARM /left upper arm
glPushMatrix();
//glRotatef(fRotation, 0.1, 0.0, 0.0);
glScalef(0.4, 1.4, 0.3);
glTranslatef(-1.0, -0.25, 0.0);
Wuerfel(0.3);
glPopMatrix();
//LINKER UNTERARM/ left under arm
glPushMatrix();
glScalef(0.4, 0.8, 0.3);
glTranslatef(-1., -0.8, 0.0);
Wuerfel(0.2);
glPopMatrix();
//RECHTER OBERARM/right upper arm
glRotatef(fRHand += 0.2, 1., 0., 0.);
glPushMatrix();
glScalef(0.4, 1.4, 0.3);
glTranslatef(1.0, -0.25, 0.0);
Wuerfel(0.3);
glPopMatrix();
//RECHTER UNTERARM /right under arm
glPushMatrix();
glScalef(0.4, 0.8, 0.3);
glTranslatef(1., -0.8, 0.0);
glRotatef(fRUnterHand += 0.2, 0.5, 0., 0.);
Wuerfel(0.2);
glPopMatrix();
glutSwapBuffers();
}
void Reshape(int width, int height)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, width, height);
gluPerspective(45., 1, 0.1, 8.); // perspektivische Kamera
glMatrixMode(GL_MODELVIEW);
}
void Animate(int value)
{
glutPostRedisplay();
int wait_msec = 10;
glutTimerFunc(wait_msec, Animate, ++value);
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(600, 600);
glutCreateWindow("Name_1; Name_2");
glutDisplayFunc(RenderScene);
glutReshapeFunc(Reshape);
glutTimerFunc(10, Animate, 0);
Init();
glutMainLoop();
return 0;
}