Flying Ball Computer Graphics Project in OpenGL Source Code – 18CSL67

Flying Ball Computer Graphics Project in OpenGL Source Code – 18CSL67

Here you can download the source code of Flying Ball Computer Graphics Project in OpenGL academic mini-project.

Project Description:

Computer graphics is one of the most exciting and rapidly growing computer fields and computer. It is also an extremely effective medium for communication between men. The human can understand the information content of a displayed diagram or perceptive view much faster than it can understand a table of numbers. There is a lot of development in hardware and software required to generate images, and nowadays the cost of such hardware and software is also dropping rapidly. Due to this, the interactive computer graphics is becoming available to more and more people.

Main( ) : The main function  initialized with function glutInit(). The main() function create window   with  gutCreateWindow( )  the string can be used to Lable  the window. To initialize the window position  and  size  using   glutInitWindowPosition() and glutInitWindowSize()  they specifies the initial hight and width of the window in pixel. glutDisplayMode set the initial display mode, glutDisplayFunc() sets the display callback for the current window.glutMainLoop() glutMainloop enters an event processing loop. In main Display( ), mykey(), Init(),menu() these functions are called.

READ  Running Trains Computer Graphics Project in OpenGL Source Code – 18CSL67

display( ) : The entire working of the program is graphically displayed on the  screen by the contents defined in this function.

Mykey( ) : This function sets the keyboard callback for the current window. When we press the key it generate an ASCII character will generate a keyboard callback when we press b the ball starts flying and key q is pressed the window screen is return to program.

Menu( ) : Here we created a menu. Menu options are flying, exit and color.

exit() – this  function is used to terminate the program.

Color- It is used to set the current color using glColor3f().

Flying()- This option is displayed in menu by using glutIdleFunc().

glutIdleFunc() – registers the display callback function that is executed whenever there are no other events to be handeled.

Mymouse(): This function is called by the GLUT when user presses a mouse button. Right button of the mouse is to set open MENU on the window. In this menu having the options flying, exit and color.

Fying(): In this function we make the objects fly by using the glRotate(), glTranslate() anscaled() Functions.

fly() – with some operation we fly the objects.

nofly() – by using this the objects will stop there fly.

READ  Python program to find union and intersection of a list

glRotate() – the glRotate() and glRotatef() functions multiply the current matrix by a rotation matrix.

glTranslate() – the glTranslate() and glTranslatef() functions multiply the current matrix by a translation matrix.

Scaled – It  multiplies the  current matrix by a general scaling matrix.

we use this function to set the coordinates of window.

glMatrixMode() this function specifies which matrix is the current matrix.

glLoadIdentity() the glLoadIdentity function replaces the current matrix with the identity matrix. glViewport() this function sets the viewport.

Flow chart of Flying Ball Computer Graphics Project

Flying Ball Computer Graphics Project in OpenGL Source Code – 18CSL67

Source code of Flying Ball Computer Graphics Project in OpenGL

#include<stdarg.h>
#include<GL/glut.h>
#include<stdio.h>
GLfloat x=0.5;
GLint submenu1,submenu2;
int count1=0,count2=0;

void stroke_output(GLfloat x, GLfloat y, char *format,...)
{
	va_list args;
	char buffer[200], *p;
	va_start(args, format);
	vsprintf(buffer, format, args);
	va_end(args);
	glPushMatrix();
	glTranslatef(-3.2, y, 0);
	glScaled(0.003, 0.002, 0.000);
	for (p = buffer; *p; p++)
    glutStrokeCharacter(GLUT_STROKE_ROMAN, *p);
	glPopMatrix();
}

void d4()
{
	glClear(GL_COLOR_BUFFER_BIT);
	glClearColor(0.2,0.4,0.3,1.0);
	glLoadIdentity();
	glTranslatef(0.0f,0.0f,-13.0f);
	stroke_output(-2.0, 3.0, "        INSTRUCTIONS     ");
	stroke_output(-2.0, 2.5, "              i:increment       ");
	stroke_output(-2.0, 2.0, "             d:decrement ");
	stroke_output(-2.0, 1.5, "         Press 'b' to next");
	glFlush();
	glutSwapBuffers();
}

