DIRECTX11 Learning note 7-Support for free-moving cameras

Source: Internet
Author: User

A camera camera will now be re-established. can move freely.

such as forward and backward, upstream dive. Rendering in all directions.

First set the key.

This time needs to be

XWindow.h inside

BOOL Xwindow::frame () {//Determines whether the ESC key is pressed if (X_input->iskeydown (vk_escape)) return false;//If 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, rotating camera//x_graphics->x_ Camera->roll (pi/180);//Start Rendering return X_graphics->frame ();}
Add some keys. If the error, it may be x_camera in the XGraphics.h class is private, 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 it's simple.

And then the camera is modified.

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);} Peek, which is to keep the viewing direction unchanged, move along the vector right direction from side to side void Xcamera::strafe (float units) {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);} /*//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;}

The comment out of place is a pit. Why is it.

Here's the difference 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. Because Xmvector

See source

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

The above section says that 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 set to XMFLOAT3.

See source

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 can only be assigned how to play how to play

The above camera class annotated function is the tragedy of the previous, I thought that float can do the operation. The result was tragic.

I checked it later. You can use a transform

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 completed then store

Is it annoying?

Isn't it

No way. That's all I'm going to do right now. You wearies direct global variables. or return to DX10MATH.H.

Nonsense not to say.

Only the walk function is changed. 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);

In a word, don't build wheels.

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.