Skip to main content

Sinking Ship (Titanic) Computer graphics project using OpenGL

 #include<stdio.h>

#include<GL/glut.h>
#include<stdlib.h>
#include<GL/freeglut.h>
#include<time.h>
#include<bits/stdc++.h>
#define WIDTH 768
#define HEIGHT 1024
using namespace std;

typedef struct {
GLfloat x;
GLfloat y;
GLfloat z;
} vec3f;

const vec3f _FONT_SIZE_NORMAL = {0.5f, 0.5f, 1.0f};
const vec3f _FONT_SIZE_BIG = {1.0f, 1.0f, 1.0f};
const vec3f _FONT_SIZE_SMALL = {0.25f, 0.25f, 1.0f};

const vec3f *FONT_SIZE_NORMAL = &_FONT_SIZE_NORMAL;
const vec3f *FONT_SIZE_BIG = &_FONT_SIZE_BIG;
const vec3f *FONT_SIZE_SMALL = &_FONT_SIZE_SMALL;

struct timespec tim, tim2;
void sun();
void ice();
void sky();
void display();
void display0();
void display1();
void display2();
void display3();
void display4();
void ship(int, double);
void rescueShip(int, double);
void drawStickMan();
void water();

vector<pair<int, int> > starpoints;
bool KEY_PRESSED = false;
GLint a=0,b=0,c=120,d=0, z = 1200,e=0,f=0,g=500,h=600,x=0,i=0;
float p = 0, q=0, r=0;

GLfloat theta=0.0;

void initVariables()
{
a=0,b=0,c=120,d=0, p = 0,q = 0, r=0,z = 1200,e=0,f=0,g=500,h=600,x=0,i=0;
}
void normalKeys(unsigned char key, int x, int y) {
switch (key) {
case 27: // ESC key
exit(EXIT_SUCCESS);
case 13: // Enter key
{
initVariables();
KEY_PRESSED = true;
}
break;

default:
break;
}
}

void drawStrokeText(std::string text, int x, int y, int z, const vec3f *fontSize) {

glPushMatrix();

glTranslatef(x, y, z);
glScalef(fontSize->x, fontSize->y, fontSize->z);
glutStrokeString(GLUT_STROKE_ROMAN, (unsigned char const *) text.c_str());

glPopMatrix();
}

void update(int value)
{
a+=20.0;
e-=10;
p-=0.01;
q-=0.01;
glutPostRedisplay();
glutTimerFunc(200,update,0); //speed of ship 200
}

void display()
{
glClear(GL_COLOR_BUFFER_BIT);

display0();
if(KEY_PRESSED == true)
display1(); //DRAW SHIP IN INITIAL POSITION AND WATER


if(a>950 && KEY_PRESSED == true) //SHIFT TO NEW SCREEN
{
b+=20;
display2();
}
if(b==180 && KEY_PRESSED == true)
{
tim.tv_sec = 1.5; //crash pause time
tim.tv_nsec = 500000000L;
// nanosleep(&tim , &tim2);
}
if(b>180 && KEY_PRESSED == true)
{
c+=10;
display3();

}
if(c>700 && KEY_PRESSED == true)
{
z-=20;
display4();
}
if(z <-100 && KEY_PRESSED == true)
exit(EXIT_SUCCESS);

glFlush();
glutSwapBuffers();
}
void display0() {

glClear(GL_COLOR_BUFFER_BIT);
glClearColor(1.0f,1.0f,1.0f,1.0f);

glLineWidth(3);
glColor3f(0.0,0.0,1.0);
drawStrokeText("SINKING SHIP", WIDTH/3, HEIGHT/2, 0, FONT_SIZE_NORMAL);

glLineWidth(1);
drawStrokeText("PRESS ENTER TO START", WIDTH/2.5, HEIGHT/2.5, 0, FONT_SIZE_SMALL);
drawStrokeText("Press Esc to Quit", WIDTH/2.5, HEIGHT/3.4, 0, FONT_SIZE_SMALL);


}
void display4()
{
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(0.2f,0.18f,0.2f,1.0f);
sky();

glPushMatrix();
glTranslated(z,75,0.0);
rescueShip(1, 1); //1 = night
glPopMatrix();
water();
}
void display1()
{


glClearColor(1.0f + q,0.6f + p,0.0f,1.0f);
//startScreen();
glPushMatrix();
glTranslated(0,e,0.0);
sun();
glPopMatrix();
glPushMatrix();
glTranslated(a,75,0.0);
ship(0, 2); //0 = day
glPopMatrix();
water();
}

void display2()
{

glClear(GL_COLOR_BUFFER_BIT);
glClearColor(0.2f,0.18f,0.2f,1.0f);
sky();

ice();
glPushMatrix();
glTranslated(b,75,0.0);
ship(1, 2); //1 = night
glPopMatrix();
water();

}

void display3()
{
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(0.2f,0.18f,0.2f,1.0f);
x-=1; //slope of sinking ship

//stars();
sky();
ice();
glPushMatrix();
glTranslated(c,100+x*5,0.0); //sinking ship axis
glRotated(-15,0,0,1);
ship(1,2);
glPopMatrix();
water();


}

/* DRAW WATER */
void water()
{

glColor3f(0.0,0.0,1.0);
glBegin(GL_POLYGON);
glVertex2f(0,0);
glVertex2f(1000,0);
glVertex2f(1000,100);
glVertex2f(0,100);
glEnd();
}

void drawRailing(vector<double> xx, vector<double> yy, double inc, double col)
{
glColor3f(col, col, col);
glBegin(GL_LINE_LOOP);
for(int i=0;i<6;i++)
{
glVertex2f(xx[i] + inc, yy[i]);
}

glEnd();
}

void drawStickMan()
{
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glBegin(GL_POLYGON);
glColor3f( 0.0f, 0.0f, 0.0f);
int cx1 = 6, cy1 = 11, r1 = 1, num_segments=1000;
for (int ii = 0; ii < num_segments; ii++)
{
float alpha = 2.0f * 3.1415926f * float(ii) / float(num_segments); ///get the current angle
float x = r1 * cosf(alpha); ///calculate the x component
float y = r1 * sinf(alpha); ///calculate the y component
glVertex2f(x + cx1, y + cy1); ///output vertex
}
glEnd();
/// Player's Face Boundary
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glBegin(GL_POLYGON);
glColor3f( 0.96f, 0.80f, 0.69f);
cx1 = 6, cy1 = 11, r1 = 1, num_segments=1000;
for (int ii = 0; ii < num_segments; ii++)
{
float alpha = 2.0f * 3.1415926f * float(ii) / float(num_segments); ///get the current angle
float x = (r1-0.1) * cosf(alpha); ///calculate the x component
float y = (r1-0.1) * sinf(alpha); ///calculate the y component
glVertex2f(x + cx1, y + cy1); ///output vertex
}
glEnd();

glColor3f(0.0,0.0,0.0);
/// Player's Hand
glBegin(GL_LINES);
glVertex2f(5,8);
glVertex2f(6,10);
glEnd();

glBegin(GL_LINES);
glVertex2f(6,10);
glVertex2f(7,8);
glEnd();
//playerbody
glBegin(GL_LINES);
glVertex2f(6,10);
glVertex2f(6,6.5);
glEnd();

//playerrightleg
glBegin(GL_LINES);
glVertex2f(5,5.5);
glVertex2f(6,6.5);
glEnd();

glBegin(GL_LINES);
glVertex2f(6,6.5);
glVertex2f(7,5.5);
glEnd();

}
void drawWindows(vector<double> xx, vector<double> yy, double inc, int flag)
{
if(flag == 0)
{
glBegin(GL_LINE_LOOP);
glColor3f(1.0,1.0,1.0);
}
else
{
glColor3f(1,1,0);
glBegin(GL_POLYGON);
}
for(int i=0;i<4;i++)
{
glVertex2f(xx[i] + inc, yy[i]);
}

glEnd();
}

void drawDeck(double x, double y)
{
glColor3f(0.69,0,0);
glBegin(GL_POLYGON);
glVertex2f(x,y);
glVertex2f(x,y+1.5);
glVertex2f(x+14,y+1.5);
glVertex2f(x+14,y);
glEnd();
}
void drawChimney(double x, double y)
{
glColor3f(0.0,0.0,0.0);
glBegin(GL_POLYGON);
glVertex2f(x,y);
glVertex2f(x,y+3);
glVertex2f(x+1.5,y+3);
glVertex2f(x + 1.5, y);
glEnd();
glColor3f(0.545, 0.271, 0.075); //brown
glBegin(GL_POLYGON);
glVertex2f(x,y+3);
glVertex2f(x,y+3.5);
glVertex2f(x+1.5,y+3.5);
glVertex2f(x+1.5,y+3);
glEnd();
}
//rescue ship
void rescueShip(int flag, double scale)
{


glScaled(10*scale,10*scale, 0);
//ship base
glColor3f(0.0,0.0,0.0);
glBegin(GL_POLYGON);
glVertex2f(1,5.5);
glVertex2f(4,1);
glVertex2f(19,1);
glVertex2f(21.5,5.5);
glEnd();
//door
glColor3f(0.545, 0.271, 0.075);
glBegin(GL_POLYGON);
glVertex2f(4,5.5);
glVertex2f(4,8);
glVertex2f(5,8);
glVertex2f(5,5.5);
glEnd();
//light
glColor4f(1, 1, 0.0, 0.5);
glBegin(GL_POLYGON);
glVertex2f(4,7.5);
glVertex2f(4,8);
glVertex2f(-7,1);
glVertex2f(-3,1);
glEnd();

drawStickMan();
//railing down
vector<double> xx,yy;
xx.push_back(1.5);
xx.push_back(1.5);
xx.push_back(2);
xx.push_back(3);
xx.push_back(3.5);
xx.push_back(3.5);

yy.push_back(5.5);
yy.push_back(6.5);
yy.push_back(6.2);
yy.push_back(6.2);
yy.push_back(6.5);
yy.push_back(5.5);
for(int i=0;i<10; i++)
{
drawRailing(xx, yy, i*2, 0);
}
}
/* TO DRAW SHIP */
void ship(int flag, double scale)
{


glScaled(10*scale,10*scale, 0);
//ship mid
glColor3f(0.5,0.5,0.5);
glBegin(GL_POLYGON);
glVertex2f(3.5,5.5);
glVertex2f(3.5,8.5);
glVertex2f(19.5,8.5);
glVertex2f(19.5,5.5);
glEnd();
//ship base
glColor3f(0.0,0.0,0.0);
glBegin(GL_POLYGON);
glVertex2f(1,5.5);
glVertex2f(4,1);
glVertex2f(19,1);
glVertex2f(21.5,5.5);
glEnd();
//door
glColor3f(0.545, 0.271, 0.075);
glBegin(GL_POLYGON);
glVertex2f(4,5.5);
glVertex2f(4,8);
glVertex2f(5,8);
glVertex2f(5,5.5);
glEnd();
//upper windows
if(flag == 0)
glColor3f(0.9,0.9,0.9);
else
glColor3f(1, 1, 0);

glBegin(GL_POLYGON);
glVertex2f(7,7.5);
glVertex2f(7,8);
glVertex2f(10,8);
glVertex2f(10,7.5);
glEnd();
glBegin(GL_POLYGON);
glVertex2f(11.5,7.5);
glVertex2f(11.5,8);
glVertex2f(15,8);
glVertex2f(15,7.5);
glEnd();
glBegin(GL_POLYGON);
glVertex2f(16,7.5);
glVertex2f(16,8);
glVertex2f(19,8);
glVertex2f(19,7.5);
glEnd();
//upper deck

drawDeck(4.5, 8.5);

double x = 5.5,y=10;

for(int i=0; i<4; i++)
{
drawChimney(x + i*3, y);
}

//railing down
vector<double> xx,yy;
xx.push_back(1.5);
xx.push_back(1.5);
xx.push_back(2);
xx.push_back(3);
xx.push_back(3.5);
xx.push_back(3.5);

yy.push_back(5.5);
yy.push_back(6.5);
yy.push_back(6.2);
yy.push_back(6.2);
yy.push_back(6.5);
yy.push_back(5.5);
for(int i=0;i<10; i++)
{
drawRailing(xx, yy, i*2, 0);
}
//window coordinates
xx.clear();
yy.clear();
xx.push_back(4.5);
xx.push_back(4.5);
xx.push_back(5.5);
xx.push_back(5.5);

yy.push_back(3.5);
yy.push_back(4.5);
yy.push_back(4.5);
yy.push_back(3.5);

for(int i=0;i<6; i++)
{

drawWindows(xx, yy, i*2.5, flag);
}
//upper railing
xx.clear();
yy.clear();
xx.push_back(5);
xx.push_back(5);
xx.push_back(5.5);
xx.push_back(6.5);
xx.push_back(7);
xx.push_back(7);

yy.push_back(10);
yy.push_back(11);
yy.push_back(10.8);
yy.push_back(10.8);
yy.push_back(11);
yy.push_back(10);
for(int i=0;i<6; i++)
{
drawRailing(xx, yy, i*2, 0.1);
}

}
void sun()
{
glPushMatrix();
glTranslated(700, 500,0.0);
float cx = 100, cy = 100, r = 50;

int num_segments = 100;

glClear(GL_COLOR_BUFFER_BIT);

glColor3f(1.0, 1.0, 0);

glBegin(GL_TRIANGLE_FAN);

for(int ii = 0; ii < num_segments; ii++)

{

float theta = 2.0f * 3.1415926f * float(ii) /

float(num_segments);//get the current angle

float x = r * cosf(theta);//calculate the x

float y = r * sinf(theta);//calculate the y
glVertex2f(x + cx, y + cy);//output vertex

}
glEnd();

glPopMatrix();

}
void sky()
{
glClear(GL_COLOR_BUFFER_BIT);
//moon
glPushMatrix();
glTranslated(700, 500,0.0);
float cx = 100, cy = 100, r = 50;

int num_segments = 100;

//glClear(GL_COLOR_BUFFER_BIT);

glColor3f(1.0, 1.0, 1.0);

glBegin(GL_TRIANGLE_FAN);

for(int ii = 0; ii < num_segments; ii++)

{

float theta = 2.0f * 3.1415926f * float(ii) /

float(num_segments);//get the current angle

float x = r * cosf(theta);//calculate the x

float y = r * sinf(theta);//calculate the y
glVertex2f(x + cx, y + cy);//output vertex

}
glEnd();

glPopMatrix();



//stars
for(int i=0;i<10;i++) {

float tx = (rand()%950) + 10, ty = 450 + rand()%400, R = 2;

glColor3f(1.0, 1.0, 1.0);

glBegin(GL_TRIANGLE_FAN);

for(int ii = 0; ii < num_segments; ii++)

{

float theta2 = 2.0f * 3.1415926f * float(ii) /

float(num_segments);//get the current angle

float x = R * cosf(theta2);//calculate the x

float y = R * sinf(theta2);//calculate the y
glVertex2f(x + tx, y + ty);//output vertex


}
glEnd();
//glPopMatrix();

}


}

/* TO DRAW ICEBERG */
void ice()
{
glPushMatrix();
glTranslated(450,50,0.0);
glScaled(10,10,0);
glColor3f(0.0,1.0,1.0);
if(c>120)
{
//ice after crash
glPushMatrix();
glTranslated(0,x,0); //sinking iceberg

glPushMatrix();
glTranslated(7,2,0.0); //translate + rotate - translate about base of iceberg
glRotated(-x,0,0,1);
glTranslated(-7,-2,0.0);
glBegin(GL_POLYGON);
glVertex2f(7,2);
glVertex2f(8,3);
glVertex2f(11,18);
glVertex2f(12,19);
glVertex2f(12,3);
glEnd();
glPopMatrix();

glPushMatrix();
glTranslated(12,3,0.0);
glRotated(x,0,0,1);
glTranslated(-12,-3,0.0);
glBegin(GL_POLYGON);
glVertex2f(12,3);
glVertex2f(12,19);
glVertex2f(17,18);
glVertex2f(18,3);
glVertex2f(19,3);
glEnd();
glPopMatrix();

glPopMatrix();
}
else
{
//iceberg before crash
glBegin(GL_POLYGON);
glVertex2f(7,2);
glVertex2f(8,3);
glVertex2f(11,18);
glVertex2f(12,19);
glVertex2f(12,3);
glEnd();
glBegin(GL_POLYGON);
glVertex2f(12,3);
glVertex2f(12,19);
glVertex2f(17,18);
glVertex2f(18,3);
glVertex2f(19,3);
glEnd();
}


glPopMatrix();

}



