Beispiel #1
0
static void _display()
{
    // 透視変換を設定
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    float view_x, view_y;
    if( glutGet(GLUT_WINDOW_WIDTH) > glutGet(GLUT_WINDOW_HEIGHT) ){
        view_x = 1.0;
        view_y = 1.0 * glutGet( GLUT_WINDOW_HEIGHT) / glutGet( GLUT_WINDOW_WIDTH );
    }else{
        view_x = 1.0 * glutGet( GLUT_WINDOW_WIDTH) / glutGet( GLUT_WINDOW_HEIGHT );
        view_y = 1.0;
    }
    view_x *= 10;
    view_y *= 10;
    glFrustum(-view_x,view_x, -view_y,view_y, 40, 100000);
    gluLookAt(0, 0, 100, 0, 0, 0, 0, 1.0, 0);
    
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
    // モデル変換を設定
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glLightfv(GL_LIGHT0, GL_AMBIENT, glm::value_ptr(glm::vec4(0.3)));
    glLightfv(GL_LIGHT0, GL_POSITION, glm::value_ptr(glm::vec4(0,1,1,0)));
    glLightfv(GL_LIGHT0, GL_DIFFUSE, glm::value_ptr(glm::vec4(1)));
    glLightfv(GL_LIGHT0, GL_SPECULAR, glm::value_ptr(glm::vec4(8)));
    
    _scene->BeforeDisplay();
    
    /* トラックボール処理による回転 */
    glMultMatrixd(trackballRotation());
    
    _scene->Display();
    CHECK();
    
    glutSwapBuffers();

}
//--------------------------------------------------------------------------------------------------
/// Rotate
/// 
/// \return  Returns true if input caused changes to the camera an false if no changes occurred
//--------------------------------------------------------------------------------------------------
bool ManipulatorTrackball::rotate(int posX, int posY)
{
    if (m_camera.isNull()) return false;
    if (posX == m_lastPosX && posY == m_lastPosY) return false;

    const double vpPixSizeX = m_camera->viewport()->width();
    const double vpPixSizeY = m_camera->viewport()->height();
    if (vpPixSizeX <= 0 || vpPixSizeY <= 0) return false;

    const double vpPosX     = posX       - static_cast<int>(m_camera->viewport()->x());
    const double vpPosY     = posY       - static_cast<int>(m_camera->viewport()->y());
    const double vpLastPosX = m_lastPosX - static_cast<int>(m_camera->viewport()->x());
    const double vpLastPosY = m_lastPosY - static_cast<int>(m_camera->viewport()->y());

    // Scale the new/last positions to the range [-1.0, 1.0] 
    double newPosX =  2.0*(vpPosX/vpPixSizeX) - 1.0;
    double newPosY =  2.0*(vpPosY/vpPixSizeY) - 1.0;
    double lastPosX = 2.0*(vpLastPosX/vpPixSizeX) - 1.0;
    double lastPosY = 2.0*(vpLastPosY/vpPixSizeY) - 1.0;

    Mat4d viewMat = m_camera->viewMatrix();

    // Compute rotation quaternion
    Quatd rotQuat = trackballRotation(lastPosX, lastPosY, newPosX, newPosY, viewMat, m_rotateSensitivity);

    // Update navigation by modifying the view matrix
    Mat4d rotMatr = rotQuat.toMatrix4();
    rotMatr.translatePostMultiply(-m_rotationPoint);
    rotMatr.translatePreMultiply(m_rotationPoint);

    viewMat = viewMat*rotMatr;
    m_camera->setViewMatrix(viewMat);

    m_lastPosX = posX;
    m_lastPosY = posY;

    return true;
}