-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBall.cpp
More file actions
91 lines (79 loc) · 2.35 KB
/
Copy pathBall.cpp
File metadata and controls
91 lines (79 loc) · 2.35 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include "Ball.h"
#include "BindableBase.h"
#include "GraphicsMacros.h"
#include "Sphere.h"
namespace dx = DirectX;
Ball::Ball(Graphics& g, std::default_random_engine& rng,
std::uniform_real_distribution<float>& adist,
std::uniform_real_distribution<float>& ddist,
std::uniform_real_distribution<float>& odist,
std::uniform_real_distribution<float>& rdist,
std::uniform_int_distribution<int>& longdist,
std::uniform_int_distribution<int>& latdist)
:
r(rdist(rng)),
droll(ddist(rng)),
dpitch(ddist(rng)),
dyaw(ddist(rng)),
dphi(odist(rng)),
dtheta(odist(rng)),
dchi(odist(rng)),
chi(adist(rng)),
theta(adist(rng)),
phi(adist(rng))
{
if (!isStaticInitialized()) {
auto pvs = std::make_unique<VertexShader>(g, L"VertexShader.cso");
auto pvsbc = pvs->getBytecode();
addStaticBind(std::move(pvs));
addStaticBind(std::make_unique<PixelShader>(g, L"PixelShader.cso"));
struct PSConsts {
struct {
float r;
float g;
float b;
float a;
} face_colors[8];
};
const PSConsts cb2 = {
{
{ 1.0f,1.0f,1.0f },
{ 1.0f,0.0f,0.0f },
{ 0.0f,1.0f,0.0f },
{ 1.0f,1.0f,0.0f },
{ 0.0f,0.0f,1.0f },
{ 1.0f,0.0f,1.0f },
{ 0.0f,1.0f,1.0f },
{ 0.0f,0.0f,0.0f },
}
};
addStaticBind(std::make_unique<PixelConstantBuffer<PSConsts>>(g, cb2));
const std::vector<D3D11_INPUT_ELEMENT_DESC> ied = {
{ "POSITION",0,DXGI_FORMAT_R32G32B32_FLOAT,0,0,D3D11_INPUT_PER_VERTEX_DATA,0 },
};
addStaticBind(std::make_unique<InputLayout>(g, ied, pvsbc));
addStaticBind(std::make_unique<Topology>(g, D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST));
}
struct Vertex {
dx::XMFLOAT3 pos;
};
auto model = Sphere::makeTesselated<Vertex>(latdist(rng), longdist(rng));
// Deforming the vertices of model by linear transformation
model.Transform(dx::XMMatrixScaling(1.0f, 1.0f, 1.2f));
addBind(std::make_unique<VertexBuffer>(g, model.vertices));
addIndexBuffer(std::make_unique<IndexBuffer>(g, model.indices));
addBind(std::make_unique<TransformConstBuffer>(g, *this));
}
void Ball::update(float dt) noexcept {
roll += droll * dt;
pitch += dpitch * dt;
yaw += dyaw * dt;
theta += dtheta * dt;
phi += dphi * dt;
chi += dchi * dt;
}
dx::XMMATRIX Ball::getTransformXM() const noexcept {
return dx::XMMatrixRotationRollPitchYaw(pitch, yaw, roll) *
dx::XMMatrixTranslation(r, 0.0f, 0.0f) *
dx::XMMatrixRotationRollPitchYaw(theta, phi, chi);
}