Introduction
The computer graphics (CG) mini project called “sinking ship” is
an simple project built using OpenGL, where a ship will float in
the sea. While the ship traveling in the sea in the middle an
obstacle will be placed and the ship will hit the obstacle and
the ship will sink into the sea.
The project is developed using the language “OpenGL” and the
project title is “sinking ship”. This mini project is implemented
in a simple manner and we have used simple inbuilt functions
of OpenGL and designed these objects which we have used in
the project.
The objects designed in this project are a ship and an obstacle
that looks like a stone. A ship will be traveling in the sea when
it reaches some extent an obstacle will appear in the way of
the ship and the ship will hit the obstacle and the ship will sink
soon into the sea. The controls will be specified and these
controls will be provided from the keyboard.
To move the ship into the sea the control will be on the
keyboard by pressing any key the ship will start to move in the
sea. the obstacle will appear automatically after some time in
the way of the ship and the ship will hit the obstacle and sink
in the sea soon.
.
Source code:
#include<stdio.h>
#include<GL/glut.h>
#include<stdlib.h>
#include<time.h>
void rock();
void display1();
void display2();
void display3();
void ship();
void water();
struct timespec jmp, jmp2;
GLint a=0,b=0,c=0,d=0,e=0,f=0,g=500,h=600,x=0,i=0;
GLfloat theta=0.0;
void update(int value)
a+=20.0;
glutPostRedisplay();
glutTimerFunc(200,update,0);
}
void display()
glClear(GL_COLOR_BUFFER_BIT);
display1();
if(a>950)
b+=20;
display2();
if(b==180)
jmp.tv_sec = 2;
jmp.tv_nsec = 500000000L;
nanosleep(&jmp , &jmp2);
if(b>250)
c+=10;
display3();
}
glFlush();
glutSwapBuffers();
void display1()
glPushMatrix();
glTranslated(a,75,0.0);
ship();
glPopMatrix();
water();
void display2()
glClear(GL_COLOR_BUFFER_BIT);
rock();
glPushMatrix();
glTranslated(b,75,0.0);
ship();
glPopMatrix();
water();
void display3()
glClear(GL_COLOR_BUFFER_BIT);
x-=1;
rock();
glPushMatrix();
glTranslated(c,100+x*5,0.0);
glRotated(-15,0,0,1);
ship();
glPopMatrix();
water();
void water()
glColor3f(0.196078,0.6,0.8);
glBegin(GL_POLYGON);
glVertex2f(0,0);
glVertex2f(1000,0);
glVertex2f(1000,100);
glVertex2f(0,100);
glEnd();
void ship()
glScaled(24,24,0);
glColor3f(0.329412,0.329412,0.329412); //base
glBegin(GL_POLYGON);
glVertex2f(0.5,5.0);
glVertex2f(3,1);
glVertex2f(22,1);
glVertex2f(24.0,5.0);
glEnd();
glColor3f(1.0,1.0,1.0); //ring
glBegin(GL_POLYGON);
glVertex2f(1.35,3.5);
glVertex2f(1.6,3.2);
glVertex2f(23.2,3.2);
glVertex2f(23.35,3.5);
glEnd();
glColor3f(0.329412,0.329412,0.329412); //base
glBegin(GL_POLYGON);
glVertex2f(21.0,5.0);
glVertex2f(21.0,6.0);
glVertex2f(24.5,6.0);
glVertex2f(24.0,5.0);
glEnd();
glColor3f(0.74902,0.847059,0.847059); //top-mid
glBegin(GL_POLYGON);
glVertex2f(2.0,5.0);
glVertex2f(2.0,12.0);
glVertex2f(15.0,12.0);
glVertex2f(19.5,5.0);
glEnd();
glColor3f(0.137255,0.137255,0.556863); //ring
glBegin(GL_POLYGON);
glVertex2f(2.0,8.2);
glVertex2f(2.0,8.8);
glVertex2f(17.1,8.8);
glVertex2f(17.5,8.2);
glEnd();
glColor3f(0.90,0.91,0.98); //window
glBegin(GL_POLYGON);
glVertex2f(3.0,6.0);
glVertex2f(3.0,7.2);
glVertex2f(4.0,7.2);
glVertex2f(4.0,6.0);
glEnd();
glBegin(GL_POLYGON);
glVertex2f(4.7,6.0);
glVertex2f(4.7,7.2);
glVertex2f(5.7,7.2);
glVertex2f(5.7,6.0);
glEnd();
glBegin(GL_POLYGON);
glVertex2f(8.1,6.0);
glVertex2f(8.1,7.2);
glVertex2f(9.1,7.2);
glVertex2f(9.1,6.0);
glEnd();
glBegin(GL_POLYGON);
glVertex2f(9.8,6.0);
glVertex2f(9.8,7.2);
glVertex2f(10.8,7.2);
glVertex2f(10.8,6.0);
glEnd();
glBegin(GL_POLYGON);
glVertex2f(6.4,6.0);
glVertex2f(6.4,7.2);
glVertex2f(7.4,7.2);
glVertex2f(7.4,6.0);
glEnd();
glBegin(GL_POLYGON);
glVertex2f(11.5,6.0);
glVertex2f(11.5,7.2);
glVertex2f(12.5,7.2);
glVertex2f(12.5,6.0);
glEnd();
glBegin(GL_POLYGON);
glVertex2f(13.3,6.0);
glVertex2f(13.3,7.2);
glVertex2f(14.3,7.2);
glVertex2f(14.3,6.0);
glEnd();
glBegin(GL_POLYGON);
glVertex2f(15.1,6.0);
glVertex2f(15.1,7.2);
glVertex2f(16.1,7.2);
glVertex2f(16.1,6.0);
glEnd();
glBegin(GL_POLYGON);
glVertex2f(16.9,6.0);
glVertex2f(16.9,7.2);
glVertex2f(17.9,7.2);
glVertex2f(17.9,6.0);
glEnd();
glColor3f(0.90,0.91,0.98); //window
glBegin(GL_POLYGON);
glVertex2f(3.0,9.8);
glVertex2f(3.0,11.0);
glVertex2f(4.0,11.0);
glVertex2f(4.0,9.8);
glEnd();
glBegin(GL_POLYGON);
glVertex2f(4.7,9.8);
glVertex2f(4.7,11.0);
glVertex2f(5.7,11.0);
glVertex2f(5.7,9.8);
glEnd();
glBegin(GL_POLYGON);
glVertex2f(8.1,9.8);
glVertex2f(8.1,11.0);
glVertex2f(9.1,11.0);
glVertex2f(9.1,9.8);
glEnd();
glBegin(GL_POLYGON);
glVertex2f(9.8,9.8);
glVertex2f(9.8,11.0);
glVertex2f(10.8,11.0);
glVertex2f(10.8,9.8);
glEnd();
glBegin(GL_POLYGON);
glVertex2f(6.4,9.8);
glVertex2f(6.4,11.0);
glVertex2f(7.4,11.0);
glVertex2f(7.4,9.8);
glEnd();
glBegin(GL_POLYGON);
glVertex2f(11.5,9.8);
glVertex2f(11.5,11.0);
glVertex2f(12.5,11.0);
glVertex2f(12.5,9.8);
glEnd();
glBegin(GL_POLYGON);
glVertex2f(13.3,9.8);
glVertex2f(13.3,11.0);
glVertex2f(14.3,11.0);
glVertex2f(14.3,9.8);
glEnd();
glColor3f(0.329412,0.329412,0.329412); //top-cover
glBegin(GL_POLYGON);
glVertex2f(1.5,12.0);
glVertex2f(1.5,12.5);
glVertex2f(16.0,12.5);
glVertex2f(16.0,12.0);
glEnd();
glColor3f(0.0,0.0,0.0); //chim
glBegin(GL_POLYGON);
glVertex2f(2.5,12.5);
glVertex2f(2.5,16.0);
glVertex2f(5.0,16.0);
glVertex2f(5.0,12.5);
glEnd();
glColor3f(1.0,0.25,0.0); //ring
glBegin(GL_POLYGON);
glVertex2f(2.5,12.5);
glVertex2f(2.5,13.5);
glVertex2f(5.0,13.5);
glVertex2f(5.0,12.5);
glEnd();
glColor3f(1.0,0.25,0.0); //ring
glBegin(GL_POLYGON);
glVertex2f(2.5,14.5);
glVertex2f(2.5,15.5);
glVertex2f(5.0,15.5);
glVertex2f(5.0,14.5);
glEnd();
glColor3f(0.0,0.0,0.0); //chim
glBegin(GL_POLYGON);
glVertex2f(6.0,12.5);
glVertex2f(6.0,16.0);
glVertex2f(8.5,16.0);
glVertex2f(8.5,12.5);
glEnd();
glColor3f(1.0,0.25,0.0); //ring
glBegin(GL_POLYGON);
glVertex2f(6.0,14.5);
glVertex2f(6.0,15.5);
glVertex2f(8.5,15.5);
glVertex2f(8.5,14.5);
glEnd();
glColor3f(1.0,0.25,0.0); //ring
glBegin(GL_POLYGON);
glVertex2f(6.0,12.5);
glVertex2f(6.0,13.5);
glVertex2f(8.5,13.5);
glVertex2f(8.5,12.5);
glEnd();
void rock()
{
glPushMatrix();
glTranslated(450,50,0.0);
glScaled(10,10,0);
glColor3f(0.36,0.25,0.20);
if(c>0)
glPushMatrix();
glTranslated(0,x,0);
glPushMatrix();
glTranslated(7,2,0.0);
glRotated(-x,0,0,1);
glTranslated(-7,-2,0.0);
glBegin(GL_POLYGON);
glVertex2f(8.1,1);
glVertex2f(7.89,1.23);
glVertex2f(11.26,18);
glVertex2f(13.23,21.33);
glVertex2f(14.23,2.53);
glEnd();
glPopMatrix();
glPushMatrix();
glTranslated(12,3,0.0);
glRotated(x,0,0,1);
glTranslated(-12,-3,0.0);
glBegin(GL_POLYGON);
glVertex2f(11.1,3.5);
glVertex2f(13.2,18.7);
glVertex2f(16.4,18.5);
glVertex2f(18.12,3.65);
glVertex2f(19.21,1.25);
glEnd();
glPopMatrix();
glPopMatrix();
else
glBegin(GL_POLYGON);
glVertex2f(8.1,1);
glVertex2f(7.89,1.23);
glVertex2f(11.26,18);
glVertex2f(13.23,21.33);
glVertex2f(14.23,2.53);
glEnd();
glBegin(GL_POLYGON);
glVertex2f(11.1,3.5);
glVertex2f(13.2,18.7);
glVertex2f(16.4,18.5);
glVertex2f(18.12,3.65);
glVertex2f(19.21,1.25);
glEnd();
glPopMatrix();
void myinit()
glClearColor(1.0f,1.0f,1.0f,1.0f);
glColor3f(1.0,0.0,0.0);
glPointSize(1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,999.0,0.0,799.0);
}
int main(int argc, char* argv[])
int chs;
printf("Project Developed By cseminiprojects.com\n");
printf("\n------------------------------------------------------------------\n");
printf("Steps to run this cg project\n");
printf("\n------------------------------------------------------------------\n");
printf("Step1: Type Any Key and Hit Enter\n");
printf("\n******************************************************************\n\n");
scanf("%d",&chs);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(1024.0,768.0);
glutInitWindowPosition(0,0);
glutCreateWindow("Sinking Ship");
glutDisplayFunc(display);
myinit();
glutTimerFunc(100,update,0);
glutMainLoop();
return 0;
Conclusion
These kinds of projects are simple to implement by implementing these
simple projects you will get a lot of knowledge about designing the objects
so that you can implement some more objects and effects to this simple
project. For the future implementations, you can create day mode and
night modes and sun in the day mode and moon and stars in the night
mode. Create movements for the stars while the ship is traveling in the
sea.