Ignore:
Timestamp:
06/19/08 18:43:17 (16 years ago)
Author:
mattausch
Message:
 
File:
1 edited

Legend:

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

    r2779 r2780  
    77{ 
    88 
    9 using namespace std; 
    10  
    119 
    1210Camera::Camera()  
     
    1513        mHeight = 100; 
    1614        mFovy = 90.0f * M_PI / 180.0f; 
    17         mIsOrtho = false; 
    18          
    19         SetPosition(Vector3(0, 0, 0)); 
    20  
    21         mPitch = mYaw = 0; 
    22         Precompute(); 
    2315} 
    2416 
     
    3022         
    3123        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(); 
    4124} 
    4225 
     
    4427void Camera::Precompute()  
    4528{ 
    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         cout<<"view: " << direction << " right: " << right << " up " << up << endl; 
    56 } 
    57  
     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} 
    5842 
    5943 
     
    6145{ 
    6246        mPosition = pos; 
     47        Precompute(); 
     48} 
     49 
     50 
     51void Camera::SetDirection(const Vector3 &dir)  
     52{ 
     53        mDirection = dir; 
     54        Precompute(); 
    6355} 
    6456 
     
    6759{ 
    6860        mNear = nearDist; 
     61} 
     62 
     63 
     64void 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 
     75void 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(); 
    6981} 
    7082 
     
    147159void Camera::SetupCameraView() 
    148160{ 
    149         Matrix4x4 viewOrientation = mViewOrientation; 
    150         Vector3 pos = -mPosition; 
    151         pos = mViewOrientation * pos; 
    152  
    153         viewOrientation.x[3][0] = pos.x; 
    154         viewOrientation.x[3][1] = pos.y; 
    155         viewOrientation.x[3][2] = pos.z; 
    156  
    157161        glLoadIdentity(); 
    158         glMultMatrixf((float *)viewOrientation.x); 
     162        gluLookAt(mPosition.x, mPosition.y, mPosition.z, 
     163                  mPosition.x + mDirection.x, mPosition.y + mDirection.y, mPosition.z + mDirection.z,  
     164                          mUp.x, mUp.y, mUp.z); 
     165 
     166        //std::cout << "dir: " << mDirection << " pos: " << mPosition << " up: " << mUp << std::endl; 
    159167} 
    160168 
     
    172180        const float w_far = h_far * GetAspect(); 
    173181 
    174         const Vector3 view = GetDirection(); 
     182        const Vector3 view = mDirection; 
    175183        const Vector3 fc = mPosition + view * z_far;  
    176184         
    177         const Vector3 up = GetUpVector(); 
    178  
    179         const Vector3 right = GetRightVector(); 
     185        const Vector3 up = mUp; 
     186 
     187        const Vector3 right = mRight; 
    180188 
    181189        Vector3 t1, t2; 
     
    189197        fbr = fc - t1 + t2; 
    190198 
    191         const Vector3 nc = mPosition + view * z_near; 
     199        const Vector3 nc = mPosition + mDirection * z_near; 
    192200         
    193201        t1 = h_near * 0.5f * up; 
     
    201209 
    202210 
    203 void Camera::SetOrtho(bool ortho) 
    204 { 
    205         mIsOrtho = true; 
    206 } 
    207  
    208  
    209 void Camera::Yaw(float angle) 
    210 { 
    211         mYaw += angle; 
    212         CalculateFromPitchAndYaw(); 
    213         /* 
    214         Matrix4x4 viewOrientation(mRight, mUp, mDirection); 
    215         Matrix4x4 rot = RotationYMatrix(angle); 
    216  
    217         viewOrientation = viewOrientation * rot; 
    218  
    219         mDirection = Vector3(viewOrientation.x[2][0], viewOrientation.x[2][1], viewOrientation.x[2][2]); 
    220         mRight =     Vector3(viewOrientation.x[0][0], viewOrientation.x[0][1], viewOrientation.x[0][2]); 
    221         mUp =        Vector3(viewOrientation.x[1][0], viewOrientation.x[1][1], viewOrientation.x[1][2]);*/ 
    222 } 
    223  
    224  
    225 void Camera::Pitch(float angle) 
    226 { 
    227         mPitch += angle; 
    228         CalculateFromPitchAndYaw(); 
    229 } 
    230  
    231  
    232 void Camera::CalculateFromPitchAndYaw() 
    233 { 
    234         mViewOrientation = mBaseOrientation; 
    235  
    236         Matrix4x4 roty = RotationYMatrix(mPitch); 
    237         Matrix4x4 rotx = RotationXMatrix(mYaw); 
    238  
    239         mViewOrientation *= roty; 
    240         mViewOrientation *= rotx; 
    241 } 
    242  
    243  
    244 Vector3 Camera::GetDirection() const 
    245 {  
    246         return Vector3(mViewOrientation.x[0][2], mViewOrientation.x[1][2], mViewOrientation.x[2][2]); 
    247 } 
    248  
    249  
    250 Vector3 Camera::GetUpVector() const  
    251 {  
    252         return Vector3(mViewOrientation.x[0][1], mViewOrientation.x[1][1], mViewOrientation.x[2][1]); 
    253 } 
    254  
    255  
    256 Vector3 Camera::GetRightVector() const  
    257 {  
    258         return Vector3(mViewOrientation.x[0][0], mViewOrientation.x[1][0], mViewOrientation.x[2][0]); 
    259                  
    260 } 
    261  
    262  
    263 } 
    264  
     211} 
     212 
Note: See TracChangeset for help on using the changeset viewer.