Skip to content

Commit

Permalink
add move modual,can use w s a d to move the camera
Browse files Browse the repository at this point in the history
  • Loading branch information
viktorika committed Jan 21, 2019
1 parent fd723fd commit 0d3cd73
Show file tree
Hide file tree
Showing 15 changed files with 230 additions and 92 deletions.
104 changes: 53 additions & 51 deletions src/ArcCamera.cpp
Original file line number Diff line number Diff line change
@@ -1,52 +1,54 @@
#include "ArcCamera.h"
#include <algorithm>

using std::max;
using std::min;

ArcCamera::ArcCamera()
:target_(0.0f,1.0f,0.0f),
distance_(5.0f),
xRotation_(0.0f),
yRotation_(0.0f),
xlimit(XM_PIDIV2),
ylimit(XM_PI)
{}

ArcCamera::~ArcCamera(){}

void ArcCamera::SetRotation(float x, float y)
{
xRotation_ = x;
yRotation_ = y;
xRotation_ = max(xRotation_, -xlimit);
xRotation_ = min(xRotation_, xlimit);
yRotation_ = max(yRotation_, -ylimit);
yRotation_ = min(yRotation_, ylimit);
}void ArcCamera::SetTarget(XMFLOAT3& target)
{
target_ = target;
}

void ArcCamera::ApplyRotation(float yawDelta, float pitchDelta)
{
xRotation_ += yawDelta;
yRotation_ += pitchDelta;
xRotation_ = max(xRotation_, -xlimit);
xRotation_ = min(xRotation_, xlimit);
yRotation_ = max(yRotation_, -ylimit);
yRotation_ = min(yRotation_, ylimit);
}

