2016-04-20 11 views
0

Ich muss einen virtuellen Trackball mit C++ erstellen. Ich habe alle Berechnungen gemacht und kann die Drehwinkel- und Achsenwerte finden. Mein Objekt rotiert, wie ich es bei jedem Mausziehen wollte, aber das Problem ist, dass es nach jeder Drehung in seine ursprüngliche Position zurückkehrt.glRotatef dreht die Kamera anstelle des Objekts

Also habe ich herausgefunden, dass ich die aktuelle Modelview-Matrix bekommen muss, multipliziere sie mit der Rotationsmatrix und lade dann das Ergebnis zurück ins opengl.

Ich habe es versucht aber leider glRotatef dreht meine Kamera anstelle des Objekts. Hier ist meine Funktion der Szene

//--- Drawing code --------------------------------------- 

/** Drawing code for one frame. */ 
void drawGLScene() 
{ 
    // Real time in seconds. 
    GLfloat t = frameStat->frameStart(width, height); 

    // Clear the frame buffer and the depth buffer 
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 

    // Set current model-view transform: 
    glLoadIdentity(); 

    // Looking from "camera" to the "center" point (y-axis defines the "up" vector) 
    gluLookAt(0.0f, 0.0f, 10.0f, 
      center[ 0 ], center[ 1 ], center[ 2 ], 
      0.0f, 1.0f, 0.0f); 

    if (dragging) 
     glLoadMatrixf(matModelView.array()); 

    glScalef(zoom, zoom, zoom); 

#ifdef USE_VBO 
    // scene rendering using VBO buffers: 
    scene.render(); 

#else 

    // client-side vertex arrays (cube): 
    glVertexPointer(3, GL_FLOAT, 6 * sizeof(GLfloat), vert); // specify vertex data array 
    glColorPointer(3, GL_FLOAT, 6 * sizeof(GLfloat), vert + 3); // specify vertex color array 

    glDrawElements(GL_QUADS, sizeof(ind)/sizeof(GLubyte), GL_UNSIGNED_BYTE, ind); 

#endif 

    frameStat->swapBuffers(drawStat);   // SDL_GL_SwapWindow() and frame statistics 
} 

//--- Event handling ------------------------------------- 

/** Function to release/destroy our resources and restore the old desktop. */ 
void Quit (int returnCode) 
{ 
    scene.deleteBuffers(); 

    if (glcontext) 
    SDL_GL_DeleteContext(glcontext); 

    // Clean up the window .. 
    SDL_Quit(); 

    // .. and exit appropriately 
    exit(returnCode); 
} 

Und hier ist meine Maus-Handling-Funktionen zu zeichnen, ich bin ohne die Freigabefunktion, da es trivial ist.

//-------------------------------------------------------- 
// Mouse handling: 

void handleMouseMove (SDL_Event &ev) 
{ 

    if (ev.button.button == SDL_BUTTON_LEFT && dragging) 
    { 
    rotation.set(MI_IDENTITY); 
    rotation.rotate(5, 0.0f, 1.0f, 0.0f); 
    matModelView = matModelView * rotation; 
    } 
} 

void handleMousePress (SDL_Event &ev) 
{ 

    if (ev.button.button == SDL_BUTTON_LEFT) 
    { 
    dragging = true; 
    glMatrixMode(GL_MODELVIEW); 

    glGetFloatv(GL_MODELVIEW_MATRIX, (GLfloat *)matModelView.array()); 
    rotation.set(MI_IDENTITY); 
    } 
} 

matModelView und rotation sind 4x4 Matrizen. In diesem Code sind meine Trackball-Berechnungen nicht enthalten. Hier erwarte ich nur, dass es sich um 5 Grad durch die x-Achse dreht, solange die Maus zieht.

Vielleicht ist es so einfach, aber ich bin in diesem Punkt stucked. Jede Leitlinie, Code-Beispiele wäre großartig.

Antwort

0

Try-Wechsel:

matModelView = matModelView * rotation; 

Um dies:

matModelView = rotation * matModelView; 
Verwandte Themen