2D Fan design in computer graphics using c++


/**Faisal Porag
 **/

///Source code : using c++

#include <iostream>
#include <stdlib.h>
#include <math.h>



#include <GL/glut.h>




//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





glPopMatrix(); //Undo the move to the center of the pentagon
glPushMatrix(); //Save the current state of transformations
glTranslated(0.0,1.0,0.0);
glTranslatef(-1.8, -1.0, 1.0); //Move to the center of the triangle
glScalef(0.4,0.4,0);
glRotatef(_angle, 0.0, 0.0, 1.0); //Rotate about the the vector (1, 2, 3)

glBegin(GL_LINES);
//glBegin(GL_LINE_LOOP);

//glBegin(GL_POLYGON);
    glColor3f(1.0,1.0,0.8);

for(int i=0;i<200;i++)
{
float pi=3.1416;
float A=(i*2*pi)/50;
float r=0.95;
float x = r * cos(A);
float y = r * sin(A);
glVertex2f(x,y );
}

glEnd();


    //=============
    ///Right

    glTranslatef(1.0, -1.5, 1.0);
    //glScalef(0.5,5.5,0.0);
    glBegin(GL_POLYGON);
    glColor3f(0.55,0.43,0.85);

    glVertex3f(0.5,1.0,0);
    glVertex3f(3.15,1.0,0);
    glVertex3f(3.15,2.0,0);
    glVertex3f(0.5,2.0,0);

    glEnd();
    glFlush();

    glTranslatef(-0.7, 0.34, 1.0);
    //glScalef(0.5,5.5,0.0);
    glBegin(GL_POLYGON);
    glColor3f(0.55,0.0,0.25);

    glVertex3f(0.5,1.0,0);
    glVertex3f(1.5,1.0,0);
    glVertex3f(1.5,1.3,0);
    glVertex3f(0.5,1.3,0);

    glEnd();

    //========================
    ///Left

     glTranslatef(-1.4, 2.68, 1.0);
    //glScalef(0.5,5.5,0.0);
    glBegin(GL_POLYGON);
    glColor3f(0.55,0.43,0.85);

    glVertex3f(-0.5,-1.0,0);
    glVertex3f(-3.15,-1.0,0);
    glVertex3f(-3.15,-2.0,0);
    glVertex3f(-0.5,-2.0,0);

    glEnd();
    glFlush();


    glTranslatef(-1.2, -2.65, 1.0);
    //glScalef(0.5,5.5,0.0);
    glBegin(GL_POLYGON);
    glColor3f(0.55,0.0,0.25);

    glVertex3f(0.5,1.0,0);
    glVertex3f(1.5,1.0,0);
    glVertex3f(1.5,1.3,0);
    glVertex3f(0.5,1.3,0);

    glEnd();

    //=======================

    ///Above

     glTranslatef(1.2, 1.5, 1.0);
    //glScalef(0.5,5.5,0.0);
    glBegin(GL_POLYGON);
    glColor3f(0.55,0.43,0.85);

    glVertex3f(0.5,1.0,0);
    glVertex3f(1.68,1.0,0);
    glVertex3f(1.68,3.5,0);
    glVertex3f(0.5,3.5,0);

    glEnd();
    glFlush();

     glTranslatef(0.40, -0.635, 1.0);
    //glScalef(0.5,5.5,0.0);
    glBegin(GL_POLYGON);
    glColor3f(0.55,0.0,0.25);

    glVertex3f(0.5,1.0,0);
    glVertex3f(0.85,1.0,0);
    glVertex3f(0.85,2.1,0);
    glVertex3f(0.5,2.1,0);

    glEnd();

    //========================

    ///Bottom


     glTranslatef(1.8, -0.3, 1.0);
    //glScalef(0.5,5.5,0.0);
    glBegin(GL_POLYGON);
    glColor3f(0.55,0.43,0.85);

    glVertex3f(-0.5,-1.0,0);
    glVertex3f(-1.68,-1.0,0);
    glVertex3f(-1.68,-3.5,0);
    glVertex3f(-0.5,-3.5,0);

    glEnd();
    glFlush();

    glTranslatef(-1.72, -2.25, 1.0);
    //glScalef(0.5,5.5,0.0);
    glBegin(GL_POLYGON);
    glColor3f(0.55,0.0,0.25);

    glVertex3f(0.5,1.0,0);
    glVertex3f(0.85,1.0,0);
    glVertex3f(0.85,2.1,0);
    glVertex3f(0.5,2.1,0);

    glEnd();

    //============================
    //clock wise
    //==============================
    //=============================
        glPopMatrix(); //Undo the move to the center of the pentagon
glPushMatrix(); //Save the current state of transformations
glTranslated(0.0,1.0,0.0);
glTranslatef(2.3, -1.0, 1.0); //Move to the center of the triangle
glScalef(0.4,0.4,0);
glRotatef(_angle, 0.0, 0.0, -1.0); //Rotate about the the vector (1, 2, 3)

glBegin(GL_LINES);

//glBegin(GL_POLYGON);
          glColor3f(1.0,1.0,0.8);

for(int i=0;i<200;i++)
{
float pi=3.1416;
float A=(i*2*pi)/50;
float r=0.95;
float x = r * cos(A);
float y = r * sin(A);
glVertex2f(x,y );
}

glEnd();


    //=============
    ///Right

    glTranslatef(1.0, -1.5, 1.0);
    //glScalef(0.5,5.5,0.0);
    glBegin(GL_POLYGON);
    glColor3f(0.55,0.43,0.85);

    glVertex3f(0.5,1.0,0);
    glVertex3f(3.15,1.0,0);
    glVertex3f(3.15,2.0,0);
    glVertex3f(0.5,2.0,0);

    glEnd();
    glFlush();

    glTranslatef(-0.7, 0.34, 1.0);
    //glScalef(0.5,5.5,0.0);
    glBegin(GL_POLYGON);
    glColor3f(0.55,0.0,0.25);

    glVertex3f(0.5,1.0,0);
    glVertex3f(1.5,1.0,0);
    glVertex3f(1.5,1.3,0);
    glVertex3f(0.5,1.3,0);

    glEnd();

    //========================
    ///Left

     glTranslatef(-1.4, 2.68, 1.0);
    //glScalef(0.5,5.5,0.0);
    glBegin(GL_POLYGON);
    glColor3f(0.55,0.43,0.85);

    glVertex3f(-0.5,-1.0,0);
    glVertex3f(-3.15,-1.0,0);
    glVertex3f(-3.15,-2.0,0);
    glVertex3f(-0.5,-2.0,0);

    glEnd();
    glFlush();


    glTranslatef(-1.2, -2.65, 1.0);
    //glScalef(0.5,5.5,0.0);
    glBegin(GL_POLYGON);
    glColor3f(0.55,0.0,0.25);

    glVertex3f(0.5,1.0,0);
    glVertex3f(1.5,1.0,0);
    glVertex3f(1.5,1.3,0);
    glVertex3f(0.5,1.3,0);

    glEnd();

    //=======================

    ///Above

     glTranslatef(1.2, 1.5, 1.0);
    //glScalef(0.5,5.5,0.0);
    glBegin(GL_POLYGON);
    glColor3f(0.55,0.43,0.85);

    glVertex3f(0.5,1.0,0);
    glVertex3f(1.68,1.0,0);
    glVertex3f(1.68,3.5,0);
    glVertex3f(0.5,3.5,0);

    glEnd();
    glFlush();

     glTranslatef(0.40, -0.635, 1.0);
    //glScalef(0.5,5.5,0.0);
    glBegin(GL_POLYGON);
    glColor3f(0.55,0.0,0.25);

    glVertex3f(0.5,1.0,0);
    glVertex3f(0.85,1.0,0);
    glVertex3f(0.85,2.1,0);
    glVertex3f(0.5,2.1,0);

    glEnd();

    //========================

    ///Bottom


     glTranslatef(1.8, -0.3, 1.0);
    //glScalef(0.5,5.5,0.0);
    glBegin(GL_POLYGON);
    glColor3f(0.55,0.43,0.85);

    glVertex3f(-0.5,-1.0,0);
    glVertex3f(-1.68,-1.0,0);
    glVertex3f(-1.68,-3.5,0);
    glVertex3f(-0.5,-3.5,0);

    glEnd();
    glFlush();

    glTranslatef(-1.72, -2.25, 1.0);
    //glScalef(0.5,5.5,0.0);
    glBegin(GL_POLYGON);
    glColor3f(0.55,0.0,0.25);

    glVertex3f(0.5,1.0,0);
    glVertex3f(0.85,1.0,0);
    glVertex3f(0.85,2.1,0);
    glVertex3f(0.5,2.1,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(700, 400);
glutInitWindowPosition(100, 100);

//Create the window
glutCreateWindow("Fan");
initRendering();

//Set handler functions
glutDisplayFunc(drawScene);

glutReshapeFunc(handleResize);

glutTimerFunc(25, update, 0); //Add a timer

glutMainLoop();
return 0;
}

 Output :




Try this source code to draw fan ......







মন্তব্যসমূহ

Popular Posts

Simple 2D hut design in computer graphics using c++

HashMap in Java