Search Projects

Thursday, July 6, 2017

WireFrame SnowMan

It's Fun to lean OpenGL!

Today we are going to learn about making an WireFrame SnowMan from scratch.

1. Overview

To draw a snowman in OpenGL C++, you need to first figure out the sketch line. Entire shape need to drawn with the help of proper OpenGL primitives and placed them at proper coordinate positions. So basically you need to understand the coordinate system properly.

The shape of Snowman looks to be a big snowball which make body, and small ball over it which forms head. Now we have two dots over the small ball for creating eye and a conical shape for nose.

>3D Coordinates
We will draw the three lines across for each of the x, y, z axis.

>User Interaction
With this program user can move the camera position to left and right and zoom in and out.

2. Draw SnowMan

To draw snow man we use the inbuilt function opengl. To draw the big ball body part, use the glutWireSphere and glutSolidSphere. To draw the eyes use only glutSolidSphere and for nose use glutSolidCone. The color has specified before same as we do in other  cases which you can change. 

glColor3f(1, 1, 1);
glTranslatef(0, 0.75, 0);
glutWireSphere(0.75, 10, 10);
glTranslatef(0, 1, 0);
glutSolidSphere(0.25, 10, 10);

//Draw eyes
glTranslatef(0.1, 0, 0.23);
glColor3f(0, 0, 1);
glutSolidSphere(0.03, 10, 10);
glTranslatef(-0.2, 0, 0);
glutSolidSphere(0.03, 10, 10);

glTranslatef(0, -0.05, 0.25);
glColor3f(1, 0, 0);
glutSolidCone(0.025, 0.25, 4, 4);

3. Keyboard Function

First define two global variable angle and zview. These will act the amount of motion or move we required when particular key is pressed. The angle will take care of movement of camera in right and left direction. For zoomin and zoomout- zview has been used. So define a keyboard function and used switch to define the input and take action accordingly. Here we will use the four navigation keys whose GLUT forms are as  - GLUT_KEY_LEFT, GLUT_KEY_RIGHT, GLUT_KEY_UP, and GLUT_KEY_DOWN. Below code explains all.

void processSpecialKeys(int key, int x, int y)
switch (key)
angle -= 0.5;

angle += 0.5;

zView -= 0.5;

zView += 0.5;

3. Display Function

We have drawn all the things and put the action for camera movement. One thing we will do in display function is draw the x, y, z axis lines in red color. Now also call all the function which allow OpenGL to display objects on the screen that is drawsnow etc.

void display(void)

gluLookAt(xView, yView, zView,
xView + lx, yView + ly, zView + lz,
0, 1, 0);
glRotatef(angle, 0, 1, 0);

glColor3f(1, 0, 0);
glVertex3f(0, 0, 0);
glVertex3f(1.5, 0, 0);
glVertex3f(0, 0, 0);
glVertex3f(0, 3, 0);
glVertex3f(0, 0, 0);
glVertex3f(0, 0, 1.5);

glColor3f(0.5, 0.5, 0.5);
glVertex3f(-100, 0, -100);
glVertex3f(-100, 0, 100);
glVertex3f(100, 0, 100);
glVertex3f(100, 0, -100);




4. Main Function

Finally code the main function which is essential part of any OpenGL program. You can Download the whole source code from the link given.

Share your feedback and ideas in the comment box.

No comments:

Post a Comment