Develop a program to demonstrate 3D transformation on 3D objects - tetrahedron
#include <windows.h>
#include <GL/glut.h>
#include <math.h>
// Define the vertices of a tetrahedron
GLfloat vertices[][3] = {
{1.0, 1.0, 1.0},
{-1.0, -1.0, 1.0},
{-1.0, 1.0, -1.0},
{1.0, -1.0, -1.0}
};
// Define the faces of the tetrahedron
GLint faces[][3] = {
{0, 1, 2},
{0, 1, 3},
{0, 2, 3},
{1, 2, 3}
};
// Colors for each face
GLfloat colors[][3] = {
{1.0, 0.0, 0.0}, // Red
{0.0, 1.0, 0.0}, // Green
{0.0, 0.0, 1.0}, // Blue
{1.0, 1.0, 0.0} // Yellow
};
GLfloat rotationX = 0.0f, rotationY = 0.0f, rotationZ = 0.0f;
GLfloat translateX = 0.0f, translateY = 0.0f, translateZ = -5.0f;
GLfloat scale = 1.0f;
// Function to draw the tetrahedron
void drawTetrahedron() {
for (int i = 0; i < 4; i++) {
glBegin(GL_TRIANGLES);
glColor3fv(colors[i]);
glVertex3fv(vertices[faces[i][0]]);
glVertex3fv(vertices[faces[i][1]]);
glVertex3fv(vertices[faces[i][2]]);
glEnd();
}
}
// Display callback
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Apply transformations
glLoadIdentity();
glTranslatef(translateX, translateY, translateZ); // Translate the object
glRotatef(rotationX, 1.0, 0.0, 0.0); // Rotate the object around the x-axis
glRotatef(rotationY, 0.0, 1.0, 0.0); // Rotate the object around the y-axis
glRotatef(rotationZ, 0.0, 0.0, 1.0); // Rotate the object around the z-axis
glScalef(scale, scale, scale); // Scale the object
// Draw the larger tetrahedron
drawTetrahedron();
// Draw smaller tetrahedrons within the larger one
for (int i = 0; i < 4; i++) {
glPushMatrix();
glTranslatef(vertices[i][0] * 0.5, vertices[i][1] * 0.5, vertices[i][2] * 0.5);
glScalef(0.5, 0.5, 0.5);
drawTetrahedron();
glPopMatrix();
}
glutSwapBuffers();
}
// Keyboard callback
void keyboard(unsigned char key, int x, int y) {
switch (key) {
case 'x': rotationX += 5.0f; break;
case 'X': rotationX -= 5.0f; break;
case 'y': rotationY += 5.0f; break;
case 'Y': rotationY -= 5.0f; break;
case 'z': rotationZ += 5.0f; break;
case 'Z': rotationZ -= 5.0f; break;
case 'w': translateY += 0.1f; break;
case 's': translateY -= 0.1f; break;
case 'a': translateX -= 0.1f; break;
case 'd': translateX += 0.1f; break;
case 'q': translateZ += 0.1f; break;
case 'e': translateZ -= 0.1f; break;
case '+': scale *= 1.1f; break;
case '-': scale /= 1.1f; break;
case 27: exit(0); break; // Escape key to exit
}
glutPostRedisplay();
}
// Initialize OpenGL
void init() {
glClearColor(0.0, 0.0, 0.0, 1.0);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
gluPerspective(45.0, 1.0, 1.0, 10.0);
glMatrixMode(GL_MODELVIEW);
}
// Main function
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutCreateWindow("3D Transformations on Tetrahedron");
init();
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}
Explanation:
1. Vertices and Faces:
o The vertices array defines the four vertices of the tetrahedron.
o The faces array defines the four triangular faces of the tetrahedron using indices
into the vertices array.
2. Colors:
o The colors array defines a unique color for each face of the tetrahedron.
3. Drawing the Tetrahedron:
o The drawTetrahedron function uses glBegin(GL_TRIANGLES) and glEnd() to
draw each face of the tetrahedron, setting the color for each face before drawing.
4. Transformation and Animation:
o The display function clears the screen and depth buffer, sets the identity matrix,
and applies transformations (translation, rotation, and scaling).
o It first draws the larger tetrahedron, then smaller tetrahedrons within the larger
one.
5. Keyboard Interaction:
o The keyboard function handles keyboard input to adjust the rotation angles,
translation values, and scaling factor.
o It uses GLUT's glutKeyboardFunc to register the keyboard callback.
6. Initialization and Main Loop:
o The init function sets up the OpenGL environment, including the clear color,
depth test, and projection matrix.
o The main function initializes GLUT, creates the window, sets up the display and
keyboard callbacks, and enters the GLUT main loop.
Controls:
x/X: Rotate around the x-axis.
y/Y: Rotate around the y-axis.
z/Z: Rotate around the z-axis.
w: Translate up.
s: Translate down.
a: Translate left.
d: Translate right.
q: Translate forward.
e: Translate backward.
+: Scale up.
-: Scale down.
Esc: Exit the program.
A tetrahedron is a type of polyhedron that is composed of four triangular faces, six straight
edges, and four vertex corners. It's the simplest of all the ordinary convex polyhedra and is a type
of pyramid. Here’s some more detailed information about a tetrahedron:
Properties of a Tetrahedron
1. Faces: A tetrahedron has four triangular faces.
2. Vertices: It has four vertices.
3. Edges: There are six edges in a tetrahedron.
4. Regular Tetrahedron: If all faces are equilateral triangles, it is called a regular
tetrahedron. In a regular tetrahedron, all edges are of the same length, and all angles are
equal.
Dual Polyhedron: The dual of a tetrahedron is another tetrahedron. This means that if you take the
midpoints of all the faces of a tetrahedron and connect them, you get another tetrahedron.
Visualization
A regular tetrahedron can be visualized as a pyramid with a triangular base. It can be inscribed
inside a cube such that its vertices are four of the eight vertices of the cube, no two of which
share a common face.
Tetrahedron in Programming
In programming, particularly in 3D graphics, a tetrahedron can be represented by its vertices and
faces. For example, in OpenGL, you can define a tetrahedron with specific coordinates for its
vertices and then use these vertices to render the faces.
Here’s a simple representation of a tetrahedron in terms of its vertices and how it might be visualized or
programmed:
Vertices:
V0: (1, 1, 1)
V1: (-1, -1, 1)
V2: (-1, 1, -1)
V3: (1, -1, -1)
Faces (triangular faces formed by connecting the vertices):
Face 1: V0, V1, V2
Face 2: V0, V1, V3
Face 3: V0, V2, V3
Face 4: V1, V2, V3
Develop a program to demonstrate 3D transformation on 3D objects – cylinder
#include <windows.h>
#include <GL/glut.h>
#include <math.h>
GLfloat rotationX = 0.0f, rotationY = 0.0f, rotationZ = 0.0f;
GLfloat translateX = 0.0f, translateY = 0.0f, translateZ = -5.0f;
GLfloat scale = 1.0f;
const int slices = 30;
const int stacks = 1;
// Function to draw a cylinder
void drawCylinder(float radius, float height) {
GLfloat angleStep = (2.0f * M_PI) / slices;
GLfloat heightStep = height / stacks;
// Draw sides
for (int i = 0; i < stacks; i++) {
GLfloat z0 = (i * heightStep) - (height / 2);
GLfloat z1 = ((i + 1) * heightStep) - (height / 2);
glBegin(GL_QUAD_STRIP);
glColor3f(0.0f, 0.0f, 1.0f); // Blue sides
for (int j = 0; j <= slices; j++) {
GLfloat angle = j * angleStep;
GLfloat x = radius * cos(angle);
GLfloat y = radius * sin(angle);
glVertex3f(x, y, z0);
glVertex3f(x, y, z1);
}
glEnd();
}
// Draw top
glBegin(GL_TRIANGLE_FAN);
glColor3f(1.0f, 0.0f, 0.0f); // Red top
glVertex3f(0.0f, 0.0f, height / 2);
for (int i = 0; i <= slices; i++) {
GLfloat angle = i * angleStep;
GLfloat x = radius * cos(angle);
GLfloat y = radius * sin(angle);
glVertex3f(x, y, height / 2);
}
glEnd();
// Draw bottom
glBegin(GL_TRIANGLE_FAN);
glColor3f(0.0f, 1.0f, 0.0f); // Green bottom
glVertex3f(0.0f, 0.0f, -height / 2);
for (int i = 0; i <= slices; i++) {
GLfloat angle = i * angleStep;
GLfloat x = radius * cos(angle);
GLfloat y = radius * sin(angle);
glVertex3f(x, y, -height / 2);
}
glEnd();
}
// Display callback
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Apply transformations
glLoadIdentity();
glTranslatef(translateX, translateY, translateZ); // Translate the object
glRotatef(rotationX, 1.0, 0.0, 0.0); // Rotate the object around the x-axis
glRotatef(rotationY, 0.0, 1.0, 0.0); // Rotate the object around the y-axis
glRotatef(rotationZ, 0.0, 0.0, 1.0); // Rotate the object around the z-axis
glScalef(scale, scale, scale); // Scale the object
// Draw the cylinder
drawCylinder(1.0, 2.0);
glutSwapBuffers();
}
// Keyboard callback
void keyboard(unsigned char key, int x, int y) {
switch (key) {
case 'x': rotationX += 5.0f; break;
case 'X': rotationX -= 5.0f; break;
case 'y': rotationY += 5.0f; break;
case 'Y': rotationY -= 5.0f; break;
case 'z': rotationZ += 5.0f; break;
case 'Z': rotationZ -= 5.0f; break;
case 'w': translateY += 0.1f; break;
case 's': translateY -= 0.1f; break;
case 'a': translateX -= 0.1f; break;
case 'd': translateX += 0.1f; break;
case 'q': translateZ += 0.1f; break;
case 'e': translateZ -= 0.1f; break;
case '+': scale *= 1.1f; break;
case '-': scale /= 1.1f; break;
case 27: exit(0); break; // Escape key to exit
}
glutPostRedisplay();
}
// Initialize OpenGL
void init() {
glClearColor(0.0, 0.0, 0.0, 1.0);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
gluPerspective(45.0, 1.0, 1.0, 10.0);
glMatrixMode(GL_MODELVIEW);
}
// Main function
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutCreateWindow("3D Transformations on Cylinder");
init();
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}
Explanation:
1. Vertices and Faces:
o In this program, the cylinder is divided into slices around the circumference and
stacks along its height.
2. Colors:
o Different colors are assigned to the top (red), bottom (green), and sides (blue) of
the cylinder.
3. Drawing the Cylinder:
o The drawCylinder function uses trigonometry to compute the vertices of the
cylinder's sides, top, and bottom.
o It uses glBegin(GL_QUAD_STRIP) to draw the side faces and
glBegin(GL_TRIANGLE_FAN) to draw the top and bottom faces.
4. Transformation and Animation:
o The display function clears the screen and depth buffer, sets the identity matrix,
and applies transformations (translation, rotation, and scaling).
o It then calls drawCylinder to render the cylinder.
5. Keyboard Interaction:
o The keyboard function handles keyboard input to adjust the rotation angles,
translation values, and scaling factor.
o It uses GLUT's glutKeyboardFunc to register the keyboard callback.
6. Initialization and Main Loop:
o The init function sets up the OpenGL environment, including the clear color,
depth test, and projection matrix.
o The main function initializes GLUT, creates the window, sets up the display and
keyboard callbacks, and enters the GLUT main loop.
Controls:
x/X: Rotate around the x-axis.
y/Y: Rotate around the y-axis.
z/Z: Rotate around the z-axis.
w: Translate up.
s: Translate down.
a: Translate left.
d: Translate right.
q: Translate forward.
e: Translate backward.
+: Scale up.
-: Scale down.
Esc: Exit the program.