Fan design using Computer Graphics (cpp)
Fan design using Computer Graphics (cpp)
//Faisal Porag//Computer Graphics
/*স্বপ্নের প্রতিবিম্ব... **/
#include <iostream>
#include <stdlib.h>
#include <math.h>
#include <GL/glut.h>
/*Faisal porag
Computer science and Engineering department in
American International University-Bangladesh*/
//Initializes 3D rendering
void initRendering() {
glEnable(GL_DEPTH_TEST);
}
//Called when the window is resized
void handleResize(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (double)w / (double)h, 1.0, 200.0);
}
float _angle = 0.0;
float _cameraAngle = 0.0;
float _ang_tri = 0.0;
//Draws the 3D scene
void drawScene() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspective
glLoadIdentity(); //Reset the drawing perspective
glRotatef(-_cameraAngle, 0.0, 1.0, 0.0); //Rotate the camera
glTranslatef(0.0, 0.0, -7.0); //Move forward 5 units
glPushMatrix(); //Save the transformations performed thus far
glTranslatef(1.0, 1.0, .85); //Move to the center of the trapezoid
//glRotatef(_ang_tri, 1.0, 0.0, 0.0); //Rotate about the z-axis
glRotatef(_ang_tri, 0.0, 1.0, 1.0); //Rotate about the z-axis
glScalef(1.3, 1.9, 0.0); //Scale by 0.7 in the x, y, and z directions
glPopMatrix(); //Undo the move to the center of the pentagon
glBegin(GL_TRIANGLES);
//Triangle
glVertex3f(0.5, -0.5, 0.0);
glVertex3f(0.0, 0.5, 0.0);
glVertex3f(-0.5, -0.5, 0.0);
glEnd();
glPopMatrix(); //Undo the move to the center of the trapezoid
glPushMatrix(); //Save the current state of transformations
glTranslatef(-1.0, 1.0, 0.0); //Move to the center of the pentagon
glRotatef(_angle, 0.0, -1.0, 0.0); //Rotate about the y-axis
glScalef(0.3, 0.3, 0.0); //Scale by 0.7 in the x, y, and z directions
glBegin(GL_QUADS);
//Trapezoid
glVertex3f(0.5, 0.5, 0.0);
glVertex3f(0.3, -0.7, 0.0);
glVertex3f(-0.8, -0.7, 0.0);
glVertex3f(-0.5, 0.5, 0.0);
glEnd();
glPopMatrix(); //Undo the move to the center of the pentagon
//================================================
glPushMatrix(); //Save the current state of transformations
glTranslatef(-2.0, 0.0, 0.5); //Move to the center of the triangle
glScalef(0.3,0.3,0.56);
glRotatef(_angle, 0.3, 0.0, 0.5); //Rotate about the the vector (1, 2, 3)
glBegin(GL_POLYGON);
glColor3f(0.60,0.86,0.80);
for(int i=0;i<200;i++)
{
float pi=3.1416;
float A=(i*2*pi)/50;
//float r=0.55;
float r = 1;
float x = r * cos(A);
float y = r * sin(A);
glVertex2f(x,y );
}
glEnd();
//==================================
glTranslatef(0.8, -1.0, 0.5);
glBegin(GL_POLYGON);
glColor3f(0.60,0.86,0.80);
glVertex3f(0.5, 0.5, 0.0);
glVertex3f(4.0, 0.5, 0.0);
glVertex3f(4.0, 1.5, 0.0);
glVertex3f(0.5, 1.5, 0.0);
glEnd();
//================================
glTranslatef(-0.7, 1.95, 0.5);
glBegin(GL_POLYGON);
glColor3f(0.60,0.86,0.80);
//
glVertex3f(-0.5, -0.5, 0.0);
glVertex3f(-4.0, -0.5, 0.0);
glVertex3f(-4.0, -1.5, 0.0);
glVertex3f(-0.5, -1.5, 0.0);
glEnd();
//==============================
glTranslatef(-0.25, -0.5, 0.5);
glBegin(GL_POLYGON);
glColor3f(0.60,0.86,0.80);
glVertex3f(0.5, 0.5, 0.0);
glVertex3f(1.6, 0.5, 0.0);
glVertex3f(1.6, 4.0, 0.0);
glVertex3f(0.5, 4.0, 0.0);
glEnd();
//==============================
glTranslatef(2.35, -0.85, 0.5);
glBegin(GL_POLYGON);
glColor3f(0.60,0.86,0.80);
//
glVertex3f(-0.5, -0.5, 0.0);
glVertex3f(-1.6, -0.5, 0.0);
glVertex3f(-1.6, -4.0, 0.0);
glVertex3f(-0.5,- 4.0, 0.0);
glEnd();
//==========================================
//==========================================
glPopMatrix(); //Undo the move to the center of the triangle
glutSwapBuffers();
}
void update(int value) {
_angle += 2.0f;
if (_angle > 360) {
_angle -= 360;
}
_ang_tri += 2.0f;
if (_ang_tri > 360) {
_ang_tri -= 360;
}
glutPostRedisplay(); //Tell GLUT that the display has changed
//Tell GLUT to call update again in 25 milliseconds
glutTimerFunc(25, update, 0);
}
int main(int argc, char** argv) {
//Initialize GLUT
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(800, 500);
//Create the window
glutCreateWindow("Transformations");
initRendering();
//Set handler functions
glutDisplayFunc(drawScene);
glutReshapeFunc(handleResize);
glutTimerFunc(25, update, 0); //Add a timer
glutMainLoop();
return 0;
}
//Output
//Try this code for 3D Fan
/*স্বপ্নের প্রতিবিম্ব... **/
মন্তব্যসমূহ
একটি মন্তব্য পোস্ট করুন