Skip to content

Commit

Permalink
Increase the transformation of first-person perspective and third-per…
Browse files Browse the repository at this point in the history
…son perspective
  • Loading branch information
viktorika committed Jan 20, 2019
1 parent f25b7f1 commit fd723fd
Show file tree
Hide file tree
Showing 25 changed files with 500 additions and 120 deletions.
52 changes: 52 additions & 0 deletions src/ArcCamera.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#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;
}
Expand Down
18 changes: 18 additions & 0 deletions src/ArcCamera.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include "Camera.h"

class ArcCamera:public Camera{
public:
ArcCamera();
virtual ~ArcCamera();
void SetRotation(float x, float y);
void SetTarget(XMFLOAT3& target);
void ApplyRotation(float yawDelta, float pitchDelta);
XMMATRIX GetViewMatrix();

private:
XMFLOAT3 target_;
float distance_;
float xRotation_, yRotation_, xlimit, ylimit;
};
9 changes: 8 additions & 1 deletion src/Brick.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
#include "Brick.h"
#include <WICTextureLoader.h>
#include "DX11DemoBase.h"
#include "VerTexPos.h"

using namespace DirectX;

Brick::Brick()
:colorMap_(nullptr)
:colorMap_(nullptr),
vertexBuffer_(nullptr),
indexBuffer_(nullptr)
{}

Brick::~Brick(){
if (colorMap_) colorMap_->Release();
if (vertexBuffer_) vertexBuffer_->Release();
if (indexBuffer_) indexBuffer_->Release();
colorMap_ = nullptr;
vertexBuffer_ = nullptr;
indexBuffer_ = nullptr;
}

bool Brick::Init_Resource(ID3D11Device* d3dDevice_) {
Expand Down
8 changes: 5 additions & 3 deletions src/Brick.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
#pragma once

#include <array>
#include "Model.h"
#include <d3d11.h>

using std::array;
using namespace::DirectX;

class Brick :public Model{
class Brick {
public:
Brick();
virtual ~Brick();
Expand All @@ -15,5 +14,8 @@ class Brick :public Model{
void Render(ID3D11DeviceContext* d3dContext_, ID3D11Buffer* worldCB_, ID3D11Buffer* viewCB_);

private:
ID3D11Buffer* vertexBuffer_;
ID3D11Buffer* indexBuffer_;

ID3D11ShaderResourceView* colorMap_;
};
13 changes: 13 additions & 0 deletions src/Camera.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once

#include <DirectXMath.h>

using namespace DirectX;

class Camera {
public:
Camera(){}
virtual ~Camera(){}
virtual XMMATRIX GetViewMatrix() = 0;
virtual void ApplyRotation(float yawDelta, float pitchDelta) = 0;
};
189 changes: 189 additions & 0 deletions src/Car.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
#include "Car.h"
#include <WICTextureLoader.h>
#include "VerTexPos.h"

Car::Car()
:bodycolorMap_(nullptr),
tirefrontcolorMap_(nullptr),
tiresidecolorMap_(nullptr),
bodyvertexBuffer_(nullptr),
tirevertexBuffer_(nullptr),
bodyindexBuffer_(nullptr),
tirefrontindexBuffer_(nullptr),
tiresideindexBuffer_(nullptr)
{}

Car::~Car() {
if (bodycolorMap_) bodycolorMap_->Release();
if (tirefrontcolorMap_) tirefrontcolorMap_->Release();
if (tiresidecolorMap_) tiresidecolorMap_->Release();
if (bodyvertexBuffer_) bodyvertexBuffer_->Release();
if (tirevertexBuffer_) tirevertexBuffer_->Release();
if (bodyindexBuffer_) bodyindexBuffer_->Release();
if (tirefrontindexBuffer_) tirefrontindexBuffer_->Release();
if (tiresideindexBuffer_) tiresideindexBuffer_->Release();
bodycolorMap_ = nullptr;
tirefrontcolorMap_ = nullptr;
tiresidecolorMap_ = nullptr;
bodyvertexBuffer_ = nullptr;
tirevertexBuffer_ = nullptr;
bodyindexBuffer_ = nullptr;
tirefrontindexBuffer_ = nullptr;
tiresideindexBuffer_ = nullptr;
}

bool Car::Init_Resource(ID3D11Device* d3dDevice_) {
HRESULT d3dResult;
//创建顶点缓冲区
D3D11_BUFFER_DESC vertexDesc;
ZeroMemory(&vertexDesc, sizeof(vertexDesc));
vertexDesc.Usage = D3D11_USAGE_DEFAULT;
vertexDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
vertexDesc.ByteWidth = sizeof(VertexPos) * 4;
d3dResult = d3dDevice_->CreateBuffer(&vertexDesc, nullptr, &bodyvertexBuffer_);
if (FAILED(d3dResult))
return false;
VertexPos tirevertices[10] =
{
{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[3] = { XMFLOAT3(-0.5,0.5,0),XMFLOAT2(0,0) };
tirevertices[4] = { XMFLOAT3(0.5,0.5,0),XMFLOAT2(1,0) };
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);
XMFLOAT3 next1, next2;
XMStoreFloat3(&next1, vec1);
XMStoreFloat3(&next2, vec2);
tirevertices[6] = { next1,XMFLOAT2(0,1) };
tirevertices[7] = { next1,XMFLOAT2(0,1) };
tirevertices[8] = { next2,XMFLOAT2(0,1) };
tirevertices[9] = { next2,XMFLOAT2(1,1) };
D3D11_SUBRESOURCE_DATA resourceData;
ZeroMemory(&resourceData, sizeof(resourceData));
resourceData.pSysMem = tirevertices;
vertexDesc.ByteWidth = sizeof(VertexPos) * 10;
d3dResult = d3dDevice_->CreateBuffer(&vertexDesc, &resourceData, &tirevertexBuffer_);
if (FAILED(d3dResult))
return false;
//创建索引缓存
WORD bodyindices[] =
{
2,1,0,2,3,1
};
WORD tirefrontindices[] = {
3,5,7,5,9,7
};
WORD tiresideindices[] = {
0,2,6,1,8,4
};
D3D11_BUFFER_DESC indexDesc;
ZeroMemory(&indexDesc, sizeof(indexDesc));
indexDesc.Usage = D3D11_USAGE_DEFAULT;
indexDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
indexDesc.ByteWidth = sizeof(WORD) * 6;
indexDesc.CPUAccessFlags = 0;
resourceData.pSysMem = bodyindices;
d3dResult = d3dDevice_->CreateBuffer(&indexDesc, &resourceData, &bodyindexBuffer_);
if (FAILED(d3dResult))
return false;
indexDesc.ByteWidth = sizeof(WORD) * 6;
resourceData.pSysMem = tirefrontindices;
d3dResult = d3dDevice_->CreateBuffer(&indexDesc, &resourceData, &tirefrontindexBuffer_);
if (FAILED(d3dResult))
return false;
resourceData.pSysMem = tiresideindices;
d3dResult = d3dDevice_->CreateBuffer(&indexDesc, &resourceData, &tiresideindexBuffer_);
if (FAILED(d3dResult))
return false;
//加载贴图获得资源视图对象
d3dResult = CreateWICTextureFromFile(d3dDevice_, L"Texture\\body.jpg", nullptr, &bodycolorMap_);
if (FAILED(d3dResult))
return false;
d3dResult = CreateWICTextureFromFile(d3dDevice_, L"Texture\\tirefront.jpg", nullptr, &tirefrontcolorMap_);
if (FAILED(d3dResult))
return false;
d3dResult = CreateWICTextureFromFile(d3dDevice_, L"Texture\\tireside.jpg", nullptr, &tiresidecolorMap_);
if (FAILED(d3dResult))
return false;
return true;
}

void Car::Renderbody(ID3D11DeviceContext* d3dContext_, ID3D11Buffer* worldCB_, ID3D11Buffer* viewCB_) {
unsigned int stride = sizeof(VertexPos);
unsigned int offset = 0;
d3dContext_->IASetVertexBuffers(0, 1, &bodyvertexBuffer_, &stride, &offset);
d3dContext_->IASetIndexBuffer(bodyindexBuffer_, DXGI_FORMAT_R16_UINT, 0);
d3dContext_->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
XMMATRIX rotationMat = XMMatrixRotationRollPitchYaw(0.0f, 0.0f, 0.0f);
XMMATRIX translationMat = XMMatrixTranslation(0.0f, 2.0f, 0.0f);
XMMATRIX scaling = XMMatrixScaling(1.5f, 1.0f, 1.5f);
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];
VertexPos vertex[] = {
{postion_[index[0]],XMFLOAT2(0,0)},
{postion_[index[1]],XMFLOAT2(1,0)},
{postion_[index[2]],XMFLOAT2(0,1)},
{postion_[index[3]],XMFLOAT2(1,1)}
};
d3dContext_->UpdateSubresource(bodyvertexBuffer_, 0, 0, &vertex, 0, 0);
d3dContext_->DrawIndexed(6, 0, 0);
}
}
void Car::Rendertire(ID3D11DeviceContext* d3dContext_, ID3D11Buffer* worldCB_, ID3D11Buffer* viewCB_) {
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;
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;
worldMat = XMMatrixTranspose(worldMat);
d3dContext_->UpdateSubresource(worldCB_, 0, 0, &worldMat, 0, 0);
d3dContext_->DrawIndexed(6, 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;
worldMat = XMMatrixTranspose(worldMat);
d3dContext_->UpdateSubresource(worldCB_, 0, 0, &worldMat, 0, 0);
d3dContext_->DrawIndexed(6, 0, 0);
}
}
}

void Car::Render(ID3D11DeviceContext* d3dContext_, ID3D11Buffer* worldCB_, ID3D11Buffer* viewCB_) {
Renderbody(d3dContext_, worldCB_, viewCB_);
Rendertire(d3dContext_, worldCB_, viewCB_);
}
36 changes: 36 additions & 0 deletions src/Car.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#pragma once

#include <array>
#include <d3d11.h>
#include "Cube.h"

using std::array;

class Car :public Cube{
public:
Car();
virtual ~Car();
bool Init_Resource(ID3D11Device* d3dDevice_);
void Render(ID3D11DeviceContext* d3dContext_, ID3D11Buffer* worldCB_, ID3D11Buffer* viewCB_);

private:
ID3D11ShaderResourceView* bodycolorMap_;
ID3D11ShaderResourceView* tirefrontcolorMap_;
ID3D11ShaderResourceView* tiresidecolorMap_;

ID3D11Buffer* bodyvertexBuffer_;
ID3D11Buffer* tirevertexBuffer_;
ID3D11Buffer* bodyindexBuffer_;
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_);
};
24 changes: 24 additions & 0 deletions src/Cube.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include "Cube.h"

Cube::Cube() {
facevertexs_ = {
array<int,4>{4,5,6,7},
{2,3,0,1},
{4,6,0,2},
{7,5,3,1},
{6,7,2,3},
{5,4,1,0}
};
postion_ = {
XMFLOAT3(-1,-1,-1),
XMFLOAT3(1,-1,-1),
XMFLOAT3(-1,-1,1),
XMFLOAT3(1,-1,1),
XMFLOAT3(-1,1,-1),
XMFLOAT3(1,1,-1),
XMFLOAT3(-1,1,1),
XMFLOAT3(1,1,1)
};
}

Cube::~Cube() {}
Loading

0 comments on commit fd723fd

Please sign in to comment.