2D Rotation Computer Graphics Project in OpenGL Source Code – 18CSL67

2D Rotation Computer Graphics Project in OpenGL Source Code – 18CSL67

Here you can download the source code of 2D Rotation Computer Graphics Project in OpenGL for academic mini-project.

Project Description of 2D Rotation CGV Mini-Project

The project idea is to display different 2D objects. This graphics package is based on the OpenGL library functions. The programming language used here is C using OpenGL libraries.

Main theme of the project is:

In this project, we designed Triangle, Quad which is placed in specific position in a plane, with specific colors and for those objects we implement rotation.

The operation implemented are:

  • Coloring.
  • Rotation.
  • Size.
  • It also illustrates how to add menus.
  • glBegin() and glEnd() functions can be used to draw the 2D objects.
  • Myreshape function is called whenever the user reshapes the openGL window.

Whenever we do right click on the window, options are available and the option we select the object will appear on the window.

Module Description of 2D Rotation CGV Mini-Project

Main( ) : In main function first we call glutInit( ) to initialise GLUT. We can set window properties properties by using APIs such as: glutInitWindowSize( ), glutInitWindowPosition( ) and glutInitDisplayMode().

READ  Twin Cars Game Computer Graphics Project in OpenGL Source Code – 18CSL67

In main Display( ), myReshape( ), myIdleFunc( ), mymenu( ), myMouse( ) these functions are called.

Display( ) : In this display function we clear the window background. Next, we set the window background color. In this function we used glRotatef( ) to rotate the object. This function used to display the graphics objects. In this we called drawPrim() and Swapbuffers( ) functions.

MyReshape( ) : This function is called whenever the user reshapes the openGL window. The glut sends the new window dimensions(x,y).

myIdleFunc( ) : This function increments the rotation variable within glutMainLoop( ).

myMenu( ) : Here we are created  a menu by using glutCreateMenu( ) function. Menu options  included here are Quit,Objects,Color.

exit( ) : exit( ) function is used to exit from window.

Sub1 : In this sub1 menu the objects is included. The objects are Triangle and Quad.

Sub2 : In this sub2 menu the size can be included. There are two options for size increament size and decreament size.

Sub3 : In this sub3 menu we included speed of rotation. The speed of rotation can be increased or decreased.

READ  Analog clock Computer Graphics Project in OpenGL Source Code – 18CSL67

Sub4 : In this sub4 menu we included color option. The colors are red,green and blue.

Sub5 : In this sub5 menu we included vertex color option. Vertex color means for each vertices we are assigning different colors.

DrawPrim( ) : This function is used to create geometric objects. The objects are Triangle and Quad.

The Triangle can be drawn using glBegin( ) and glEnd( ). The Quad is also can be drawn by using glBegin( ) and glEnd( ).

The objects are filled object so we can draw these objects by calling GL_POLYGON within in glBegin( ). In between glBegin( ) and glEnd( ) we had included vertices.

Flow Chart of 2D Rotation CGV Mini-Project

2D Rotation Computer Graphics Project in OpenGL Source Code – 18CSL67

Source Code of 2D Rotation CGV Mini-Project

#include<GL/glut.h>
#include<float.h>
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
//GLfloat ROT_INC=0.05;

GLint i,vertices=7;
GLint sub1,sub2,sub3,sub4,sub5,flag=0,flag1=0,flag2=0,flag3=0,flag4=0,flag5=0,flag6=0,flag7=0;
GLfloat m=0.0,j=0.0,o=0.0,x=0.0,y=0.0,u;
GLfloat s=0.05;

void drawTriangle(void);
static GLfloat g_rotate=0;
static GLfloat g_rotInc=0;

static void (*drawPrimP)(void)=drawTriangle;

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(1.0,0.0,1.0);
	drawstring(-0.40,0.9,0.0,"CGV MINI PROJECT");
	drawstring(-0.15,0.8,0.0,"BY");
	drawstring(-0.25,0.7,0.0,"SUNRISE");
	drawstring(-0.15,0.6,0.0,"ON");
	drawstring(-0.30,0.5,0.0,"2D-ROTATION");
	drawstring(-.80,0.3,0.0,"By:");
	drawstring(-1.0,0.2,0.0,"Name ");
	drawstring(-1.0,0.1,0.0,"Name");
	drawstring(0.2,0.2,0.0,"USN");
	drawstring(0.2,0.1,0.0,"USN");
	drawstring(-0.25,-0.1,0.0,"GUIDE");
	drawstring(-0.30,-0.2,0.0,"Prof ");
	drawstring(-1.0,-0.75,0.0,"Press 'b' to start the project");
