Develop A Program To Demonstrate 3D Transformation On 3D Objects - Tetrahedron
Develop A Program To Demonstrate 3D Transformation On 3D Objects - Tetrahedron
#include <windows.h>
#include <GL/glut.h>
#include <math.h>
// 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
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:
Controls:
Properties of a Tetrahedron
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
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)
#include <windows.h>
#include <GL/glut.h>
#include <math.h>
// 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
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:
Controls: