Ask Question

Name:
Title:
Your Question:

Answer Question

Name:
Your Answer:
User Submitted Source Code!


Description:
  okk
Language: C/C++
Code:


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


 struct timespec tim, tim2;

void ice();
void display1();
void display2();
void display3();
void ship();
void water();

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)
     {
          tim.tv_sec = 2;
                       tim.tv_nsec = 500000000L;
                       nanosleep(&tim , &tim2);
     }
     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);
     ice();
     glPushMatrix();
     glTranslated(b,75,0.0);
     ship();
     glPopMatrix();
     water();

}

void display3()
{
     glClear(GL_COLOR_BUFFER_BIT);
     x-=1;
     ice();
     glPushMatrix();
     glTranslated(c,100+x*5,0.0);
     glRotated(-15,0,0,1);
     ship();
     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();
}

/* TO DRAW SHIP */
void ship()
{


     glScaled(20,20,0);
     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();
     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();
     glColor3f(0.7,0.4,0.2);
     glBegin(GL_POLYGON);
          glVertex2f(4,5.5);
          glVertex2f(4,8);
          glVertex2f(5,8);
          glVertex2f(5,5.5);
     glEnd();
     glColor3f(0.9,0.9,0.9);
     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();
     glColor3f(0.5,0.5,0.5);
     glBegin(GL_POLYGON);
          glVertex2f(4.5,8.5);
          glVertex2f(4.5,10);
          glVertex2f(18.5,10);
          glVertex2f(18.5,8.5);
     glEnd();
     glColor3f(0.0,0.0,0.0);
     glBegin(GL_POLYGON);
          glVertex2f(5.5,10);
          glVertex2f(5.5,12.9);
          glVertex2f(7,12.9);
          glVertex2f(7,10);
     glEnd();
     glColor3f(0.9,0.5,0.2);
     glBegin(GL_POLYGON);
          glVertex2f(5.5,12.9);
          glVertex2f(5.5,13.5);
          glVertex2f(7,13.5);
          glVertex2f(7,12.9);
     glEnd();
     glColor3f(0.0,0.0,0.0);
     glBegin(GL_POLYGON);
          glVertex2f(8.5,10);
          glVertex2f(8.5,12.9);
          glVertex2f(10,12.9);
          glVertex2f(10,10);
     glEnd();
     glColor3f(0.9,0.5,0.2);
     glBegin(GL_POLYGON);
          glVertex2f(8.5,12.9);
          glVertex2f(8.5,13.5);
          glVertex2f(10,13.5);
          glVertex2f(10,12.9);
     glEnd();
     glColor3f(0.0,0.0,0.0);
     glBegin(GL_POLYGON);
          glVertex2f(11.5,10);
          glVertex2f(11.5,12.9);
          glVertex2f(13,12.9);
          glVertex2f(13,10);
     glEnd();
     glColor3f(0.9,0.5,0.2);
     glBegin(GL_POLYGON);
          glVertex2f(11.5,12.9);
          glVertex2f(11.5,13.5);
          glVertex2f(13,13.5);
          glVertex2f(13,12.9);
     glEnd();
     glColor3f(0.0,0.0,0.0);
     glBegin(GL_POLYGON);
          glVertex2f(14.5,10);
          glVertex2f(14.5,12.9);
          glVertex2f(16,12.9);
          glVertex2f(16,10);
     glEnd();
     glColor3f(0.9,0.5,0.2);
     glBegin(GL_POLYGON);
          glVertex2f(14.5,12.9);
          glVertex2f(14.5,13.5);
          glVertex2f(16,13.5);
          glVertex2f(16,12.9);
     glEnd();
     glColor3f(0.0,0.0,0.0);
     glBegin(GL_LINE_LOOP);
          glVertex2f(1.5,5.5);
          glVertex2f(1.5,6.5);
          glVertex2f(2,6.2);
          glVertex2f(3,6.2);
          glVertex2f(3.5,6.5);
          glVertex2f(3.5,5.5);
     glEnd();
     glBegin(GL_LINE_LOOP);
          glVertex2f(3.5,5.5);
          glVertex2f(3.5,6.5);
          glVertex2f(4,6.2);
          glVertex2f(5,6.2);
          glVertex2f(5.5,6.5);
          glVertex2f(5.5,5.5);
     glEnd();
     glBegin(GL_LINE_LOOP);
          glVertex2f(5.5,5.5);
          glVertex2f(5.5,6.5);
          glVertex2f(6,6.2);
          glVertex2f(7,6.2);
          glVertex2f(7.5,6.5);
          glVertex2f(7.5,5.5);
     glEnd();
     glBegin(GL_LINE_LOOP);
          glVertex2f(7.5,5.5);
          glVertex2f(7.5,6.5);
          glVertex2f(8,6.2);
          glVertex2f(9,6.2);
          glVertex2f(9.5,6.5);
          glVertex2f(9.5,5.5);
     glEnd();
     glBegin(GL_LINE_LOOP);
          glVertex2f(9.5,5.5);
          glVertex2f(9.5,6.5);
          glVertex2f(10,6.2);
          glVertex2f(11,6.2);
          glVertex2f(11.5,6.5);
          glVertex2f(11.5,5.5);
     glEnd();
     glBegin(GL_LINE_LOOP);
     glVertex2f(11.5,5.5);
          glVertex2f(11.5,6.5);
          glVertex2f(12,6.2);
          glVertex2f(13,6.2);
          glVertex2f(13.5,6.5);
          glVertex2f(13.5,5.5);
     glEnd();
     glBegin(GL_LINE_LOOP);
     glVertex2f(13.5,5.5);
          glVertex2f(13.5,6.5);
          glVertex2f(14,6.2);
          glVertex2f(15,6.2);
          glVertex2f(15.5,6.5);
          glVertex2f(15.5,5.5);
     glEnd();
     glBegin(GL_LINE_LOOP);
          glVertex2f(15.5,5.5);
          glVertex2f(15.5,6.5);
          glVertex2f(16,6.2);
          glVertex2f(17,6.2);
          glVertex2f(17.5,6.5);
          glVertex2f(17.5,5.5);
     glEnd();
     glBegin(GL_LINE_LOOP);
          glVertex2f(17.5,5.5);
          glVertex2f(17.5,6.5);
          glVertex2f(18,6.2);
          glVertex2f(19,6.2);
          glVertex2f(19.5,6.5);
          glVertex2f(19.5,5.5);
     glEnd();
     glBegin(GL_LINE_LOOP);
          glVertex2f(19.5,5.5);
          glVertex2f(19.5,6.5);
          glVertex2f(20,6.2);
          glVertex2f(20.5,6.2);
          glVertex2f(21,6.5);
          glVertex2f(21,5.5);
     glEnd();
     glColor3f(1.0,1.0,1.0);
     glBegin(GL_LINE_LOOP);
          glVertex2f(4.5,3.5);
          glVertex2f(4.5,4.5);
          glVertex2f(5.5,4.5);
          glVertex2f(5.5,3.5);
     glEnd();
     glBegin(GL_LINE_LOOP);
          glVertex2f(7,3.5);
          glVertex2f(7,4.5);
          glVertex2f(8,4.5);
          glVertex2f(8,3.5);
     glEnd();
     glBegin(GL_LINE_LOOP);
          glVertex2f(9.5,3.5);
          glVertex2f(9.5,4.5);
          glVertex2f(10.5,4.5);
          glVertex2f(10.5,3.5);
     glEnd();
     glBegin(GL_LINE_LOOP);
          glVertex2f(12,3.5);
          glVertex2f(12,4.5);
          glVertex2f(13,4.5);
          glVertex2f(13,3.5);
     glEnd();
     glBegin(GL_LINE_LOOP);
          glVertex2f(14.5,3.5);
          glVertex2f(14.5,4.5);
          glVertex2f(15.5,4.5);
          glVertex2f(15.5,3.5);
     glEnd();
     glBegin(GL_LINE_LOOP);
          glVertex2f(17,3.5);
          glVertex2f(17,4.5);
          glVertex2f(18,4.5);
          glVertex2f(18,3.5);
     glEnd();
     glColor3f(0.0,0.0,0.0);
     glBegin(GL_LINE_LOOP);
          glVertex2f(5,10);
          glVertex2f(5,11);
          glVertex2f(5.5,10.8);
          glVertex2f(6.5,10.8);
          glVertex2f(7,11);
          glVertex2f(7,10);
     glEnd();
     glBegin(GL_LINE_LOOP);
          glVertex2f(7,10);
          glVertex2f(7,11);
          glVertex2f(7.5,10.8);
          glVertex2f(8.5,10.8);
          glVertex2f(9,11);
          glVertex2f(9,10);
     glEnd();
     glBegin(GL_LINE_LOOP);
          glVertex2f(9,10);
          glVertex2f(9,11);
          glVertex2f(9.5,10.8);
          glVertex2f(10.5,10.8);
          glVertex2f(11,11);
          glVertex2f(11,10);
     glEnd();
     glBegin(GL_LINE_LOOP);
          glVertex2f(11,10);
          glVertex2f(11,11);
          glVertex2f(11.5,10.8);
          glVertex2f(12.5,10.8);
          glVertex2f(13,11);
          glVertex2f(13,10);
     glEnd();
     glBegin(GL_LINE_LOOP);
          glVertex2f(13,10);
          glVertex2f(13,11);
          glVertex2f(13.5,10.8);
          glVertex2f(14.5,10.8);
          glVertex2f(15,11);
          glVertex2f(15,10);
     glEnd();
     glBegin(GL_LINE_LOOP);
          glVertex2f(15,10);
          glVertex2f(15,11);
          glVertex2f(15.5,10.8);
          glVertex2f(16.5,10.8);
          glVertex2f(17,11);
          glVertex2f(17,10);
     glEnd();
}

/* TO DRAW ICEBERG */
void ice()
{
     glPushMatrix();
     glTranslated(450,50,0.0);
     glScaled(10,10,0);
     glColor3f(0.0,1.0,1.0);
     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(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
     {
          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.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 key=0;
     printf("project by\n\t namratha and sweekruthi \n");
     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("titanic ship");
     glutDisplayFunc(display);
     myinit();
     glutTimerFunc(100,update,0);
     glutMainLoop();
     return 0;
}

          
Comments: