Rotating Circle Computer Graphics Project in OpenGL Source Code – 18CSL67

Rotating Circle Computer Graphics Project in OpenGL Source Code – 18CSL67

Here you can download the source code of Rotating Circle Computer Graphics Project in OpenGL for academic mini-project.

Computer Graphics Overview

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.

Computer graphics today is largely interactive: The user controls the contents, structure, and appearance of the objects and of their displayed images by using input devices, such as a keyboard, mouse, or touch-sensitive panel on the screen. Because of the close relationship between the input devices and the display, the handling of such devices is included in the study of computer graphics.

READ  Dora Catch Game Computer Graphics Project in OpenGL Source Code – 18CSL67

The applications of computer graphics may be divided into four major areas.

1. Display of information

2. Design

3. Simulation and Animation

4. User Interfaces.

Project Description of Rotating Circle CGV Project

The main idea behind this project is to display a rotating circle with computer graphics. This graphics package is based on the OpenGL library functions. The programming language used here is C using OpenGL libraries. In this project, we are demonstrating the circle filled with different colors and displayed at the center and initially rotating in an anti-clockwise direction.

Using keyboard buttons we are rotating the circle in the clockwise direction and in an anti-clockwise direction along with that we are increasing rotation speed and decreasing rotation speed. We added some more functions that are adding triangle as well as removing, and rotating circles around the origin.

Flow Diagram of Rotating Circle CGV Project

The Flow diagram of Rotating Circle Computer Graphics Project in OpenGL is shown below.

Rotating Circle Computer Graphics Project in OpenGL Source Code – 18CSL67

FLow Diagram of Rotating Circle CGV Mini-Project

READ  Additive Color Model Computer Graphics Project in OpenGL – 18CSL67

Source Code of Rotating Circle CGV Project

#include<GL/glut.h>
#include<math.h>
#include<stdio.h> 
#define ROT_INC 0.05


int i,j;
int triangles=10;
int xDir = 1;//1-right,0-left
GLfloat r=15,flag1=0,a=5;
GLfloat twoPi=2.0*3.142;
GLfloat x=0,y=0,xTrans=0.0,yTrans=0.0,zTrans=0.0,xRot=0.0,yRot=0.0,zRot=0.0;
//float xTrans = 0.0;//keeps track of X translation
GLint flag2=0,flag3=0;
static GLfloat g_rotate=0;
static GLfloat g_rotInc=ROT_INC;

void circle();
void anticlockwise();
void clockwise();
void keyboard(unsigned char key,int x,int y);
void reshape();
void mouse(int btn, int state, int x, int y);
void Transform(float xTrans, float yTrans, float zTrans, float xRot, float yRot, float zRot);

void drawstring(float x,float y,float z,char *string)
{
 char *c;
 glRasterPos3f(x,y,z);
 for(c=string;*c!='\0';c++)
 {
  glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,*c);
 }
}
void screen()
{
	glColor3f(0.0,0.0,1.0);
	drawstring(-25,40,0.0,"CGV Lab Mini Project  ");
	drawstring(-25,35,0.0,"On Rotating Circle ");
	glColor3f(1.0,0.0,0.0);
	drawstring(-20,25,0.0,"Group Name:");
	drawstring(-23,20,0.0,"Innovating Group");
	glColor3f(0.5,0.0,0.5);
	drawstring(-5,10,0.0,"By:");
	drawstring(-35,5,0.0,"Name and USN");
	drawstring(-45,0,0.0,"Name and USN");
	glColor3f(0.0,0.0,0.0);
	drawstring(-25,-10,0.0,"Under The Guidance of:");
	drawstring(-20,-15,0.0,"Prof:  ");
	glColor3f(rand()%2,rand()%2,0.0);
	drawstring(10,-45,0.0,"Press 'e' to start");
glFlush();
}

void keyboard(unsigned char key, int x, int y)
{
   switch (key) 
   {
   	case 'e': flag3=1;
				break;
	case 'i':g_rotInc+=ROT_INC;
				break;
	case 'd':g_rotInc-=ROT_INC;
				break;
	case 'a':glutIdleFunc(anticlockwise);
				break;
	case 'c':glutIdleFunc(clockwise);
				break;
	case 't':triangles=triangles+a;
				break;
	case 'T':triangles=triangles-a;
				break;
	}
}

