Ignore:
Timestamp:
06/19/08 18:07:09 (16 years ago)
Author:
mattausch
Message:

updated the camera model

File:
1 edited

Legend:

Unmodified
Added
Removed
  • GTP/trunk/App/Demos/Vis/CHC_revisited/Camera.cpp

    r2776 r2778  
    77{ 
    88 
     9using namespace std; 
     10 
    911 
    1012Camera::Camera()  
     
    1315        mHeight = 100; 
    1416        mFovy = 90.0f * M_PI / 180.0f; 
     17        mIsOrtho = false; 
     18         
     19        SetPosition(Vector3(0, 0, 0)); 
     20 
     21        mPitch = mYaw = 0; 
     22        Precompute(); 
    1523} 
    1624 
     
    2230         
    2331        mFovy = fieldOfView * M_PI / 180.0f; 
     32 
     33        mIsOrtho = false; 
     34 
     35        SetPosition(Vector3(0, 0, 0)); 
     36 
     37        SetPosition(Vector3(0, 0, 0)); 
     38 
     39        mPitch = mYaw = 0; 
     40        Precompute(); 
    2441} 
    2542 
     
    2744void Camera::Precompute()  
    2845{ 
    29         mDirection.Normalize(); 
    30  
    31         //Vector3 side = CrossProd(Vector3(0, 1, 0), mDirection); 
    32         Vector3 side = CrossProd(Vector3(0, 0, 1), mDirection); 
    33          
    34         mUp = -Normalize(CrossProd(side, mDirection)); 
    35         mRight = -Normalize(CrossProd(mDirection, mUp)); 
    36  
    37  
    38         float k = tan(mFovy/2); 
    39         mUp *= k; 
    40         mRight *= k*mWidth/mHeight; 
    41 } 
     46        Vector3 direction = Vector3(0, 1, 0); 
     47 
     48        Vector3 side = CrossProd(Vector3(1, 0, 0), direction); 
     49         
     50        Vector3 up = -Normalize(CrossProd(side, direction)); 
     51        Vector3 right = -Normalize(CrossProd(direction, up)); 
     52 
     53        mBaseOrientation = Matrix4x4(right, up, direction); 
     54        mViewOrientation = mBaseOrientation; 
     55} 
     56 
    4257 
    4358 
     
    4560{ 
    4661        mPosition = pos; 
    47         Precompute(); 
    48 } 
    49  
    50  
    51 void Camera::SetDirection(const Vector3 &dir)  
    52 { 
    53         mDirection = dir; 
    54         Precompute(); 
    5562} 
    5663 
     
    5966{ 
    6067        mNear = nearDist; 
    61 } 
    62  
    63  
    64 void Camera::LookInBox(const AxisAlignedBox3 &box) 
    65 { 
    66         //mDirection = Vector3(0, 0, 1); 
    67         mDirection = Vector3(0, 1, 0); 
    68         mPosition = box.Center(); 
    69         mPosition.z = box.Min(2) + 0.9f * box.Size(2); 
    70  
    71         Precompute(); 
    72 } 
    73  
    74  
    75 void Camera::LookAtBox(const AxisAlignedBox3 &box) 
    76 { 
    77         mDirection = Vector3(0, box.Min().y - box.Max().y, 0); 
    78         mPosition = Vector3(0);//box.Max() - mDirection; 
    79  
    80         Precompute(); 
    8168} 
    8269 
     
    159146void Camera::SetupCameraView() 
    160147{ 
     148#if 1 
     149        //Matrix4x4 viewOrientation(mRight, mDirection, mUp); 
     150        //Matrix4x4 viewOrientation(mRight, mUp, mDirection); 
     151        Matrix4x4 tview = mViewOrientation; 
     152 
     153        //Vector3 pos = tview * mPosition; 
     154        Vector3 pos = -mPosition; 
     155        pos = tview * pos; 
     156 
     157        Debug << "vieworient:\n" << mViewOrientation << " pos " << pos << " position " << mPosition << endl; 
     158 
     159        Matrix4x4 viewOrientation = mViewOrientation; 
     160 
     161        viewOrientation.x[3][0] = pos.x; 
     162        viewOrientation.x[3][1] = pos.y; 
     163        viewOrientation.x[3][2] = pos.z; 
     164 
    161165        glLoadIdentity(); 
     166        glMultMatrixf((float *)viewOrientation.x); 
     167        //glMultMatrixf((float *)tm.x); 
     168        Debug << "matrix:\n" << viewOrientation << endl; 
     169#else 
     170        glLoadIdentity(); 
     171 
    162172        gluLookAt(mPosition.x, mPosition.y, mPosition.z, 
    163173                  mPosition.x + mDirection.x, mPosition.y + mDirection.y, mPosition.z + mDirection.z,  
    164174                          mUp.x, mUp.y, mUp.z); 
    165  
     175#endif 
    166176        //std::cout << "dir: " << mDirection << " pos: " << mPosition << " up: " << mUp << std::endl; 
    167177} 
     
    180190        const float w_far = h_far * GetAspect(); 
    181191 
    182         const Vector3 view = mDirection; 
     192        const Vector3 view = GetDirection(); 
    183193        const Vector3 fc = mPosition + view * z_far;  
    184194         
    185         const Vector3 up = mUp; 
    186  
    187         const Vector3 right = mRight; 
     195        const Vector3 up = GetUpVector(); 
     196 
     197        const Vector3 right = GetRightVector(); 
    188198 
    189199        Vector3 t1, t2; 
     
    197207        fbr = fc - t1 + t2; 
    198208 
    199         const Vector3 nc = mPosition + mDirection * z_near; 
     209        const Vector3 nc = mPosition + view * z_near; 
    200210         
    201211        t1 = h_near * 0.5f * up; 
     
    209219 
    210220 
    211 } 
    212  
     221void Camera::SetOrtho(bool ortho) 
     222{ 
     223        mIsOrtho = true; 
     224} 
     225 
     226 
     227void Camera::Yaw(float angle) 
     228{ 
     229        mYaw += angle; 
     230        CalculateFromPitchAndYaw(); 
     231        /* 
     232        Matrix4x4 viewOrientation(mRight, mUp, mDirection); 
     233        Matrix4x4 rot = RotationYMatrix(angle); 
     234 
     235        viewOrientation = viewOrientation * rot; 
     236 
     237        mDirection = Vector3(viewOrientation.x[2][0], viewOrientation.x[2][1], viewOrientation.x[2][2]); 
     238        mRight =     Vector3(viewOrientation.x[0][0], viewOrientation.x[0][1], viewOrientation.x[0][2]); 
     239        mUp =        Vector3(viewOrientation.x[1][0], viewOrientation.x[1][1], viewOrientation.x[1][2]);*/ 
     240} 
     241 
     242 
     243void Camera::Pitch(float angle) 
     244{ 
     245        mPitch += angle; 
     246        CalculateFromPitchAndYaw(); 
     247} 
     248 
     249 
     250void Camera::CalculateFromPitchAndYaw() 
     251{ 
     252        mViewOrientation = mBaseOrientation; 
     253 
     254        Matrix4x4 roty = RotationYMatrix(mPitch); 
     255        Matrix4x4 rotx = RotationXMatrix(mYaw); 
     256 
     257        mViewOrientation *= roty; 
     258        mViewOrientation *= rotx; 
     259} 
     260 
     261 
     262Vector3 Camera::GetDirection() const 
     263{  
     264        return Vector3(mViewOrientation.x[0][2], mViewOrientation.x[1][2], mViewOrientation.x[2][2]); 
     265} 
     266 
     267 
     268Vector3 Camera::GetUpVector() const  
     269{  
     270        return Vector3(mViewOrientation.x[0][1], mViewOrientation.x[1][1], mViewOrientation.x[2][1]); 
     271} 
     272 
     273 
     274Vector3 Camera::GetRightVector() const  
     275{  
     276        return Vector3(mViewOrientation.x[0][0], mViewOrientation.x[1][0], mViewOrientation.x[2][0]); 
     277                 
     278} 
     279 
     280 
     281} 
     282 
Note: See TracChangeset for help on using the changeset viewer.