mehdi shahidi
mehdi shahidi

Reputation: 73

OpenGL reverse movement

I'm trying to decrease the values of 'left_eye_b' and 'left_eye_b'and then increase the values again to simulate wink on a human I made on OpenGL.

Here I created the eye:

left_eye_b=10.0;
left_eye_s=10.0;
    glColor3f(1.0f, 1.0f, 0.0f);
    glBegin(GL_POLYGON);
    for (float i = 0; i <= (2 * p); i += 0.001) {           // Left Eye Inside
        x = 10.0 * cos(i) - 24.56;
        y = left_eye_s * sin(i) + 56.45;
        glVertex2f(x, y);
    }
    glEnd();

    glColor3f(0.0f, 0.0f, 0.0f);
    glPointSize(2);
    glBegin(GL_POINTS);
    for (float i = 0; i <= (2 * p); i += 0.001) {           // Left Eye Border
        x = 10.0 * cos(i) - 24.56;
        y = left_eye_b * sin(i) + 56.45;
        glVertex2f(x, y);
    }
    glEnd();

Here is where I tried to make the eye wink:

void timer(int a) {
    /// Simulating Wink
    if (left_eye_b >= 1.12)
        left_eye_b -= 0.1;          // Eye Close
    if (left_eye_s >= 1.12)
        left_eye_s -= 0.1;
    glutTimerFunc(10, timer, 1);
    if (left_eye_b <= 10.0)
        left_eye_b += 0.1;          // Eye Open
    if (left_eye_s <= 10.0)
        left_eye_s += 0.1;

    glutPostRedisplay();
    glutTimerFunc(10, timer, 1);

}

Problem statement: If I remove the 'eye open' part, I works perfectly and the eye closes. However, I want to use the 'Eye Open' part to make increase the values back and open the eye again after a few seconds.

Any suggestion about how that can be achieved?

Upvotes: 1

Views: 143

Answers (1)

Rabbid76
Rabbid76

Reputation: 211287

Add a variable that indicates whether the eye will be opened or closed. The value of the variable is 1.0 to open and -1.0 to close:

double eye_change = -1.0;

Change the sign of the variable when the exe has opened or closed:

void timer(int a) {

    left_eye_b += 0.1 * eye_change; 
    left_eye_s += 0.1 * eye_change; 

    if (left_eye_b <= 1.12)
        eye_change = 1.0;
    if (left_eye_b >= 10.0)
        eye_change = -1.0;

    glutPostRedisplay();
    glutTimerFunc(10, timer, 1);
}

Upvotes: 1

Related Questions