C++  Coding by using Open Gl Creating Small Animation

Hii, I am beginner of this filed of coding and still learning. I have to 1 assignment Or you can say project on based on open gl. My team and i have create animation Or small application so we created small animation of battery and you can control that animation by using mouse button if you not get this try the code one time then you will know what I taking thats not over we created almost 9 , 10 S mall animation Or small application I will post one by one till that see this code below. All has done my group or you can say team work. With team we created many project or slides and 1 research paper i will post everything just give me some times. 

Important Msg - I typed all program from phone if you read this form phone you can correctly in phone but if your reading through the pc there is problem in code it not proper space not placed correctly not showing correctly "sorry for that inconvenience I will look at that finding solution till sorry for that " Thank you for your support . 

Codes:-



// 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;









Output:-









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. 

Thank you if you reading till now, come back again . 

Have a Great Day!!!