Search Projects

Tuesday, January 16, 2024

New Village Scenery Computer Graphics Project in OpenGL Source Code

Many of the University including VTU have 'The Department of Computer Science and Engineering' which have Lab projects for students to show thier learning skill which got during thier course study. 

VTU Computer Graphics and Visualization Laboratory [15CSL68]

Above mentioned link have the lab programs prescribed by VTU under it course id mentioned have computer graphics project in OpenGL. Under this program students use thier learning ability to show of thier excellence, what they learnt in the course. Many students keeping in focus special efforts to impart  thier knowledge to give best projects.

We in this article we are going to show one of the best project in Computer Graphics Project in OpenGL with free source code. The name of the Project is New Village Scenery. 

Project Description:

In this Computer Graphics Project in OpenGL with free Source Code are going to draw sun, clouds, sky, river, boats, fence, trees, huts. In this projects we demonstrate the moving clouds in the sky and moving boats in the river,  Above is achived by using circle, lines traingle and spheres.




Draw Sky

 ///sky///
    glBegin(GL_POLYGON);
    glColor3ub(112,160,228);
    glVertex2f(38.0,3.0);
    glVertex2f(38.0,14.0);
    glVertex2f(-12.0,14.0);
    glVertex2f(-12.0,3.0);
    glEnd();

Draw the Sun

    /// Sun
    glPushMatrix();
    glColor3d(255,0,0);
    glTranslatef(1.0, 7.0, 0);
    glutSolidSphere(1.0, 750, 300);
    glPopMatrix();


See Also


2. Transformers - Free OpenGL Source code

3. Day and Night OpenGL Computer graphics Project


Implementation 

We use GL_QUADS with tranlation in opengl to draw most of the scenery in this new village scenery opengl projects program. As name suggested quads means fourt vertices. We use the glvertex in opengl to join the vertices and make the objects. Below is how vertices are used in opengl.


Draw Components


In this project we are going to use the different functions to implement the different object like sun in the program. void DrawAllComponents() functions is used to draw all the mentioned objects required in this program. After we draw all the objects and then we call them in the display function - void display(void)

After above functions are implemented we call the keyboard and mouse functions to implement the user interactions and call all of them in main functions.

User Interaction

In this opengl project below is user interactions

Mouse interactions

Left Mouse button - Increase the Movement of clouds and boats

Right Mouse button - Decrease the Movement of clouds and boats

Key Interactions

s - to stop the boat

r -  to run the boat again

Source code and Reports Computer Graphics Project in OpenGL 

Here you can download computer graphics projects with source code and reports.

Below is soure code -

#include<windows.h>

#include <iostream>

#include<GL/gl.h>

#include <GL/glut.h>

#include <math.h>

#include<windows.h>


float moveC = 0.0f;

float moveB1 = 0.0f;

float moveB2 = 0.0f;

float speed = 0.02f;



void DrawAllComponents()

{

    glClearColor(1.0f,1.0f,1.0f,1.0f);

    glClear(GL_COLOR_BUFFER_BIT);

    glColor3d(1,0,0);

    glLoadIdentity();

    gluOrtho2D(-12,38,-19,14);//range

    glMatrixMode(GL_MODELVIEW);



    ///sky///

    glBegin(GL_POLYGON);

    glColor3ub(112,160,228);

    glVertex2f(38.0,3.0);

    glVertex2f(38.0,14.0);

    glVertex2f(-12.0,14.0);

    glVertex2f(-12.0,3.0);

    glEnd();


    /// Sun

    glPushMatrix();

    glColor3d(255,0,0);

    glTranslatef(1.0, 7.0, 0);

    glutSolidSphere(1.0, 750, 300);

    glPopMatrix();


    ///Cloud-01

    glPushMatrix();


    glTranslatef(moveC,0.0f,0.0f);

    glPushMatrix();

    glColor3d(255,255,255);

    glTranslatef(1.0, 6.0, 0);

    glutSolidSphere(0.7, 250, 250);

    glPopMatrix();


    glPushMatrix();

    glColor3d(255,255,255);

    glTranslatef(1.0, 7.0, 0);

    glutSolidSphere(0.7, 250, 250);

    glPopMatrix();


    glPushMatrix();

    glColor3d(255,255,255);

    glTranslatef(2.0, 7.0, 0);

    glutSolidSphere(0.7, 250, 250);

    glPopMatrix();


    glPushMatrix();

    glColor3d(255,255,255);

    glTranslatef(2.0, 6.0, 0);

    glutSolidSphere(0.7, 250, 250);

    glPopMatrix();


    glPushMatrix();

    glColor3d(255,255,255);

    glTranslatef(0.0, 6.5, 0);

    glutSolidSphere(0.7, 250, 250);

    glPopMatrix();


    glPushMatrix();

    glColor3d(255,255,255);

    glTranslatef(3.0, 6.5, 0);

    glutSolidSphere(0.7, 250, 250);

    glPopMatrix();


    glPopMatrix();



    ///cloud-02


    glPushMatrix();


    glTranslatef(moveC,0.0f,0.0f);

    glPushMatrix();

    glColor3d(255,255,255);

    glTranslatef(15.0, 9.0, 0);

    glutSolidSphere(0.8, 250, 250);

    glPopMatrix();


    glPushMatrix();

    glColor3d(255,255,255);

    glTranslatef(15.0, 8.0, 0);

    glutSolidSphere(0.8, 250, 250);

    glPopMatrix();


    glPushMatrix();

    glColor3d(255,255,255);

    glTranslatef(16.0, 8.0, 0);

    glutSolidSphere(0.8, 250, 250);

    glPopMatrix();


    glPushMatrix();

    glColor3d(255,255,255);

    glTranslatef(16.0, 9.0, 0);

    glutSolidSphere(0.8, 250, 250);

    glPopMatrix();


    glPushMatrix();

    glColor3d(255,255,255);

    glTranslatef(14.0, 8.5, 0);

    glutSolidSphere(0.8, 250, 250);

    glPopMatrix();


    glPushMatrix();

    glColor3d(255,255,255);

    glTranslatef(17.0, 8.5, 0);

    glutSolidSphere(0.8, 250, 250);

    glPopMatrix();


    glPopMatrix();




    ///cloud-03

    glPushMatrix();


    glTranslatef(moveC,0.0f,0.0f);


    glPushMatrix();

    glColor3d(255,255,255);

    glTranslatef(22.0, 8.0, 0);

    glutSolidSphere(0.75, 250, 250);

    glPopMatrix();


    glPushMatrix();

    glColor3d(255,255,255);

    glTranslatef(22.0, 7.0, 0);

    glutSolidSphere(0.75, 250, 250);

    glPopMatrix();


    glPushMatrix();

    glColor3d(255,255,255);

    glTranslatef(23.0, 8.0, 0);

    glutSolidSphere(0.75, 250, 250);

    glPopMatrix();


    glPushMatrix();

    glColor3d(255,255,255);

    glTranslatef(23.0, 7.0, 0);

    glutSolidSphere(0.75, 250, 250);

    glPopMatrix();


    glPushMatrix();

    glColor3d(255,255,255);

    glTranslatef(24.0, 7.5, 0);

    glutSolidSphere(0.75, 250, 250);

    glPopMatrix();


    glPushMatrix();

    glColor3d(255,255,255);

    glTranslatef(21.0, 7.5, 0);

    glutSolidSphere(0.75, 250, 250);

    glPopMatrix();


    glPopMatrix();





    ///See portion

    glPushMatrix();

    glBegin(GL_POLYGON);

    glColor3ub(65,105,225);


    glVertex2f(40.0,-19.0);  /// right-down

    glVertex2f(38.0,-11.0); /// right-top

    glVertex2f(-12.0,-11.0); /// left-top

    glVertex2f(-12.0,-19.0);  /// left-down

    glEnd();



    ///Ground portion

    glBegin(GL_POLYGON);

    glColor3ub(25,128,0);


    glVertex2f(90.0f, -12.5f); /// angle

    glVertex2f(38.0f,-12.0f);  /// right-down

    glVertex2f(38.0f,3.0f); /// right-top

    glVertex2f(-12.0f,3.0f); /// left-top

    glVertex2f(-12.0f,-11.0f);  /// left-down

    glEnd();





    /// House Drawing


    ///1st House

    /// FEG

    glColor3ub(162, 116, 36);

    glBegin(GL_TRIANGLES);

    glVertex2f(-8, -2);

    glVertex2f(-6, 0);

    glVertex2f(-4, -2);

    glEnd();


    /// FGIH

    glColor3ub(214, 42, 50);

    glBegin(GL_QUADS);

    glVertex2f(-8.023, -5.184);

    glVertex2f(-8, -2);

    glVertex2f(-4, -2);

    glVertex2f(-3.999, -7.219);

    glEnd();


    ///ROPQ

    glColor3ub(19, 105, 51);

    glBegin(GL_QUADS);

    glVertex2f(-6.73, -5.67);

    glVertex2f(-6.76, -3.58);

    glVertex2f(-5.19, -3.58);

    glVertex2f(-5.19, -6.62);

    glEnd();


    ///EGKJ

    glColor3ub(124, 85, 17);

    glBegin(GL_QUADS);

    glVertex2f(-6, 0);

    glVertex2f(4, 0);

    glVertex2f(6, -2);

    glVertex2f(-4, -2);

    glEnd();



    ///GLMI

    glColor3ub(156, 9, 16);

    glBegin(GL_QUADS);

    glVertex2f(-4, -2);

    glVertex2f(5.22, -2);

    glVertex2f(5.26, -5.11);

    glVertex2f(-3.99, -7.22);

    glEnd();


    ///VSTU

    glColor3ub(216, 215, 111);

    glBegin(GL_QUADS);

    glVertex2f(-0.39, -6.40);

    glVertex2f(-0.42, -3.62);

    glVertex2f(1.95, -3.58);

    glVertex2f(1.97, -5.86);

    glEnd();



    ///SZA1V

    glColor3ub(19, 105, 51);

    glBegin(GL_QUADS);

    glVertex2f(-0.42, -3.62);

    glVertex2f(0.38, -4.38);

    glVertex2f(0.38, -5.53);

    glVertex2f(-0.39, -6.40);

    glEnd();



    ///TB1C1U

    glColor3ub(19, 105, 51);

    glBegin(GL_QUADS);

    glVertex2f(1.95, -3.58);

    glVertex2f(1.21, -4.38);

    glVertex2f(1.21, -5.46);

    glVertex2f(1.97, -5.86);

    glEnd();




    ///2nd House

    ///S1R1W1

    glColor3ub(0,0,0);

    glBegin(GL_TRIANGLES);

    glVertex2f(8.09,-5.01);

    glVertex2f(10.96,-2.02);

    glVertex2f(14.10, -5.01);

    glEnd();


    ///U1V1Z1W1

    glColor3ub(145, 0, 12);

    glBegin(GL_QUADS);

    glVertex2f(9.15, -5.01);

    glVertex2f(9.14, -9.02);

    glVertex2f(12.85, -9.04);

    glVertex2f(12.84, -5.01);

    glEnd();


    ///A2B2C2D2

    glColor3ub(245, 10, 49);

    glBegin(GL_QUADS);

    glVertex2f(10.36, -6.79);

    glVertex2f(10.35, -8.91);

    glVertex2f(11.59, -8.99);

    glVertex2f(11.58, -6.79);

    glEnd();






    ///1st Tree


    glColor3ub(22, 208, 70);

    glBegin(GL_QUADS);

    glVertex2f(14.88, -6.00);

    glVertex2f(20, 0);

    glVertex2f(24.98, -6.16);

    glVertex2f(21.05, -4.96);

    glVertex2f(19.09, -4.95);

    glEnd();


    ///NWJ1H1

    glColor3ub(191, 158, 24);

    glBegin(GL_QUADS);

    glVertex2f(19.02, -6.06);

    glVertex2f(20.96, -6.09);

    glVertex2f(21, -10.08);

    glVertex2f(19.01, -10.08);

    glEnd();



    /// 2nd Tree

    ///L1K1M1

    glColor3ub(17, 218, 45);

    glBegin(GL_TRIANGLES);

    glVertex2f(27.02, -6.16);

    glVertex2f(30, 0);

    glVertex2f(32.93, -6);

    glEnd();


    ///E1F1P1Q1

    glColor3ub(191, 158, 24);

    glBegin(GL_QUADS);

    glVertex2f(29.09, -6.10);

    glVertex2f(31.03, -6.05);

    glVertex2f(31.05, -10.03);

    glVertex2f(29.06, -9.97);

    glEnd();








    ///Rail Line

    ///N1B

    glColor3ub(155, 29, 29);

    glBegin(GL_QUADS);

    glVertex2f(-12, 2);

    glVertex2f(38, 3);

    glVertex2f(37.98, 2.69);

    glVertex2f(-11.98, 1.71);

    glEnd();



    glColor3ub(155, 29, 29);

    glBegin(GL_QUADS);

    glVertex2f(-12.07, 0.72);

    glVertex2f(-12.07, 0.48);

    glVertex2f(38.02, 1.52);

    glVertex2f(38, 1.70);

    glEnd();



    glColor3ub(0, 0, 0);

    glBegin(GL_QUADS);

    glVertex2f(-8, 2.08);

    glVertex2f(-7.64, 2.08);

    glVertex2f(-7.63, 0.63);

    glVertex2f(-8, 0.62);

    glEnd();


    glColor3ub(0, 0, 0);

    glBegin(GL_QUADS);

    glVertex2f(0.97, 2.31);

    glVertex2f(1.28, 2.27);

    glVertex2f(1.34, 0.76);

    glVertex2f(0.97, 0.75);

    glEnd();


    glColor3ub(0, 0, 0);

    glBegin(GL_QUADS);

    glVertex2f(11.98, 2.48);

    glVertex2f(12.48, 2.49);

    glVertex2f(12.5, 1);

    glVertex2f(11.98, 0.98);

    glEnd();


    glColor3ub(0, 0, 0);

    glBegin(GL_QUADS);

    glVertex2f(25.45, 2.75);

    glVertex2f(26.09, 2.76);

    glVertex2f(26.09, 1.27);

    glVertex2f(25.45, 1.26);

    glEnd();


    glColor3ub(0, 0, 0);

    glBegin(GL_QUADS);

    glVertex2f(33.99, 2.92);

    glVertex2f(34.48, 2.93);

    glVertex2f(34.58, 1.45);

    glVertex2f(34.03, 1.44);

    glEnd();


    glColor3ub(0, 0, 0);

    glBegin(GL_QUADS);

    glVertex2f(18.39, 2.69);

    glVertex2f(19.16, 2.62);

    glVertex2f(19.18, 1.13);

    glVertex2f(18.42, 1.11);

    glEnd();







    ///boat-1 speed left to right

    glPushMatrix();

    glTranslatef(moveB1, 0.0f, 0.0f);

    glBegin(GL_QUADS);

    glColor3ub(139,69,19);

    glVertex2f(1.0f, -14.0f);

    glVertex2f(1.50f, -13.0f);

    glVertex2f(-2.0f, -13.0f);

    glVertex2f(-3.0f, -14.0f);

    glEnd();


    glBegin(GL_QUADS);

    glColor3ub(0,0,0);

    glVertex2f(2.0f, -14.5f);

    glVertex2f(2.0f, -14.0f);

    glVertex2f(-3.0f, -14.0f);

    glVertex2f(-3.0f, -14.5f);

    glEnd();


    glBegin(GL_TRIANGLES);

    glColor3ub(0,0,0);

    glVertex2f(-3.0f, -14.5f);

    glVertex2f(-3.0f, -14.0f);

    glVertex2f(-4.5f, -13.7f);


    glEnd();


    glBegin(GL_TRIANGLES);

    glColor3ub(0,0,0);

    glVertex2f(2.0f, -14.5f);

    glVertex2f(3.5f, -13.7f);

    glVertex2f(2.0f, -14.0f);


    glEnd();


    glBegin(GL_TRIANGLES);

    glColor3ub(0,0,0);

    glVertex2f(2.0f, -14.0f);

    glVertex2f(1.5f, -13.0f);

    glVertex2f(1.0f, -14.0f);


    glEnd();


    glPopMatrix();




    ///boat-2 motion right to left

    glPushMatrix();

    glTranslatef(moveB2, 0.0f, 0.0f);

    glBegin(GL_QUADS);

    glColor3ub(139,69,19);

    glVertex2f(22.0f, -17.5f);

    glVertex2f(21.0f, -16.5f);

    glVertex2f(17.50f, -16.5f);

    glVertex2f(18.0f, -17.5f);

    glEnd();


    glBegin(GL_QUADS);

    glColor3ub(0,0,0);

    glVertex2f(22.0f, -18.0f);

    glVertex2f(22.0f, -17.5f);

    glVertex2f(17.0f, -17.5f);

    glVertex2f(17.0f, -18.0f);

    glEnd();


    glBegin(GL_QUADS);

    glColor3ub(255,99,71);

    glVertex2f(20.5f, -16.5f);

    glVertex2f(21.0f, -14.5f);

    glVertex2f(18.5f, -14.5f);

    glVertex2f(18.0f, -16.5f);

    glEnd();


    glBegin(GL_QUADS);

    glColor3ub(139,69,19);

    glVertex2f(19.8f, -14.5f);

    glVertex2f(19.8f, -14.0f);

    glVertex2f(19.7f, -14.0f);

    glVertex2f(19.7f, -14.5f);

    glEnd();


    glBegin(GL_TRIANGLES);

    glColor3ub(0,0,0);

    glVertex2f(17.0f, -18.0f);

    glVertex2f(17.0f, -17.5f);

    glVertex2f(15.5f, -17.2f);

    glEnd();


    glBegin(GL_TRIANGLES);

    glColor3ub(0,0,0);

    glVertex2f(22.0f, -17.5f);

    glVertex2f(22.0f, -18.0f);

    glVertex2f(23.5f, -17.2f);

    glEnd();


    glBegin(GL_TRIANGLES);

    glColor3ub(0,0,0);

    glVertex2f(17.0f, -17.5f);

    glVertex2f(18.0f, -17.5f);

    glVertex2f(17.5f, -16.5f);

    glEnd();


    glPopMatrix();




    //end

    glPopMatrix();

    glutSwapBuffers();

}