void myinit()
{
glClearColor(1,1,1,1);
glColor3f(1.0,0.0,0.0);
glPointSize(1.0);
glutKeyboardFunc(normalKeys);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,999.0,0.0,799.0);
}

int main(int argc, char* argv[])

{
int key=0;

printf("Sinking Ship Project Using OpenGL");
printf("Press 1 to Start\nPress 0 to Quit\n");
scanf("%d",&key);
if(key==0) return 0;
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;
}

Comments

Popular posts from this blog

Maxon Cinema 4D Studio R13.058 full Keygen Cracked Version | Mediafire Torrent Download Link

Download Maxon CINEMA 4D Studio Full Version With Keygen -Everything You Need for High-End 3D 32 and 64 bit MAXON CINEMA 4D STUDIO R13.058 (x86/x64) CINEMA 4D Studio is the very best that MAXON has to offer for professional 3D artists. If you want to create advanced 3D graphics but need a helping hand to ensure you create jaw-dropping graphics quickly and easily, then this is the choice for you.                     All You Ever Wanted This is our top-of-the-line product. It not only contains everything the other CINEMA 4D versions offer - it goes far beyond. CINEMA 4D Studio has all the tools you need to tackle even the most complex project. Minimum System Requirements Windows XP, Vista or 7 running on Intel or AMD CPU with SSE2-Support; Mac OS X 10.5.8 or higher running on an Intel-based Mac; 1024 MB free RAM, DVD ROM drive. Standard DVD installation can take up to 4 GB of disk space. Software must be registered for permanent use. SUPPORTED OPERATING SYS

C Program to add two matrices using pointers.

  #include<stdio.h> main() { int a[5][5],b[5][5],c[5][5],i,j,m,n; printf("\n Enter the Dimension of the Matrix:\n"); scanf("%d%d",&m,&n); printf("\nEnter the Elements of the first matix:"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { scanf("%d",(*(a+i)+j)); } } printf("\nEnter the elements of the second matrix:\n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { scanf("%d",(*(b+i)+j)); } } for(i=0;i<m;i++) { for(j=0;j<n;j++) { *(*(c+i)+j)=*(*(a+i)+j)+ *(*(b+i)+j); } } printf("\nResultantMatrix:\n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { printf("%5d",*(*(c+i)+j)); } printf("\n"); } getch(); } Output:  Enter the Dimension of the Matrix: 2  2 Enter the Elements of the first matix: 1   2   3   4 Enter the elements of the second matrix: 9   8   7  6 ResultantMatrix:  

C Program to create a Teapot using OpenGL

  C Program to create a Teapot using OpenGL # include <stdio.h> # include <GL/glut.h> void wall ( double thickness ) { glPushMatrix ( ) ; glTranslated ( 0.5 , 0.5 * thickness , 0.5 ) ; glScaled ( 1.0 , thickness , 1.0 ) ; glutSolidCube ( 1.0 ) ; glPopMatrix ( ) ; } void tableLeg ( double thick , double len ) { glPushMatrix ( ) ; glTranslated ( 0 , len / 2 , 0 ) ; glScaled ( thick , len , thick ) ; glutSolidCube ( 1.0 ) ; glPopMatrix ( ) ; } void table ( double topWid , double topThick , double legThick , double legLen ) { glPushMatrix ( ) ; glTranslated ( 0 , legLen , 0 ) ; glScaled ( topWid , topThick , topWid ) ; glutSolidCube ( 1.0 ) ; glPopMatrix ( ) ; double dist = 0.95 * topWid / 2.0 - legThick / 2.0 ; glPushMatrix ( ) ; glTranslated ( dist , 0 , dist ) ; tableLeg ( legThick , legLen ) ; glTranslated ( 0.0 , 0.0 , - 2 * dist ) ; tableLeg ( legThick , legLen ) ; glTransla