Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as C++ by registered user whutever09 ( 14 years ago )
#include <glut.h>
     
    #define ESCAPE 27
    #define SPACEBAR 32
     
     
    int windowHandle;
    int win;
 int turn;
 GLfloat toy1x;
 GLfloat toy1y;
 GLfloat toy1z;
 GLfloat toy2x;
 GLfloat toy2y;
 GLfloat toy2z;
 GLfloat toy3x;
 GLfloat toy3y;
 GLfloat toy3z;
    GLfloat maincranex;
    GLfloat maincraney;
    GLfloat maincranez;
    GLfloat cranex;
    GLfloat craney;
    GLfloat cranez;
    GLfloat rotatey;
    GLfloat rotatedeg;
    GLfloat Gatey;
    GLfloat Zoom;
     
    /* The function called when our window is resized (which shouldn't happen, because we're fullscreen) */
     
    void resizeScene(int width, int height)
    {
    // Prevent A Divide By Zero If The Window Is Too Small
     
    if(height == 0)
    {
    height=1;
    }
     
    // Reset The Current Viewport And Perspective Transformation
     
    glViewport(0, 0, width, height);
     
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
     
    gluPerspective(45.0f, (GLfloat) width / (GLfloat) height, 0.1f, 100.0f);
    glMatrixMode(GL_MODELVIEW);
    }
     
    //============================================DRAWING=================================================

    void Toy1()
    {
    //Front
    glBegin(GL_QUADS);
    glVertex3f(0,0,0);
    glVertex3f(4,0,0);
    glVertex3f(4,4,0);
    glVertex3f(0,4,0);
    glEnd();
    
    //Back
    glBegin(GL_QUADS);
    glVertex3f(0,0,5);
    glVertex3f(4,0,5);
    glVertex3f(4,4,5);
    glVertex3f(0,4,5);
    glEnd();

    //Left
    glBegin(GL_QUADS);
    glVertex3f(0,0,0);
    glVertex3f(0,0,5);
    glVertex3f(0,4,5);
    glVertex3f(0,4,0);
    glEnd();

    //Right
    glBegin(GL_QUADS);
    glVertex3f(4,0,0);
    glVertex3f(4,0,5);
    glVertex3f(4,4,5);
    glVertex3f(4,4,0);
    glEnd();

    //Top
    glBeing(GL_QUADS);
    glVertex3f(4,4,5);
    glVertex3f(0,4,5);
    glVertex3f(0,4,0);
    glVertex3f(4,4,0);
    glEnd();

    //Bottom
    glBeing(GL_QUADS);
    glVertex3f(4,0,5);
    glVertex3f(0,0,5);
    glVertex3f(0,0,0);
    glVertex3f(4,0,0);
    glEnd();
    }

    void Toy2()
    {
    //Front
    glBegin(GL_QUADS);
    glVertex3f(0,0,0);
    glVertex3f(4,0,0);
    glVertex3f(4,4,0);
    glVertex3f(0,4,0);
    glEnd();
    
    //Back
    glBegin(GL_QUADS);
    glVertex3f(0,0,5);
    glVertex3f(4,0,5);
    glVertex3f(4,4,5);
    glVertex3f(0,4,5);
    glEnd();

    //Left
    glBegin(GL_QUADS);
    glVertex3f(0,0,0);
    glVertex3f(0,0,5);
    glVertex3f(0,4,5);
    glVertex3f(0,4,0);
    glEnd();

    //Right
    glBegin(GL_QUADS);
    glVertex3f(4,0,0);
    glVertex3f(4,0,5);
    glVertex3f(4,4,5);
    glVertex3f(4,4,0);
    glEnd();

    //Top
    glBeing(GL_QUADS);
    glVertex3f(4,4,5);
    glVertex3f(0,4,5);
    glVertex3f(0,4,0);
    glVertex3f(4,4,0);
    glEnd();

    //Bottom
    glBeing(GL_QUADS);
    glVertex3f(4,0,5);
    glVertex3f(0,0,5);
    glVertex3f(0,0,0);
    glVertex3f(4,0,0);
    glEnd();
    }

    void Toy3()
    {
    //Front
    glBegin(GL_QUADS);
    glVertex3f(0,0,0);
    glVertex3f(4,0,0);
    glVertex3f(4,4,0);
    glVertex3f(0,4,0);
    glEnd();
    
    //Back
    glBegin(GL_QUADS);
    glVertex3f(0,0,5);
    glVertex3f(4,0,5);
    glVertex3f(4,4,5);
    glVertex3f(0,4,5);
    glEnd();

    //Left
    glBegin(GL_QUADS);
    glVertex3f(0,0,0);
    glVertex3f(0,0,5);
    glVertex3f(0,4,5);
    glVertex3f(0,4,0);
    glEnd();

    //Right
    glBegin(GL_QUADS);
    glVertex3f(4,0,0);
    glVertex3f(4,0,5);
    glVertex3f(4,4,5);
    glVertex3f(4,4,0);
    glEnd();

    //Top
    glBeing(GL_QUADS);
    glVertex3f(4,4,5);
    glVertex3f(0,4,5);
    glVertex3f(0,4,0);
    glVertex3f(4,4,0);
    glEnd();

    //Bottom
    glBeing(GL_QUADS);
    glVertex3f(4,0,5);
    glVertex3f(0,0,5);
    glVertex3f(0,0,0);
    glVertex3f(4,0,0);
    glEnd();
    }

    void BoundingBox()
    {
    glPushMatrix();
    glTranslatef(cranex,craney,cranez);

   
    glBegin(GL_QUADS);
    glVertex3f(0,0,0);
    glVertex3f(0,4,0);
    glVertex3f(4,4,0);
    glVertex3f(4,0,0);
    glEnd();

    glBegin(GL_QUADS);
    glVertex3f(0,0,5);
    glVertex3f(0,4,5);
    glVertex3f(4,4,5);
    glVertex3f(4,0,5);

    glPopMatrix();
    }

    void craneleftclaw()
    {
    glPushMatrix();
    glTranslatef(cranex,craney,cranez);


    //*~~~~~~~~~BOX THING~~~~~~~~~*
    //back
    glBegin(GL_QUADS);
    glColor3f(0.3,0,0);
    glVertex3f(0,0,0);
    glVertex3f(1,0,0);
    glVertex3f(1,4,0);
    glVertex3f(0,4,0);
    glEnd();
    //front
    glBegin(GL_QUADS);
    glColor3f(0.3,0,0);
    glVertex3f(0,0,5);
    glVertex3f(1,0,5);
    glVertex3f(1,4,5);
    glVertex3f(0,4,5);
    glEnd();

    glPopMatrix();     
     
    //*~~~~~~~~~~~~~~~~LOWER TRIANGLE~~~~~~~~~~~~~~~~~~~~~~*
     
     
    //back
    glBegin(GL_TRIANGLES);
    glColor3f(0,0.6,0);
    glVertex3f(-6,-12,-5);
    glVertex3f(-2,-12,-5);
    glVertex3f(-2,-16,-5);
    glEnd();
    //front
    glBegin(GL_TRIANGLES);
    glColor3f(0.3,0.6,0);
    glVertex3f(-6,-12,0);
    glVertex3f(-2,-12,0);
    glVertex3f(-2,-16,0);
    glEnd();
     
    //*~~~~~~~~~~~~~~~~UPPER TRIANGLE~~~~~~~~~~~~~~~~~~~~~~*
     
    //right
    glBegin(GL_QUADS);
    glColor3f(0.7,0.6,0.6);
    glVertex3f(-2,-8,0);
    glVertex3f(-2,-8,-5);
    glVertex3f(-2,-12,-5);
    glVertex3f(-2,-12,0);
    glEnd();
     
    //back
    glBegin(GL_TRIANGLES);
    glColor3f(0,1,0);
    glVertex3f(-2,-8,-5);
    glVertex3f(-2,-12,-5);
    glVertex3f(-6,-12,-5);
    glEnd();
     
    //left
    glBegin(GL_QUADS);
    glColor3f(0.7,0.6,0.4);
    glVertex3f(-2,-8,0);
    glVertex3f(-2,-8,-5);
    glColor3f(0.6,0.3,0);
    glVertex3f(-6,-12,-5);
    glColor3f(0.6,0.3,0);
    glVertex3f(-6,-12,0);
    glEnd();
     
    //front
    glBegin(GL_TRIANGLES);
    glColor3f(0.7,0.6,0.4);
    glVertex3f(-2,-8,0);
    glColor3f(0.6,0.3,0);
    glVertex3f(-2,-12,0);
    glColor3f(0.3,0.0,0);
    glVertex3f(-6,-12,0);
    glEnd();
     
     
    }
    void cranerightclaw()
    {
    //*~~~~~~~~~~~~~~~~~~~~BOX THING~~~~~~~~~~~~~~~~~~~~~~~*
    glPushMatrix();
    glTranslatef(cranex+4,craney,cranez);
   //back
    glBegin(GL_QUADS);
    glColor3f(0.3,0,0);
    glVertex3f(0,0,0);
    glVertex3f(1,0,0);
    glVertex3f(1,4,0);
    glVertex3f(0,4,0);
    glEnd();
    //front
    glBegin(GL_QUADS);
    glColor3f(0.3,0,0);
    glVertex3f(0,0,5);
    glVertex3f(1,0,5);
    glVertex3f(1,4,5);
    glVertex3f(0,4,5);
    glEnd();

    glPopMatrix();     


  //back
    /*glBegin(GL_QUADS);
    glColor3f(0.3,0,0);
    glVertex3f(2,-12,-5);
    glVertex3f(1,-12,-5);
    glVertex3f(1,-16,-5);
    glVertex3f(2,-16,-5);
    glEnd();*/
    //front
    /*glBegin(GL_QUADS);
    glColor3f(0.3,0,0);
    glVertex3f(2,-12,0.0);
    glVertex3f(1,-12,0);
    glVertex3f(1,-16,0);
    glVertex3f(2,-16,0);
    glEnd();*/
    //*~~~~~~~~~~~~~~~~~~~~LOWER TRIANGLE~~~~~~~~~~~~~~~~~~~~*
     
    //back
    glBegin(GL_TRIANGLES);
    glColor3f(0,0.6,0);
    glVertex3f(6,-12,-5);
    glVertex3f(2,-12,-5);
    glVertex3f(2,-16,-5);
    glEnd();
    //front
    glBegin(GL_TRIANGLES);
    glColor3f(0.3,0.6,0);
    glVertex3f(6,-12,0);
    glVertex3f(2,-12,0);
    glVertex3f(2,-16,0);
    glEnd();
    //*~~~~~~~~~~~~~~~~~~~~~~~~~UPPER TRIANGLE~~~~~~~~~~~~~~~~~~~~~~~~~~*
     
    //right
    glBegin(GL_QUADS);
    glColor3f(0.7,0.6,0.6);
    glVertex3f(2,-8,0);
    glVertex3f(2,-8,-5);
    glVertex3f(2,-12,-5);
    glVertex3f(2,-12,0);
    glEnd();
    //back
    glBegin(GL_TRIANGLES);
    glColor3f(0,1,0);
    glVertex3f(2,-8,-5);
    glVertex3f(2,-12,-5);
    glVertex3f(6,-12,-5);
    glEnd();
     
    //left
    glBegin(GL_QUADS);
    glColor3f(0.7,0.6,0.4);
    glVertex3f(2,-8,0);
    glVertex3f(2,-8,-5);
    glColor3f(0.6,0.3,0);
    glVertex3f(6,-12,-5);
    glColor3f(0.6,0.3,0);
    glVertex3f(6,-12,0);
    glEnd();
    //front
    glBegin(GL_TRIANGLES);
    glColor3f(0.7,0.6,0.4);
    glVertex3f(2,-8,0);
    glColor3f(0.6,0.3,0);
    glVertex3f(2,-12,0);
    glColor3f(0.3,0.0,0);
    glVertex3f(6,-12,0);
    glEnd();
    }
     
     
    //*~~~~~~~~~~~~~~~~~~~~~MAIN CRANE BODY~~~~~~~~~~~~~*
    void cranebody()
    {
     
    //back
    glBegin(GL_QUADS);
    glColor3f(0,1,1);
    glVertex3f(-2.0f, 0.0f, -5.0f);
    glVertex3f(2.0f, 0.0f, -5.0f);
    glVertex3f(2.0f, -8.0f, -5.0f);
    glVertex3f(-2.0f, -8.0f, -5.0f);
    glEnd();
     
    //lower
    glBegin(GL_QUADS);
    glColor3f(0,0,1);
    glVertex3f(-2,-8,0);
    glVertex3f(2,-8,0);
    glVertex3f(2,-8,-5);
    glVertex3f(-2,-8,-5);
    glEnd();
     
    //right
    glBegin(GL_QUADS);
    glColor3f(1,1,0);
    glVertex3f(2,0,0);
    glVertex3f(2,-8,0);
    glVertex3f(2,-8,-5);
    glVertex3f(2,0,-5);
    glEnd();
     
    //left
    glBegin(GL_QUADS);
    glColor3f(0.7,0.6,0.4);
    glVertex3f(-2,0,0);
    glVertex3f(-2,-8,0);
    glColor3f(1,1,1);
    glVertex3f(-2,-8,-5);
    glColor3f(0.7,0.6,0.4);
    glVertex3f(-2,0,-5);
    glEnd();
     
     
    //upper
    glBegin(GL_QUADS);
    glColor3f(0.7,0.6,0.4);
    glVertex3f(-2,0,0);
    glVertex3f(-2,0,-5);
    glColor3f(1,1,1);
    glVertex3f(2,0,-5);
    glVertex3f(2,0,0);
    glEnd();
     
     
    //front
    glBegin(GL_QUADS);
    glColor3f(0.7,0.6,0.4);
    glVertex3f(-2.0f, 0.0f, 0.0f);
    glVertex3f(2.0f, 0.0f, 0.0f);
    glVertex3f(2.0f, -8.0f, 0.0f);
    glColor3f(1,1,1);
    glVertex3f(-2.0f, -8.0f, 0.0f);
    glEnd();
     
    }
     
    void base()
    {
    glPushMatrix();
    glTranslatef(0,-25,0);
    glBegin(GL_LINES);
    glVertex3f(15,-15,-15);
    glVertex3f(-15,-15,-15);
    glVertex3f(-15,-15,15);
    glVertex3f(15,-15,15);
    glEnd();
    glPopMatrix();
    }
     
     
    //=======================================ANIMATIONS,COMMANDS,CHECKS======================================
    //Makes the toy "falldown"
    void ToyFallingDown()
    {
    if (win==1)
         toy1y--;
    else if (win==2)
         toy2y--;
    else if (win==3)
         toy3y--;
    }
     
    //Makes Crane GO Down
    void CraneGoDown()
    {
    do
  {
         maincraney--;
         }
   while (maincraney!=-5);
    }
     
    //Makes Crane Go Up
    void CraneGoUp()
    {
    do
  {
        maincraney++;
        }
 while (maincraney!=5);
    }
     
    //Go back to default place...
    void CraneGoBack()
    {
     while (maincranex!=-5)
         {
         maincranex--;
         }
     while (maincranez!=5)
         {
         maincranez++;
         }
    }
     
     
    void Check1()
    {
     if (toy1x == cranex && toy1z == cranez)
         {
         toy1x=cranex;
         toy1y=craney;
         toy1z=cranez;
         win=1;
         }
     else if (toy2x == cranex && toy2z == cranez)
         {
         toy2x=cranex;
         toy2y=craney;
         toy2z=cranez;
         win=2;
         }
     else if (toy3x == cranex && toy3z == cranez)
         {
         toy3x=cranex;
         toy3y=craney;
         toy3z=cranez;
         win=3;
         }
    }
     
     
    void showScene()
    {
     
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
     
     
     
    // Write your 3D stuff below this line!
    glPushMatrix();
    glTranslatef(0,0,-40);
    base();
    glPopMatrix();
     
    glPushMatrix();
    glTranslatef(toy1x,toy1y,toy1z);
    toy1();
    glPopMatrix();

    glPushMatrix();
    glTranslatef(toy2x,toy2y,toy2z);
    toy2();
    glPopMatrix();

    glPushMatrix();
    glTranslatef(toy3x,toy3y,toy3z);
    toy3();
    glPopMatrix();


    glTranslatef(0,0,-40);
    glRotatef(45,1,1,0);
     
    glTranslatef(maincranex,maincraney,maincranez);
    cranerightclaw();
 cranebody();
    craneleftclaw();
    
     
    /*
     
    don't wait!
    start processing buffered OpenGL routines
     
    */
     
    glFlush();
     
    // Need to swap buffer to show latest render
     
    glutSwapBuffers();
    }
     
    /*
     
    Prepare everything for viewing
     
    */
     
    void initMyComponents(int width, int height)
    {
    GLfloat aspectRatio;
     
    // select clearing (background) color
     
    glClearColor(0.0, 0.0, 0.0, 0.0);
     
    // initialize viewing values
     
    // Clear both screen and depth buffer
     
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     
    glMatrixMode(GL_PROJECTION);
     
    // Reset The Projection Matrix
     
    glLoadIdentity();
     
    // No need for this advanced clipping function from the red book.
     
    //glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
     
    // Calculate The Aspect Ratio Of The Window
     
    aspectRatio = (GLfloat) width / (GLfloat) height;
    gluPerspective(45.0f, aspectRatio, 0.1f, 100.0f);
    glMatrixMode(GL_MODELVIEW);
    }
     
    void keyPressed(unsigned char key, int x, int y)
    {
    /* avoid thrashing this procedure. Uncomment the line if using gcc */
     
    //usleep(100);
     
    /* If escape is pressed, kill everything. */
     
    if (key == ESCAPE)
    {
    /* shut down our window */
     
    glutDestroyWindow(windowHandle);
     
    /* exit the program...normal termination. */
    }
     
     
    //MOVES THE CRANE
    //x-axis
    if (key=='d')
    {
    maincranex-=1;
    cranex-=1;
    turn-=1;
    }
    if (key=='a')
    {
    maincranex+=1;
    cranex+=1;
    turn-=1;
    }
     
    //Z-axis
    if (key == 'w')
    {
    maincranez+=1;
    cranez+=1;
    turn-=1;
    }
    if(key == 's')
    {
    maincranez-=1;
    cranez-=1;
    turn-=1;
    }
     
    //Drop
    if (key == SPACEBAR)
    {
    turn=0;
    }
     
    }
     
    /*
     
    Declare initial window size, position, and display mode
    (single buffer and RGBA). Open window with "hello"
    in its title bar. Call initialization routines.
    Register callback function to display graphics.
    Enter main loop and process events.
     
    */
     
    int main(int argc, char** argv)
    {
    glutInit(&argc;, argv);
    rotatey=1;
    rotatedeg=45;
     
    //Declaration of Values
    maincranex=-5;
    maincraney=5;
    maincranez=5;
    cranex=-2;
    craney=-12;
    cranez=-5;
    turn=10;
    win=0;
   
    toy1x=20;
    toy1y=-22;
    toy1z=4;
    toy2x=12;
    toy2y=-22;
    toy2z=3;
    toy3x=5;
    toy3y=-22;
    toy3z=-10;
     
    // Do double-buffer for smoother rendering and animation.
     
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH);
    glutInitWindowSize(640, 480);
    //glutInitWindowPosition(100, 100);
    glutInitWindowPosition(0, 0);
    windowHandle = glutCreateWindow("Your OpenGL Program");
     
    initMyComponents(640, 480);
     
    /* Register the function called when the keyboard is pressed. */
     
    glutKeyboardFunc(&keyPressed;);
     
    // Show in full screen. Can be used later by uncommenting.
     
    //glutFullScreen();
     
    // Draw the scenery
     
    glutDisplayFunc(&showScene;);
    glutReshapeFunc(&resizeScene;);
    glutIdleFunc(&showScene;);
    glutMainLoop();
     
    if (turn==0)
    {
     CraneGoDown();
     Check1();
     CraneGoUp();
     CraneGoBack();
     ToyFallingDown();
    }
     
     
    return 0;
    }

 

Revise this Paste

Parent: 47780
Children: 47782
Your Name: Code Language: