Fog Test Computer Graphics Project in OpenGL Source Code – 18CSL67

Fog Test Computer Graphics Project in OpenGL Source Code – 18CSL67

Here you can download source code of Fog Test Computer Graphics Project in OpenGL for academic min-project.

Project Description:

In the modern world, there is no field without computers, because of its easy use and efficient working one can get the work done easily whenever and where ever required. It is all because of the portability that the applications provide to the computers. In the same way, the graphics are the major advantage of the computers which has made its presence so worthy that there is no field in computers without the graphics being used, thus in any display system or any application we look into there will be graphics.

Graphics has found its application in:

  • Scientific applications
  • Simulations
  • Designing
  • Animations
  • Entertainment and so on…

This package i.e. FOG TEST defines all about the determining effects of fog and lighting. In this project I’m used light sources i.e. ambient light means uniform lighting. The surface of the object is specified as specular (shiny surface) and diffuse (uniform scattering of light). The perspective view is used.

READ  15ME53 Turbo Machine VTU CBCS Notes

In this project i.e. FOG TEST I’m implementing the graphics operations like translation, rotating, and scaling. 3-D objects are used. On these objects, the effects of fog are shown.  Translation, rotation and scaling are done by glTranslatef(), glRotatef() and glScalef() functions respectively.

Module Description of Fog Test Computer Graphics Project

myinit: It is used to initialize the projection matrix, identity matrix, modelview matrix and lighting effects.

Draw : In this function I’m used different colors to draw the objects specified.

In this we have four different cases:

case 1 Teapot is drawn by using red color.

In case 2 Tetrahedron is drawn by using purple color.

case 3 Dodecahedron sky blue color.

case 4 pipe is drawn by specifying points and colored green.

Special key: This function is used for rotating the defined objects in the project. This function is used to specify the keyboard arrow keys for the rotation of the objects.

First case up arrow is used for upward rotation of object.

 Second case down arrow is used for downward rotation of object.

In third case right arrow is used for right rotation of object

READ  15CS54 Automata Theory and Computability

Fourth case left arrow is used for left rotation of object

Key: This function is used for Fog effects on the defined objects in the project. In this function, I’m specified keyboard keys for seeing the effects of the fog on objects.

In first case “d” is used for increase the fog density on object.

second case “D” is used for decrease the fog density on object.

Flow Diagram of Fog Test Computer Graphics Project

Fog Test Computer Graphics Project in OpenGL Source Code – 18CSL67

Source Code of Fog Test Computer Graphics Project

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <GL/glut.h>

float rotX = 0.0, rotY = 0.0, zTranslate = -65.0;
float fogDensity = 0.02;
int i,j=0;
GLfloat fog_color[]={1.0,1.0,1.0,1.0};
GLenum doubleBuffer;

void Key(unsigned char key, int x, int y)
{
switch (key) 
  {
case 'd':fogDensity *= 1.10;  
	glFogf(GL_FOG_DENSITY, fogDensity);
	glutPostRedisplay();
	break;
case 'D':fogDensity /= 1.10;  
	glFogf(GL_FOG_DENSITY, fogDensity);
	glutPostRedisplay();
	break;
case 27:exit(0);
  } 
}

void Myinit(void)
{	
GLfloat light_ambient[]={0.3, 0.3, 0.3, 1.0};
GLfloat light_diffuse[]={6.0, 6.0, 6.0, 1.0};
GLfloat light_specular[]={1.0, 1.0, 1.0, 1.0};
GLfloat light_position[]={1.0, 1.0, 0.0, 0.0};

glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);

glEnable(GL_LIGHT0);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, 1.0, 1.0, 200.0);
glMatrixMode(GL_MODELVIEW);
}

void SpecialKey(int key, int x, int y)
{
     switch (key)
  {
	case GLUT_KEY_UP:
		rotX -= 5;     
		glutPostRedisplay();
		break;
	case GLUT_KEY_DOWN:
		rotX += 5;    
		glutPostRedisplay();
		break;
	case GLUT_KEY_LEFT:
		rotY -= 5;    
		glutPostRedisplay();
		break;
	case GLUT_KEY_RIGHT:
		rotY += 5;
		glutPostRedisplay();
		break;
  }
}

void main_menu(int value)
{
switch (value)
  {
	case 1:i=0;
	glutPostRedisplay();
break;
	case 2:i=1;
		glutPostRedisplay();
	break;
	case 3:i=2;
	glutPostRedisplay();
		break;
	case 4:exit(0);
break;
  }
}

void Draw(void)
{
    float red[]={1.0, 0.0, 0.0, 1.0};
    float grey[]={0.8, 0.5, 1.0, 1.0};
   float green[]={0.0, 0.8, 0.0, 1.0};
    float blue[]={0,1,1};

  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

 glPushMatrix();
 glTranslatef(0, 0, zTranslate);
 glRotatef(rotY, 0, 1, 0);
 glRotatef(rotX, 1, 0, 0);
 glScalef(2, 2, 2);
  if(i==0)
  {
	glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, red);
	glutSolidTeapot(3);                                                    
  }
else if(i==1)
  {
	glScalef(5,5,5);
	glRotatef(90.0, 1.0, 0.0, 0.0);
	glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, grey);
	glutSolidTetrahedron();                                
  }
else if(i==2)
  {
	glScalef(2,2,2);glRotatef(90.0, 1.0, 0.0, 0.0);
	glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, blue);
	glutSolidDodecahedron();                          
  }

glPopMatrix();
if (doubleBuffer) 
  {
	glutSwapBuffers();
  } 
else
  {
	glFlush();
  }	
}

void Menu()
{
  glutCreateMenu(main_menu);
  glutAddMenuEntry("Teapot", 1);
  glutAddMenuEntry("Tetrahedron", 2);
  glutAddMenuEntry("DodecaHedron", 3);
  glutAddMenuEntry("Quit", 4);
  glutAttachMenu(GLUT_RIGHT_BUTTON);
}


void fog()
{
   glEnable(GL_FOG);
   glFogi(GL_FOG_MODE, GL_EXP);
  glFogf(GL_FOG_DENSITY, fogDensity);
  glFogfv(GL_FOG_COLOR, fog_color);	
  glClearColor(1.0, 1.0, 1.0, 1.0);
}

int main(int argc, char **argv)
{
  GLenum type;
  glutInit(&argc, argv);

 type=GLUT_RGB | GLUT_DEPTH;
  type|=(doubleBuffer) ? GLUT_DOUBLE:GLUT_SINGLE;
  glutInitDisplayMode(type);
  glutInitWindowSize(500, 500);
  glutCreateWindow("Fog Test");
  glFrontFace(GL_CW);
  glClearColor(1.0, 1.0, 1.0, 1.0);
 Myinit();
 fog();  
 Menu();
 glutKeyboardFunc(Key);
 glutSpecialFunc(SpecialKey);
 glutDisplayFunc(Draw);
 glutMainLoop();
 return 0;            
}

Screenshots of Fog Test CGV mini-Project

Increase and decrease of fog on objects and rotation

Increase and decrease of fog on objects and rotation

Seletcing Dodecahedron from menu object is displayed as above

Seletcing Dodecahedron from menu object is displayed as above

By selecting Tetrahedron from the menu the object is displayed as above

By selecting Tetrahedron from the menu the object is displayed as above.

READ  Bricks breaker Computer Graphics Project in OpenGL Source Code – 18CSL67

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