Flexible Camera class

Source: Internet
Author: User

Class camera <br/>{< br/> Public: <br/> Enum cameratype <br/>{< br/> landobject, Aircraft <br/> }; <br/> camera (); <br/> camera (cameratype); <br/> ~ Camera (); <br/> void strafe (float units); // left/right; <br/> void fly (float units); // up/down; <br/> void walk (float units); // forward/backward; <br/> void pitch (float angle); // rotate on right vector; <br/> void yaw (float angle); // rotate on up vector; <br/> void roll (float angle); // rotate on look vector; <br/> void getviewmatrix (d3dxmatrix * V); <br/> void setcameratype (cameratype); <br/> void getposition (d3dxvector3 * POS ); <br/> void setposition (d3dxvector3 * POS); <br/> void getright (d3dxvector3 * right); <br/> void getup (d3dxvector3 * up ); <br/> void getlook (d3dxvector3 * Look); <br/> PRIVATE: <br/> cameratype _ cameratype; <br/> d3dxvector3 _ right; <br/> d3dxvector3 _ up; <br/> d3dxvector3 _ Look; <br/> d3dxvector3 _ Pos; <br/>}; <br/> void camera :: getviewmatrix (d3dxmatrix * V) <br/>{< br/> // keep camera's axes orthogonal to eachother <br/> d3dxvec3normalize (& _ Look, & _ Look ); <br/> d3dxvec3cross (& _ up, & _ Look, & _ right); <br/> d3dxvec3normalize (& _ up, & _ up ); </P> <p> d3dxvec3cross (& _ right, & _ up, & _ Look); <br/> d3dxvec3normalize (& _ right, & _ right ); <br/> // build the view matrix: <br/> float x =-d3dxvec3dot (& _ right, & _ POS ); <br/> float y =-d3dxvec3dot (& _ up, & _ POS); <br/> float Z =-d3dxvec3dot (& _ Look, & _ POS ); <br/> (* V) (0, 0) = _ right. x; <br/> (* V) (0, 1) = _ up. x; <br/> (* V) (0, 2) = _ look. x; <br/> (* V) (0, 3) = 0.0f; <br/> (* V) (1, 0) = _ right. y; <br/> (* V) (1, 1) = _ up. y; <br/> (* V) (1, 2) = _ look. y; <br/> (* V) (1, 3) = 0.0f; <br/> (* V) (2, 0) = _ right. z; <br/> (* V) (2, 1) = _ up. z; <br/> (* V) (2, 2) = _ look. z; <br/> (* V) (2, 3) = 0.0f; <br/> (* V) (3, 0) = x; <br/> (* V) (3, 1) = y; <br/> (* V) (3, 2) = z; <br/> (* V) (3, 3) = 1.0f; <br/>}< br/> void camera: pitch (& T, & _ right, angle) <br/>{< br/> d3dxmatrix t; <br/> d3dxmatrixrotationaxis (& T, & _ right, angle ); <br/> // rotate _ up and _ Look around _ right vector <br/> d3dxvec3transformcoord (& _ up, & _ up, & T ); <br/> d3dxvec3transformcoord (& _ Look, & _ Look, & T); <br/>}< br/> void camera: yaw (float angle) <br/> {<br/> d3dxmatrix t; <br/> // rotate around World Y (0, 1, 0) always for land object <br/> If (_ cameratype = landobject) <br/> d3dxmatrixrotationy (& T, angle ); </P> <p> // rotate around own up vector for Aircraft <br/> If (_ cameratype = aircraft) <br/> d3dxmatrixrotationaxis (& T, & _ up, angle); <br/> // rotate _ Right and _ Look around _ up or Y-axis <br/> d3dxvec3transformcoord (& _ right, & _ right, & T); <br/> d3dxvec3trandformcoord (& _ Look, & _ Look, & T); <br/>}< br/> void camera: Roll (float angle) <br/>{< br/> // only roll for aircraft type <br/> If (_ cameratype = aircraft) <br/>{< br/> d3dxmatrix T; <br/> d3dxmatrixrotationaxis (& T, & _ Look, angle ); <br/> // rotate _ up and _ right around _ Look vector <br/> d3dxvec3transformcoord (& _ right, & _ right, & T ); <br/> d3dxvec3transformcoord (& _ Look, & _ Look, & T); <br/>}< br/> void camera :: walk (float units) <br/>{< br/> // move only on xz plane for land object <br/> If (_ cameratype = landobject) <br/> _ POS + = d3dxcector3 (_ look. x, 0.0f, _ look. z) * units; <br/> If (_ cameratype = aircraft) <br/> _ POS + = _ look * units; <br/>}< br/> void camera: strafe (float units) <br/> {<br/> // move only on xz plane for land object <br/> If (_ cameratype = landobject) <br/> _ POS + = d3dxvector3 (_ right. x, 0.0f, _ right. z) * units; <br/> If (_ cameratype = aircraft) <br/> _ POS + = _ right * units; <br/>}< br/> void camera: Fly (float units) <br/> {<br/> // move only on Y-axis for land object <br/> If (_ cameratype = landobject) <br/> _ POS. Y + = units; <br/> If (_ cameratype = aircrafe) <br/> _ POS + = _ up * units; <br/>}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.