void updateC(int value)

{


    if(moveC > +36)

    {

        moveC = -38;

    }

    moveC += 0.15;


    glutTimerFunc(20, updateC, 0);

    glutPostRedisplay();

}



void updateB1(int value)

{


    moveB1 += speed;

    if(moveB1 > 38)

    {

        moveB1 = -38;

    }


//   moveB1 += 0.17;


    glutTimerFunc(20, updateB1, 0); //Notify GLUT to call update again in 25 milliseconds

    glutPostRedisplay();

}




void updateB2(int value)

{



    if(moveB2 < -36)

    {

        moveB2 = +38;

    }

    //Notify GLUT that the display has changed


    moveB2 -= 0.13;


    glutTimerFunc(20, updateB2, 0); //Notify GLUT to call update again in 25 milliseconds

    glutPostRedisplay();

}




void handleKeypress(unsigned char key, int x, int y)

{

    switch (key)

    {

    case 's'://stops

        speed = 0.0f;

        break;

    case 'r'://runs

        speed = 0.02f;

        break;

        glutPostRedisplay();

    }

}



void handleMouse(int button, int state, int x, int y)

{


    if (button == GLUT_LEFT_BUTTON)

    {

        speed += 0.05f;

    }



    else if (button == GLUT_RIGHT_BUTTON)

    {

        speed -= 0.05f;

    }

    glutPostRedisplay();

}






void display(void)

{

    DrawAllComponents();

    glFlush ();

}


void init()

{


    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);


}


int main(int argc, char** argv)

{

    glutInit(&argc, argv);

    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);


    glutInitWindowSize(1300, 700);

    glutInitWindowPosition(100,100);

    glutCreateWindow(" Village Scenery ");

    init();


    glutTimerFunc(100, updateC, 1);    // cloud speed

    glutTimerFunc(100, updateB1, 0); //Boat 1

    glutTimerFunc(100, updateB2, 0); //boat 2


    glutKeyboardFunc(handleKeypress);

    glutMouseFunc(handleMouse);

    glutDisplayFunc(display);


    glutMainLoop();

    return 0;

}


Monday, December 25, 2023

Black Pearl - The Cursed Ship sailing Day Night Computer Graphics OpenGL Projects

 

Problem Statement: Create a program using C/C++ using OpenGL API for  Black Pearl - The Cursed Ship which sail day night all day around.

Project Description:

This computer graphics project using OpenGL API  is implemented using the C/C++ language. Computer graphics is concerned with all aspects of producing graphics pictures images using a computerprocessing with aspect of animation.
We have designed a ship inspired by Movie  Pirates of the Caribbean: The Curse of the Black Pearl which sail from moring from early hour to late night.
In this mini-project Black Pearl - The Cursed Ship is implemented by using OpenGL API’s glut library and other computer graphics concepts. To make the project more interative, we have used different keyboard functions. The key board functions with different display to make the program user interactive.


See also - 

“The Voyage”- Opengl Projects with source code

Coding:

For Ship we have designed as - 

//base
    glColor3f(0.2+col,0.2+col,0.2+col);
    glBegin(GL_POLYGON);
    glVertex2f(10,119);
    glVertex2f(10,110);
    glVertex2f(41,70);
    glColor3f(0.3+col,0.3+col,0.8+col);
    glVertex2f(219,42);
    glVertex2f(292,98);
    glVertex2f(300,110);
    glEnd();

//p1
glColor3f(1.0+col,1.0+col,1.0+col);
glBegin(GL_POLYGON);
glVertex2f(35,118);
glVertex2f(35,128);
glColor3f(0.5+col,0.5+col,0.5+col);
glVertex2f(239,131);
glVertex2f(239,111);
glVertex2f(35,119);
glEnd();
//side
    glBegin(GL_POLYGON);
glColor3f(0.8+col,0.8+col,0.8+col);
glVertex2f(239,131);
glVertex2f(239,111);
glVertex2f(257,110);
glVertex2f(257,127);
glEnd();

//p2
    glColor3f(0.0+col,0.0+col,0.5+col);
glBegin(GL_POLYGON);
glVertex2f(45,129);
glVertex2f(45,140);
glVertex2f(233,149);
glVertex2f(233,131);
glEnd();
    //side
    glBegin(GL_POLYGON);
glColor3f(0.1+col,0.1+col,0.8+col);
glVertex2f(233,149);
glVertex2f(233,131);
glVertex2f(254,128);
glVertex2f(254,145);
glEnd();

//p3
    glColor3f(0.2+col,0.5+col,0.2+col);
glBegin(GL_POLYGON);
glVertex2f(51,151);
glVertex2f(51,140);
glVertex2f(221,149);
glColor3f(0.9+col,0.6+col,0.3+col);
glVertex2f(221,165);
glVertex2f(51,151);
glEnd();
//side
    glBegin(GL_POLYGON);
glColor3f(0.1+col,0.4+col,0.1+col);
glVertex2f(221,164);
glVertex2f(221,149);
glVertex2f(247,147);
glVertex2f(247,162);
glEnd();

//p4
    //pipe1
glColor3f(0.48+col,0.27+col,0.44+col);
glBegin(GL_POLYGON);
glVertex2f(79,152);
glVertex2f(79,194);
glVertex2f(94,194);
glColor3f(0.0+col,0.0+col,0.0+col);
glVertex2f(94,155);
glEnd();
cloud(59,194);

    //pipe2
    glColor3f(0.44+col,0.48+col,0.27+col);
glBegin(GL_POLYGON);
glVertex2f(112,156);
glVertex2f(112,198);
glVertex2f(127,198);
glColor3f(0.0+col,0.0+col,0.0+col);
glVertex2f(127,158);
glEnd();
cloud(92,198);

//pipe3
    glColor3f(0.27+col,0.48+col,0.44+col);
glBegin(GL_POLYGON);
    glVertex2f(159,161);
glVertex2f(159,203);
glVertex2f(179,203);
glColor3f(0.0+col,0.0+col,0.0+col);
    glVertex2f(179,160);
glEnd();

In this Computer Graphics OpenGL Projects we have also included a rocket which fly in the sky. 

User Interaction:

By using the keyboard interface, we have performed the required action for user interaction for a better interface. The functionality of various keyboard key implement in this program is listed as below :
z -> Shoot rocket
y -> Early Morning
m -> Morning
a -> Afternoon
e -> Evening
n -> Night
d -> Mid Night
Num 6 -> Moving Ship Fwd
Num 4 -> Moving Ship bckwd
Esc -> Exit
Left Arrow -> Moving Ship backward
Right Arrow -> Moving Ship Forward

Improvement -

You can add the pirate flag to make it look much better. Also rocket in the sky can be allow to hit the ship and it will avoid the damage

Source Code - 

Find the source code mentioned below -
#include <windows.h>
#include<GL/glut.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>

