100% found this document useful (1 vote)
969 views8 pages

CG Mini Project - Model of A Rocket

This document contains the source code for a computer graphics project that models a 3D rocket using OpenGL and GLUT libraries. The code defines functions for initializing lighting, particles, and displaying the rocket model comprised of 3D shapes. It also includes functions for handling user input via arrow keys to control rotation and particle effects, and menus for settings like view type and particle count. The main function sets up the window and display and rendering callbacks before entering the main loop.

Uploaded by

gagankapula
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
969 views8 pages

CG Mini Project - Model of A Rocket

This document contains the source code for a computer graphics project that models a 3D rocket using OpenGL and GLUT libraries. The code defines functions for initializing lighting, particles, and displaying the rocket model comprised of 3D shapes. It also includes functions for handling user input via arrow keys to control rotation and particle effects, and menus for settings like view type and particle count. The main function sets up the window and display and rendering callbacks before entering the main loop.

Uploaded by

gagankapula
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 8

/*Program created by:

Name : Gaganpreet Singh Kapula


College : Guru Nanak Dev Engineering College , Bidar
University : Visvesvaraya Technological University , Belgaum
USN : 3GN10CS022
as a Computer Graphics Mini Project
IDE : Visual Studio 2012 Ultimate
GLUT libraries used */
#include<windows.h>
#include<gl/glut.h>
#include<stdio.h>
#define SIZEW 70.0
GLUquadricObj *theObj;
GLfloat LightAmbient[]={0.5,0.5,0.5,1.0};
GLfloat LightDiffuse[]={1.0,1.0,1.0,1.0};
GLfloat LightPosition[]={0.0,0.0,2.0,1.0};
bool scr_stt=1; // Screen State
bool light; // Lighting ON/OFF ( NEW )
bool gview; // Ortho by default
int rshp,scrn,prtl,lght,mainmenu;
int MAX_PARTICLES=1000; // Number Of Particles To Create
float i,b,a,xrot,yrot;
float g_slowdown = 2.0f; // Slow Down Particles
float g_xspeed = 0.0f; // X Rotation Speed
float g_yspeed = 0.0f; // Y Rotation Speed
GLuint g_col = 0; // Current Color Selection
typedef struct { // Create A Structure For Particle
bool active; // Active (Yes/No)
float life; // Particle Life
float fade; // Fade Speed
float r; // Red Value
float g; // Green Value
float b; // Blue Value
float x; // X Position
float y; // Y Position
float z; // Z Position
float xi; // X Direction
float yi; // Y Direction
float zi; // Z Direction
float yg; // Y Gravity
} particles; // Particles Structure
particles g_particle[100000]; // Particle Array (Room For Particle Info)
void init(void)
// Initialize Lighting
{
glClearColor(0.0,0.0,0.0,0.0);
theObj=gluNewQuadric();
glEnable(GL_LIGHTING);
glEnable(GL_DEPTH_TEST);
glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient); // Setup The Amb
ient Light
glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse); // Setup The Dif
fuse Light
glLightfv(GL_LIGHT1, GL_POSITION,LightPosition); // Position The
Light
glEnable(GL_LIGHT1);
// Enable Light One
glColorMaterial ( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE );
glEnable ( GL_COLOR_MATERIAL );
}
void init1(void)
// Initialize all Particles
{
int i;
for (i=0 ; i<MAX_PARTICLES ; i++)
{
g_particle[i].active = true;
// Make All The Particles Active
g_particle[i].life = 1.0f;
// Give All The Particles Full Life
g_particle[i].fade = float(rand()%100)/1000.0f+0.003f; // Rando
m Fade Speed
g_particle[i].yg = -1.0f;
// Set Vertical Pull Downward
}
}
void display(void)
{
glTranslatef(0,xrot,0);
glRotatef(0.12,0,b,0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//Rocket Construction
glPushMatrix();
gluQuadricDrawStyle(theObj,GLU_FILL);
glRotatef(90.0,1.0,0.0,0.0);
gluCylinder(theObj,8.0,8.0,45.0,20,100);
glPopMatrix();
glPushMatrix();
glTranslatef(0.0,10.0,0.0);
glRotatef(90.0,1.0,0.0,0.0);
gluCylinder(theObj,8.0,8.0,10.0,20,5);
glPopMatrix();
glPushMatrix();
glTranslatef(0.0,17.0,0.0);
glRotatef(90.0,1.0,0.0,0.0);
gluCylinder(theObj,5.0,8.0,7.0,20,5);
glPopMatrix();
glPushMatrix();
glTranslatef(0.0,55.0,0.0);
glRotatef(90.0,1.0,0.0,0.0);
gluCylinder(theObj,5.0,5.0,38.0,20,20);
glPopMatrix();
glPushMatrix();
glTranslatef(0,55,0);
glScaled(1,4.0,1);
glutSolidSphere(5,30,30);
glPopMatrix();
glPushMatrix();
glTranslatef(0.0,-45.0,0.0);
glRotatef(90.0,1.0,0.0,0.0);
gluCylinder(theObj,8.0,8.0,20.0,20,10);
glPopMatrix();
glPushMatrix();
glTranslatef(0.0,-50.0,0.0);
glRotatef(90.0,1.0,0.0,0.0);
gluCylinder(theObj,0.0,4.9,21.0,20,20);
glPopMatrix();
glPushMatrix();
glTranslatef(8.5,-43.0,0.0);
glRotatef(90.0,1.0,0.0,0.0);
gluCylinder(theObj,2.5,2.5,25.0,20,20);
glPopMatrix();
glPushMatrix();
glTranslatef(8.5,-43.0,0.0);
glScaled(1,4.0,1);
glutSolidSphere(2.5,20,20);
glPopMatrix();
glPushMatrix();
glTranslatef(8.5,-65.0,0.0);
glRotatef(90.0,1.0,0.0,0.0);
gluCylinder(theObj,0.0,2.5,5.0,20,20);
glPopMatrix();
glPushMatrix();
glTranslatef(-8.5,-43.0,0.0);
glRotatef(90.0,1.0,0.0,0.0);
gluCylinder(theObj,2.5,2.5,25.0,20,20);
glPopMatrix();
glPushMatrix();
glTranslatef(-8.5,-43.0,0.0);
glScaled(1,4.0,1);
glutSolidSphere(2.5,20,20);
glPopMatrix();
glPushMatrix();
glTranslatef(-8.5,-65.0,0.0);
glRotatef(90.0,1.0,0.0,0.0);
gluCylinder(theObj,0.0,2.5,5.0,20,20);
glPopMatrix();
glPushMatrix();
glTranslatef(0,-43.0,8.5);
glRotatef(90.0,1.0,0.0,0.0);
gluCylinder(theObj,2.5,2.5,25.0,20,20);
glPopMatrix();
glPushMatrix();
glTranslatef(0,-43.0,8.5);
glScaled(1,4.0,1);
glutSolidSphere(2.5,20,20);
glPopMatrix();
glPushMatrix();
glTranslatef(0.0,-65.0,8.5);
glRotatef(90.0,1.0,0.0,0.0);
gluCylinder(theObj,0.0,2.5,5.0,20,20);
glPopMatrix();
glPushMatrix();
glTranslatef(0,-43.0,-8.5);
glRotatef(90.0,1.0,0.0,0.0);
gluCylinder(theObj,2.5,2.5,25.0,20,20);
glPopMatrix();
glPushMatrix();
glTranslatef(0,-43.0,-8.5);
glScaled(1,4.0,1);
glutSolidSphere(2.5,20,20);
glPopMatrix();
glPushMatrix();
glTranslatef(0,-65.0,-8.5);
glRotatef(90.0,1.0,0.0,0.0);
gluCylinder(theObj,0.0,2.5,5.0,20,20);
glPopMatrix();
//Particle Engine
int i;
for (i=0 ; i<MAX_PARTICLES ; i++)
// Loop Through All The Particles
{
if (g_particle[i].active)
// If The Particle Is Active
{
float x=g_particle[i].x;
// Grab Our Particle X Position
float y=g_particle[i].y;
// Grab Our Particle Y Position
// Draw The Particle Using Our RGB Values, Fade The Part
icle Based On It's Life
if(g_particle[i].life>=0.3)
glColor4f(1,1,0,g_particle[i].life);
else
glColor4f(1,0,0,g_particle[i].life);
//Point source for each of the 5 thrusters
glBegin(GL_POINTS);
glVertex2f(x+0.5f,y+0.5f);
glVertex2f(x+8.5f,y+0.5f);
glVertex2f(x-8.5f,y+0.5f);
glVertex3f(x+0.5f,y-0.5f,-8.5);
glVertex3f(x+0.5f,y-0.5f,8.5);
glEnd();
g_particle[i].x += g_particle[i].xi/(g_slowdown*1000);//
Move On The X Axis By X Speed
g_particle[i].y += g_particle[i].yi/(g_slowdown*1000);//
Move On The Y Axis By Y Speed
g_particle[i].yi += g_particle[i].yg;
// Take Pull On Y Axis Into Account
g_particle[i].life -= g_particle[i].fade;
// Reduce Particles Life By 'Fade'
if (g_particle[i].life < 0.0f)
// If Particle Is Burned Out
{
g_particle[i].life = 1.0f;
// Give It New Life
g_particle[i].fade = float(rand()%100)/1000.0f+0
.003f; // Random Fade Value
g_particle[i].x = 0.0f;
// Center On X Axis
g_particle[i].y = -70.0f;
// Center On Y Axis
g_particle[i].z = 0.0f;
// Center On Z Axis
g_particle[i].xi = g_xspeed+float((rand()%60)-32
.0f); // X Axis Speed And Direction
g_particle[i].yi = g_yspeed+float((rand()%60)-30
.0f); // Y Axis Speed And Direction
g_particle[i].zi = float((rand()%60)-30.0f);// Z
Axis Speed And Direction
}
}
}
glColor3f(0.45,0.45,0.45);
glutSwapBuffers();
}
void arrow_keys ( int a_keys, int x, int y ) // Create Special Function (requir
ed for arrow keys)
{
switch ( a_keys )
{
case GLUT_KEY_UP: init1(); // When Up Arrow Is Pre
ssed...
xrot+=0.015f;
MAX_PARTICLES+=1000;
break;
case GLUT_KEY_DOWN: xrot-=0.015f; // When Down Arrow Is Pressed...
MAX_PARTICLES-=1000;
break;
case GLUT_KEY_RIGHT: b=b+1;
break;
case GLUT_KEY_LEFT: b=b-1;
break;
}
}
void reshape(int w,int h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if(gview==0)
{
if(w<=h && w>0)
glOrtho(-SIZEW,SIZEW,(GLdouble)-h/w*SIZEW,(GLdouble)h/w*
SIZEW,0.0,2*SIZEW);
else if(h>0)
glOrtho((GLdouble)-w/h*SIZEW,(GLdouble)w/h*SIZEW,-SIZEW,
SIZEW,0.0,2*SIZEW);
}
else
gluPerspective(90, (float)w/(float)h, 10.0, 190.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0,0.0,-100);
}
void menu(int value)
{
switch(value)
{
case 1:gview=0;
scr_stt==0?reshape(500,600):reshape(600,700);
break;
case 2:gview=1;
scr_stt==0?reshape(500,600):reshape(600,700);
break;
case 3:glutReshapeWindow ( 500, 600 );
scr_stt=0;
break;
case 4:glutReshapeWindow ( 600, 700 );
scr_stt=1;
break;
case 5:MAX_PARTICLES=100;
init1();
break;
case 6:MAX_PARTICLES=1000;
init1();
break;
case 7:MAX_PARTICLES=10000;
init1();
break;
case 8:MAX_PARTICLES=20000;
init1();
break;
case 9:glEnable(GL_LIGHTING);
break;
case 10:glDisable(GL_LIGHTING);
break;
case 11:MessageBox(NULL,L"The Model of a Rocket\n\nMade in OpenGL using
GLUT Libraries\n\nmade by :\n\tGaganpreet Singh Kapula\n\t(3GN10CS022)\n\tVikash
Kumar Ray\n\t(3GN10CS088)",L"About",MB_OK | MB_ICONINFORMATION);
break;
case 12:MessageBox(NULL,L"Press \"UP arrow\" key to propel\nTap it for m
ore speed and flame.\n\nUse RIGHT and LEFT arrow keys to rotate the rocket.",L"C
ontrols",MB_OK | MB_ICONQUESTION);
break;
case 13:
scr_stt=1;
xrot=0.0;
b=0;
gview=0;
glEnable(GL_LIGHTING);
glutReshapeWindow ( 600, 700 );
reshape(600,700);
MAX_PARTICLES=0;
break;
case 14:value=MessageBox(NULL,L"Do you really want to Quit?",L"Confirmat
ion",MB_OKCANCEL | MB_ICONQUESTION);
if(value==1)
exit(0);
}
}
void createmenu(void)
{
rshp=glutCreateMenu(menu);
glutAddMenuEntry("Ortho View (Default)",1);
glutAddMenuEntry("Perspective View",2);
scrn=glutCreateMenu(menu);
glutAddMenuEntry("500x600",3);
glutAddMenuEntry("600x700 (Default)",4);
prtl=glutCreateMenu(menu);
glutAddMenuEntry("100",5);
glutAddMenuEntry("1000 (Default)",6);
glutAddMenuEntry("10000",7);
glutAddMenuEntry("20000",8);
lght=glutCreateMenu(menu);
glutAddMenuEntry("Light On (Default)",9);
glutAddMenuEntry("Light Off",10);
mainmenu=glutCreateMenu(menu);
glutAddMenuEntry("About The Project",11);
glutAddMenuEntry("Controls",12);
glutAddSubMenu("Reshape View",rshp);
glutAddSubMenu("Sceen",scrn);
glutAddSubMenu("Particle No.",prtl);
glutAddSubMenu("Lighting",lght);
glutAddMenuEntry("Reset Rocket",13);
glutAddMenuEntry("Quit",14);
glutAttachMenu(GLUT_RIGHT_BUTTON);
}
void main(int argc,char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE);
glutInitWindowSize(600,700);
glutCreateWindow("The Model of a Rocket");
init();
createmenu();
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutSpecialFunc( arrow_keys );
glutIdleFunc(display);
glutMainLoop();
}

You might also like