DREHENDER WÜRFEL



  • Okay also anders gefragt..

    ich habe einen würfel und diesen möchte ich um eine der drei achsen drehen, je nachdem welche maustaste ich drücke.
    Also rechte maustaste-> drehe um die y-Achse
    linke maustaste-> drehe um die x-Achse
    mittlere maustaste-> drehe um die z-Achse

    Hier mein Code:

    Irgendwas stimmt da nicht und ich glaube esliegt an der reshape function.
    Bin mir aber nicht sicher.

    #include <stdlib.h>
    #include <GL/glut.h>
    
    GLfloat theta[1];
    int axis;
    GLfloat vertices[][3]={{-1,-1,-1},{1,-1,-1},{1,1,-1},{-1,1,-1},{-1,-1,1},{1,-1,1},{1,1,1},{-1,1,1}};
    
    void colorcube()
    {
    GLubyte cubeIndices[24]={0,3,2,1,2,3,7,6,0,4,7,3,1,2,6,5,4,5,6,7,0,1,5,4};
    glDrawElements(GL_QUADS,24,GL_UNSIGNED_BYTE,cubeIndices);
    }
    
    void spinCube()
    {
    theta[axis] +=2.0;
    if(theta[axis]>360.0)
    theta[axis]-=360.0;
    glutPostRedisplay;
    }
    
    void mouse(int btn, int state, int x, int y)
    {
    if (btn == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
    axis = 0;
    if (btn == GLUT_MIDDLE_BUTTON && state == GLUT_DOWN)
    axis = 1;
    if (btn == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
    axis = 2;
    }
    
    void reshape(int w, int h)
    {
    glViewport(0, 0, (GLsizei) w,(GLsizei) h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-2.0,2.0,-2.0,2.0,-2.0,2.0);
    gluLookAt(0.25,0.25,0.5, 0,0,0, 0,1,0);
    glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    
    }
    void display()
    {
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    glRotatef(theta[0],1,0,0);
    glRotatef(theta[1],0,1,0);
    glRotatef(theta[2],0,0,1);
    colorcube();
    glutSwapBuffers();
    }
    
    void init()
    {
    glColor3f(1.0,1.0,1.0);
    glClearColor(0.0,0.0,0.0, 0.0);
    glEnableClientState(GL_VERTEX_ARRAY);
    glVertexPointer(3,GL_FLOAT,0,vertices);
    }
    
    void main(int argc, char** argv)
    {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB| GLUT_DEPTH);
    glutInitWindowSize(500, 500);
    glutCreateWindow("Übung_3-Problem_1");
    glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
    init();
    glutReshapeFunc(reshape);
    glutDisplayFunc(display);
    glutIdleFunc(spinCube);
    glutMouseFunc(mouse);
    glEnable(GL_DEPTH_TEST);
    glutMainLoop();
    }
    

    Also mein hauptproblem ist, dass sich der würfel nicht dreht, wenn ich die maustasten drücke.
    vielleicht sieht einer von euch den Fehler.
    Wäre supernett wenn ihr mir weiterhelfen könntet.

    Ich komm einfach nicht drauf woran es liegen könnte.

    Dank euch
    🙂



  • das theta-Array ist auf jeden Fall mal deutlich zu klein



  • Hm

    bringt aber irgendwie nix...

    vielleicht noch andere tipps

    Wäre super



  • glutPostRedisplay; -> glutPostRedisplay();

    void main(int argc, char** argv) -> int main(int argc, char** argv)



  • ich würd mal sagen, du bist deutlich zu nah dran:

    gluLookAt(0.25,0.25,0.5, 0,0,0, 0,1,0);
    

    Aber ich weiß nicht wie sich das bei einer orthogonalen Kamera auswirkt.


Anmelden zum Antworten