float pos=0,x=0,col=0,i,p,c,cf=0,pr,pl=0;
int screen=0;

//Introduction Page
char name[35]="Black Pearl - The Cursed Ship";
char college[100]="Institute of Technology";
char dept[100]="Department of Computer Science & Engineering";
char heading[100]="Made by                                                                 Guides";
char row1[100]="                                Prof. ";
char row2[100]="                            Prof.";
char emsg[100]="Press ENTER to start";
char title[50]="Sailing of Ship Day And NighT...!";
char dash[100]="--------------------------------------------------------------------------------------";

void frontscreen(void)
{
     glPushMatrix();
     glTranslatef(50,100,0);
     glClearColor(0.8,0.8,1.0,1.0);
     int s;
     glClear(GL_COLOR_BUFFER_BIT);

     glRasterPos2i(184,480);                                                    //displays college name
     for(s=0;college[s]!='\0';s++)
     {
     glColor3f(0.0,0.0,1.0);
     glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,college[s]);
     }

     glRasterPos2i(180,450);                                                     //displays dept.
     for(s=0;dept[s]!='\0';s++)
     {
     glColor3f(1.0,0.0,0.0);
     glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,dept[s]);
     }

     glRasterPos2i(210,380);                                                    //displays project name
     for(s=0;title[s]!='\0';s++)
     {
     glColor3f(0.0,0.0,0.0);
     glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,title[s]);
     }

     glRasterPos2i(155,370);                                                    //displays dashes
     for(s=0;dash[s]!='\0';s++)
     {
     glColor3f(0.0,0.0,0.0);
     glutBitmapCharacter(GLUT_BITMAP_9_BY_15,dash[s]);
     }

     glRasterPos2i(170,340);                                                    //displays heading of table
     for(s=0;heading[s]!='\0';s++)
     {
     glColor3f(0.0,0.0,0.0);
     glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,heading[s]);
     }

     glRasterPos2i(155,330);                                                    //displays dashes
     for(s=0;dash[s]!='\0';s++)
     {
     glColor3f(0.0,0.0,0.0);
     glutBitmapCharacter(GLUT_BITMAP_9_BY_15,dash[s]);
     }

     glRasterPos2i(170,310);                                                    //displays 1st row in table
     for(s=0;row1[s]!='\0';s++)
     {
     glColor3f(0.0,0.0,0.0);
     glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,row1[s]);
     }

     glRasterPos2i(170,290);                                                    //displays 2nd row in table
     for(s=0;row2[s]!='\0';s++)
     {
     glColor3f(0.0,0.0,0.0);
     glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,row2[s]);
     }

     glRasterPos2i(155,270);                                                    //displays dashes
     for(s=0;dash[s]!='\0';s++)
     {
     glColor3f(0.0,0.0,0.0);
     glutBitmapCharacter(GLUT_BITMAP_9_BY_15,dash[s]);
     }

     glRasterPos2i(200,100);                                                    //displays "enter msg"
     for(s=0;emsg[s]!='\0';s++)
     {
     glColor3f(1.0,0.0,0.5);
     glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,emsg[s]);
     }

     glPopMatrix();
     glFlush();
}

//sun/moon
void Drawarc(float sa,float ea,float cx,float cy,float rd)
{
     float PI = 3.14;
     float step=1.0;
     float angle,x=0,y=0,centerX=cx,centerY=cy,radius=rd;

     glBegin(GL_POLYGON);
     for(angle=sa;angle<ea; angle+=step)
     {
     float rad ;
     rad = PI*angle/180;
     x  = centerX+radius*cos(rad);
     y  = centerY+radius*sin(rad);
     glVertex2f(x,y);
     }
     glEnd();
     glFlush();
}

//smoke
void cloud(int m, int n)
{
     for(c=p=0;c<31;c+=10,p-=1)
     {
     glColor3f(0.5,0.5,0.5);
     Drawarc(0,360,m+c,n,10+p);
     }
}

void ship(float x)
{
    glPushMatrix();
    glTranslatef(x,0,0);
//base
    glColor3f(0.2+col,0.2+col,0.2+col);
    glBegin(GL_POLYGON);
    glVertex2f(10,119);
    glVertex2f(10,110);
    glVertex2f(41,70);
    glColor3f(0.3+col,0.3+col,0.8+col);
    glVertex2f(219,42);
    glVertex2f(292,98);
    glVertex2f(300,110);
    glEnd();

//p1
glColor3f(1.0+col,1.0+col,1.0+col);
glBegin(GL_POLYGON);
glVertex2f(35,118);
glVertex2f(35,128);
glColor3f(0.5+col,0.5+col,0.5+col);
glVertex2f(239,131);
glVertex2f(239,111);
glVertex2f(35,119);
glEnd();
//side
    glBegin(GL_POLYGON);
glColor3f(0.8+col,0.8+col,0.8+col);
glVertex2f(239,131);
glVertex2f(239,111);
glVertex2f(257,110);
glVertex2f(257,127);
glEnd();

//p2
    glColor3f(0.0+col,0.0+col,0.5+col);
glBegin(GL_POLYGON);
glVertex2f(45,129);
glVertex2f(45,140);
glVertex2f(233,149);
glVertex2f(233,131);
glEnd();
    //side
    glBegin(GL_POLYGON);
glColor3f(0.1+col,0.1+col,0.8+col);
glVertex2f(233,149);
glVertex2f(233,131);
glVertex2f(254,128);
glVertex2f(254,145);
glEnd();

//p3
    glColor3f(0.2+col,0.5+col,0.2+col);
glBegin(GL_POLYGON);
glVertex2f(51,151);
glVertex2f(51,140);
glVertex2f(221,149);
glColor3f(0.9+col,0.6+col,0.3+col);
glVertex2f(221,165);
glVertex2f(51,151);
glEnd();
//side
    glBegin(GL_POLYGON);
glColor3f(0.1+col,0.4+col,0.1+col);
glVertex2f(221,164);
glVertex2f(221,149);
glVertex2f(247,147);
glVertex2f(247,162);
glEnd();

//p4
    //pipe1
glColor3f(0.48+col,0.27+col,0.44+col);
glBegin(GL_POLYGON);
glVertex2f(79,152);
glVertex2f(79,194);
glVertex2f(94,194);
glColor3f(0.0+col,0.0+col,0.0+col);
glVertex2f(94,155);
glEnd();
cloud(59,194);

    //pipe2
    glColor3f(0.44+col,0.48+col,0.27+col);
glBegin(GL_POLYGON);
glVertex2f(112,156);
glVertex2f(112,198);
glVertex2f(127,198);
glColor3f(0.0+col,0.0+col,0.0+col);
glVertex2f(127,158);
glEnd();
cloud(92,198);

//pipe3
    glColor3f(0.27+col,0.48+col,0.44+col);
glBegin(GL_POLYGON);
    glVertex2f(159,161);
glVertex2f(159,203);
glVertex2f(179,203);
glColor3f(0.0+col,0.0+col,0.0+col);
    glVertex2f(179,160);
glEnd();
cloud(144,203);
glPopMatrix();
}

void water()
{
    glBegin(GL_POLYGON);
    glColor3f(0.2+col,0.2+col,0.6+col);
    glVertex2f(00,00);
    glVertex2f(00,300);
    glVertex2f(1024,300);
    glVertex2f(1024,00);
glEnd();
}

void mountain2()
{
    float a,b;
    glColor3f(0.6+col,0.4+col,0.2+col);
    for(a=0,b=300;a<1025;a=a+80)
    {
    glBegin(GL_POLYGON);
    glVertex2f(-40+a,b);
    glVertex2f(10+a,b+140);
    glVertex2f(60+a,b);
    glEnd();
    }
}

void mountain()
{
    float a,b;
    glColor3f(0.8+col,0.6+col,0.4+col);
    for(a=0,b=300;a<1025;a=a+80)
    {
    glBegin(GL_POLYGON);
    glVertex2f(0+a,b);
    glVertex2f(50+a,b+100);
    glVertex2f(100+a,b);
    glEnd();
    }
}

void mountain3()
{
    float a,b;
    glColor3f(0.4+col,0.2+col,0.0+col);
    for(a=0,b=350;a<1025;a=a+80)
    {
    glBegin(GL_POLYGON);
    glVertex2f(0+a,b);
    glVertex2f(50+a,b+180);
    glVertex2f(100+a,b);
    glEnd();
    }
}

void flag(float x)
{
     int s;
     glPushMatrix();
     glTranslatef(x,0,0);
     glColor3f(0.0,0.0,0.0);
     glBegin(GL_POLYGON);
     glVertex2f(245,160);
     glVertex2f(245,250);
     glVertex2f(242,250);
     glVertex2f(242,160);
     glEnd();
     glColor3f(0.8,0.1,0.1);
     glBegin(GL_POLYGON);
     glVertex2f(245,250);
     glVertex2f(275,215);
     glVertex2f(245,180);
     glEnd();
     glRasterPos2i(50,80);                                                    //displays college name
     for(s=0;college[s]!='\0';s++)
     {
     glColor3f(1.0,1.0,1.0);
     glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,name[s]);
     }
     glPopMatrix();
}
void crackers()
{
    // if(cf==1)
     {
     glColor3f(1,0,0);
     glBegin(GL_POLYGON);
     glVertex2f(100+pos,100+pr);
     glVertex2f(100+pos,110+pr);
     glVertex2f(101+pos,110+pr);
     glVertex2f(101+pos,100+pr);
     glEnd();
     glFlush();
     }
     glutPostRedisplay();
}

void init()
{
glClearColor(0.0,0.0,0.0,1.0);
glLoadIdentity();
gluOrtho2D(0,1024,0,768);
}