XMMATRIX ArcCamera::GetViewMatrix()
{
XMVECTOR zoom = XMVectorSet(0.0f, 0.0f, -distance_, 1.0f);
XMMATRIX rotation = XMMatrixRotationRollPitchYaw(xRotation_, yRotation_, 0.0f);
zoom = XMVector3Transform(zoom, rotation);
XMVECTOR lookAt = XMLoadFloat3(&target_);
XMVECTOR pos = lookAt + zoom;
XMVECTOR up = XMVectorSet(0.0f, 1.0f, 0.0f, 1.0f);
up = XMVector3Transform(up, rotation);
XMMATRIX viewMat = XMMatrixLookAtLH(pos, lookAt, up);
return viewMat;
#include "ArcCamera.h"
#include <algorithm>

using std::max;
using std::min;

ArcCamera::ArcCamera()
:target_(0.0f,2.0f,0.0f),
distance_(5.0f),
xRotation_(0.0f),
yRotation_(0.0f),
xlimit(XM_PIDIV2),
ylimit(XM_PI)
{}

ArcCamera::~ArcCamera(){}

void ArcCamera::SetRotation(float x, float y)
{
xRotation_ = x;
yRotation_ = y;
xRotation_ = max(xRotation_, -xlimit);
xRotation_ = min(xRotation_, xlimit);
yRotation_ = max(yRotation_, -ylimit);
yRotation_ = min(yRotation_, ylimit);
}

void ArcCamera::SetPositions(XMFLOAT3 pos, XMFLOAT3 target)
{
target_ = target;
}

void ArcCamera::ApplyRotation(float yawDelta, float pitchDelta)
{
xRotation_ += yawDelta;
yRotation_ += pitchDelta;
xRotation_ = max(xRotation_, -xlimit);
xRotation_ = min(xRotation_, xlimit);
yRotation_ = max(yRotation_, -ylimit);
yRotation_ = min(yRotation_, ylimit);
}

XMMATRIX ArcCamera::GetViewMatrix()
{
XMVECTOR zoom = XMVectorSet(0.0f, 0.0f, -distance_, 1.0f);
XMMATRIX rotation = XMMatrixRotationRollPitchYaw(xRotation_, yRotation_, 0.0f);
zoom = XMVector3Transform(zoom, rotation);
XMVECTOR lookAt = XMLoadFloat3(&target_);
XMVECTOR pos = lookAt + zoom;
XMVECTOR up = XMVectorSet(0.0f, 1.0f, 0.0f, 1.0f);
up = XMVector3Transform(up, rotation);
XMMATRIX viewMat = XMMatrixLookAtLH(pos, lookAt, up);
return viewMat;
}
2 changes: 1 addition & 1 deletion src/ArcCamera.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class ArcCamera:public Camera{
ArcCamera();
virtual ~ArcCamera();
void SetRotation(float x, float y);
void SetTarget(XMFLOAT3& target);
void SetPositions(XMFLOAT3 pos, XMFLOAT3 target);
void ApplyRotation(float yawDelta, float pitchDelta);
XMMATRIX GetViewMatrix();

Expand Down
1 change: 0 additions & 1 deletion src/Brick.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ void Brick::Render(ID3D11DeviceContext* d3dContext_, ID3D11Buffer* worldCB_,ID3D
XMMATRIX rotationMat = XMMatrixRotationRollPitchYaw(0.0f, 0.0f, 0.0f);
XMMATRIX scaling = XMMatrixScaling(1.0f, 1.0f, 1.0f);
XMMATRIX rsMat = rotationMat * scaling;
d3dContext_->VSSetConstantBuffers(0, 1, &worldCB_);
d3dContext_->PSSetShaderResources(0, 1, &colorMap_);
for (float x = -1000; x < 100; x += 10) {
for (float z = -1000; z < 100; z += 10) {
Expand Down
1 change: 1 addition & 0 deletions src/Camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ class Camera {
virtual ~Camera(){}
virtual XMMATRIX GetViewMatrix() = 0;
virtual void ApplyRotation(float yawDelta, float pitchDelta) = 0;
virtual void SetPositions(XMFLOAT3 pos, XMFLOAT3 target) = 0;
};
53 changes: 27 additions & 26 deletions src/Car.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,26 +43,31 @@ bool Car::Init_Resource(ID3D11Device* d3dDevice_) {
d3dResult = d3dDevice_->CreateBuffer(&vertexDesc, nullptr, &bodyvertexBuffer_);
if (FAILED(d3dResult))
return false;
VertexPos tirevertices[10] =
VertexPos tirevertices[1442] =
{
{XMFLOAT3(-0.5f,0.0f,0.0f),XMFLOAT2(0.0f,0.0f)},
{XMFLOAT3(0.5f,0.0f,0.0f),XMFLOAT2(0.0f,0.0f)}
};
tirevertices[2] = { XMFLOAT3(-0.5,0.5,0), XMFLOAT2(1,0) };
tirevertices[2] = { XMFLOAT3(-0.5,0.5,0), XMFLOAT2(0,0.5) };
tirevertices[3] = { XMFLOAT3(-0.5,0.5,0),XMFLOAT2(0,0) };
tirevertices[4] = { XMFLOAT3(0.5,0.5,0),XMFLOAT2(1,0) };
tirevertices[4] = { XMFLOAT3(0.5,0.5,0),XMFLOAT2(0,0.5) };
tirevertices[5] = { XMFLOAT3(0.5,0.5,0),XMFLOAT2(1,0) };
XMMATRIX rotationMat = XMMatrixRotationRollPitchYaw(-XM_PI/90, 0.0f, 0.0f);
XMVECTOR vec1 = XMVectorSet(-0.5, 0.5, 0, 1);
XMVECTOR vec2 = XMVectorSet(0.5, 0.5, 0, 1);
vec1 = XMVector4Transform(vec1, rotationMat);
vec2 = XMVector4Transform(vec2, rotationMat);
XMMATRIX rotationMat1 = XMMatrixRotationRollPitchYaw(-XM_PI/90.0f, 0.0f, 0.0f);
XMMATRIX rotationMat2 = XMMatrixRotationRollPitchYaw(0.0f, 0.0f, XM_PI / 90.0f);
XMVECTOR vec1 = XMVectorSet(-0.5f, 0.5f, 0.0f, 1.0f);
XMVECTOR vec2 = XMVectorSet(0.5f, 0.5f, 0.0f, 1.0f);
XMVECTOR vec3 = XMVectorSet(0.0f, 0.5f, 0.0f, 1.0f);
vec1 = XMVector4Transform(vec1, rotationMat1);
vec2 = XMVector4Transform(vec2, rotationMat1);
vec3 = XMVector4Transform(vec3, rotationMat2);
XMFLOAT3 next1, next2;
XMFLOAT2 next3;
XMStoreFloat3(&next1, vec1);
XMStoreFloat3(&next2, vec2);
tirevertices[6] = { next1,XMFLOAT2(0,1) };
XMStoreFloat2(&next3, vec3);
tirevertices[6] = { next1,next3 };
tirevertices[7] = { next1,XMFLOAT2(0,1) };
tirevertices[8] = { next2,XMFLOAT2(0,1) };
tirevertices[8] = { next2,next3 };
tirevertices[9] = { next2,XMFLOAT2(1,1) };
D3D11_SUBRESOURCE_DATA resourceData;
ZeroMemory(&resourceData, sizeof(resourceData));
Expand Down Expand Up @@ -126,8 +131,6 @@ void Car::Renderbody(ID3D11DeviceContext* d3dContext_, ID3D11Buffer* worldCB_, I
XMMATRIX worldMat = rotationMat * scaling * translationMat;
worldMat = XMMatrixTranspose(worldMat);
d3dContext_->UpdateSubresource(worldCB_, 0, 0, &worldMat, 0, 0);
d3dContext_->VSSetConstantBuffers(0, 1, &worldCB_);
d3dContext_->VSSetConstantBuffers(1, 1, &viewCB_);
d3dContext_->PSSetShaderResources(0, 1, &bodycolorMap_);
for (int i = 0; i < 6; ++i) {
array<int, 4> index = facevertexs_[i];
Expand All @@ -141,49 +144,47 @@ void Car::Renderbody(ID3D11DeviceContext* d3dContext_, ID3D11Buffer* worldCB_, I
d3dContext_->DrawIndexed(6, 0, 0);
}
}
void Car::Rendertire(ID3D11DeviceContext* d3dContext_, ID3D11Buffer* worldCB_, ID3D11Buffer* viewCB_) {
void Car::Rendertire(ID3D11DeviceContext* d3dContext_, ID3D11Buffer* worldCB_, ID3D11Buffer* viewCB_, ID3D11VertexShader* tiresolidColorVS_, ID3D11Buffer* textureCB_) {
unsigned int stride = sizeof(VertexPos);
unsigned int offset = 0;
d3dContext_->IASetVertexBuffers(0, 1, &tirevertexBuffer_, &stride, &offset);
//d3dContext_->IASetIndexBuffer(tireindexBuffer_, DXGI_FORMAT_R16_UINT, 0);
d3dContext_->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
//XMMATRIX translationMat = XMMatrixTranslation(0.0f, 0.5f, -1.0f);
XMMATRIX scaling = XMMatrixScaling(1.0f, 1.0f, 1.0f);
//XMMATRIX st = scaling * translationMat;
d3dContext_->VSSetConstantBuffers(0, 1, &worldCB_);
d3dContext_->VSSetConstantBuffers(1, 1, &viewCB_);
//d3dContext_->PSSetShaderResources(0, 1, &tirefrontcolorMap_);
float radian = -XM_PI / 90;
const float radian = -XM_PI / 90.0f;
XMMATRIX translationMat[4] = {
XMMatrixTranslation(-1.0f,0.5f,-1.0f),
XMMatrixTranslation(1.0f,0.5f,-1.0f),
XMMatrixTranslation(-1.0f,0.5f,1.0f),
XMMatrixTranslation(1.0f,0.5f,1.0f),
};
for (int j = 0; j < 4;++j) {
XMMATRIX st = scaling * translationMat[j];
d3dContext_->IASetIndexBuffer(tirefrontindexBuffer_, DXGI_FORMAT_R16_UINT, 0);
d3dContext_->PSSetShaderResources(0, 1, &tirefrontcolorMap_);
for (int i = 0; i < 180; ++i) {
XMMATRIX rotationMat = XMMatrixRotationRollPitchYaw(radian*i, 0.0f, 0.0f);
XMMATRIX worldMat = rotationMat * st;
XMMATRIX worldMat = rotationMat * translationMat[j];
worldMat = XMMatrixTranspose(worldMat);
d3dContext_->UpdateSubresource(worldCB_, 0, 0, &worldMat, 0, 0);
d3dContext_->DrawIndexed(6, 0, 0);
}
}
for (int j = 0; j < 4; ++j) {
d3dContext_->VSSetShader(tiresolidColorVS_, 0, 0);
d3dContext_->IASetIndexBuffer(tiresideindexBuffer_, DXGI_FORMAT_R16_UINT, 0);
d3dContext_->PSSetShaderResources(0, 1, &tiresidecolorMap_);
for (int i = 0; i < 180; ++i) {
XMMATRIX rotationMat = XMMatrixRotationRollPitchYaw(radian*i, 0.0f, 0.0f);
XMMATRIX worldMat = rotationMat * st;
XMMATRIX worldMat = rotationMat * translationMat[j];
worldMat = XMMatrixTranspose(worldMat);
d3dContext_->UpdateSubresource(worldCB_, 0, 0, &worldMat, 0, 0);
XMMATRIX textureMat = XMMatrixRotationRollPitchYaw(0.0f, 0.0f, radian*i);
textureMat *= XMMatrixTranslation(0.5f, 0.5f, 0.0f);
d3dContext_->UpdateSubresource(textureCB_, 0, 0, &textureMat, 0, 0);
d3dContext_->DrawIndexed(6, 0, 0);
}
}
}

void Car::Render(ID3D11DeviceContext* d3dContext_, ID3D11Buffer* worldCB_, ID3D11Buffer* viewCB_) {
void Car::Render(ID3D11DeviceContext* d3dContext_, ID3D11Buffer* worldCB_, ID3D11Buffer* viewCB_, ID3D11VertexShader* tiresolidColorVS_, ID3D11Buffer* textureCB_) {
Renderbody(d3dContext_, worldCB_, viewCB_);
Rendertire(d3dContext_, worldCB_, viewCB_);
Rendertire(d3dContext_, worldCB_, viewCB_, tiresolidColorVS_, textureCB_);
}
7 changes: 2 additions & 5 deletions src/Car.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Car :public Cube{
Car();
virtual ~Car();
bool Init_Resource(ID3D11Device* d3dDevice_);
void Render(ID3D11DeviceContext* d3dContext_, ID3D11Buffer* worldCB_, ID3D11Buffer* viewCB_);
void Render(ID3D11DeviceContext* d3dContext_, ID3D11Buffer* worldCB_, ID3D11Buffer* viewCB_, ID3D11VertexShader* tiresolidColorVS_, ID3D11Buffer* textureCB_);

private:
ID3D11ShaderResourceView* bodycolorMap_;
Expand All @@ -24,13 +24,10 @@ class Car :public Cube{
ID3D11Buffer* tirefrontindexBuffer_;
ID3D11Buffer* tiresideindexBuffer_;

//车身
//Cube body;
//模型空间轮胎的坐标
array<array<int, 6>, 4> tirevertexs_;
//世界空间汽车的中心坐标
array<float, 3> center_;

void Renderbody(ID3D11DeviceContext* d3dContext_, ID3D11Buffer* worldCB_, ID3D11Buffer* viewCB_);
void Rendertire(ID3D11DeviceContext* d3dContext_, ID3D11Buffer* worldCB_, ID3D11Buffer* viewCB_);
void Rendertire(ID3D11DeviceContext* d3dContext_, ID3D11Buffer* worldCB_, ID3D11Buffer* viewCB_, ID3D11VertexShader* tiresolidColorVS_, ID3D11Buffer* textureCB_);
};
36 changes: 36 additions & 0 deletions src/Fx/tiresolid.fx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
Texture2D colorMap_ : register(t0);
SamplerState colorSampler_ : register(s0);
cbuffer worldCb : register(b0)
{
matrix worldMatrix;
};
cbuffer viewCb : register(b1)
{
matrix viewMatrix;
};
cbuffer projCb : register(b2)
{
matrix projMatrix;
};
cbuffer textureCb:register(b3) {
matrix textMatirx;
};
struct VS_Input
{
float4 pos : POSITION;
float2 tex0 : TEXCOORD0;
};
struct PS_Input
{
float4 pos : SV_POSITION;
float2 tex0 : TEXCOORD0;
};
PS_Input VS_Main(VS_Input vertex)
{
PS_Input vsOut = (PS_Input)0;
vsOut.pos = mul(vertex.pos, worldMatrix);
vsOut.pos = mul(vsOut.pos, viewMatrix);
vsOut.pos = mul(vsOut.pos, projMatrix);
vsOut.tex0 = mul(textMatirx, float4(vertex.tex0, 0.0f, 1.0f));
return vsOut;
}
38 changes: 36 additions & 2 deletions src/GameDemo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ bool GameDemo::LoadContent()
vsBuffer->Release();
return false;
}
compileResult = CompileD3DShader(L"Fx\\tiresolid.fx", "VS_Main", "vs_5_0", &vsBuffer);
if (compileResult == false)
return false;
d3dResult = d3dDevice_->CreateVertexShader(vsBuffer->GetBufferPointer(), vsBuffer->GetBufferSize(), 0, &tiresolidColorVS_);
if (FAILED(d3dResult))
{
if (vsBuffer)
vsBuffer->Release();
return false;
}
//创建顶点输入布局
D3D11_INPUT_ELEMENT_DESC solidColorLayout[] =
{
Expand Down Expand Up @@ -66,7 +76,7 @@ bool GameDemo::LoadContent()
d3dResult = d3dDevice_->CreateSamplerState(&colorMapDesc, &colorMapSampler_);
if (FAILED(d3dResult))
return false;
//创建投影矩阵和常量缓存
//创建变换矩阵和常量缓存
D3D11_BUFFER_DESC constDesc;
ZeroMemory(&constDesc, sizeof(constDesc));
constDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
Expand All @@ -81,6 +91,9 @@ bool GameDemo::LoadContent()
if (FAILED(d3dResult))
return false;
d3dResult = d3dDevice_->CreateBuffer(&constDesc, 0, &projCB_);
if (FAILED(d3dResult))
return false;
d3dResult = d3dDevice_->CreateBuffer(&constDesc, 0, &textureCB_);
if (FAILED(d3dResult))
return false;
//初始化skybox
Expand All @@ -106,7 +119,26 @@ void GameDemo::Update()
PostQuitMessage(0);
if (KEYDOWN(prevKeyboardKeys_, DIK_V) && !KEYDOWN(keyboardKeys_, DIK_V))
Switch();
if (!KEYDOWN(prevKeyboardKeys_, DIK_W) && KEYDOWN(keyboardKeys_, DIK_W))
motion_.setDirection(1);
else if (KEYDOWN(prevKeyboardKeys_, DIK_W) && !KEYDOWN(keyboardKeys_, DIK_W))
motion_.setDirection(0);
if (!KEYDOWN(prevKeyboardKeys_, DIK_S) && KEYDOWN(keyboardKeys_, DIK_S))
motion_.setDirection(-1);
else if (KEYDOWN(prevKeyboardKeys_, DIK_S) && !KEYDOWN(keyboardKeys_, DIK_S))
motion_.setDirection(0);
if (!KEYDOWN(prevKeyboardKeys_, DIK_A) && KEYDOWN(keyboardKeys_, DIK_A))
motion_.setTurn(-1);
else if (KEYDOWN(prevKeyboardKeys_, DIK_A) && !KEYDOWN(keyboardKeys_, DIK_A))
motion_.setTurn(0);
if (!KEYDOWN(prevKeyboardKeys_, DIK_D) && KEYDOWN(keyboardKeys_, DIK_D))
motion_.setTurn(1);
else if (KEYDOWN(prevKeyboardKeys_, DIK_D) && !KEYDOWN(keyboardKeys_, DIK_D))
motion_.setTurn(0);
motion_.Move();
memcpy(prevKeyboardKeys_, keyboardKeys_, sizeof(keyboardKeys_));
camera_[FIRST]->SetPositions(motion_.getPos(),motion_.getFacing());
camera_[THIRD]->SetPositions(motion_.getPos(), motion_.getPos());
if (status_)
camera_[THIRD]->ApplyRotation(mouseState_.lY*radian, mouseState_.lX*radian);
}
Expand All @@ -126,11 +158,13 @@ void GameDemo::Render()
viewMatrix_ = XMMatrixTranspose(viewMatrix_);
d3dContext_->UpdateSubresource(viewCB_, 0, 0, &viewMatrix_, 0, 0);
d3dContext_->UpdateSubresource(projCB_, 0, 0, &projMatrix_, 0, 0);
d3dContext_->VSSetConstantBuffers(0, 1, &worldCB_);
d3dContext_->VSSetConstantBuffers(1, 1, &viewCB_);
d3dContext_->VSSetConstantBuffers(2, 1, &projCB_);
d3dContext_->VSSetConstantBuffers(3, 1, &textureCB_);
skybox_.Render(d3dContext_, worldCB_,viewCB_);
brick_.Render(d3dContext_, worldCB_,viewCB_);
car_.Render(d3dContext_, worldCB_, viewCB_);
car_.Render(d3dContext_, worldCB_, viewCB_, tiresolidColorVS_, textureCB_);
swapChain_->Present(0, 0);
}

Expand Down
Loading

0 comments on commit 0d3cd73

Please sign in to comment.