diff --git a/src/ArcCamera.cpp b/src/ArcCamera.cpp new file mode 100644 index 0000000..140fc9b --- /dev/null +++ b/src/ArcCamera.cpp @@ -0,0 +1,52 @@ +#include "ArcCamera.h" +#include + +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; +} \ No newline at end of file diff --git a/src/ArcCamera.h b/src/ArcCamera.h new file mode 100644 index 0000000..3bf2d2a --- /dev/null +++ b/src/ArcCamera.h @@ -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; +}; \ No newline at end of file diff --git a/src/Brick.cpp b/src/Brick.cpp index 900dbf5..2f82612 100644 --- a/src/Brick.cpp +++ b/src/Brick.cpp @@ -1,16 +1,23 @@ #include "Brick.h" #include #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_) { diff --git a/src/Brick.h b/src/Brick.h index 8161e9f..4d800c1 100644 --- a/src/Brick.h +++ b/src/Brick.h @@ -1,12 +1,11 @@ #pragma once #include -#include "Model.h" +#include using std::array; -using namespace::DirectX; -class Brick :public Model{ +class Brick { public: Brick(); virtual ~Brick(); @@ -15,5 +14,8 @@ class Brick :public Model{ void Render(ID3D11DeviceContext* d3dContext_, ID3D11Buffer* worldCB_, ID3D11Buffer* viewCB_); private: + ID3D11Buffer* vertexBuffer_; + ID3D11Buffer* indexBuffer_; + ID3D11ShaderResourceView* colorMap_; }; diff --git a/src/Camera.h b/src/Camera.h new file mode 100644 index 0000000..d8eef34 --- /dev/null +++ b/src/Camera.h @@ -0,0 +1,13 @@ +#pragma once + +#include + +using namespace DirectX; + +class Camera { +public: + Camera(){} + virtual ~Camera(){} + virtual XMMATRIX GetViewMatrix() = 0; + virtual void ApplyRotation(float yawDelta, float pitchDelta) = 0; +}; diff --git a/src/Car.cpp b/src/Car.cpp new file mode 100644 index 0000000..1231f09 --- /dev/null +++ b/src/Car.cpp @@ -0,0 +1,189 @@ +#include "Car.h" +#include +#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 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_); +} \ No newline at end of file diff --git a/src/Car.h b/src/Car.h new file mode 100644 index 0000000..81c3cf8 --- /dev/null +++ b/src/Car.h @@ -0,0 +1,36 @@ +#pragma once + +#include +#include +#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, 4> tirevertexs_; + //ÊÀ½ç¿Õ¼äÆû³µµÄÖÐÐÄ×ø±ê + array center_; + + void Renderbody(ID3D11DeviceContext* d3dContext_, ID3D11Buffer* worldCB_, ID3D11Buffer* viewCB_); + void Rendertire(ID3D11DeviceContext* d3dContext_, ID3D11Buffer* worldCB_, ID3D11Buffer* viewCB_); +}; \ No newline at end of file diff --git a/src/Cube.cpp b/src/Cube.cpp new file mode 100644 index 0000000..f7d51a1 --- /dev/null +++ b/src/Cube.cpp @@ -0,0 +1,24 @@ +#include "Cube.h" + +Cube::Cube() { + facevertexs_ = { + array{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() {} \ No newline at end of file diff --git a/src/Cube.h b/src/Cube.h new file mode 100644 index 0000000..1cf6928 --- /dev/null +++ b/src/Cube.h @@ -0,0 +1,19 @@ +#pragma once + +#include +#include + +using std::array; +using namespace DirectX; + +class Cube { +public: + Cube(); + virtual ~Cube(); + +protected: + //8¸ö¶¥µã + array postion_; + //ÿ¸öÃæ¶ÔÓ¦µÄ¶¥µã + array, 6> facevertexs_; +}; diff --git a/src/Dx11DemoBase.cpp b/src/Dx11DemoBase.cpp index 2121b6f..7954b49 100644 --- a/src/Dx11DemoBase.cpp +++ b/src/Dx11DemoBase.cpp @@ -36,12 +36,20 @@ void Dx11DemoBase::Shutdown() if (d3dDevice_) d3dDevice_->Release(); if (depthStencilView_) depthStencilView_->Release(); if (depthTexture_) depthTexture_->Release(); + if (keyboardDevice_) + { + keyboardDevice_->Unacquire(); + keyboardDevice_->Release(); + } + if (directInput_) directInput_->Release(); backBufferTarget_ = nullptr; swapChain_ = nullptr; d3dContext_ = nullptr; d3dDevice_ = nullptr; depthStencilView_ = nullptr; depthTexture_ = nullptr; + keyboardDevice_ = nullptr; + directInput_ = nullptr; } bool Dx11DemoBase::Initialize(HINSTANCE hInstance, HWND hwnd) @@ -150,6 +158,55 @@ bool Dx11DemoBase::Initialize(HINSTANCE hInstance, HWND hwnd) //ÉèÖÃÊÓͼ d3dContext_->OMSetRenderTargets(1, &backBufferTarget_, depthStencilView_); + //³õʼ»¯¼üÅÌÊäÈëÉ豸 + ZeroMemory(keyboardKeys_, sizeof(keyboardKeys_)); + ZeroMemory(prevKeyboardKeys_, sizeof(prevKeyboardKeys_)); + result = DirectInput8Create(hInstance_, DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&directInput_, 0); + if (FAILED(result)) + { + return false; + } + result = directInput_->CreateDevice(GUID_SysKeyboard, &keyboardDevice_, 0); + if (FAILED(result)) + { + return false; + } + result = keyboardDevice_->SetDataFormat(&c_dfDIKeyboard); + if (FAILED(result)) + { + return false; + } + result = keyboardDevice_->SetCooperativeLevel(hwnd_, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE); + if (FAILED(result)) + { + return false; + } + result = keyboardDevice_->Acquire(); + if (FAILED(result)) + { + return false; + } + //³õʼ»¯Êó±êÉ豸 + result = directInput_->CreateDevice(GUID_SysMouse, &mouseDevice_, 0); + if (FAILED(result)) + { + return false; + } + result = mouseDevice_->SetDataFormat(&c_dfDIMouse); + if (FAILED(result)) + { + return false; + } + result = mouseDevice_->SetCooperativeLevel(hwnd_, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE); + if (FAILED(result)) + { + return false; + } + result = mouseDevice_->Acquire(); + if (FAILED(result)) + { + return false; + } return LoadContent(); } diff --git a/src/Dx11DemoBase.h b/src/Dx11DemoBase.h index f875422..7e3b160 100644 --- a/src/Dx11DemoBase.h +++ b/src/Dx11DemoBase.h @@ -2,6 +2,10 @@ #include #include +#include + +#define KEYDOWN(name, key) ( name[key] & 0x80 ) +#define BUTTONDOWN(name, key) ( name.rgbButtons[key] & 0x80 ) class Dx11DemoBase{ public: @@ -13,7 +17,7 @@ class Dx11DemoBase{ virtual bool LoadContent(); virtual void UnloadContent(); - virtual void Update(float dt) = 0; + virtual void Update() = 0; virtual void Render() = 0; protected: @@ -31,6 +35,13 @@ class Dx11DemoBase{ ID3D11Texture2D* depthTexture_; ID3D11DepthStencilView* depthStencilView_; + LPDIRECTINPUT8 directInput_; + LPDIRECTINPUTDEVICE8 keyboardDevice_; + LPDIRECTINPUTDEVICE8 mouseDevice_; + char keyboardKeys_[256]; + char prevKeyboardKeys_[256]; + DIMOUSESTATE mouseState_; + bool CompileD3DShader(LPCTSTR filePath, const char* entry, const char* shaderModel, ID3DBlob** buffer); }; diff --git a/src/GameDemo.cpp b/src/GameDemo.cpp index 9a22653..a65b632 100644 --- a/src/GameDemo.cpp +++ b/src/GameDemo.cpp @@ -5,10 +5,16 @@ using std::string; using namespace DirectX; const string texture = "\\Texture\\"; +enum CAMERA{FIRST,THIRD}; +const float radian = XM_PI / 180; GameDemo::GameDemo() - :colorMapSampler_(nullptr) -{} + :colorMapSampler_(nullptr), + status_(FIRST) +{ + camera_.emplace_back(new LookAtCamera()); + camera_.emplace_back(new ArcCamera()); +} GameDemo::~GameDemo() {} @@ -77,11 +83,10 @@ bool GameDemo::LoadContent() d3dResult = d3dDevice_->CreateBuffer(&constDesc, 0, &projCB_); if (FAILED(d3dResult)) return false; - //ÉèÖÃÕÕÏà»ú - camera_.SetPositions(XMFLOAT3(0.0f, 10.0f, -10.0f), XMFLOAT3(0.0f, 10.0f, 10.0f)); //³õʼ»¯skybox if (!skybox_.Init_Resource(d3dDevice_)) return false; if (!brick_.Init_Resource(d3dDevice_)) return false; + if (!car_.Init_Resource(d3dDevice_)) return false; return true; } @@ -91,8 +96,20 @@ void GameDemo::UnloadContent() colorMapSampler_ = nullptr; } -void GameDemo::Update(float dt) -{} +void GameDemo::Update() +{ + keyboardDevice_->Acquire(); + mouseDevice_->Acquire(); + keyboardDevice_->GetDeviceState(sizeof(keyboardKeys_), (LPVOID)&keyboardKeys_); + mouseDevice_->GetDeviceState(sizeof(mouseState_), (LPVOID)&mouseState_); + if (KEYDOWN(prevKeyboardKeys_, DIK_ESCAPE) && !KEYDOWN(keyboardKeys_, DIK_ESCAPE)) + PostQuitMessage(0); + if (KEYDOWN(prevKeyboardKeys_, DIK_V) && !KEYDOWN(keyboardKeys_, DIK_V)) + Switch(); + memcpy(prevKeyboardKeys_, keyboardKeys_, sizeof(keyboardKeys_)); + if (status_) + camera_[THIRD]->ApplyRotation(mouseState_.lY*radian, mouseState_.lX*radian); +} void GameDemo::Render() { @@ -105,7 +122,7 @@ void GameDemo::Render() d3dContext_->VSSetShader(solidColorVS_, 0, 0); d3dContext_->PSSetShader(solidColorPS_, 0, 0); d3dContext_->PSSetSamplers(0, 1, &colorMapSampler_); - XMMATRIX viewMatrix_ = camera_.GetViewMatrix(); + XMMATRIX viewMatrix_ = camera_[status_]->GetViewMatrix(); viewMatrix_ = XMMatrixTranspose(viewMatrix_); d3dContext_->UpdateSubresource(viewCB_, 0, 0, &viewMatrix_, 0, 0); d3dContext_->UpdateSubresource(projCB_, 0, 0, &projMatrix_, 0, 0); @@ -113,7 +130,11 @@ void GameDemo::Render() d3dContext_->VSSetConstantBuffers(2, 1, &projCB_); skybox_.Render(d3dContext_, worldCB_,viewCB_); brick_.Render(d3dContext_, worldCB_,viewCB_); + car_.Render(d3dContext_, worldCB_, viewCB_); swapChain_->Present(0, 0); } +void GameDemo::Switch() { + status_ ^= 1; +} diff --git a/src/GameDemo.h b/src/GameDemo.h index 74a3056..5450a47 100644 --- a/src/GameDemo.h +++ b/src/GameDemo.h @@ -1,12 +1,18 @@ #pragma once #include +#include #include "Dx11DemoBase.h" #include "LookAtCamera .h" +#include "ArcCamera.h" #include "SkyBox.h" #include "Brick.h" +#include "Car.h" +#include using DirectX::XMMATRIX; +using std::shared_ptr; +using std::vector; class GameDemo :public Dx11DemoBase{ public: @@ -14,13 +20,17 @@ class GameDemo :public Dx11DemoBase{ virtual ~GameDemo(); bool LoadContent(); void UnloadContent(); - void Update(float dt); + void Update(); void Render(); + void Switch(); private: - LookAtCamera camera_; + vector> camera_; + int status_; + SkyBox skybox_; Brick brick_; + Car car_; ID3D11SamplerState* colorMapSampler_; @@ -33,4 +43,5 @@ class GameDemo :public Dx11DemoBase{ ID3D11PixelShader* solidColorPS_; ID3D11InputLayout* inputLayout_; + }; diff --git a/src/LookAtCamera .h b/src/LookAtCamera .h index cec5008..c648620 100644 --- a/src/LookAtCamera .h +++ b/src/LookAtCamera .h @@ -1,18 +1,20 @@ #pragma once #include +#include "Camera.h" using DirectX::XMFLOAT3; using DirectX::XMMATRIX; -class LookAtCamera { +class LookAtCamera :public Camera{ public: LookAtCamera(); - LookAtCamera(XMFLOAT3 pos, XMFLOAT3 target); void SetPositions(XMFLOAT3 pos, XMFLOAT3 target); XMFLOAT3 getPosition(); XMMATRIX GetViewMatrix(); + void ApplyRotation(float yawDelta, float pitchDelta){} + private: XMFLOAT3 position_; XMFLOAT3 target_; diff --git a/src/LookAtCamera.cpp b/src/LookAtCamera.cpp index efbf09f..558ece8 100644 --- a/src/LookAtCamera.cpp +++ b/src/LookAtCamera.cpp @@ -2,15 +2,11 @@ using DirectX::XMMatrixLookAtLH; LookAtCamera::LookAtCamera() - : position_(XMFLOAT3(0.0f, 0.0f, 0.0f)), - target_(XMFLOAT3(0.0f, 0.0f, 0.0f)), - up_(XMFLOAT3(0.0f, 1.0f, 0.0f)) -{} -LookAtCamera::LookAtCamera(XMFLOAT3 pos, XMFLOAT3 target) - :position_(pos), - target_(target), + : position_(XMFLOAT3(0.0f, 1.0f, 0.0f)), + target_(XMFLOAT3(0.0f, 1.0f, 10.0f)), up_(XMFLOAT3(0.0f, 1.0f, 0.0f)) {} + void LookAtCamera::SetPositions(XMFLOAT3 pos, XMFLOAT3 target) { position_ = pos; diff --git a/src/Model.cpp b/src/Model.cpp deleted file mode 100644 index f3570d2..0000000 --- a/src/Model.cpp +++ /dev/null @@ -1,13 +0,0 @@ -#include "Model.h" - -Model::Model() - :vertexBuffer_(nullptr), - indexBuffer_(nullptr) -{} - -Model::~Model() { - if (vertexBuffer_) vertexBuffer_->Release(); - if (indexBuffer_) indexBuffer_->Release(); - vertexBuffer_ = nullptr; - indexBuffer_ = nullptr; -} \ No newline at end of file diff --git a/src/Model.h b/src/Model.h deleted file mode 100644 index 450ec2a..0000000 --- a/src/Model.h +++ /dev/null @@ -1,24 +0,0 @@ -#pragma once - -#include -#include -#include "LookAtCamera .h" - -using namespace DirectX; - -struct VertexPos { - XMFLOAT3 pos; - XMFLOAT2 tex0; -}; - -class Model { -public: - Model(); - virtual ~Model(); - virtual bool Init_Resource(ID3D11Device* d3dDevice_) = 0; - virtual void Render(ID3D11DeviceContext* d3dContext_, ID3D11Buffer* worldCB_, ID3D11Buffer* viewCB_) = 0; - -protected: - ID3D11Buffer* vertexBuffer_; - ID3D11Buffer* indexBuffer_; -}; diff --git a/src/SkyBox.cpp b/src/SkyBox.cpp index ac22070..a34f186 100644 --- a/src/SkyBox.cpp +++ b/src/SkyBox.cpp @@ -1,6 +1,7 @@ #include "SkyBox.h" #include #include "DX11DemoBase.h" +#include "VertexPos.h" using namespace DirectX; @@ -10,24 +11,6 @@ SkyBox::SkyBox() { for (auto &it : colorMap_) it = nullptr; - facevertexs_ = { - array{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) - }; } SkyBox::~SkyBox() { @@ -36,7 +19,7 @@ SkyBox::~SkyBox() { vertexBuffer_ = nullptr; indexBuffer_ = nullptr; for (auto &it : colorMap_) { - it->Release(); + if (it) it->Release(); it = nullptr; } } @@ -48,7 +31,7 @@ bool SkyBox::Init_Resource(ID3D11Device* d3dDevice_) { ZeroMemory(&vertexDesc, sizeof(vertexDesc)); vertexDesc.Usage = D3D11_USAGE_DEFAULT; vertexDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER; - vertexDesc.ByteWidth = sizeof(VertexPos) * 24; + vertexDesc.ByteWidth = sizeof(VertexPos) * 4; d3dResult = d3dDevice_->CreateBuffer(&vertexDesc, nullptr, &vertexBuffer_); if (FAILED(d3dResult)) return false; diff --git a/src/SkyBox.h b/src/SkyBox.h index 635ef15..91bfa9c 100644 --- a/src/SkyBox.h +++ b/src/SkyBox.h @@ -1,13 +1,14 @@ #pragma once #include -#include "Model.h" +#include "Cube.h" +#include using std::array; using DirectX::XMFLOAT3; using DirectX::XMFLOAT2; -class SkyBox :public Model{ +class SkyBox :public Cube{ public: SkyBox(); virtual ~SkyBox(); @@ -21,10 +22,5 @@ class SkyBox :public Model{ ID3D11Buffer* indexBuffer_; array colorMap_; - //array, 6> vertexNumber_; - //8¸ö¶¥µã - array postion_; - //µ¥¸öÃæ°üº¬4¸öµã - array, 6> facevertexs_; }; diff --git a/src/Texture/body.jpg b/src/Texture/body.jpg new file mode 100644 index 0000000..7edb4da Binary files /dev/null and b/src/Texture/body.jpg differ diff --git a/src/Texture/tirefront.jpg b/src/Texture/tirefront.jpg new file mode 100644 index 0000000..454d9e4 Binary files /dev/null and b/src/Texture/tirefront.jpg differ diff --git a/src/Texture/tireside.jpg b/src/Texture/tireside.jpg new file mode 100644 index 0000000..f85c5c9 Binary files /dev/null and b/src/Texture/tireside.jpg differ diff --git a/src/VertexPos.h b/src/VertexPos.h new file mode 100644 index 0000000..5a7d1f1 --- /dev/null +++ b/src/VertexPos.h @@ -0,0 +1,9 @@ +#pragma once +#include + +using namespace DirectX; + +struct VertexPos { + XMFLOAT3 pos; + XMFLOAT2 tex0; +}; diff --git a/src/car game.sln b/src/car game.sln deleted file mode 100644 index fd537db..0000000 --- a/src/car game.sln +++ /dev/null @@ -1,31 +0,0 @@ - -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 15 -VisualStudioVersion = 15.0.28307.271 -MinimumVisualStudioVersion = 10.0.40219.1 -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "car game", "car game\car game.vcxproj", "{31210CDC-0773-4E64-8DA4-E610471375CC}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|x64 = Debug|x64 - Debug|x86 = Debug|x86 - Release|x64 = Release|x64 - Release|x86 = Release|x86 - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {31210CDC-0773-4E64-8DA4-E610471375CC}.Debug|x64.ActiveCfg = Debug|x64 - {31210CDC-0773-4E64-8DA4-E610471375CC}.Debug|x64.Build.0 = Debug|x64 - {31210CDC-0773-4E64-8DA4-E610471375CC}.Debug|x86.ActiveCfg = Debug|Win32 - {31210CDC-0773-4E64-8DA4-E610471375CC}.Debug|x86.Build.0 = Debug|Win32 - {31210CDC-0773-4E64-8DA4-E610471375CC}.Release|x64.ActiveCfg = Release|x64 - {31210CDC-0773-4E64-8DA4-E610471375CC}.Release|x64.Build.0 = Release|x64 - {31210CDC-0773-4E64-8DA4-E610471375CC}.Release|x86.ActiveCfg = Release|Win32 - {31210CDC-0773-4E64-8DA4-E610471375CC}.Release|x86.Build.0 = Release|Win32 - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection - GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {34207EA3-9C2F-49A1-A870-6D7A1F699F5C} - EndGlobalSection -EndGlobal diff --git a/src/main.cpp b/src/main.cpp index 65fbbc5..ffa5e28 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,4 +1,5 @@ #include +#include #include #include"GameDemo.h" @@ -6,6 +7,7 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { PAINTSTRUCT paintStruct; HDC hDC; + int xPos, yPos; switch (message) { case WM_PAINT: @@ -53,7 +55,7 @@ int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE prevInstance, LPWSTR cmdLine, std::shared_ptr demo(new GameDemo()); // Demo Initialize bool result = demo->Initialize(hInstance, hwnd); - if (result == false) + if (!result) return -1; MSG msg = { 0 }; while (msg.message != WM_QUIT) @@ -64,7 +66,7 @@ int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE prevInstance, LPWSTR cmdLine, DispatchMessage(&msg); } // Update and Draw - demo->Update(0.0f); + demo->Update(); demo->Render(); } // Demo Shutdown