/
MyMesh.cpp
executable file
·219 lines (190 loc) · 5.89 KB
/
MyMesh.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
/*
MyMesh.cpp
Kaya Oguz <0705907>
*/
#include "MyMesh.h"
MyMesh::MyMesh()
{
numMtrls = 0;
D3DXMatrixIdentity(&worldTransform);
D3DXMatrixIdentity(&translationMatrix);
D3DXMatrixIdentity(&rotationMatrix);
D3DXMatrixIdentity(&scalingMatrix);
minBounds = maxBounds = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
transparent = false;
}
bool MyMesh::Init(IDirect3DDevice9 *d, char *fn)
{
Device = d;
HRESULT hr;
ID3DXBuffer* adjBuffer = 0;
ID3DXBuffer* mtrlBuffer = 0;
D3DXCreateTextureFromFile(Device, "whitetex.dds", &whiteTex);
D3DXMATERIAL* mtrls = 0;
D3DXLoadMeshFromX(fn, D3DXMESH_MANAGED, Device, &adjBuffer, &mtrlBuffer, 0, &numMtrls, &mesh);
// Louis Natanson Lab. Code
if (!mesh)
{
MessageBox(0, "Mesh Load Failed", fn, MB_OK);
return false;
}
if ( (mtrlBuffer != 0) && (numMtrls != 0 ) )
{
mtrls = (D3DXMATERIAL*)mtrlBuffer->GetBufferPointer();
for (DWORD i = 0; i < numMtrls; i++)
{
Mtrl m;
m.ambient = mtrls[i].MatD3D.Diffuse;
m.diffuse = mtrls[i].MatD3D.Diffuse;
m.spec = mtrls[i].MatD3D.Specular;
m.specPower = mtrls[i].MatD3D.Power;
// mtrls[i].MatD3D.Ambient = mtrls[i].MatD3D.Diffuse;
// Mtrls.push_back( mtrls[i].MatD3D );
Mtrls.push_back(m);
if(mtrls[i].pTextureFilename != 0)
{
IDirect3DTexture9* tex = 0;
hr = D3DXCreateTextureFromFile(Device, mtrls[i].pTextureFilename, &tex);
if (hr == S_OK) Textures.push_back(tex);
}
else Textures.push_back(0);
}
}
mtrlBuffer->Release();
D3DXMatrixIdentity(&worldTransform);
D3DXMatrixIdentity(&translationMatrix);
D3DXMatrixIdentity(&rotationMatrix);
D3DXMatrixIdentity(&scalingMatrix);
// calculate the bounding box
BYTE* pVertices = NULL;
hr = mesh->LockVertexBuffer(D3DLOCK_READONLY, (LPVOID*)&pVertices);
if (!FAILED(hr))
{
D3DXComputeBoundingBox(
(D3DXVECTOR3*)pVertices,
mesh->GetNumVertices(),
D3DXGetFVFVertexSize(mesh->GetFVF()),
&minBounds, &maxBounds);
// calculate object bounds for Oriented Bounding Box
objectBounds[0] = D3DXVECTOR3( minBounds.x, minBounds.y, minBounds.z );
objectBounds[1] = D3DXVECTOR3( maxBounds.x, minBounds.y, minBounds.z );
objectBounds[2] = D3DXVECTOR3( minBounds.x, maxBounds.y, minBounds.z );
objectBounds[3] = D3DXVECTOR3( maxBounds.x, maxBounds.y, minBounds.z );
objectBounds[4] = D3DXVECTOR3( minBounds.x, minBounds.y, maxBounds.z );
objectBounds[5] = D3DXVECTOR3( maxBounds.x, minBounds.y, maxBounds.z );
objectBounds[6] = D3DXVECTOR3( minBounds.x, maxBounds.y, maxBounds.z );
objectBounds[7] = D3DXVECTOR3( maxBounds.x, maxBounds.y, maxBounds.z );
}
mesh->UnlockVertexBuffer();
return true;
}
void MyMesh::Draw(ID3DXEffect* effect)
{
mhTech = effect->GetTechniqueByName("MyMeshLighting");
mhWVP = effect->GetParameterByName(0, "gWVP");
mhWorldInvTrans = effect->GetParameterByName(0, "gWorldInvTrans");
mhMtrl = effect->GetParameterByName(0, "gMtrl");
mhEyePos = effect->GetParameterByName(0, "gEyePosition");
mhWorld = effect->GetParameterByName(0, "gWorld");
mhTex = effect->GetParameterByName(0, "gTex");
worldTransform = scalingMatrix * rotationMatrix * translationMatrix;
D3DXMATRIX proj;
D3DXMATRIX view;
Device->GetTransform(D3DTS_PROJECTION, &proj);
Device->GetTransform(D3DTS_VIEW, &view);
D3DXVECTOR3 eyePos = D3DXVECTOR3(view._41, view._42, view._43);
effect->SetValue(mhEyePos, &eyePos, sizeof(D3DXVECTOR3));
effect->SetMatrix(mhWVP, &(worldTransform * view * proj) );
D3DXMATRIX worldInvTrans;
D3DXMatrixInverse(&worldInvTrans, 0, &worldTransform);
D3DXMatrixTranspose(&worldInvTrans, &worldInvTrans);
effect->SetMatrix(mhWorldInvTrans, &worldInvTrans);
effect->SetMatrix(mhWorld, &worldTransform);
effect->SetTechnique(mhTech);
UINT numPasses = 0;
effect->Begin(&numPasses, 0);
effect->BeginPass(0);
if (transparent)
Device->SetRenderState(D3DRS_ALPHABLENDENABLE, true);
for (DWORD i=0; i<numMtrls; i++)
{
effect->SetValue(mhMtrl, &Mtrls[i], sizeof(Mtrl));
if (Textures[i] != 0)
effect->SetTexture(mhTex, Textures[i]);
else effect->SetTexture(mhTex, whiteTex);
effect->CommitChanges();
mesh->DrawSubset(i);
}
if (transparent)
Device->SetRenderState(D3DRS_ALPHABLENDENABLE, false);
effect->EndPass();
effect->End();
}
void MyMesh::CalcAABBFromOBB(D3DXVECTOR3 *minB, D3DXVECTOR3 *maxB)
{
D3DXVECTOR3 obb[8];
for (int i=0;i<8;i++)
{
D3DXVec3TransformCoord(&obb[i], &objectBounds[i], &worldTransform);
}
/*
assert(minB);
assert(maxB);
assert(obb);
*/
minB->x=maxB->x=obb[0].x;
minB->y=maxB->y=obb[0].y;
minB->z=maxB->z=obb[0].z;
for (int i=1;i<8;i++)
{
if (obb[i].x < minB->x) minB->x=obb[i].x;
if (obb[i].x > maxB->x) maxB->x=obb[i].x;
if (obb[i].y < minB->y) minB->y=obb[i].y;
if (obb[i].y > maxB->y) maxB->y=obb[i].y;
if (obb[i].z < minB->z) minB->z=obb[i].z;
if (obb[i].z > maxB->z) maxB->z=obb[i].z;
}
}
/*
Translating, Rotating and Scaling are done with
the worldTransform...
*/
void MyMesh::Translate(FLOAT x, FLOAT y, FLOAT z)
{
D3DXMATRIX trans;
D3DXMatrixIdentity(&trans);
D3DXMatrixTranslation(&trans, x, y, z);
translationMatrix = translationMatrix * trans;
}
void MyMesh::RotateX(FLOAT q)
{
D3DXMATRIX rot;
D3DXMatrixIdentity(&rot);
D3DXMatrixRotationX(&rot, q);
rotationMatrix = rotationMatrix * rot;
}
void MyMesh::RotateY(FLOAT q)
{
D3DXMATRIX rot;
D3DXMatrixIdentity(&rot);
D3DXMatrixRotationY(&rot, q);
rotationMatrix = rotationMatrix * rot;
}
void MyMesh::RotateZ(FLOAT q)
{
D3DXMATRIX rot;
D3DXMatrixIdentity(&rot);
D3DXMatrixRotationZ(&rot, q);
rotationMatrix = rotationMatrix * rot;
}
void MyMesh::Scale(FLOAT sx, FLOAT sy, FLOAT sz)
{
D3DXMATRIX scale;
D3DXMatrixIdentity(&scale);
D3DXMatrixScaling(&scale, sx, sy, sz);
scalingMatrix = scalingMatrix * scale;
}
void MyMesh::Scale(FLOAT s)
{
Scale(s, s, s);
}