void motion(int x, int y)
{   
    Real32 w = win->getWidth(), h = win->getHeight();
    

    Real32  
        a = -2. * ( lastx / w - .5 ),
        b = -2. * ( .5 - lasty / h ),
        c = -2. * ( x / w - .5 ),
        d = -2. * ( .5 - y / h );
    
    if(mouseb & (1 << GLUT_LEFT_BUTTON))
    {
        tball.updateRotation(a, b, c, d);     
    }
    else if(mouseb & (1 << GLUT_MIDDLE_BUTTON))
    {
        tball.updatePosition(a, b, c, d);     
    }
    else if(mouseb & (1 << GLUT_RIGHT_BUTTON))
    {
        tball.updatePositionNeg(a, b, c, d);  
    }

    lastx = x;
    lasty = y;
}
void motion(int x, int y)
{   
    Real32 w = win->getWidth(), h = win->getHeight();
    

    Real32  
        a = -2. * ( lastx / w - .5 ),
        b = -2. * ( .5 - lasty / h ),
        c = -2. * ( x / w - .5 ),
        d = -2. * ( .5 - y / h );
    
    if ( mouseb & ( 1 << GLUT_LEFT_BUTTON ) )
    {
        pActiveTBall->updateRotation( a, b, c, d );     
    }
    else if ( mouseb & ( 1 << GLUT_MIDDLE_BUTTON ) )
    {
        pActiveTBall->updatePosition( a, b, c, d );     
    }
    else if ( mouseb & ( 1 << GLUT_RIGHT_BUTTON ) )
    {
        pActiveTBall->updatePositionNeg( a, b, c, d );  
    }

    if(move_obj == true)
    {
/*
        fprintf(stderr, "%f %f %f\n",
                pActiveTBall->getPosition()[0],
                pActiveTBall->getPosition()[1],
                pActiveTBall->getPosition()[2]);
 */
    }

    lastx = x;
    lasty = y;
}
void terrainRenderCB(DrawEnv *pEnv)
{
    const float res = 1.0E4f;

    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();

#if 1
    Vec3f vTo(0.f, 0.f, -1.f);
    Vec3f vUp(0.f, 1.f,  0.f);

    Matrix mde = pEnv->getCameraViewing();

    glLoadMatrixf(&(mde[0][0]));

    vTo = m1c * vTo;
    vUp = m1c * vUp;

    if(bWireframe == true)
        glPolygonMode( GL_FRONT_AND_BACK, GL_LINE);


    terrain->draw(res,
                  //exr,ey,ezr,
                  m1c[3][0], m1c[3][1], m1c[3][2],
                  //dxr,dy,dzr,
                  //mde[2][0], mde[2][1], mde[2][2],
                  vTo[0], vTo[1], vTo[2],
                  //ux,uy,uz,
                  //mde[1][0], mde[1][1], mde[1][2],
                  vUp[0], vUp[1], vUp[2],
                  0.f, //90.f, //fovy,
                  (float) win->getWidth() / win->getHeight(),
                  1000.f, //nearp,
                  100000000.f,
                  0); //farp);


    if(bWireframe == true)
        glPolygonMode( GL_FRONT_AND_BACK, GL_FILL);
#else

    float ex = 0.000000;
    float ey = 27.223131;
    float ez = 0.000000;
    
    float sina = 0.422618;
    float tanp = 0.000000;
    float cosa = 0.906308;

    static const float fovy=90.0f,nearp=0.1f,farp=100000.0f;
    static const float scale=1.f; //00.0f; // 1:100

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(ex,ey,-ez,ex+sina,ey-tanp,-ez-cosa,0.0f,1.0f,0.0f);

    // update vertex arrays
    terrain->draw(res,
                  ex,ey,-ez,
                  sina,-tanp,-cosa,
                  0.0f,1.0f,0.0f,
                  fovy,(float) win->getWidth() / win->getHeight(),
                  nearp/scale,farp/scale,
                  0 /*upd*/);
#endif

    glMatrixMode(GL_MODELVIEW);
    glPopMatrix();
}