In this blogpost we are going to show you simple scenery opengl computer graphics project with source code. We are going to code a simple scenery use the opengl graphics library using c/c++.
Aim of the Project
As said above we aim to create a scenery but big question is which type of scenery and what will be shown.
We are going to create mountains with Sun/Moon in sky. There is one road crossing the mountain. We also going to draw tall buildings, traffic light signals and tower. There will be two mode in the scenery - day and night. In day light scene in sky sun will be display along with some birds fluying around. In the night scene we have moon and airplane flying in the sky along star twinkling.
In both day and night scene, there is bus running on the road and clouds roaming in sky. Color of sky will be skyblue in day and black-opaque in night. Cloud will be white color. Road will be in black color along with some white strips.
Few Lines from song "City Of Stars" - Movie: La La Land
City of stars
Are you shining just for me?
City of stars
There's so much that I can't see
Implementation of computer graphics project
This project uses different OpenGL functions to create different objects.
Coding for Sun
glLoadIdentity();
glBegin(GL_POLYGON);
glColor3ub(255.0f, 255.0f, 128.0f);
float angle10;
for (int i=0;i<360;i++)
{
angle10=i*3.1416/180;
glVertex2f(0.0+0.12*cos(angle10),0.9+0.12*sin(angle10));
}
glEnd();
Coding for Moon
glLoadIdentity();
glBegin(GL_POLYGON);
glColor3ub(255, 255, 255);
GLfloat x5=.7f;
GLfloat y5=.7f;
GLfloat radius5 =.07f;
GLfloat twicePi5 = 2.0f * PI;
GLfloat triangleAmount5=100;
glBegin(GL_TRIANGLE_FAN);
glVertex2f(x5, y5); // center of circle
for(int i = 0; i <= triangleAmount5; i++)
{
glVertex2f(
x5 + (radius5 * cos(i * twicePi5 / triangleAmount5)),
y5 + (radius5 * sin(i * twicePi5 / triangleAmount5))
);
}
glEnd();
Code for Airplane
glLoadIdentity();
glPushMatrix();
glScalef(0.4,0.4,0);
glTranslatef(0.0f,1.5f, 0.0f);
glTranslatef(position6, 0.0f, 0.0f);
glBegin(GL_POLYGON); // airplane outer body
glColor3ub(234, 229, 229); // white
glVertex2f(-0.3f, 0.2f);
glVertex2f(0.3f, 0.2f);
glVertex2f(0.4f, 0.3f);
glVertex2f(0.45f, 0.4f);
glVertex2f(0.45f, 0.5f);
glVertex2f(0.3f, 0.4f);
glVertex2f(0.0f, 0.4f);
glVertex2f(-0.2f, 0.4f);
glVertex2f(-0.3f, 0.4f);
glVertex2f(-0.35f, 0.38f);
glVertex2f(-0.4f, 0.3f);
glVertex2f(-0.35f, 0.23f);
glVertex2f(-0.3f, 0.2f);
glEnd();
glBegin(GL_POLYGON); // airplane front window
glColor3ub(117, 69, 160); // purple one
glVertex2f(-0.37f, 0.35f);
glVertex2f(-0.32f, 0.35f);
glVertex2f(-0.3f, 0.36f);
glVertex2f(-0.3f, 0.38f);
glVertex2f(-0.35f, 0.38f);
glEnd();
glBegin(GL_LINES); // airplane front window
glColor3ub(255, 255, 255); // white
glVertex2f(-0.35f, 0.35f);
glVertex2f(-0.33f, 0.38f);
glVertex2f(-0.33f, 0.35f);
glVertex2f(-0.31f, 0.38f);
glEnd();
glBegin(GL_LINES); // airplane front door
glColor3ub(144, 126, 181); // purple one
glVertex2f(-0.25f, 0.26f);
glVertex2f(-0.23f, 0.24f);
glVertex2f(-0.23f, 0.24f);
glVertex2f(-0.18f, 0.24f);
glVertex2f(-0.18f, 0.24f);
glVertex2f(-0.16f, 0.26f);
glVertex2f(-0.16f, 0.26f);
glVertex2f(-0.16f, 0.36f);
glVertex2f(-0.16f, 0.36f);
glVertex2f(-0.18f, 0.38f);
glVertex2f(-0.18f, 0.38f);
glVertex2f(-0.23f, 0.38f);
glVertex2f(-0.23f, 0.38f);
glVertex2f(-0.25f, 0.36f);
glVertex2f(-0.25f, 0.36f);
glVertex2f(-0.25f, 0.26f);
glEnd();
glBegin(GL_QUADS); // Airplane front wind
glColor3ub(117, 69, 160); // purple one
glVertex2f(0.02f, 0.15f);
glVertex2f(0.05f, 0.15f);
glVertex2f(-0.03f, 0.26f);
glVertex2f(-0.1f, 0.26f);
glEnd();
glBegin(GL_QUADS); // Airplane back wind
glColor3ub(117, 69, 160); // purple one
glVertex2f(-0.08f, 0.4f);
glVertex2f(0.0f, 0.4f);
glVertex2f(0.07f, 0.49f);
glVertex2f(0.04f, 0.49f);
glEnd();
// Airplane window
glColor3ub(117, 69, 160); // purple one
GLfloat x2=-0.09f;
GLfloat y2=0.35f;
GLfloat radius1 =.02f;
int triangleAmount1 = 100;
GLfloat twicePi1 = 2.0f * PI;
glBegin(GL_TRIANGLE_FAN);
glVertex2f(x2, y2); // center of circle
for(int i = 0; i <= triangleAmount1; i++)
{
glVertex2f(
x2 + (radius1 * cos(i * twicePi1 / triangleAmount1)),
y2 + (radius1 * sin(i * twicePi1 / triangleAmount1))
);
}
glEnd();
// Airplane window
glColor3ub(117, 69, 160); // purple one
GLfloat x3=-0.02f;
GLfloat y3=0.35f;
radius1 =.02f;
twicePi1 = 2.0f * PI;
glBegin(GL_TRIANGLE_FAN);
glVertex2f(x3, y3); // center of circle
for(int i = 0; i <= triangleAmount1; i++)
{
glVertex2f(
x3 + (radius1 * cos(i * twicePi1 / triangleAmount1)),
y3 + (radius1 * sin(i * twicePi1 / triangleAmount1))
);
}
glEnd();
// Airplane window
glColor3ub(117, 69, 160); // purple one
GLfloat x4=0.06f;
GLfloat y4=0.35f;
radius1 =.02f;
twicePi1 = 2.0f * PI;
glBegin(GL_TRIANGLE_FAN);
glVertex2f(x4, y4); // center of circle
for(int i = 0; i <= triangleAmount1; i++)
{
glVertex2f(
x4 + (radius1 * cos(i * twicePi1 / triangleAmount1)),
y4 + (radius1 * sin(i * twicePi1 / triangleAmount1))
);
}
glEnd();
glPopMatrix();
Code for Birds
glLoadIdentity();
glPushMatrix();
glTranslatef(position5,0.0f, 0.0f);
glBegin(GL_POLYGON);
glColor3ub(0.0, 0.0, 0.0);
float angle41;
for (int i=0;i<360;i++)
{
angle41=i*3.1416/180;
glVertex2f(0.8+0.04*cos(angle41),0.7+0.02*sin(angle41));
}
glEnd();
glBegin(GL_POLYGON);
glColor3ub(0.0, 0.0, 0.0);
float angle42;
for (int i=0;i<360;i++)
{
angle42=i*3.1416/180;
glVertex2f(0.75+0.02*cos(angle42),0.71+0.02*sin(angle42));
}
glEnd();
glBegin(GL_POLYGON);
glColor3ub(255.0, 255.0, 255.0);
float angle43;
for (int i=0;i<360;i++)
{
angle43=i*3.1416/180;
glVertex2f(0.74+0.005*cos(angle43),0.71+0.005*sin(angle43));
}
glEnd();
//wing
glBegin(GL_TRIANGLES);
glColor3ub(255.0, 255.0, 255.0);
glVertex2f(0.8f,0.75f);
glVertex2f(0.79,0.71f);
glVertex2f(0.785f,0.8f);
glEnd();
//lip
glBegin(GL_TRIANGLES);
glColor3ub(255.0, 0.0, 0.0);
glVertex2f(0.73f,0.72f);
glVertex2f(0.69f,0.71f);
glVertex2f(0.73f,0.70f);
glEnd();
In our Simple City Scenery opengl computer graphics program we have used three birds. You can use many birds you like, just adjust the coordinate of thier body so they fit in the scene. Code for the bird will be common for all just need some tweak in the coordinates.
You can find full source code in the download link.
User Interaction
In this opengl computer graphics project we have used both keyboard and mouse interaction. Separate opengl functions has been use for calling mouse events and keyboard events. There only few intractions that user and do with this program.
Keyboard Interaction
Following are keyboard functions used for user in this opengl program.
's': With this key Bus/Vechile in the program stop moving and halt at the postion where this key has been place.
'r': With this key Bus/Vechile in the program start running/movin. It will start from postion where it halting.
'd': With this key user can switch to day mode when in the night mode.
'n': This key will allow user to change from day mode to night mode.
Learn about Functions used
Funcrions | Descritpions |
---|---|
GL_TRIANGLE_FAN | The first vertex is always held fixed. From there on, every group of 2 adjacent vertices form a triangle with the first. |
glLoadIdentity() | id |
glutTimerFunc() | d |
glutPostRedisplay() | |
GL_QUADS | |
glPushMatrix |
Future Encashment
We would love to hear from you with below feature you can add in this Computer Graphics Mini Project to make it more interesting.
- Add more building/houses in the project.
- Draw river around which flow from the mountain in the program and it contain bridge if possible.
- Add more vechiles on the road with sound effects of horns.
- Add footpath for people and let some human walki in.
Video Demo
Source Code Download
Download source code of this Computer Graphics Mini Project from the link provide in the post below. You are free to ask anything about this projects to us in comments section. You may also write an email to us.
No comments:
Post a Comment