void display()
{
	glClear(GL_COLOR_BUFFER_BIT);
	glClearColor(1.0,1.0,1.0,1.1);
	if(flag2==0)
	{
		screen();
	}
	if(flag3==1)
	{
		flag2=1;
	glColor3f(1.0,0.0,0.0);
	circle();
	glLoadIdentity();
	glPushMatrix();
	glRotatef(g_rotate,0.0,0.0,1.0);
	if(flag1==1)
	{
	Transform(-5.0f,0.0f,0.0f,0.0f,0.0f,g_rotate);	
	}
	glFlush();
	}
}

void Transform(float xTrans, float yTrans, float zTrans, float xRot, float yRot, float zRot)
{
	 glTranslatef(-5.0f,0.0f,0.0f);
   // glLoadIdentity();//load identity matrix
	xTrans=-25.0f,yTrans=0.0f,zTrans=0.0f;
    glTranslatef(xTrans,yTrans,zTrans);//move forward 4 units
    
    //checking if polygon can move right along x-axis
    /*if(xDir==1 && xTrans<50.0f)
    {
        xTrans+=0.1f;//increment xTrans
    }
    else//polygon has reached the right edge
    {
        xDir=0;//change direction
    }
    glRotatef(-g_rotate,0,0,1);
    
   if(xDir==0 && xTrans<-50.0f)//checking if polygon can move left along x-axis
    {
        xTrans-=0.1f;//decrement xTrans
    }             
    else//polygon has reached the left edge
    {
        xDir=1;//change direction
    }*/
	//glTranslatef(xTrans, yTrans, zTrans);
    glRotatef(g_rotate,0,0,1);
}

void mouse(int btn, int state, int x, int y)
{
	if(btn==GLUT_LEFT_BUTTON && state==GLUT_DOWN) 
	{
	flag1=1;
    glutPostRedisplay();
	}
	
}


void circle()
{
glBegin(GL_TRIANGLE_FAN);
glVertex2f(0,0);
for(i=0;i<=triangles;i++)
{
	x=r*cos(i*twoPi/triangles);
	y=r*sin(i*twoPi/triangles);
glVertex2f(x,y);
}
for(j=0;j<=triangles;j++)
{
	x=r*cos(j*twoPi/triangles);
	y=r*sin(j*twoPi/triangles);
glVertex2f(x,y);
if(j%10==0)
glColor3f(1,0,0);
if(j%10==1)
glColor3f(0,1,0);
if(j%10==2)
glColor3f(0,0,1);
if(j%10==3)
glColor3f(1,1,0);
if(j%10==4)
glColor3f(1,0,1);
if(j%10==5)
glColor3f(0,1,1);
if(j%10==6)
glColor3f(0,0,0);
if(j%10==7)
glColor3f(1,1,1);
if(j%10==8)
glColor3f(0.5,0,1);
if(j%10==9)
glColor3f(0.5,0.5,0);
}
glEnd();
glFlush();
}

void anticlockwise()
{
	g_rotate+=g_rotInc;
	glutPostRedisplay();
}

void clockwise()
{
	g_rotate-=g_rotInc;
	glutPostRedisplay();
}

void myidle(void)
{
	g_rotate+=g_rotInc;
	//pos=pos+0.01;
	glutPostRedisplay();
}

void reshape(int w,int h)
{
	glViewport(0,0,w,h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	if(w>h)
	gluOrtho2D((GLfloat)w/(GLfloat)h*(-50),(GLfloat)w/(GLfloat)h*(50),-50,50);
	else
	gluOrtho2D(-50,50,(GLfloat)h/(GLfloat)w*(-50),(GLfloat)h/(GLfloat)w*(50));
	glMatrixMode(GL_MODELVIEW);
	glutPostRedisplay();
}

void main(int argc, char **argv)
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE);
	glutInitWindowPosition(0,0);
	glutInitWindowSize(500,500);
    glutCreateWindow("rotating_circle");
	glutDisplayFunc(display);
	glutMouseFunc(mouse);
	glutIdleFunc(myidle);
	glutKeyboardFunc(keyboard);
	glutReshapeFunc(reshape);
	glutMainLoop();
}	

Screenshots of Rotating Circle CGV Project

Initial Position of Rotating Circle CGV Project

Initial Position of Rotating Circle CGV Project

After adding triangle of Rotating Circle CGV Project

After adding triangles of Rotating Circle CGV Project

After deleting triangles of Rotating Circle CGV Project

After deleting triangles of Rotating Circle CGV Project

READ  3D House Computer Graphics Project in OpenGL Source Code

The project report will be given on request. Download Source Code of Rotating Circle 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 *