-
Notifications
You must be signed in to change notification settings - Fork 0
/
butterfly.cpp
139 lines (113 loc) · 3.87 KB
/
butterfly.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
#include "butterfly.h"
#include "camera.h"
#include "config.h"
namespace
{
constexpr D3DVERTEXELEMENT9 VERTEX_ELEMENT[]
{
{ 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
{ 0, 3 * 4, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
D3DDECL_END()
};
struct Vertex
{
[[maybe_unused]] D3DXVECTOR3 position;
[[maybe_unused]] D3DXVECTOR2 texcoord;
};
const Vertex BUTTERFLY[]
{
{ { -1, 0, -1 }, { 0, 1 } },
{ { -1, 0, 1 }, { 0, 0 } },
{ { 0, 0, -1 }, { 0.5f, 1 } },
{ { 0, 0, 1 }, { 0.5f, 0 } },
{ { 1, 0, -1 }, { 1, 1 } },
{ { 1, 0, 1 }, { 1, 0 } }
};
}
Butterfly::Butterfly(std::shared_ptr<IDirect3DDevice9> pDevice, Camera* pCamera, IDirect3DTexture9* pShadowZ)
: mDevice{ std::move(pDevice) }
, mCamera{ pCamera }
, mShadowZ{ pShadowZ }
, mVertexBuffer{ makeVertexBuffer() }
, mTexture{ makeTexture() }
, mEffect{ makeEffect() }
, mVertexDeclaration{ makeVertexDeclaration() }
, mPos{ 0.0f, 3.0f, 55.0f }
, mFlap{ 10.0f }, mFlapDir{ 1.0f }, mFlapPower{ 10.0f }
, mRoll{ 0 }, mRollDir{ 1.0f }, mPitch{ 0 }, mPitchDir{ 1.0f }, mYaw{ 0 }
, mAngle{ 0 }
{
}
bool Butterfly::init()
{
mVertexBuffer.reset(loadVertexBuffer(mDevice.get(), BUTTERFLY, sizeof(Vertex), 6, 0));
if (!mVertexBuffer)
return false;
mVertexDeclaration.reset(loadVertexDeclaration(mDevice.get(), VERTEX_ELEMENT));
if (!mVertexDeclaration)
return false;
mTexture.reset(loadTexture(mDevice.get(), L"res\\butterfly.png"));
if (!mTexture)
return false;
mEffect.reset(loadEffect(mDevice.get(), L"butterfly.fx"));
if (!mEffect)
return false;
mEffect->SetTexture("TextureDiffuse", mTexture.get());
mEffect->SetTexture("TextureDepthShadow", mShadowZ);
mEffect->SetInt("ShadowTexSize", Config::SHADOW_TEX_SIZE);
mEffect->SetTechnique("Normal");
return true;
}
void Butterfly::update(const float tick) noexcept
{
mFlap += mFlapDir * mFlapPower * tick;
if (mFlap >= 150 || mFlap <= 10)
mFlapDir = -mFlapDir;
mRoll += mRollDir * tick;
if (mRoll >= 30 || mRoll <= -30)
mRollDir = -mRollDir;
mPitch += mPitchDir * 0.5f * tick;
if (mPitch >= 10 || mPitch <= -50)
mPitchDir = -mPitchDir;
mAngle += 0.5f * tick;
if (mAngle >= 360)
mAngle = 0;
mYaw = mAngle + 90;
}
void Butterfly::draw(const D3DXMATRIX& matLightViewProj) const
{
mEffect->SetFloat("Angle", D3DXToRadian(mFlap));
const float x = mPos.x + 5 * sinf(D3DXToRadian(mAngle));
const float z = mPos.z + 5 * cosf(D3DXToRadian(mAngle));
const float y = mPos.y + 0.15f * sinf(D3DXToRadian(mFlap) - 0.5f * D3DX_PI) + 0.65f * cosf(D3DXToRadian(mAngle * 3));
constexpr float radius = 0.25f;
const D3DXVECTOR3 center(x, y, z);
if (!mCamera->isSphereInFrustum(center, radius))
return;
D3DXMATRIX matWorld, matTrans, matScale, matRotZ, matRotX, matRotY;
D3DXMatrixScaling(&matScale, 0.25f, 0.25f, 0.25f);
D3DXMatrixTranslation(&matTrans, x, y, z);
D3DXMatrixRotationZ(&matRotZ, D3DXToRadian(mRoll));
D3DXMatrixRotationX(&matRotX, D3DXToRadian(mPitch));
D3DXMatrixRotationY(&matRotY, D3DXToRadian(mYaw));
matWorld = matScale * matRotY * matRotX * matRotZ * matTrans;
mDevice->SetTransform(D3DTS_WORLD, &matWorld);
D3DXMatrixTranspose(&matWorld, &matWorld);
mEffect->SetMatrix("World", &matWorld);
D3DXMATRIX matView;
mDevice->GetTransform(D3DTS_VIEW, &matView);
D3DXMatrixTranspose(&matView, &matView);
mEffect->SetMatrix("View", &matView);
D3DXMATRIX matProjection;
mDevice->GetTransform(D3DTS_PROJECTION, &matProjection);
D3DXMatrixTranspose(&matProjection, &matProjection);
mEffect->SetMatrix("Projection", &matProjection);
D3DXMatrixTranspose(&matProjection, &matLightViewProj);
mEffect->SetMatrix("LightViewProj", &matProjection);
mDevice->SetVertexDeclaration(mVertexDeclaration.get());
mDevice->SetStreamSource(0, mVertexBuffer.get(), 0, sizeof(Vertex));
renderEffect(mEffect.get(), [this]()
{
mDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 4);
});
}