How to change the lift button color in OpenGL using glPushAttrib()

  2d, c++, graphics, opengl

This is a simple animation I done so far. I am wondering how I can make changes like this:
Once the man stop walking towards the lift buttons, the 8th button change its color to the same color as the 5th button (to portray that he press the button).

I’m thinking of using glPushAttrib but not sure how to insert into the code.

#include <stdlib.h>
#include <stdio.h>
#include <glglut.h>
#include <math.h>
#include <time.h>

#define SCREEN_WIDTH 1280   
#define SCREEN_HEIGHT   720
#define PI 3.141592653

char buf[100] = { 0 };

//text
void renderbitmap(float x, float y, void* font, char* string) {
    char* c;
    glRasterPos2f(x, y);
    for (c = string; *c != '{$content}'; c++) {
        glutBitmapCharacter(font, *c);
    }
}

// Function to Create Delay
void delay(float secs) {
    float end = clock() / CLOCKS_PER_SEC + secs;
    while ((clock() / CLOCKS_PER_SEC) < end);
}

void ellipse(int n, int x, int y, int r) {
    double inc = 2 * PI / (double)n;
    glBegin(GL_POLYGON);
    for (double theta = 0.0; theta <= 2 * PI; theta += inc) {
        glVertex2i(r * cos(theta) + x, 1.5 * r * sin(theta) + y);
    }
    glEnd();
}

void horizontal_ellipse(int n, int x, int y, int r) {
    double inc = 2 * PI / (double)n;
    glBegin(GL_POLYGON);
    for (double theta = 0.0; theta <= 2 * PI; theta += inc) {
        glVertex2i(1.5* r * cos(theta) + x, r * sin(theta) + y);
    }
    glEnd();
}

void floor() {
    //floor
    glColor3ub(92, 84, 83);
    glBegin(GL_POLYGON);
    glVertex2i(0, 0);  //bottom left corner
    glVertex2i(1280, 0);   //bottom right corner
    glVertex2i(900, 150); // top right corner
    glVertex2i(0, 150);  //top left corner
    glEnd();
}

void wall() {
    //wall
    glColor3ub(191, 179, 176);
    glBegin(GL_POLYGON);
    glVertex2i(0, 100);      //bottom left corner
    glVertex2i(950, 100);   //bottom right corner
    glVertex2i(950, 720);   // top right corner
    glVertex2i(0, 720);      //top left corner
    glEnd();

    //lower wall front side
    glColor3ub(113, 99, 87);
    glBegin(GL_POLYGON);
    glVertex2i(0, 300);      //bottom left corner
    glVertex2i(950, 300);   //bottom right corner
    glVertex2i(950, 330);   // top right corner
    glVertex2i(0, 330);      //top left corner
    glEnd();


    //wall right side
    glColor3ub(168, 161, 156);
    glBegin(GL_POLYGON);
    glVertex2i(950, 100);  //bottom left corner
    glVertex2i(1280, 0);   //bottom right corner
    glVertex2i(1280, 720); // top right corner
    glVertex2i(950, 720);  //top left corner
    glEnd();

    //lower wall right side
    glColor3ub(109, 95, 84);
    glBegin(GL_POLYGON);
    glVertex2i(950, 300);      //bottom left corner
    glVertex2i(1280, 235);   //bottom right corner
    glVertex2i(1280, 265);   // top right corner
    glVertex2i(950, 330);      //top left corner
    glEnd();

}

void liftButton() {

    //button rectangle   
    glColor3ub(141, 140, 152);
    glBegin(GL_POLYGON);
    glVertex2i(1050, 350);  //bottom left corner
    glVertex2i(1190, 327);   //bottom right corner
    glVertex2i(1190, 600); // top right corner
    glVertex2i(1050, 610);  //top left corner
    glEnd();

    //bottom square button (1)
    glColor3ub(236, 203, 68);
    glBegin(GL_POLYGON);
    glVertex2i(1060, 360);  //bottom left corner
    glVertex2i(1095, 355);   //bottom right corner
    glVertex2i(1095, 370); // top right corner
    glVertex2i(1060, 375);  //top left corner
    glEnd();

    //bottom square button (2)
    glColor3ub(236, 203, 68);
    glBegin(GL_POLYGON);
    glVertex2i(1105, 354);  //bottom left corner
    glVertex2i(1140, 348);   //bottom right corner
    glVertex2i(1140, 363); // top right corner
    glVertex2i(1105, 369);  //top left corner
    glEnd();

    //bottom square button (3)
    glColor3ub(236, 203, 68);
    glBegin(GL_POLYGON);
    glVertex2i(1150, 346);  //bottom left corner
    glVertex2i(1185, 340);   //bottom right corner
    glVertex2i(1185, 355); // top right corner
    glVertex2i(1150, 360);  //top left corner
    glEnd();

    //number button  - from top left side to downward
    glColor3ub(230, 222, 217);
    ellipse(100, 1100, 560, 15);
    

    //number button 
    glColor3ub(230, 222, 217);
    ellipse(100, 1100, 510, 15);

    //number button 
    glColor3ub(224, 66, 30);
    ellipse(100, 1100, 460, 15);

    //number button 
    glColor3ub(230, 222, 217);
    ellipse(100, 1100, 410, 15);

    //number button - from bottom right side to upward
    
    glColor3ub(230, 222, 217);
    ellipse(100, 1140, 405, 15);
    glPushAttrib(GL_CURRENT_BIT);
    glColor3ub(224, 66, 30);
    glPopAttrib();

    //number button 
    glColor3ub(230, 222, 217);
    ellipse(100, 1140, 455, 15);

    //number button 
    glColor3ub(230, 222, 217);
    ellipse(100, 1140, 505, 15);

    //number button 
    glColor3ub(230, 222, 217);
    ellipse(100, 1140, 555, 15);
    
}

void liftButtonUpdate() {

    glPushMatrix();
    glColor3ub(224, 66, 30);
    ellipse(100, 1140, 405, 15);

    glPopMatrix();
}

// male character //
void rectangle(int x, int y, int w, int h) {
    glBegin(GL_POLYGON);
    glVertex2i(x, y);
    glVertex2i(x + w, y);
    glVertex2i(x + w, y + h);
    glVertex2i(x, y + h);
    glEnd();
}

void triangle(int x, int y, int w, int h) {
    glBegin(GL_POLYGON);
    glVertex2i(x, y);
    glVertex2i(x + w, y);
    glVertex2i(x + w / 2, y + h);
    glEnd();

}

void circle(int n, int x, int y, int r) {
    double inc = 2 * PI / (double)n;
    glBegin(GL_POLYGON);
    for (double theta = 0.0; theta <= 2 * PI; theta += inc) {
        glVertex2i(r * cos(theta) + x, r * sin(theta) + y);
    }
    glEnd();
}

void maleChar(int x, int y, int w) {
    glPushMatrix();
    glTranslatef(male_walk, 0, 0);

    glColor3f(0.f, 0.f, 0.f);
    sprintf_s(buf, "Pardon, can you please move aside?");
    renderbitmap(450, 400, GLUT_BITMAP_TIMES_ROMAN_24, buf);

    //draw body shaped like an egg
    glColor3ub(244, 177, 131);
    ellipse(200, x, y, 110);

    //draw hat 
    glColor3ub(68, 114, 196); //blue
    rectangle(445, x - 270, y + 40, 40); //lowerhat

    glColor3ub(68, 114, 196);
    rectangle(470, x - 230, y -10 , 40); //upperhat

    //draw eyes
    glColor3ub(255, 255, 255); //white
    circle(100, x - 40, y + 40, 23);  //outer LEFT eye

    glColor3ub(0, 0, 0); //black
    circle(100, x - 40, y + 40, 10);  //inner LEFT eye

    glColor3ub(255, 255, 255);
    circle(100, x + 40, y + 40, 23);  //outer RIGHT eye

    glColor3ub(0, 0, 0);
    circle(100, x + 40, y + 40, 10);  //inner RIGHT eye

    //draw nose (small triangle in the middle of face)
    glColor3ub(237, 125, 49);
    triangle(530, x - 430, y /5, 50);

    // draw mouth (long, thin rectangle)
    glColor3ub(109, 12, 12);
    rectangle(590, x - 470, y - 255, 10);

    glPopMatrix();
}