void plane()
{
glPushMatrix();
glTranslatef(0+pl,700,0);
glColor3f(0.0,0.0,0.0);
glBegin(GL_POLYGON);//rectangular body
glVertex2f(0.0,30.0/3);
glVertex2f(0.0,55.0/3);
glVertex2f(135.0/3,55.0/3);
glColor3f(1.0,0.0,0.0);
glVertex2f(135.0/3,30.0/3);
glEnd();

glColor3f(0.0,0.0,0.0);
glBegin(GL_POLYGON);//upper triangle construction plane
glVertex2f(135.0/3,55.0/3);
glVertex2f(150.0/3,50.0/3);
glVertex2f(155.0/3,45.0/3);
glVertex2f(160.0/3,40.0/3);
glVertex2f(135.0/3,40.0/3);
glEnd();

glColor3f(0.0,0.0,0.0);
glBegin(GL_LINE_LOOP);//outline of upper triangle plane
glVertex2f(135.0/3,55.0/3);
glVertex2f(150.0/3,50.0/3);
glVertex2f(155.0/3,45.0/3);
glVertex2f(160.0/3,40.0/3);
glVertex2f(135.0/3,40.0/3);
glEnd();

glColor3f(1.0,0.0,0.0);
glBegin(GL_POLYGON);//lower triangle
glVertex2f(135.0/3,40.0/3);
glVertex2f(160.0/3,40.0/3);
glVertex2f(160.0/3,37.0/3);
glVertex2f(145.0/3,30.0/3);
glVertex2f(135.0/3,30.0/3);
glEnd();

glColor3f(1.0,0.0,0.0);
glBegin(GL_POLYGON);//back wing
glVertex2f(0.0,55.0/3);
glVertex2f(0.0,80.0/3);
glVertex2f(10.0/3,80.0/3);
glVertex2f(40.0/3,55.0/3);
glEnd();

glColor3f(1.0,0.0,0.0);
glBegin(GL_POLYGON);//left side wing
glVertex2f(65.0/3,55.0/3);
glVertex2f(50.0/3,70.0/3);
glVertex2f(75.0/3,70.0/3);
glVertex2f(90.0/3,55.0/3);
glEnd();

glColor3f(1.0,0.0,0.0);
glBegin(GL_POLYGON);//rightside wing
glVertex2f(70.0/3,40.0/3);
glVertex2f(100.0/3,40.0/3);
glVertex2f(80.0/3,15.0/3);
glVertex2f(50.0/3,15.0/3);
glEnd();
glPopMatrix();
}

void display()
{
    glClear(GL_COLOR_BUFFER_BIT);
    if(screen==0)
    {
    frontscreen();
    glFlush();
    }
    else if(screen==1)
{
    mountain3();
mountain2();
mountain();
water();
if(cf==1)
    {
    crackers();
pr+=2;
     }
ship(pos);
flag(pos);

plane();
pl+=2;
if(pl==1200)
pl=0;

    if(i==1)
    {
    glColor3f(0.9,0.9,0.5);
    Drawarc(0,360,200,700,10);
    }
     if(i==2)
    {
    glColor3f(0.8,0.7,0.4);
    Drawarc(0,360,400,700,10);
    }
     if(i==3)
    {
    glColor3f(1.0,1.0,1.0);
    Drawarc(0,360,600,700,10);
    }
     if(i==4)
    {
    glColor3f(1.0,1.0,1.0);
    Drawarc(0,360,800,700,10);
    }
    glFlush();
    }
    glutPostRedisplay();
    glutSwapBuffers();
    glFlush();
}

void keyboard(unsigned char key, int x, int y)
{
   switch (key)
   {
    case 13:if(screen==0)
                 {
                 screen=1;
                 glutPostRedisplay();
                 }
                 break;
    case 27:exit (0);
        break;
    case 'y':glClearColor(0.8,0.5,0.4,1.0);
             col=-0.1;
             i=0;
             glutPostRedisplay();
         break;
    case 'm':glClearColor(0.5,0.5,1.0,1.0);
             i=1;
             col=0.2;
             glutPostRedisplay();
         break;
    case 'a':glClearColor(0.9,0.9,0.3,1.0);
             i=2;
             col=0.3;
             glutPostRedisplay();
         break;
    case 'e':glClearColor(0.8,0.5,0.4,1.0);
             col=-0.1;
             i=0;
             glutPostRedisplay();
         break;
    case 'n':glClearColor(0.5,0.5,0.5,1.0);
             i=3;
             glutPostRedisplay();
             col=-0.2;
             break;
    case 'd':glClearColor(0.0,0.0,0.0,0.0);
             i=4;
             col=-0.3;
             glutPostRedisplay();
         break;
    case '6':pos+=2;
             break;
    case '4':pos-=2;
             break;
    case 'z':cf=1;
             pr=0;
             glutPostRedisplay();
             break;
   }
}
void SpecialInput(int key, int x, int y)
{
switch(key)
{
case GLUT_KEY_UP:
break;
case GLUT_KEY_DOWN:
break;
case GLUT_KEY_LEFT:
pos-=2;
break;
case GLUT_KEY_RIGHT:
pos+=2;
break;
}
}

int main()
{
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
glutInitWindowSize(1024,768);
glutInitWindowPosition(0,0);
glutCreateWindow("Black Pearl - The Cursed Ship");
glutSwapBuffers();
glutKeyboardFunc(keyboard);
glutDisplayFunc(display);
glutSpecialFunc(SpecialInput);
init();
glutMainLoop();
}