Search code examples
copenglanimationtranslate-animation

Animating a scene using OpenGL 3 in C


I am creating an small animation film using OpenGL 3 and implementing it in visual studio community 2015. I am trying to make an animation of a dinosaur moving to the right at least in the first but I cannot make it animate somehow.

Here is my main:

#include "glut.h"
#include "globaldeclare.h"
#include "dino.h"
#include "scene1.h"
#include "scene1_animation.h"
#include <math.h>

void init(void)
{
    glClearColor(0.0, 204.0, 0.0, 0.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0, 500, 0, 500);
    glMatrixMode(GL_MODELVIEW);

}

void myIdle() {
    frame++;
    scene1_dino_idle();
    glutPostRedisplay();
}

void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT);
    //Animation happens here
    if (scene_counter == 1) {
        scene1();
        scene1_animation();
    }

    glutSwapBuffers();
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glutInitWindowSize(1300, 800);
    glutInitWindowPosition(100, 100);
    glutCreateWindow("Walking with dinosaur");
    glutDisplayFunc(display);
    glutIdleFunc(myIdle);
    init();
    glutMainLoop();
    return 0; 
}

This is where animation happens for scene1 (but it's not happening.) :)

void scene1_dino_idle() {
    //idle
    if (dino_slidey > 0 && dino_count == 1) dino_movey = 1;
    if (dino_movey == 1 && dino_count == 1) dino_slidey -= 2;//1
    if (dino_slidey < -30 && dino_count == 1) dino_movey = 0;
    if (dino_movey == 0 && dino_count == 1) dino_slidey += 2;//1

    if (dino_slidex > 1000 && dino_count == 1) { dino_movex = 1; dino_count = 2; dinoleg_count = 2; }
    if (dino_movex == 1 && dino_count == 1) dino_slidex -= 6;//4
    if (dino_slidex < -1600 && dino_count == 1) dino_movex = 0;
    if (dino_movex == 0 && dino_count == 1) dino_slidex += 6;//4
}

void scene1_dino_animation() {
    //moving to the right
    glPushMatrix();
    if (dino_count == 1) {
        glTranslatef(dino_slidex, dino_slidey, 0);
        glTranslatef(0, 100, 0);
        glScalef(0.6, 0.6, 1);
    }
    glScalef(dinoR_lost, dinoR_lost, 0);
    dino();
    glPopMatrix();
}

void scene1_animation() {
    scene1_dino_animation();
}

My global variables //frame counter int frame, scene_counter = 1;

//dino animation
float dino_movex, dino_movey, dino_count, dino_slidex, dino_slidey, dinoR_lost, dinoL_lost;

//dino leg
float dinoleg_angle = 0, dinoleg_move_x, dinoleg_move_y, dinoleg_count = 0;

I have done drawing the scene but I can't animate it. It seems to me that I have done right in the animation and maybe somewhere in my idle function does not allow me to make it work. I tried everything but I am not able to make it work and I don't know really where is the problem. Please let me know what is the main issue. Thanks in advance.


Solution

  • I manage to make it work. In some scenes that I've drawn, there was only a dino but it doesn't move so I removed the dino() from my scene1() from scene1.h . It went missing after that, but I noticed some dot moving (I don't where did that came from). Now I fixed it by adding the glutPostRedisplay() in scene1_dino_idle() function. Then I remove this code glScalef(dinoR_lost, dinoR_lost, 0); so the dino appears. Thank you all for helping. Happy new year.