2
struct Point{
    float x;
    float y;
};

Using this struct I made 4 points, and assigned them values. The instances of the Points are then used to create the lines shown in the picture.

PO.x = -0.5f;
PO.y = 0.5f;

P1.x = -1.0f;
P1.y = -0.5f;

Q0.x = 0.5;
Q0.y = -1.0f;

Q1.x = 0.7f;
Q1.y = 0.1f;

glPushMatrix();

glLineWidth(2.5);
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_LINES);
glVertex2f(PO.x, PO.y);//PO
glVertex2f(P1.x, P1.y);//P1

glVertex2f(Q0.x, Q0.y);//QO
glVertex2f(Q1.x, Q1.y);//Q1
glEnd();

glPopMatrix();

Edited picture

The dotted lines should be the transition from points P0 to Q0 and so fourth.

What I do understand is the concept of this technique, LERP, creates in-between frames between the two lines (or Key frame). What I do not understand is how it works.

x = x_start + ((x_final - x_start) * time)// where time is a value between zero and one

The above is the formula for this technique, but how will I implement this into my glut application?

Could not find any sources online that use GLUT and LERP to demonstrate animation.

Moynul
  • 635
  • 1
  • 8
  • 30

1 Answers1

2

The idle function is called every time when there is no user input.

void idle()
{

    timer += vec;
    cout << timer << endl;
    if (timer > 1){
        vec = 0;
    }
    glutPostRedisplay();
}

Since I have the points I need to get the inbetween points and this is done by using this function.

float interpolate(float startPos, float finalPos, float time) {
    return startPos + ((finalPos - startPos) * time);// ; 
}

This function plots points across a linear line depending on the time.

T0.x = interpolate(PO.x, Q0.x, timer);

T0 is the inbetween key frames, I interpolate the starting point of my line, and the end point. Thus interpolating.

Without the timer incrementing, it would not give the effect of "animation"

T0.x = interpolate(PO.x, Q0.x, timer);
T0.y = interpolate(PO.y, Q0.y, timer);

T1.x = interpolate(P1.x, Q1.x, timer);
T1.y = interpolate(P1.y, Q1.y, timer);


glPushMatrix();

glLineWidth(2.5);
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_LINES);
glVertex2f(T0.x, T0.y);
glVertex2f(T1.x, T1.y);

glEnd();

After calculating the points, the line is created by using the draw line function.

Moynul
  • 635
  • 1
  • 8
  • 30