Muhammad Catubig
Muhammad Catubig

Reputation: 311

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.

Upvotes: 0

Views: 239

Answers (2)

Muhammad Catubig
Muhammad Catubig

Reputation: 311

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.

Upvotes: 1

Steve
Steve

Reputation: 493

I assume that most of the variables named are global. Since you didn't show us initial values, I'll assume they're all zero (except scene_counter and dino_count; nothing happens if either of them is zero), and step through a few iterations. (Please excuse the crowded layout of this trace.) These are the values at the entrance to each call to scene1_dino_idle():

  scene_counter   dino_movey   dino_slidey  dinoleg_count
frame  | dino_count | dino_movex | dino_slidex   |
  |    |      |     |      |     |      |        |
  1    1*     1*    0      0     0      0        0
  2    1      1     0      0     2      6        0
  3    1      1     1      0     0     12        0
  4    1      1     1      0    -2     18        0
  5    1      1     1      0    -4     24        0
. . .
  f    1      1     1      0 (f-3)*-2 (f-1)*6    0
. . .
 18    1      1     1      0   -30    102        0
 19    1      1     0      0   -30    108        0
 20    1      1     0      0   -28    114        0
. . .

Judging from this little trace, it seems that if you're having a problem, it's because scene_counter or dino_count (or both) are zero. Another possibility is that you're having scope problems with your global variables; if there's a typo somewhere in your declarations C might be treating one or more of them as local variables. (In some versions of C, variables that are not explicitly declared will default to int.)

Without your headers, we can't tell what's wrong.

Since some debuggers get along with graphics code poorly, you might consider adding debug code to trace your variables into a file, just like I did manually above.

Best I can tell without compiling it myself and trying to guess what you left out, the OpenGL code is OK.

Upvotes: 1

Related Questions