i am designing a walking dinosaur any body please tell me how to move legs ?
i am designing a walking dinosaur any body please tell me how to move legs ?
How to do this in opengl? Any idea
I am juct trying to move legs method
#include<glut.h>
GLfloat xRotated = 0.0;
GLfloat yRotated = 0.0;
GLfloat zRotated = 0.0;
void legs(void);
void body(void);
float angle = 0;
void myInit(void)
{
glColor3f(1.0f,0.0f,0.0f);
//glMatrixMode(GL_PROJECTION);
//gluOrtho2D(0.0, 640.0, 0.0, 480.0);
glClearColor(1.0, 1.0, 1.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-100, 600, -100, 480);
}
void Display(void)
{
glLoadIdentity();
glClearColor(1.0, 1.0, 1.0, 1.0);
glOrtho(0.0, 20.0, 0.0, 20.0, -10.0, 10.0);
body();
//legs();
}
void body()
{
glClear(GL_COLOR_BUFFER_BIT);
glViewport(100,100,400,400);
glColor3f(1.0,0.6,0.0);
glBegin(GL_POLYGON);
glVertex2i(310, 370);
glVertex2i(280, 400); //(x,y) First point
glVertex2i(180, 400);
glVertex2i(130, 370);
glVertex2i(130, 340);
glVertex2i(155, 330);
glVertex2i(130, 310);
glVertex2i(130, 290);
glVertex2i(180, 270);
glVertex2i(180, 150);
glVertex2i(250, 150);
glVertex2i(300, 80);
glVertex2i(390, 80);
glVertex2i(430, 120);
glEnd();
//legs();
glColor3f(1.0,1.6,0.0);
glBegin(GL_POLYGON);
glVertex2i(260, 250);
glVertex2i(230, 270);
glVertex2i(230, 230);
glVertex2i(180, 230);
glVertex2i(180, 250);
glEnd();
}
void legs()
{
angle += 10;
glClearColor(1.0, 1.0, 1.0, 1.0);
xRotated =xRotated + 0.1;
glLineWidth(3.0);
glPushMatrix();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(60, 20, 0);
glRotatef( angle, 0, 0, 1);
glTranslatef(-60,-20,0);
//glTranslatef(100.0, 100.0, 0.0);
//glColor3f(0.0,1.0,0.0);
//glRotatef(-xRotated, 0.0, 0.0, 4.0);
//glScalef(1.0,1.0,1.0);
/*glBegin(GL_LINES);
glVertex2i(0, 0);
glVertex2i(250, 170);
glVertex2i(230, 190);
glEnd();
glBegin(GL_LINES);
glVertex2i(0,0);
glVertex2i(50,50);
glEnd();
*/
glBegin(GL_POLYGON);
glVertex2i(250, 170);
glVertex2i(230, 190);
glVertex2i(200, 190);
glVertex2i(180, 170);
glVertex2i(160, 130);
glVertex2i(200, 110);
glVertex2i(150, 50);
glVertex2i(230, 50);
glVertex2i(250, 70);
glEnd();
glFlush();
glPopMatrix();
glutPostRedisplay();
body();
}
int main(int argc, char **argv)
{
glutInit(&argc , argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(800,600);
glutCreateWindow("Dino");
glutDisplayFunc(legs);
myInit();
glutMainLoop();
}
Because you took the time to post your code and because your code actually compiles and runs, I took some time to look at it. Originally, I thought you were talking about a 3D model of a dinosaur which would require animating all 4 legs. Your situation is much simpler. It's 2D and there's really only one leg. Also you are not worried about the interface between the leg and the body. A few comments on your code. You've gotten an animation effect by putting 'glutPostRedisplay' in your DisplayFunc (legs). This is not the recommended way to do animation, though I've seen it done before. The conventional approach is to use an Idle function. It is called continuously after each display until you set the idle behaviour back to the default using glutIdleFunc(NULL). glutPostRedisplay goes in the 'Walk', not in the display function. In my code below you see the function 'Walk' declared as the IdleFunc. The 'Walk' function simply changes the value of 'angle' which is used to rotate the leg at the hip. Remember the modeling transformations are applied to geometry in the reverse order of how they appear in the code. Look at my legs routine to see this. The code below is reworking of your code (without deviating too much from what you posted). Next time you post code put it between [ code] and [ /code] tags.
Code :#include <glut.h> // The GL Utility Toolkit (Glut) Header float angle = 0; void body (void) { glColor3f (1.0, 0.6, 0.0); glBegin (GL_POLYGON); glVertex2i(310, 370); glVertex2i(280, 400); glVertex2i(180, 400); glVertex2i(130, 370); glVertex2i(130, 340); glVertex2i(155, 330); glVertex2i(130, 310); glVertex2i(130, 290); glVertex2i(180, 270); glVertex2i(180, 150); glVertex2i(250, 150); glVertex2i(300, 80); glVertex2i(390, 80); glVertex2i(430, 120); glEnd(); } void legs (void) { glColor3f (0.1, 0.3, 0.9); glPushMatrix(); glTranslatef ( 210, 160, 0); // Move hip joint back to dino body. glRotatef (angle, 0,0,1); // Rotate leg around origin. glTranslatef ( -210, -160, 0); // Place hip joint of leg at origin. glBegin(GL_POLYGON); glVertex2i (250, 170); glVertex2i (230, 190); glVertex2i (200, 190); glVertex2i (180, 170); glVertex2i (160, 130); glVertex2i (200, 110); glVertex2i (150, 50); glVertex2i (230, 50); glVertex2i (250, 70); glEnd(); glPopMatrix(); } void Walk (void) { static float da = 1.0; angle += da; if (angle > 30.0) da = -1.0; if (angle < -30.0) da = 1.0; glutPostRedisplay (); } void Display (void) { glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); body (); legs (); // glutPostRedisplay(); not a good idea glutSwapBuffers (); } void myInit (void) { glViewport (0, 0, 800, 600); glMatrixMode (GL_PROJECTION); glLoadIdentity (); gluOrtho2D (-100, 600, -100, 480); glMatrixMode (GL_MODELVIEW); glLoadIdentity (); glClearColor (0.2, 0.4, 0.3, 1.0); } int main (int argc, char **argv) { glutInit (&argc, argv); glutInitDisplayMode (GLUT_RGB | GLUT_DOUBLE); glutInitWindowPosition (300, 300); glutInitWindowSize (800, 600); glutCreateWindow ("Dino"); glutIdleFunc ( Walk ); glutDisplayFunc (Display); myInit (); glutMainLoop (); }
Thanks you very much. This one is my latest code:
Code :#include<glut.h> GLfloat angle = 160.0; GLfloat angle2 = 200.0; void legs(void); void body(void); void myInit(void) { glColor3f(1.0f,0.0f,0.0f); glMatrixMode(GL_PROJECTION); gluOrtho2D(0.0, 640.0, 0.0, 480.0); } void body() { glClear(GL_COLOR_BUFFER_BIT); // Second Leg angle2 glColor3f(0.0,0.6,0.0); glLineWidth(4.0); glPushMatrix(); glTranslatef(220, 200, 0); glRotatef( -angle2, 0, 0, 1); glBegin(GL_LINE_LOOP); glVertex2i(40, 10); glVertex2i(0, 0); glVertex2i(-40, 10); glVertex2i(-40, 160); glVertex2i(-10, 180); glVertex2i(60, 180); glVertex2i(20, 100); glVertex2i(60, 60); glEnd(); glPopMatrix(); // Body glViewport(200,200,400,400); glColor3f(1.0,0.6,0.0); glBegin(GL_POLYGON); glVertex2i(310, 370); glVertex2i(280, 400); //(x,y) First point glVertex2i(180, 400); glVertex2i(130, 370); glVertex2i(130, 340); glVertex2i(155, 330); glVertex2i(130, 310); glVertex2i(130, 290); glVertex2i(180, 270); glVertex2i(180, 150); glVertex2i(250, 150); glVertex2i(300, 80); glVertex2i(390, 80); glVertex2i(430, 120); glEnd(); // Arm glColor3f(.0,0.6,1.0); glBegin(GL_POLYGON); glVertex2i(220, 270); glVertex2i(250, 250); glVertex2i(220, 230); glVertex2i(100, 230); glVertex2i(70, 240); glVertex2i(40, 250); glVertex2i(20, 240); glVertex2i(0, 275); glVertex2i(100, 270); glEnd(); legs(); } void myKeyboard(unsigned char theKey, int mouseX, int mouseY) { switch(theKey) { case 'a': angle += 10; angle2 -= 10; break; case 's': angle -= 10; angle2 += 10; break; default: break; // do nothing } } void legs() { glClearColor(1.0, 1.0, 1.0, 1.0); // Leg glColor3f(0.0,0.6,0.0); glLineWidth(4.0); glPushMatrix(); glTranslatef(220, 200, 0); glRotatef( -angle, 0, 0, 1); glBegin(GL_LINE_LOOP); glVertex2i(40, 10); glVertex2i(0, 0); glVertex2i(-40, 10); glVertex2i(-40, 160); glVertex2i(-10, 180); glVertex2i(60, 180); glVertex2i(20, 100); glVertex2i(60, 60); glEnd(); glPopMatrix(); glFlush(); glutPostRedisplay(); } int main(int argc, char **argv) { glutInit(&argc , argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(600,600); glutCreateWindow("Cube Example"); glutDisplayFunc(body); myInit(); glutKeyboardFunc(myKeyboard); glutMainLoop(); }
You have something that works. Your code is much easier to read now that you put it between the [ code] and [ /code] tags.