DIRECTX11 Learning note 7-Support for free-moving cameras

Source: Internet
Author: User

A camera camera will now be developed again. able to move freely.

For example, forward and backward, upstream dive. Rendering in all directions.

First set the key.

This time needs to be

XWindow.h inside

BOOL Xwindow::frame () {//infers whether the ESC key is pressed if (X_input->iskeydown (vk_escape)) return false;//assumes the A,S,D,W,Q,E,Z,X,C key is pressed. Mobile Camera if (getasynckeystate (' W ') & 0x8000)//front and back X_graphics->x_camera->walk ( -0.1f); if (getasynckeystate (' S ') & 0x8000) X_graphics->x_camera->walk (0.1f); if (getasynckeystate (' A ') & 0x8000)//Around X_graphics->x_camera->strafe ( -0.1f); if (getasynckeystate (' D ') & 0x8000) X_graphics->x_camera->strafe (0.1f); if (getasynckeystate (' Q ') & 0x8000)//Up/down//x_graphics->x_camera->fly ( -0.1f); if (getasynckeystate (' E ') & 0x8000)//x_graphics->x_camera->fly (0.1f); if (getasynckeystate (' Z ') & 0x8000)//x_graphics->x_camera->pitch (pi/180); if (getasynckeystate (' X ') & 0x8000)//x_graphics->x_camera->yaw (pi/180); if (getasynckeystate (' C ') & 0x8000)//x_graphics->x_camera->roll (pi/180);//animation. Rotate the camera//x_graphics->x_camera->roll (pi/180);//Start Rendering return X_graphics->frame ();}
Add some keys. Assuming an error, it may be that X_camera is privately owned in the XGraphics.h class and can be set to public

Private:bool render (); Public:xcamera *x_camera;//camera private:xd3device *x_d3d;//3d device Xmodel *x_model;//model Xshader *x_shader;//renderer HWND hwnd;


And then very easy.

And then you change the camera.

Let's start with the code.

#pragma once#include <xnamath.h>class xcamera{public:enum cameratype {landobject, aircraft}; Xcamera (); void Strafe (float units);    L Left and right//void fly (float units);   Upper and lower void Walk (float units); Front and rear//void pitch (float angle);   Rotate the view coordinate system right vector//void yaw (float angle);  Rotate up vector//void roll (float angle); Rotation look vector void Getviewmatrix (xmmatrix& V); void Setcameratype (Cameratype cameratype); void GetPosition (xmfloat3* pos); void SetPosition (xmfloat3* pos); void GetRight (xmfloat3* right), void GetUp (xmfloat3* up), void Getlook (xmfloat3* look);p rivate:cameratype _cameratype; XMFLOAT3 _right,_up,_look,_pos;}; Xcamera::xcamera () {_CAMERATYPE=AIRCRAFT;_POS=XMFLOAT3 (0.0f, 0.0f, -10.0f); _right=xmfloat3 (1.0f,0.0f,0.0f); _up= XMFLOAT3 (0.0f,1.0f,0.0f); _look=xmfloat3 (0.0f,0.0f,1.0f);} void Xcamera::getposition (xmfloat3* pos) {*pos = _pos;} void Xcamera::setposition (xmfloat3* pos) {_pos = *pos;} void Xcamera::getright (xmfloat3* right) {*right = _right;} void Xcamera::getup (xmfloat3* up) {*up = _up;} VoID Xcamera::getlook (xmfloat3* look) {*look = _look;} Walking, moving along the camera Watch direction void Xcamera::walk (float units) {Xmvector VPOS,VLOOK;VPOS=XMLOADFLOAT3 (&_pos); vlook= XMLOADFLOAT3 (&_look)///Only in X,z plane move if (_cameratype = = Landobject) {vPOS + = Xmvectorset (_look.x, 0.0f, _look.z,0) * Units;} if (_cameratype = = aircraft) vPOS + = Vlook * Units; XMSTOREFLOAT3 (&_pos,vpos);} Scan. It means to keep the viewing direction constant, to shift from one side of the vector right direction to the other side void Xcamera::strafe (float units) {Xmvector VPOS,VRIGHT;VPOS=XMLOADFLOAT3 (&_pos ); Vright=xmloadfloat3 (&_right);//Only in X,z plane move if (_cameratype = = landobject) vPOS + = Xmvectorset (_right.x, 0.0f, _ right.z,0.0f) * UNITS;IF (_cameratype = = aircraft) vPOS + = Vright * Units; XMSTOREFLOAT3 (&_pos,vpos);} /*//flight mode, ascending or descending, refers to moving void Xcamera::fly (float units) along the vector up direction {//only in the y-axis move if (_cameratype = = landobject) _pos.y + = Units;if (_ Cameratype = = aircraft) _pos + = _up * Units;} void Xcamera::p itch (float angle) {Xmmatrix T; T=xmmatrixrotationaxis (_right, angle);//around right vector. Rotate up and Look_up=xmvector3transformcoord (_UP,_UP,t); _look=xmvector3transformcoord (_look, t);} void Xcamera::yaw (float angle) {D3dxmatrix t;//to Landobject, always rotates around (0,1,0).

if (_cameratype = = landobject) t=xmmatrixrotationy (angle);//for aircraft, rotate if (_cameratype = aircraft) around the up vector t= Xmmatrixrotationaxis (_up, angle);//Rotate Right and Look_right=xmvector3transformcoord (_right, T) around the up or Y axis; _look= Xmvector3transformcoord (_look, T);} void Xcamera::roll (float angle) {//Only the aircraft mode is left roll rotation if (_cameratype = = aircraft) {D3dxmatrix T; T=xmmatrixrotationaxis (_look, angle);//round the look vector, rotate up and Right_right=xmvector3transformcoord (_right, T); _up= Xmvector3transformcoord (_up, T);}} */void Xcamera::getviewmatrix (Xmmatrix &v) {xmvector vlook,vup,vright,vpos;vpos=xmloadfloat3 (&_pos); vlook= XMLOADFLOAT3 (&_look); Vup=xmloadfloat3 (&AMP;_UP); Vright=xmloadfloat3 (&_right);//Maintain view local coordinate system, Each axis is orthogonal to each other vlook=xmvector3normalize (vlook);//Look X Rightvup=xmvector3cross (Vlook, vright); Vup=xmvector3normalize (vup ); Vright=xmvector3cross (Vup, Vlook); Vright=xmvector3normalize (vright); V=XMMATRIXLOOKATLH (Vpos,vlook, vup);//Generate View matrix://float x =-d3dxvec3dot (&_right, &_pos);//float y =-d3dxvEc3dot (&_up, &_pos);//float z =-d3dxvec3dot (&_look, &_pos);//(*V) (0,0) = _right.x; (*V) (0, 1) = _up.x; (*V) (0, 2) = _look.x; (*V) (0, 3) = 0.0f;//(*v) (1,0) = _right.y; (*V) (1, 1) = _UP.Y; (*V) (1, 2) = _LOOK.Y; (*V) (1, 3) = 0.0f;//(*v) (2,0) = _right.z; (*V) (2, 1) = _up.z; (*V) (2, 2) = _look.z; (*V) (2, 3) = 0.0f;//(*v) (3,0) = x; (*V) (3, 1) = y; (*V) (3, 2) = Z; (*V) (3, 3) = 1.0f;} void Xcamera::setcameratype (Cameratype cameratype) {_cameratype = Cameratype;}


Staring out of place is a pit. Why is it.

Here are the differences between Xmvector and XMFLOAT3.

The former is a vector. The latter is a point structure

The former supports various operations.

Almost multiply and subtract.

The latter can only be assigned a value AH or something.

is not particularly strange.

Since Xmvector

See Source code

Vector Intrinsic:four (floating point) aligned on a bytes//boundary and mapped to hardware vector registers#if defined (_xm_sse_intrinsics_) &&!defined (_xm_no_intrinsics_) typedef __m128 Xmvector; #else

128 people understand. Can not play casually, or the error will be related to the alignment problem

Said the last verse. Global variables and local variables can be used Xmvector but class variables are not recommended

The camera is a class variable.

Die. It's only been set to XMFLOAT3.

See Source code

3D Vector; A-bit floating point componentstypedef struct _xmfloat3{    FLOAT x;    FLOAT y;    FLOAT z; #ifdef __cplusplus    _xmfloat3 () {};    _XMFLOAT3 (float _x, float _y, float _z): X (_x), Y (_y), Z (_z) {};    _XMFLOAT3 (CONST FLOAT *parray);    _xmfloat3& operator= (CONST _xmfloat3& Float3); #endif//__cplusplus

32-bit only can be assigned how to play how to play

The above camera class added the gaze function to the previous tragedy, and I thought that float could do the math. The result was tragic.

I checked it later. Be able to use a conversion

Xmvector VPOS,VRIGHT;VPOS=XMLOADFLOAT3 (&_pos); Vright=xmloadfloat3 (&_right);//move only on x,z plane if (_cameraType = = Landobject) vPOS + = Xmvectorset (_right.x, 0.0f, _right.z,0.0f) * UNITS;IF (_cameratype = aircraft) vPOS + = Vright * Units ; XMSTOREFLOAT3 (&_pos,vpos);

Do you understand me, see? Load put float into vector local variable and then perform operation

After the operation is finished, store

Is it annoying?

Isn't it

No way.

I'm just going to do this right now. You wearies direct global variables.

or return to DX10MATH.H.

Nonsense not to say.

Just changed the walk function. Other change yourself, exercise yourself

(*V) (0,0) = _right.x; (*V) (0, 1) = _up.x; (*V) (0, 2) = _look.x; (*V) (0, 3) = 0.0f;//(*v) (1,0) = _right.y; (*V) (1, 1) = _UP.Y; (*V) (1, 2) = _LOOK.Y; (*V) (1, 3) = 0.0f;//(*v) (2,0) = _right.z; (*V) (2, 1) = _up.z; (*V) (2, 2) = _look.z; (*V) (2, 3) = 0.0f;//(*v) (3,0) = x;        (*V) (3, 1) = y;     (*V) (3, 2) = Z;       (*V) (3, 3) = 1.0f;

Yes, and the top one. The original tutorial is the direct calculation of a matrix, trouble it.

Trouble, please. Can not understand the meaning of it, look at the Dragon book to understand.

Understand what to do after. Still knocking so much??

V=XMMATRIXLOOKATLH (Vpos,vlook, vup);

A word is done. Don't build wheels.

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvy3eznjexmdyzmdy=/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ">

DIRECTX11 Learning note 7-Support for free-moving cameras

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.