void d6()
{

	glClear(GL_COLOR_BUFFER_BIT);
	glClearColor(1.0,0.0,1.0,1.0);
	glFlush();
	glutSwapBuffers();
}
void d7()
{

	glClear(GL_COLOR_BUFFER_BIT);
	glClearColor(0.0,1.0,1.0,1.0);
	glFlush();
	glutSwapBuffers();
}
void d8()
{

	glClear(GL_COLOR_BUFFER_BIT);
	glClearColor(0.3,1.5,0.5,1.0);
	glFlush();
	glutSwapBuffers();
}

void flying(double ang)
{
	glClear(GL_COLOR_BUFFER_BIT);
        glLoadIdentity();
        glTranslatef(0.0,-0.5,-13.0);
        glRotatef(115,1.0,0.0,0.0);

	 
	glPushMatrix();
	glRotatef(ang,0.0,0.0,1.0);
	glScaled(2.9,0.2,0.1);
	glTranslatef(0.0,0.0,10.0);
        glColor3f(0.5,0.0,0.0);
	glRotatef(ang,0.0,0.0,1.0);
	glutSolidSphere(0.7,20,60);
	glPopMatrix();
	
	
	glPushMatrix();
        glColor3f(0.0,0.0,1.0);
	glRotatef(ang,0.0,0.0,1.0);
	glScaled(0.2,2.9,0.1);
	glTranslatef(0.0,0.0,10.0);
	glRotatef(ang,0.0,0.0,1.0);
	glutSolidSphere(0.7,20,60);
	glPopMatrix();
        
        glPushMatrix();
	glRotatef(ang+50,0.0,0.0,1.0);
	glScaled(2.9,0.2,0.1);
	glTranslatef(0.0,0.0,10.0);
	glRotatef(ang+50,0.0,0.0,1.0);
        glColor3f(1.0,0.0,0.0);
	glutSolidSphere(0.7,20,60);
	glPopMatrix();

        glPushMatrix();
	glRotated(ang,0.0,1.0,0.0);
	glTranslatef(0.05,-4.0,1.0);
	glRotated(ang,0.0,1.0,0.0);
	glutSolidSphere(0.3,20,60);
	glPopMatrix();
 
        glPushMatrix();
	glColor3f(1.5,0.5,1.5);
	glRotated(ang,0.0,1.0,0.0);
	glTranslatef(0.0,1.6,0.0);
	glRotated(ang,0.1,3.0,0.0);
	glutSolidSphere(0.15,20,60);
	glPopMatrix();
	glFlush();
	glutSwapBuffers();


      
 }

 void fly()
{
	x+=6;
 
   flying(x);
}
void slow()
{
  x+=0.03;

 flying(x);
}


void fast()
{
  x+=15;
 flying(x);
}
 void fly1()
{
	x+=8;
 
   flying(x);
}
 void fly2()
{
	x+=10;
 
   flying(x);
}
 void fly3()
{
	x+=12;
 
   flying(x);
}
 void fly4()
{
	x+=14;
 
   flying(x);
}
 


 void flyd()
{
	x+=6;
 
   flying(x);
}
 void flyd1()
{
	x+=7;
 
   flying(x);
}
 void flyd2()
{
	x+=8;
 
   flying(x);
}
 void flyd3()
{
	x+=9;
 
   flying(x);
}
 void flyd4()
{
	x+=11;
 
   flying(x);
}


 void nofly()
 {
	flying(0);
 }


 void Init() 
 {
   glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(30.0,(GLfloat)500/(GLfloat)500,1.0,200.0);    
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}
 void Display()
 {
	glClear(GL_COLOR_BUFFER_BIT );
	glColor3f(1.5,0.5,1.9);
	glLoadIdentity();
	glTranslatef(0.0f,0.0f,-13.0f);
	stroke_output(-2.0, 3.0, "     * WEL COME *   ");
	stroke_output(-2.0, 2.5, "         To    ");
	stroke_output(-2.0, 2.0, "    CGV MINIPROJECT ");
	stroke_output(-2.0, 1.5, "Project Name: FLYING BALL ");
	stroke_output(-2.0, 1.0, "Group Name: PRINCES    ");
	stroke_output(-2.0, 0.5, "Project Created By:    ");
	stroke_output(-2.0, -0.1, "             Name");
	stroke_output(-2.0, -0.6, "             Name");
	stroke_output(-2.0, -1.2, " * GUIDE *    ");
	stroke_output(-2.0, -1.7, "prof.  ");
	stroke_output(-2.0, -3.3, "          press '2' to next ");
	glFlush();
	glutSwapBuffers();
    
 }
  void Menu(int id)
  {
	switch(id)
	{
	case 1:exit(0);
		break;
	}
 }
 void flying_ball(int id)
{
 switch(id)
  {
    case 2:glutIdleFunc(fly);
            glutPostRedisplay();
		break;
	case 3:glutIdleFunc(nofly);
            glutPostRedisplay();
	        break;
	case 0:glutIdleFunc(slow);
            glutPostRedisplay();
	        break;
	case 9:glutIdleFunc(fast);
            glutPostRedisplay();
	        break;
  }
}

 void Color(int id)
{
 switch(id)
  {
        case 4:glutIdleFunc(d6);
                glutPostRedisplay();
	        break;
        case 5:glutIdleFunc(d7);
	        glutPostRedisplay();
                break;
		case 6: glutIdleFunc(d8);                
                glutPostRedisplay();
	        break;
       glutSwapBuffers();
 }

}
void mykey(unsigned char key,int x,int y)
{
	
	if(key=='q'||key=='Q')
	{
		exit(0);
	}
      
	if(key=='2')
	{
	      glutIdleFunc(d4);
	}
	
	if(key=='b')
	{
		glutIdleFunc(fly);
	}
	if(key=='B')
	{
		glutIdleFunc(nofly);
	}
	 if(key=='i'||key=='I')
	{	
		count1=count1+1;
		if(count1==1)
		glutIdleFunc(fly1);
		if(count1==2)
		glutIdleFunc(fly2);
		if(count1==3)
		glutIdleFunc(fly3);
		if(count1==4)
		glutIdleFunc(fly4);

    }
    if(key=='d'||key=='D')
	{	
		count1=count1-1;
		if(count1==4)
		glutIdleFunc(flyd4);
		if(count1==3)
		glutIdleFunc(flyd3);
		if(count1==2)
		glutIdleFunc(flyd2);
		if(count1==1)
		glutIdleFunc(flyd1);
	}     
}

int main(int argc, char *argv[]) 
{


    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
    glutInitWindowSize(500,500);
    glutInitWindowPosition(0,0);
    glutCreateWindow("Flying ball");
    glutDisplayFunc(Display);   
    glutKeyboardFunc(mykey);
   submenu1= glutCreateMenu(flying_ball);
                 glutAddMenuEntry("fly",2);
                 glutAddMenuEntry("nofly",3);
	          glutAddMenuEntry("slow",0);
                  glutAddMenuEntry("fast",9);
    submenu2=glutCreateMenu(Color);
               glutAddMenuEntry("pink",4);
               glutAddMenuEntry("blue",5);
               glutAddMenuEntry("green",6);
    glutCreateMenu(Menu);
    glutAddMenuEntry("Exit",1);
    glutAddSubMenu("flying_ball",submenu1);
    glutAddSubMenu("Color",submenu2);
    glutAttachMenu(GLUT_RIGHT_BUTTON);
    Init();
    glutMainLoop();
	 
}

Screenshots of Flying Ball CGV Mini-Project

Flying Ball Computer Graphics Project Instruction Page

Flying Ball Computer Graphics Project Instruction Page

Flying Ball Computer Graphics Project Initial Page

Flying Ball Computer Graphics Project Initial Page

Flying Ball Computer Graphics Project Final Page

Flying Ball Computer Graphics Project Final Page

READ  Python program extract decimal number in the range of 00-99

The project report will be given on request. Download Source Code of Flying Ball Computer Graphics Project in OpenGL – 18CSL67. Subscribe to our YouTube channel for more videos and like the Facebook page for regular updates.

Leave a Comment

Your email address will not be published. Required fields are marked *

error: Content is protected !!