glFlush();
}

void DrawTextXY(double x,double y,double z,double scale,char *s)
{
	int i;
	glPushMatrix();
	glTranslatef(x,y,z);
	glScalef(scale,scale,scale);
	for(i=0;i<strlen(s);i++)
	glutStrokeCharacter(GLUT_STROKE_ROMAN,s[i]);
	glPopMatrix();
}

void drawTriangle(void)
{
	glClear(GL_COLOR_BUFFER_BIT);
	if(flag4==0)
	{
	glBegin(GL_POLYGON);
		glColor3f(m,j,o);
		glVertex2f(x-s,y-s);
		glColor3f(m,j,o);
		glVertex2f(x+s,y-s);
		glColor3f(m,j,o);
		glVertex2f(x,y+s);
	glEnd();
	}
	if(flag4==1)
	{
	glBegin(GL_POLYGON);
		glColor3f(m=1.0,j=0.0,o=0.0);
		glVertex2f(x-s,y-s);
		glColor3f(m=0.0,j=1.0,o=0.0);
		glVertex2f(x+s,y-s);
		glColor3f(m=0.0,j=0.0,o=1.0);
		glVertex2f(x,y+s);
		glColor3f(m=0.0,j=0.0,o=0.0);
	glEnd();
	}
}

void drawQuad(void)
{
	glClear(GL_COLOR_BUFFER_BIT);
	if(flag5==0)
	{
	glBegin(GL_POLYGON);
		glColor3f(m,j,o);	
		glVertex2f(x-s,y-s);
		glColor3f(m,j,o);
		glVertex2f(x+s,y-s);
		glColor3f(m,j,o);
		glVertex2f(x+s,y+s);
		glColor3f(m,j,o);
		glVertex2f(x-s,y+s);
	glEnd();
	}
	if(flag5==1)
	{
	glBegin(GL_POLYGON);
		glColor3f(m=1.0,j=0.0,o=0.0);	
		glVertex2f(x-s,y-s);
		glColor3f(m=0.0,j=1.0,o=0.0);
		glVertex2f(x+s,y-s);
		glColor3f(m=0.0,j=0.0,o=1.0);
		glVertex2f(x+s,y+s);
		glColor3f(m=1.0,j=0.0,o=1.0);
		glVertex2f(x-s,y+s);
		glColor3f(m=0.0,j=0.0,o=0.0);
	glEnd();
	}	
}

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


void mymenu(GLint id)
{
	switch(id)
	{
		case 1:exit(0);
			break;
	}
	glutPostRedisplay();
}

void submenu1(int value)
{
	switch(value)
	{
		case 2:drawPrimP=drawTriangle;
			break;
		case 3:drawPrimP=drawQuad;
			break;
	}
	glutPostRedisplay();
}

void submenu2(int id)
{
	switch(id)
	{
		case 4:s=s*2;
			break;
		case 5:s=s/2;
			break;
	}
	glutPostRedisplay();
}

void submenu3(int id)
{
	switch(id)
	{
		case 6:flag4=0;
			flag5=0;
			m=1.0,j=0.0,o=0.0;
			break;
		case 7:m=0.0,j=1.0,o=0.0;
			break;
		case 8:m=0.0,j=0.0,o=1.0;
			break;
		default:m=0.0,j=0.0,o=0.0;
			break;
	}
	glutPostRedisplay();
}

void submenu4(int id)
{
	switch(id)
	{
		case 9:g_rotInc+=0.05;
			break;
		case 10:g_rotInc-=0.05;
			break;
	}
}

void submenu5(int id)
{
	switch(id)
	{
		case 11: 
			if(drawPrimP==drawTriangle)
			{
			flag4=1;
			}
			if(drawPrimP==drawQuad)
			{
			flag5=1;
			}
			break;
	}
	glutPostRedisplay();
}

