Wednesday, September 24, 2014

Tetra Turns into a Sphere and Viceversa

You may be aware of how a tetrahedron looks like. It is a polyhedra made up of four equilateral triangles.
 In this program all the equilateral triangles have a sierpinski gasket on them. Sierpinski gasket is a fractal drawn recursively by dividing the edges of the triangle and then inscribing a triangle with vertices at the center of each edge. Except the inscribed triangle all other triangles are repeatedly subdivided. Here is how a Sierpinski gasket looks like...
In my program that is the first one where the tetra turns into a sphere....i have colored the triangles inside the tetrahedron using Red, Green, Blue and Black Colors.
I will show a snippet of code which is doing this magic.
Here is the source.
 In the function normalize i have added the following code:(See comments)
void normalize(GLfloat *p)
{ double d=0.0;
int i;
for(i=0;i<3;i++) d+=p[i]*p[i]; //find the length of the vector
d=sqrt(d); // starting from origin to face of tetra
d=d+(1-d)*sf; // sf(scaling factor) has abs of sine wave 
                           // which is set in idle function
 if(max<d) max=d; // this statement i guess is not needed
if(d>0.0)
for(i=0;i<3;i++) p[i]/=d;//divide the vector components by the
}                                            // newly required length in d
Here is the idle function
void idle()
{ sf=fabs(sin(angle)); // as mentioned abs of sine wave
angle+=0.0004; // increment angle
if(angle>=360) angle=0; // if angle overflows
glutPostRedisplay(); // call display every possible moment
}
Happy Coding!
Small modification now the up and down arrow increases or decreases the angle and hence the scaling factor which decides the difference between the tetra and the sphere. Here is the modified code.

Tuesday, September 23, 2014

Another modification to the tetra program

Another modification to the tetra program that i made my poor students do were to add a timer to the program. In the timer function the color of the four faces of the tetra are changed. Its a glittering program where the tetra changes color every second. Some may say i went crazy making the students do these modifications. They were begging me to explain the code and i could just mumble that you will not understand now. When i covered the topic on timers i guess they understood.

Here are the snapshots:



Here is the source.

Modification to house rotation program

This program asks for initial rotation angle and starting from that orientation keeps rotating the house about the pivot point. Meaning less modification you may say but this is what i taught the students in the lab.

Snapshots:




Here is the source.

Modifications made to Cylinder and Parallelepiped

Modifications made to Cylinder and Parallelepiped program. The scene has been made to rotate on mouse input.
Several modifications need to be made in code to achieve this.

Snapshots:






Here is the source.

Program to draw a teapot over a table and rotate the scene using mouse

Program to draw a teapot over a table and rotate the scene using mouse input. This is the same program as the teapot program. The only modifications done are color of the teapot and the walls are changed. The scene is rotated using mouse input(on dragging the mouse).

Here are the snapshots:








Here is the source.

Drawing a sphere using a tetrahedron in OpenGL

This is a program in the seventh unit of Computer Graphics & Visualization (14CS65) of 6th semester CSE branch in VTU. This program has been modified to rotate the scene using mouse input. It asks for number of divisions. Once that is entered it draws a sphere using the same program to draw a tetra. A brief explanation of the code will follow soon.

Here are the snapshots:







Here is the source. Have a look at an animation of the above transformation where the tetra turns into a sphere and vice-versa. In another modification I have created a tetra with colors of sides changing every second. Both the above modifications require us to use a timer and an idle function.