Results 1 to 3 of 3

Thread: Emergency Help

  1. #1
    Junior Member Newbie
    Join Date
    Dec 2002
    Location
    Ann Arbor, MI , USA
    Posts
    4

    Emergency Help

    Hi All,
    I met a very odd problem. I using glRotatef() to rotate a cube. My problem is the cube rotates according to one axis, after a while, it rotates according to another axis. I do not change anything at this time. Ater a while, it changes back. Does someone know how to deal with this problem? Many thanks!
    This is my code:
    #include <iostream.h>
    #include <stdio.h>
    #include <GL/glut.h>

    GLfloat vertices[][3]={
    {-1.0,-1.0,1.0},{-1.0,1.0,1.0},{1.0,1.0,1.0},
    {1.0,-1.0,1.0},{-1.0,-1.0,-1.0},{-1.0,1.0,-1.0},
    {1.0,1.0,-1.0},{1.0,-1.0,-1.0}};

    GLfloat colors[][3]={
    {1.0,0.0,0.0},{0.0,1.0,1.0},{1.0,1.0,0.0},
    {0.0,1.0,0.0},{0.0,0.0,1.0},{1.0,0.0,1.0}};

    char key;
    bool RotateSign=false, EyeSign=true;
    GLfloat Theta=0.0,dx=0.0,dy=0.0,dz=1.0,Eye_x=0.0,Eye_y=0.0 ,Eye_z=1.0;

    void polygon(int a, int b, int c, int d)
    {
    glColor3fv(colors[a]);
    glBegin(GL_POLYGON);
    glVertex3fv(vertices[a]);
    glVertex3fv(vertices[B]);
    glVertex3fv(vertices[c]);
    glVertex3fv(vertices[d]);
    glEnd();
    }

    void cube()
    {
    polygon(0,3,2,1);
    polygon(2,3,7,6);
    polygon(3,0,4,7);
    polygon(1,2,6,5);
    polygon(4,5,6,7);
    polygon(5,4,0,1);
    }


    void idle()
    {
    }

    void myidle()
    {
    Theta=Theta+2.0;
    if(Theta>=360) Theta=Theta-360;
    glutPostRedisplay();
    }

    void display()
    {
    glClear(GL_COLOR_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(Eye_x,Eye_y,Eye_z,0.0,0.0,0.0,0.0,1.0,0. 0);
    glRotatef(Theta,dx,dy,dz);
    cube();
    glFlush();
    glutSwapBuffers();
    }

    void reshape(int w, int h)
    {
    glViewport(0,0,w,h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-4.0,4.0,-4.0,4.0,-4.0,4.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    }

    void mymouse(int button,int state, int x, int y)
    {

    if(state==GLUT_DOWN && button==GLUT_LEFT_BUTTON)
    {
    RotateSign=!RotateSign;
    if(RotateSign==true)
    glutIdleFunc(myidle);
    else glutIdleFunc(idle);
    }

    if(state==GLUT_DOWN && button==GLUT_RIGHT_BUTTON)
    {
    EyeSign=!EyeSign;
    if(EyeSign==true)
    {
    Eye_x=0.0;
    Eye_y=0.0;
    Eye_z=1.0;

    }
    else
    {
    Eye_x=1.0;
    Eye_y=1.0;
    Eye_z=1.0;
    }
    myidle();
    }
    }

    void mykey(unsigned char key, int x, int y)
    {
    if(key=='x'&#0124; &#0124;key=='X')
    {
    dx=1.0;
    dy=0.0;
    dz=0.0;
    }

    if(key=='y'&#0124; &#0124;key=='Y')
    {
    dx=0.0;
    dy=1.0;
    dz=0.0;
    }

    if(key=='z'&#0124; &#0124; key=='Z')
    {
    dx=0.0;
    dy=0.0;
    dz=1.0;
    }
    myidle();
    }

    int main(int argc, char** argv)
    {
    //intro();
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
    glutInitWindowSize(600,600);
    glutCreateWindow("Assignment6");
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutKeyboardFunc(mykey);
    glutMouseFunc(mymouse);
    glutMainLoop();
    return 0;
    }

  2. #2
    Senior Member OpenGL Guru Relic's Avatar
    Join Date
    Apr 2000
    Posts
    2,450

    Re: Emergency Help

    Pretty simple: You have no depth buffer.
    Add this in main
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPT H);
    and this in display
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glEnable(GL_DEPTH_TEST);


    [This message has been edited by Relic (edited 12-05-2002).]

  3. #3
    Junior Member Newbie
    Join Date
    Dec 2002
    Location
    Ann Arbor, MI , USA
    Posts
    4

    Re: Emergency Help

    Hi Relic:
    Thank you very much for your help! You save my life!

Similar Threads

  1. Need emergency help with setting Normal vectors!
    By rovolution in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 06-29-2010, 12:28 AM
  2. bezier emergency i get madddddd
    By courious in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 09-25-2002, 04:54 AM
  3. Emergency! pixel rendering question???
    By shreker in forum OpenGL: Linux
    Replies: 1
    Last Post: 02-08-2002, 08:11 PM
  4. Emergency! Helpp Needed@ Can anyone compile the file?
    By jojozhong in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 09-06-2000, 01:16 AM
  5. Emergency! Need help
    By jojozhong in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 09-05-2000, 01:14 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean