-
Notifications
You must be signed in to change notification settings - Fork 0
/
grass.cpp
219 lines (177 loc) · 6.14 KB
/
grass.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include "grass.h"
#include "random.h"
#include "wavefront.h"
#include "config.h"
#include "camera.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{ 1000 };
Instance instance[MAX_INSTANCE_COUNT];
}
Grass::Grass(std::shared_ptr<IDirect3DDevice9> pDevice, Camera* pCamera, IDirect3DTexture9* pShadowZ)
: mDevice{ std::move(pDevice) }
, mCamera{ pCamera }
, mShadowZ{ pShadowZ }
, mVertexBuffer{ makeVertexBuffer() }
, mIndexBuffer{ makeIndexBuffer() }
, mInstanceBuffer{ makeVertexBuffer() }
, mTexture{ makeTexture() }
, mEffect{ makeEffect() }
, mVertexDeclaration{ makeVertexDeclaration() }
, mIndexCount{ 0 }
, mCamPos{ 0, 0, 0 }
, mCamDir{ 0.0f, 1.0f, 0.0f }
, mInstanceCount{ 0 }
, mHeight{ nullptr }
, mAngle{ nullptr }
{
}
bool Grass::init(const std::function<float(float, float)>& height, const std::function<float(float, float)>& angle)
{
mHeight = height;
mAngle = angle;
if (!loadTbnObject(mDevice.get(), "res\\grass\\grass2.obj", mVertexBuffer, mIndexBuffer, mIndexCount, mSphere))
return false;
mInstanceBuffer.reset(loadVertexBuffer(mDevice.get(), instance, sizeof(Instance), MAX_INSTANCE_COUNT, 0));
if (!mInstanceBuffer)
return false;
createInstances();
mVertexDeclaration.reset(loadVertexDeclaration(mDevice.get(), VERTEX_ELEMENT));
if (!mVertexDeclaration)
return false;
mTexture.reset(loadTexture(mDevice.get(), L"res\\grass\\grass.png"));
if (!mTexture)
return false;
mEffect.reset(loadEffect(mDevice.get(), L"grass.fx"));
if (!mEffect)
return false;
mEffect->SetTexture("TextureDiffuse", mTexture.get());
mEffect->SetTexture("TextureDepthShadow", mShadowZ);
mEffect->SetInt("ShadowTexSize", Config::SHADOW_TEX_SIZE);
return true;
}
void Grass::update(const float tick)
{
const D3DXVECTOR3 camPos = mCamera->getPos();
const float a = camPos.x - mCamPos.x;
const float b = camPos.z - mCamPos.z;
const float d = sqrtf(a * a + b * b);
const D3DXVECTOR3 camDir = mCamera->getDir();
const float f = D3DXVec3Dot(&camDir, &mCamDir);
if (d > 5 || f < 0.99f)
{
mCamPos = camPos;
mCamDir = camDir;
createInstances();
}
}
void Grass::draw(const GrassRenderMode mode, const D3DXMATRIX& matLightViewProj) const
{
if (mode == GrassRenderMode::PLAIN)
mEffect->SetTechnique("Plain");
else
mEffect->SetTechnique("Blend");
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);
D3DXMatrixTranspose(&matProjection, &matLightViewProj);
mEffect->SetMatrix("LightViewProj", &matProjection);
mDevice->SetVertexDeclaration(mVertexDeclaration.get());
mDevice->SetStreamSource(0, mVertexBuffer.get(), 0, sizeof(TbnVertex));
mDevice->SetStreamSourceFreq(0, (D3DSTREAMSOURCE_INDEXEDDATA | mInstanceCount));
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);
}
void Grass::createInstances()
{
Hash hash;
hash.setseed(1);
Random random;
mInstanceCount = 0;
for (int j = 0; j < 65; j++)
{
for (int i = 0; i < 65; i++)
{
unsigned int s = static_cast<int>(mCamPos.x) + i;
unsigned int t = static_cast<int>(mCamPos.z) + j;
random.setseed(hash(s, t));
unsigned int r = random() % 100;
if (r > 50)
{
float x = (static_cast<int>(mCamPos.x) + (i - 32) + static_cast<float>(random() % 10) * 0.01f);
float z = (static_cast<int>(mCamPos.z) + (j - 32) + static_cast<float>(random() % 10) * 0.01f);
float y = mHeight(x, z) - 0.15f;
if (y < 1)
continue;
float a = mAngle(x, z);
if (a < 0.5f)
continue;
D3DXMATRIX matTrans;
D3DXMatrixTranslation(&matTrans, x, y, z);
D3DXMATRIX matScale;
float c = 0.1f + static_cast<float>(random() % 5) * 0.1f;
D3DXMatrixScaling(&matScale, c, c, c);
const float radius = mSphere.w * c;
const D3DXVECTOR3 center(mSphere.x * c + x, mSphere.y * c + y, mSphere.z * c + z);
if (!mCamera->isSphereInFrustum(center, radius))
continue;
D3DXMATRIX matRotY;
D3DXMatrixRotationY(&matRotY, D3DXToRadian(random() % 360));
D3DXMATRIX matWorld = matRotY * matScale * matTrans;
D3DXMatrixTranspose(&matWorld, &matWorld);
for (int n = 0; n < 4; n++)
{
instance[mInstanceCount].m0[n] = matWorld.m[0][n];
instance[mInstanceCount].m1[n] = matWorld.m[1][n];
instance[mInstanceCount].m2[n] = matWorld.m[2][n];
instance[mInstanceCount].m3[n] = matWorld.m[3][n];
}
mInstanceCount++;
}
if (mInstanceCount >= MAX_INSTANCE_COUNT)
break;
}
if (mInstanceCount >= MAX_INSTANCE_COUNT)
break;
}
void* pData{ nullptr };
IDirect3DVertexBuffer9* pVertexBuffer = mInstanceBuffer.get();
if (SUCCEEDED(pVertexBuffer->Lock(0, 0, &pData, 0)))
{
memcpy(pData, instance, mInstanceCount * sizeof(Instance));
pVertexBuffer->Unlock();
}
}