void male()
{
    maleChar(550, 170, 200);
}

//female character
void femaleChar(int x, int y, int w) {
    glPushMatrix();
    glTranslatef(female_stepAside2, female_stepAside, 0);

    //draw body shaped like an egg
    glColor3ub(228, 198, 141);  //gold sand first layer
    ellipse(180, x, y, 100);

    //draw ribbon bow on head
    glColor3ub(212, 72, 87); //cabaret pink
    ellipse(98, x - 55, y + 127, 25);
    glColor3ub(212, 72, 87); //cabaret pink
    ellipse(97, x - 9, y + 140, 24);
    glColor3ub(177, 56, 68); //medium carmine 
    circle(100, x - 33, y + 134, 19);

    //draw eyes
    glColor3ub(255, 255, 255); //white
    circle(100, x - 40, y + 40, 23);  //outer LEFT eye

    glColor3ub(70, 49, 17); //dark brown
    ellipse(100, x - 40, y + 40, 14);  //inner LEFT eye

    glColor3ub(255, 255, 255); //white
    ellipse(10, x - 40, y + 40, 7);  //innermost LEFT eye

    glColor3ub(255, 255, 255); //white
    circle(100, x + 40, y + 40, 23);  //outer RIGHT eye

    glColor3ub(70, 49, 17); // dark brown 
    ellipse(100, x + 40, y + 40, 14);  //inner RIGHT eye

    glColor3ub(255, 255, 255); //white
    ellipse(10, x + 40, y + 40, 7);  //innermost RIGHT eye

    //draw nose (small triangle in the middle of face)
    glColor3ub(214, 156, 116);
    triangle(845, x - 750, y / 6, 35);

    // draw cheek (feel shame) - right
    glColor3ub(240, 128, 128);
    horizontal_ellipse(300, x + 50, y - 10, 15);

    // draw cheek (feel shame) - left
    glColor3ub(240, 128, 128);
    horizontal_ellipse(300, x - 50, y - 10, 15);

    glPopMatrix();

}

void female()
{
    femaleChar(860, 150, 200);
}

void scene8() {
    floor();
    wall();
    liftButton();
    female();
    male();
}

void display() {
    glClear(GL_COLOR_BUFFER_BIT);

    scene8();
    //liftButtonUpdate();

    glFlush();
    glutSwapBuffers();

}

//for both characters start moving animation
void scene8_animation(int value)
{
    if (female_stepAside < 80) {
        female_stepAside++;
    }
    if (female_stepAside2 > -200) {
        female_stepAside2--;
    }
    if (male_walk < 450) {
        male_walk += 1;
    }
    liftButtonUpdate();
        
    glutTimerFunc(15, scene8_animation, 0);
    glutPostRedisplay();
}

//void buttonLiftUpdate() {}

void initGL() {
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0, SCREEN_WIDTH, 0, SCREEN_HEIGHT);
    glMatrixMode(GL_MODELVIEW);
    glPointSize(10.0);
    glLineWidth(5.0);
}

int main(int argc, char** argv) {
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
    glutInit(&argc, argv);
    glutInitWindowSize(SCREEN_WIDTH, SCREEN_HEIGHT);
    glutCreateWindow("LIFT LOVE");
    glutDisplayFunc(display);
    glutTimerFunc(10, scene8_animation, 0);
    initGL();
    glutIdleFunc(display);
    glMatrixMode(GL_MODELVIEW);
    glutMainLoop();
}


Source: Windows Questions C++

LEAVE A COMMENT