void display(void)
{
	glClear(GL_COLOR_BUFFER_BIT);
	if(flag1==0)
	{
		screen();
	}
	else if(flag2==0)
	{
	glMatrixMode(GL_MODELVIEW);
	glPushMatrix();
	glRotatef(g_rotate,0.0,0.0,1.0);
	(*drawPrimP)();
	glPopMatrix();
	glColor3f(0.0,0.0,0.0);
	DrawTextXY(0.4,0.9,0.0,0.00044,"i: increase speed");
	DrawTextXY(0.4,0.8,0.0,0.00044,"d: decrease speed");
	DrawTextXY(0.4,0.7,0.0,0.00044,"a: anticlockwise");
	DrawTextXY(0.4,0.6,0.0,0.00044,"c: clockwise");
	DrawTextXY(0.4,0.5,0.0,0.00044,"p: increase size");
	DrawTextXY(0.4,0.4,0.0,0.00044,"q: decrease size");
	glutSwapBuffers();
	}
	
	glFlush();
}

void anticlockwise()
{	
	g_rotate+=g_rotInc;
	

	glutPostRedisplay();
}

void clockwise()
{

	
	g_rotate-=g_rotInc;

	
	glutPostRedisplay();
}

void key(unsigned char k,int x,int y)
{
	switch(k)
	{
	
	case 'i':if(k=='i')
			if(g_rotInc<360)
			{
				
			g_rotInc+=0.05;
			}
			else
			{
			g_rotInc=0.0;	
			}
		
			break;
	case 'd': if(k=='d')
			if(g_rotInc>0)
			{
			g_rotInc-=0.05;
			}
			if(g_rotInc<0)
			{
			g_rotInc=0.0;	
			}
	        break;
	case 'b':flag=1;
			 flag1=1;
			 break;
	case 'a':if(k=='a')
		 glutIdleFunc(anticlockwise);
			 break;
	case 'c':if(k=='c')
			glutIdleFunc(clockwise); 
				break;
	case 'p':if(k=='p')
			s=s*2;
				break;
	case 'q':if(k=='q')
			s=s/2;
				break;
	}
}

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

int main(int argc,char **argv)
{
	glutInit(&argc,argv);
	glutInitWindowPosition(0,0);
	glutInitWindowSize(600,600);
	glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE);
	glutCreateWindow("mywindow");
	glutDisplayFunc(display);
	glutKeyboardFunc(key);
	glutIdleFunc(myidle);
	glutReshapeFunc(Reshape);

	sub1=glutCreateMenu(submenu1);
	glutAddMenuEntry("Triangle",2);
	glutAddMenuEntry("Quad",3);

	sub2=glutCreateMenu(submenu2);
	glutAddMenuEntry("Inc_size",4);
	glutAddMenuEntry("Dec_size",5);	

	sub3=glutCreateMenu(submenu3);
	glutAddMenuEntry("Red",6);
	glutAddMenuEntry("Green",7);
	glutAddMenuEntry("Blue",8);

	sub4=glutCreateMenu(submenu4);
	glutAddMenuEntry("Inc_speed",9);
	glutAddMenuEntry("Dec_speed",10);

	sub5=glutCreateMenu(submenu5);
	glutAddMenuEntry("vertex_color",11);

	glutCreateMenu(mymenu);
	glutAddMenuEntry("Quit",1);
	glutAddSubMenu("objects",sub1);
	glutAddSubMenu("size",sub2);
	glutAddSubMenu("color",sub3);
	glutAddSubMenu("speed",sub4);
	glutAddSubMenu("vertex_color",sub5);

	glutAttachMenu(GLUT_RIGHT_BUTTON);
	glClear(GL_COLOR_BUFFER_BIT);
	glClearColor(1.0,1.0,1.0,1.0);
	glColor3f(0.0,0.0,0.0);
	glutMainLoop();
	return 0;
}

Screenshots of 2D Rotation CGV Mini-Project

Initial Position with all options

Initial Position with all options

After Rotation with options

After Rotation with options

READ  17CS32 Analog and Digital Electronics Module 4 and 5

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