// Battery Animation
#include <GL/glut.h>
float y_pos =146.5, red_value = 0.86,green_value = 0.07,blue_value =0.23,speed =0.50;
int state = 1, angle = - 90;
void display()
{
glClear( GL_COLOR_BUFFER_BIT ) ; glPushMatrix();
glTranslatef( x: 250, y: 250, z: 0.0);
glMatrixMode( GL_PROJECTION ); glLoadIdentity();
double w = glutGet( GLUT_WINDOW_WIDTH ) ; double h = glutGet( GLUT_WINDOW_HEIGHT ); glortho( left: 8, w, bottom: 0, h, near_val: -1, far_val: 1);
glMatrixMode( GL_MODELVIEW ) ; glLoadIdentity();
glTranslatef( x: 250, y: 261.5, z: 1.0); glRotatef(angle, x: 0.Of, y: 0.0f, z 1.0f); glTranslatef( x: -250, y: -261.5, z -1.0);
// Battery glColor3f(red_value, green_value, blue_value);
glRecti ( x1: 187.5, y1: 138.5, x2: 312.5, y2: 361.5); glRecti ( x1: 223.5, y1: 353.5, x2: 276.5, y2: 384.5) ; glColor3f( red: 0.0, green: 0.0, blue 0.0);
glRecti ( x1: 194.5, y1: 146.5, x2: 305.5, y2: 353.5) ; glRecti ( x1: 230.5, y1: 361.5, x2: 269.5, y2: 377.5); glColor3f(red_value, green_value, blue_value);
// transformer
int transform = 40;
glRecti ( x1: 204, y1: 155, x2: 296, y2: 187);
glRecti ( x1: 204, y1: transform + 155, x2: 296, y2: transform + 187);
glRecti ( x1: 204, yl: (2 * transform) + 155, x2: 296, y2: (2 * transform) + 187);
glRecti ( x1: 204, y1: (3 * transform) + 155, x2: 296, y2: (3 * transform) + 187);
glRecti ( x1: 204, y): (4 * transform) + 155, x2: 296, y2: (4 * transform) + 187);
//black_cover
glColor3f( red: 0.0, green: 0.0, blue: 0.0) ;
glRecti( x1: 194.5, y_pos, x2: 305.5, y2: 353.5) ; glutSwapBuffers();
glPopMatrix();
void timer (int)
glutPostRedisplay();
glutTimerFunc( time: 1000/60, timer, value: 0); switch(state)
case 1:
if (y_pos<353.5) y_pos+=speed; else state= - 1; break; case -1:
if (y_pos>146.5) y_pos -= speed; else state=1; break;
void myMouse(int button, int state, intx, inty)
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
if (speed == 0.0) speed=0.50;
glutTimerFunc( time: 0, timer, value: 8) ;
if (button == GLUT_LEFT_BUTTON && state == GLUT_UP) speed=0.0;
else if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN) exit (0);
y_pos+=speed; else state= - 1; break; case -1:
if (y_pos>146.5) y_pos -= speed;
else
state=1;
break;
void myMouse (int button, int state, int x, int y)
{ if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) if (speed == 0.0) speed=0.50; glutTimerFunc( time: 0, timer, value: 0);
if (button == GLUT_LEFT_BUTTON && state == GLUT_UP) speed=0.0;
else if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN) exit(0);
int main( int argc, char ** argv )
glutInit( &argc, argv );
glutInitDisplayMode( displayMode: GLUT_RGBA GLUT_DOUBLE ) ;
glutInitWindowSize( width: 500, height: 500 ); glutCreateWindow( "Battery Animation" ); glutDisplayFunc( display );
glutMouseFunc (myMouse);
glutMainLoop();
return 0;
Hope you like it, try this code change some code or modify code give your touch create best as you can learn new things,if any Question and Queries you can post in comment.
0 Comments