This repository was archived by the owner on Jun 2, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameObject.cpp
More file actions
42 lines (31 loc) · 1.68 KB
/
GameObject.cpp
File metadata and controls
42 lines (31 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include "GameObject.h"
GameObject::GameObject(ID3D11Device* d3dDevice, const wchar_t* textureFileName, char* meshFileName) {
meshData = OBJLoader::Load(meshFileName, d3dDevice);
CreateDDSTextureFromFile(d3dDevice, textureFileName, nullptr, &texture);
}
GameObject::~GameObject() {
if (texture) texture->Release();
meshData.VertexBuffer->Release();
meshData.IndexBuffer->Release();
}
void GameObject::Draw(ID3D11DeviceContext* context, ID3D11VertexShader* vertexShader, ID3D11PixelShader* pixelShader, ID3D11Buffer* buffer, ID3D11SamplerState* sampler, ConstantBuffer cb) {
//set vertex buffer for model
context->IASetVertexBuffers(0, 1, &meshData.VertexBuffer, &meshData.VBStride, &meshData.VBOffset);
//set index buffer for model
context->IASetIndexBuffer(meshData.IndexBuffer, DXGI_FORMAT_R16_UINT, 0);
//set shaders
context->VSSetShader(vertexShader, nullptr, 0);
context->VSSetConstantBuffers(0, 1, &buffer);
context->PSSetConstantBuffers(0, 1, &buffer);
context->PSSetShader(pixelShader, nullptr, 0);
context->PSSetShaderResources(0, 1, &texture);
context->PSSetSamplers(0, 1, &sampler);
//set world transpose
XMFLOAT4X4 currentWorld;
XMStoreFloat4x4(¤tWorld, XMMatrixScaling(transform.scale.x, transform.scale.y, transform.scale.z)
* XMMatrixRotationRollPitchYaw(transform.rotation.x, transform.rotation.y, transform.rotation.z)
* XMMatrixTranslation(transform.position.x, transform.position.y, transform.position.z));
cb.mWorld = XMMatrixTranspose(XMLoadFloat4x4(¤tWorld));
context->UpdateSubresource(buffer, 0, nullptr, &cb, 0, 0);
context->DrawIndexed(meshData.IndexCount, 0, 0);
}