Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 3 of 3

Thread: pushmatrix and popmatrix

  1. #1
    Junior Member Newbie
    Join Date
    Nov 2003
    Posts
    14

    pushmatrix and popmatrix

    Hi iv designed a swing, but i want to use pushmatrix and popmatrix. I am trying to have the seat on the swing move.Could anybody help me.Thanks
    #include "glut.h"
    #include "stdlib.h"

    float rotate;
    float rotate2;
    float rotate3;

    void drawBar()
    {
    glBegin(GL_POLYGON);
    glColor3f(0.3,0.3,0.3);//colour shading from corner to corner
    glVertex3f(0,0,0);
    glVertex3f(10,0,0);
    glColor3f(1,0,0);//colour shading from corner to corner
    glVertex3f(10,2,0);
    glVertex3f(0,2,0);
    glEnd();

    //back panel of base
    glBegin(GL_POLYGON);
    glColor3f(0.3,0.3,0.3);//colour shading from corner to corner
    glVertex3f(0,0,-10);
    glVertex3f(10,0,-10);
    glColor3f(1,0,0);//colour shading from corner to corner
    glVertex3f(10,2,-10);
    glVertex3f(0,2,-10);
    glEnd();

    //right panel of base
    glBegin(GL_POLYGON);
    glColor3f(0.3,0.3,0.3);//colour shading from corner to corner
    glVertex3f(10,0,0);
    glVertex3f(10,0,-10);
    glColor3f(1,0,0);//colour shading from corner to corner
    glVertex3f(10,2,-10);
    glVertex3f(10,2,0);
    glEnd();

    //left
    glBegin(GL_POLYGON);
    glColor3f(0.3,0.3,0.3);
    glVertex3f(0,0,0);//colour shading from corner to corner
    glVertex3f(0,0,-10);
    glColor3f(1,0,0);//colour shading from corner to corner
    glVertex3f(0,2,-10);
    glVertex3f(0,2,0);
    glEnd();


    //bottom panel of base
    glBegin(GL_POLYGON);
    glColor3f(0.3,0.3,0.3);//colour shading from corner to corner
    glVertex3f(0,0,0);
    glVertex3f(10,0,0);
    glColor3f(1,0,0);//colour shading from corner to corner
    glVertex3f(10,0,-10);
    glVertex3f(0,0,-10);
    glEnd();

    //top panel of base
    glBegin(GL_POLYGON);
    glColor3f(0.3,0.3,0.3);//colour shading from corner to corner
    glVertex3f(0,2,0);
    glVertex3f(10,2,0);
    glColor3f(1,0,0);//colour shading from corner to corner
    glVertex3f(10,2,-10);
    glVertex3f(0,2,-10);
    glEnd();

    }


    void drawRope()
    {
    glBegin(GL_LINES);
    glColor3f(1,1,1);
    glVertex3f(0,0,0);
    glVertex3f(0,12,0);
    glEnd();
    }

    void drawRope2()
    {
    glBegin(GL_LINES);
    glColor3f(1,1,1);
    glVertex3f(0,0,0);
    glVertex3f(0,-12,0);
    glEnd();
    }

    void drawSeat()
    {
    //front
    glBegin(GL_POLYGON);
    glColor3f(0.3,0.3,0.3);//colour shading from corner to corner
    glVertex3f(0,0,0);
    glVertex3f(-5,0,0);
    glColor3f(1,0,0);//colour shading from corner to corner
    glVertex3f(-5,2,0);
    glVertex3f(0,2,0);
    glEnd();

    //back panel of base
    glBegin(GL_POLYGON);
    glColor3f(0.3,0.3,0.3);//colour shading from corner to corner
    glVertex3f(0,0,-5);
    glVertex3f(-5,0,-5);
    glColor3f(1,0,0);//colour shading from corner to corner
    glVertex3f(-5,2,-5);
    glVertex3f(0,2,-5);
    glEnd();

    //right panel of base
    glBegin(GL_POLYGON);
    glColor3f(0.3,0.3,0.3);//colour shading from corner to corner
    glVertex3f(-5,0,0);
    glVertex3f(-5,0,-5);
    glColor3f(1,0,0);//colour shading from corner to corner
    glVertex3f(-5,2,-5);
    glVertex3f(-5,2,0);
    glEnd();

    //left
    glBegin(GL_POLYGON);
    glColor3f(0.3,0.3,0.3);
    glVertex3f(0,0,0);//colour shading from corner to corner
    glVertex3f(0,0,-5);
    glColor3f(1,0,0);//colour shading from corner to corner
    glVertex3f(0,2,-5);
    glVertex3f(0,2,0);
    glEnd();


    //bottom panel of base
    glBegin(GL_POLYGON);
    glColor3f(0.3,0.3,0.3);//colour shading from corner to corner
    glVertex3f(0,0,0);
    glVertex3f(-5,0,0);
    glColor3f(1,0,0);//colour shading from corner to corner
    glVertex3f(-5,0,-5);
    glVertex3f(0,0,-5);
    glEnd();

    //top panel of base
    glBegin(GL_POLYGON);
    glColor3f(0.3,0.3,0.3);//colour shading from corner to corner
    glVertex3f(0,2,0);
    glVertex3f(-5,2,0);
    glColor3f(1,0,0);//colour shading from corner to corner
    glVertex3f(-5,2,-5);
    glVertex3f(0,2,-5);
    glEnd();

    }

    void drawSwing()
    {

    glTranslatef(0,0,-20);
    drawBar();

    glPushMatrix();
    glTranslatef(2,0,0);
    glRotatef(rotate,1,0,0);
    glTranslatef(0,-12,0);
    drawRope();
    glTranslatef(5,0,0);
    glRotatef(rotate,1,0,0);
    drawSeat();
    glTranslatef(0,12,0);
    drawRope2();

    glPopMatrix();
    }


    void draw(void)
    {
    //clear the current window
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    //make changes to the modelview matrix

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    //this calls my main function windchime function to draw it on the screen
    drawSwing();

    //flush the scene into the buffer
    glFlush();
    //swap the back buffer with the front buffer
    glutSwapBuffers();
    }

    void key(unsigned char k, int x, int y)
    {
    switch(k)

    {
    case 'a':
    rotate -=10;
    break;


    case 'z':
    rotate2 -=10;
    break;
    }

    draw();
    }


    void idle()
    {
    //this is good place to do animation
    //since thee are no animations in the test we can leave idle empty
    }

    void reshape(int width, int height)
    {
    //set the viewport to be the same width and height as the window
    glViewport(0,0,width, height);
    //make changes to the projection matrix
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    //set up our projection type
    //we'll be using a perspective projection, with a 90 degree
    //field of view
    gluPerspective(90.0, (float) width / (float) height, 0.1, 100.0);
    //redraw the view during resizing
    draw();
    }

    //set up OpenGL before we do any drawing
    //this function is only called once at the start of the program
    void init_drawing(void)
    {
    //blend colours across the surface of the polygons
    //another mode to try is GL_FLAT which is flat shading
    glShadeModel(GL_SMOOTH);
    //turn lighting off
    glDisable(GL_LIGHTING);
    //enable OpenGL hidden surface removal
    glDepthFunc(GL_LEQUAL);
    glEnable(GL_DEPTH_TEST);
    }

    int main(int argc, char *argv[])
    {
    //Initialise Glut and create a window
    glutInit(&argc, argv);
    //sets up our display mode
    //here we've selected an RGBA display with depth testing
    //and double buffering enabled
    glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
    //create a window and pass through the windows title
    glutCreateWindow("Basic Glut Application");

    //run our own drawing initialisation routine
    init_drawing();

    //tell glut the names of our callback functions point to our
    //functions that we defined at the start of this file
    glutReshapeFunc(reshape);
    glutKeyboardFunc(key);
    glutIdleFunc(idle);
    glutDisplayFunc(draw);

    //request a window size of 600 x 600
    glutInitWindowSize(800,800);
    glutReshapeWindow(800,800);

    //go into the main loop
    //this loop won't terminate until the user exits the
    //application
    glutMainLoop();

    return 0;
    }

  2. #2
    Intern Contributor
    Join Date
    Jul 2002
    Location
    Seri Kembangan, Selangor, Malaysia
    Posts
    92

    Re: pushmatrix and popmatrix

    quote:
    --------------------------------------------------------------------------------
    Originally posted by hoganh:

    void drawSwing()
    {

    glTranslatef(0,0,-20);
    drawBar();

    glPushMatrix();
    glTranslatef(2,0,0);
    glRotatef(rotate,1,0,0);
    glTranslatef(0,-12,0);
    drawRope();
    glTranslatef(5,0,0);
    glRotatef(rotate,1,0,0);
    drawSeat();
    glTranslatef(0,12,0);
    drawRope2();

    glPopMatrix();
    }

    ********************************************

    void drawSwing()
    {
    glPushMatrix();
    glTranslatef(0,0,-20);
    drawBar();
    glPopMatrix();

    glPushMatrix();
    glTranslatef(2,0,0);
    glRotatef(rotate,1,0,0);
    glPopMatrix();

    glPushMatrix();
    glTranslatef(0,-12,0);
    drawRope();
    glPopMatrix();

    glPushMatrix();
    glTranslatef(5,0,0);
    glRotatef(rotate,1,0,0);
    drawSeat();
    glPopMatrix();

    glPushMatrix();
    glTranslatef(0,12,0);
    drawRope2();
    glPopMatrix();

    }

    Refer to chapter 3 in Red Book. Hope it can help. Thanks.

  3. #3
    Junior Member Newbie
    Join Date
    Nov 2003
    Posts
    14

    Re: pushmatrix and popmatrix

    Cheers.Thanks for your help

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •