-
Notifications
You must be signed in to change notification settings - Fork 0
/
fish.cpp
158 lines (127 loc) · 4.4 KB
/
fish.cpp
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include "fish.h"
#include "wavefront.h"
namespace
{
constexpr D3DVERTEXELEMENT9 VERTEX_ELEMENT[]
{
{ 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
{ 0, 3 * 4, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0 },
{ 0, 6 * 4, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TANGENT, 0 },
{ 0, 9 * 4, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_BINORMAL, 0 },
{ 0, 12 * 4, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
{ 1, 0, D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 1 },
{ 1, 4 * 4, D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 2 },
{ 1, 8 * 4, D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 3 },
{ 1, 12 * 4, D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 4 },
D3DDECL_END()
};
struct Instance
{
D3DXVECTOR4 m0;
D3DXVECTOR4 m1;
D3DXVECTOR4 m2;
D3DXVECTOR4 m3;
};
constexpr int MAX_INSTANCE_COUNT{ 2 };
Instance instance[MAX_INSTANCE_COUNT];
}
Fish::Fish(std::shared_ptr<IDirect3DDevice9> pDevice)
: mDevice{ std::move(pDevice) }
, mVertexBuffer{ makeVertexBuffer() }
, mIndexBuffer{ makeIndexBuffer() }
, mInstanceBuffer{ makeVertexBuffer() }
, mTexture{ makeTexture() }
, mEffect{ makeEffect() }
, mVertexDeclaration{ makeVertexDeclaration() }
, mIndexCount{ 0 }
, mAngle{ 0 }
{
}
bool Fish::init()
{
if (!loadTbnObject(mDevice.get(), "res\\fish\\tropicalfish12.obj", mVertexBuffer, mIndexBuffer, mIndexCount, mSphere))
return false;
if (!createInstances())
return false;
mVertexDeclaration.reset(loadVertexDeclaration(mDevice.get(), VERTEX_ELEMENT));
if (!mVertexDeclaration)
return false;
mTexture.reset(loadTexture(mDevice.get(), L"res\\fish\\results\\tropicalfish12_jpg_dxt1_1.dds"));
if (!mTexture)
return false;
mEffect.reset(loadEffect(mDevice.get(), L"fish.fx"));
if (!mEffect)
return false;
mEffect->SetTexture("TextureDiffuse", mTexture.get());
return true;
}
void Fish::update(const float tick) noexcept
{
mAngle += 3.0f * tick;
if (mAngle >= 360)
mAngle = 0;
}
void Fish::draw(const FishRenderMode mode) const
{
if (mode == FishRenderMode::REFLECT)
mEffect->SetTechnique("Reflect");
else
mEffect->SetTechnique("Normal");
mEffect->SetFloat("Angle", D3DXToRadian(mAngle));
D3DXMATRIX matProjection;
mDevice->GetTransform(D3DTS_PROJECTION, &matProjection);
D3DXMatrixTranspose(&matProjection, &matProjection);
mEffect->SetMatrix("Projection", &matProjection);
D3DXMATRIX matView;
mDevice->GetTransform(D3DTS_VIEW, &matView);
D3DXMatrixTranspose(&matView, &matView);
mEffect->SetMatrix("View", &matView);
mDevice->SetVertexDeclaration(mVertexDeclaration.get());
mDevice->SetStreamSource(0, mVertexBuffer.get(), 0, sizeof(TbnVertex));
mDevice->SetStreamSourceFreq(0, (D3DSTREAMSOURCE_INDEXEDDATA | MAX_INSTANCE_COUNT));
mDevice->SetStreamSource(1, mInstanceBuffer.get(), 0, sizeof(Instance));
mDevice->SetStreamSourceFreq(1, (D3DSTREAMSOURCE_INSTANCEDATA | 1ul));
mDevice->SetIndices(mIndexBuffer.get());
renderEffect(mEffect.get(), [this]()
{
mDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, mIndexCount, 0, mIndexCount / 3);
});
mDevice->SetStreamSourceFreq(0, 1);
mDevice->SetStreamSourceFreq(1, 1);
mDevice->SetStreamSource(1, nullptr, 0, 0);
}
bool Fish::createInstances()
{
D3DXMATRIX matTrans;
D3DXMatrixTranslation(&matTrans, -10, -1, 40);
float s = 0.005f;
D3DXMATRIX matScale;
D3DXMatrixScaling(&matScale, s, s, s);
D3DXMATRIX matRotY;
D3DXMatrixRotationY(&matRotY, D3DXToRadian(rand() % 360)); // NOLINT(concurrency-mt-unsafe)
D3DXMATRIX matWorld = matRotY * matScale * matTrans;
D3DXMatrixTranspose(&matWorld, &matWorld);
for (int n = 0; n < 4; n++)
{
instance[0].m0[n] = matWorld.m[0][n];
instance[0].m1[n] = matWorld.m[1][n];
instance[0].m2[n] = matWorld.m[2][n];
instance[0].m3[n] = matWorld.m[3][n];
}
D3DXMatrixTranslation(&matTrans, -11, -1.5, 40);
s = 0.003f;
D3DXMatrixScaling(&matScale, s, s, s);
matWorld = matRotY * matScale * matTrans;
D3DXMatrixTranspose(&matWorld, &matWorld);
for (int n = 0; n < 4; n++)
{
instance[1].m0[n] = matWorld.m[0][n];
instance[1].m1[n] = matWorld.m[1][n];
instance[1].m2[n] = matWorld.m[2][n];
instance[1].m3[n] = matWorld.m[3][n];
}
mInstanceBuffer.reset(loadVertexBuffer(mDevice.get(), instance, sizeof(Instance), MAX_INSTANCE_COUNT, 0));
if (!mInstanceBuffer)
return false;
return true;
}