Ignore:
Timestamp:
06/22/08 09:23:45 (16 years ago)
Author:
mattausch
Message:
 
File:
1 edited

Legend:

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

    r2787 r2796  
    77{ 
    88 
     9using namespace std; 
     10 
    911 
    1012Camera::Camera()  
     
    1214        mWidth = 100; 
    1315        mHeight = 100; 
    14         mFovy = 90.0f * M_PI / 180.0f; 
     16        mFovy = 60.0f * M_PI / 180.0f; 
     17        mIsOrtho = false; 
     18         
     19        SetPosition(Vector3(0, 0, 0)); 
     20 
     21        mPitch = mYaw = 0; 
     22        Precompute(Vector3(0, 1, 0)); 
    1523} 
    1624 
     
    2230         
    2331        mFovy = fieldOfView * M_PI / 180.0f; 
    24 } 
    25  
    26  
    27 void Camera::Precompute()  
    28 { 
    29         mDirection.Normalize(); 
    30  
    31         /*Vector3 side = CrossProd(Vector3(0, 0, 1), mDirection); 
    32         mUp = -Normalize(CrossProd(side, mDirection)); 
    33         mRight = -Normalize(CrossProd(mDirection, mUp));*/ 
    34  
    35         mUp = Vector3(0, 0, 1); 
    36         mRight = -CrossProd(mDirection, mUp); 
    37         mUp = -Normalize(CrossProd(mRight, mDirection)); 
    38 } 
     32 
     33        mIsOrtho = false; 
     34 
     35        SetPosition(Vector3(0, 0, 0)); 
     36 
     37        mPitch = mYaw = 0; 
     38        Precompute(Vector3(0, 1, 0)); 
     39} 
     40 
     41 
     42void Camera::Precompute(const Vector3 &direction)  
     43{ 
     44        /* 
     45        Vector3 side = CrossProd(Vector3(1, 0, 0), direction); 
     46        Vector3 up = -Normalize(CrossProd(side, direction)); 
     47        Vector3 right = -Normalize(CrossProd(direction, up)); 
     48        */ 
     49        Vector3 up = Vector3(0, 0, 1); 
     50        Vector3 right = -Normalize(-CrossProd(direction, up)); 
     51        up = -Normalize(CrossProd(right, direction)); 
     52 
     53        mBaseOrientation = Matrix4x4(right, up, direction); 
     54        mViewOrientation = mBaseOrientation; 
     55} 
     56 
    3957 
    4058 
     
    4260{ 
    4361        mPosition = pos; 
    44         Precompute(); 
    45 } 
    46  
    47  
    48 void Camera::SetDirection(const Vector3 &dir)  
    49 { 
    50         mDirection = dir; 
    51         Precompute(); 
    5262} 
    5363 
     
    5666{ 
    5767        mNear = nearDist; 
    58 } 
    59  
    60  
    61 void Camera::LookInBox(const AxisAlignedBox3 &box) 
    62 { 
    63         //mDirection = Vector3(0, 0, 1); 
    64         mDirection = Vector3(0, 1, 0); 
    65         mPosition = box.Center(); 
    66         mPosition.z = box.Min(2) + 0.9f * box.Size(2); 
    67  
    68         Precompute(); 
    69 } 
    70  
    71  
    72 void Camera::LookAtBox(const AxisAlignedBox3 &box) 
    73 { 
    74         mDirection = Vector3(0, box.Min().y - box.Max().y, 0); 
    75         mPosition = Vector3(0);//box.Max() - mDirection; 
    76  
    77         Precompute(); 
    7868} 
    7969 
     
    156146void Camera::SetupCameraView() 
    157147{ 
    158         glLoadIdentity(); 
    159         gluLookAt(mPosition.x, mPosition.y, mPosition.z, 
    160                   mPosition.x + mDirection.x, mPosition.y + mDirection.y, mPosition.z + mDirection.z,  
    161                           mUp.x, mUp.y, mUp.z); 
    162  
    163         //std::cout << "dir: " << mDirection << " pos: " << mPosition << " up: " << mUp << std::endl; 
     148        Matrix4x4 tview = mViewOrientation; 
     149 
     150        Vector3 pos = -mPosition; 
     151        pos = tview * pos; 
     152 
     153        Matrix4x4 viewOrientation = mViewOrientation; 
     154 
     155        viewOrientation.x[3][0] = pos.x; 
     156        viewOrientation.x[3][1] = pos.y; 
     157        viewOrientation.x[3][2] = pos.z; 
     158 
     159        glLoadMatrixf((float *)viewOrientation.x); 
    164160} 
    165161 
     
    177173        const float w_far = h_far * GetAspect(); 
    178174 
    179         //const Vector3 view = mDirection; 
    180         const Vector3 view = mDirection; 
     175        const Vector3 view = GetDirection(); 
    181176        const Vector3 fc = mPosition + view * z_far;  
    182177         
    183         const Vector3 up = mUp; 
    184         const Vector3 right = mRight; 
     178        const Vector3 up = GetUpVector(); 
     179 
     180        const Vector3 right = GetRightVector(); 
    185181 
    186182        Vector3 t1, t2; 
     
    194190        fbr = fc - t1 + t2; 
    195191 
    196         const Vector3 nc = mPosition + mDirection * z_near; 
     192        const Vector3 nc = mPosition + view * z_near; 
    197193         
    198194        t1 = h_near * 0.5f * up; 
     
    206202 
    207203 
    208 } 
    209  
     204void Camera::SetOrtho(bool ortho) 
     205{ 
     206        mIsOrtho = true; 
     207} 
     208 
     209 
     210void Camera::Yaw(float angle) 
     211{ 
     212        mYaw += angle; 
     213        CalculateFromPitchAndYaw(); 
     214        /* 
     215        Matrix4x4 viewOrientation(mRight, mUp, mDirection); 
     216        Matrix4x4 rot = RotationYMatrix(angle); 
     217 
     218        viewOrientation = viewOrientation * rot; 
     219 
     220        mDirection = Vector3(viewOrientation.x[2][0], viewOrientation.x[2][1], viewOrientation.x[2][2]); 
     221        mRight =     Vector3(viewOrientation.x[0][0], viewOrientation.x[0][1], viewOrientation.x[0][2]); 
     222        mUp =        Vector3(viewOrientation.x[1][0], viewOrientation.x[1][1], viewOrientation.x[1][2]);*/ 
     223} 
     224 
     225 
     226void Camera::Pitch(float angle) 
     227{ 
     228        mPitch += angle; 
     229        CalculateFromPitchAndYaw(); 
     230} 
     231 
     232 
     233void Camera::CalculateFromPitchAndYaw() 
     234{ 
     235        mViewOrientation = mBaseOrientation; 
     236 
     237        Matrix4x4 roty = RotationYMatrix(mPitch); 
     238        Matrix4x4 rotx = RotationXMatrix(mYaw); 
     239 
     240        mViewOrientation *= roty; 
     241        mViewOrientation *= rotx; 
     242} 
     243 
     244 
     245Vector3 Camera::GetDirection() const 
     246{  
     247        return -Vector3(mViewOrientation.x[0][2], mViewOrientation.x[1][2], mViewOrientation.x[2][2]); 
     248} 
     249 
     250 
     251Vector3 Camera::GetUpVector() const  
     252{  
     253        return Vector3(mViewOrientation.x[0][1], mViewOrientation.x[1][1], mViewOrientation.x[2][1]); 
     254} 
     255 
     256 
     257Vector3 Camera::GetRightVector() const  
     258{  
     259        return Vector3(mViewOrientation.x[0][0], mViewOrientation.x[1][0], mViewOrientation.x[2][0]); 
     260                 
     261} 
     262 
     263 
     264} 
     265 
Note: See TracChangeset for help on using the changeset viewer.