-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWinMain.cpp
364 lines (295 loc) · 11.3 KB
/
WinMain.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#include <windows.h>
#include "d3d9.h"
#include "d3dx9.h"
#include "Direct3D.h"
// Direct3D objects
IDirect3D9 *g_pD3D = NULL;
IDirect3DDevice9 *g_pD3DDevice = NULL;
// A generic coordinate vertex structure
typedef struct {
D3DXVECTOR3 vecPos;
} sVertex;
// Structure to contain a morphing mesh
typedef struct sMorphMesh {
// Mesh containers (along w/resulting morphed mesh)
D3DXMESHCONTAINER_EX *SourceMesh;
D3DXMESHCONTAINER_EX *TargetMesh;
D3DXMESHCONTAINER_EX *ResultMesh;
// Normal offset in vertex structure and vertex pitch
long NormalOffset;
DWORD VertexPitch;
sMorphMesh() {
SourceMesh = NULL;
TargetMesh = NULL;
ResultMesh = NULL;
}
~sMorphMesh() {
delete SourceMesh; SourceMesh = NULL;
delete TargetMesh; TargetMesh = NULL;
delete ResultMesh; ResultMesh = NULL;
}
} sMorphMesh;
// Instance morphing meshes (for the water and the dolphin)
sMorphMesh *g_Water = NULL;
sMorphMesh *g_Dolphin = NULL;
// Background vertex structure, fvf, vertex buffer, and texture
typedef struct {
float x, y, z, rhw;
float u, v;
} sBackdropVertex;
#define BACKDROPFVF (D3DFVF_XYZRHW | D3DFVF_TEX1)
IDirect3DVertexBuffer9 *g_BackdropVB = NULL;
IDirect3DTexture9 *g_BackdropTexture = NULL;
// Window class and caption text
char g_szClass[] = "MorphClass";
char g_szCaption[] = "Morphing Demo by Jim Adams";
// Function prototypes
int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int nCmdShow);
long FAR PASCAL WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
BOOL DoInit(HWND hWnd);
void DoShutdown();
void DoFrame();
// Function to load a group of meshes for morphing
void LoadMorphingMesh(sMorphMesh *Mesh,
char *SourceMesh,
char *TargetMesh,
char *TexturePath);
// Function to build a resulting morphed mesh
void BuildMorphMesh(sMorphMesh *Mesh, float Scalar);
int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int nCmdShow)
{
WNDCLASSEX wcex;
MSG Msg;
HWND hWnd;
// Initialize the COM system
CoInitialize(NULL);
// Create the window class here and register it
wcex.cbSize = sizeof(wcex);
wcex.style = CS_CLASSDC;
wcex.lpfnWndProc = WindowProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInst;
wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = NULL;
wcex.lpszMenuName = NULL;
wcex.lpszClassName = g_szClass;
wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClassEx(&wcex))
return FALSE;
// Create the main window
hWnd = CreateWindow(g_szClass, g_szCaption,
WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,
0, 0, 640, 480,
NULL, NULL, hInst, NULL);
if(!hWnd)
return FALSE;
ShowWindow(hWnd, SW_NORMAL);
UpdateWindow(hWnd);
// Call init function and enter message pump
if(DoInit(hWnd) == TRUE) {
// Start message pump, waiting for user to exit
ZeroMemory(&Msg, sizeof(MSG));
while(Msg.message != WM_QUIT) {
if(PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE)) {
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
// Render a single frame
DoFrame();
}
}
// Call shutdown
DoShutdown();
// Unregister the window class
UnregisterClass(g_szClass, hInst);
// Shut down the COM system
CoUninitialize();
return 0;
}
long FAR PASCAL WindowProc(HWND hWnd, UINT uMsg, \
WPARAM wParam, LPARAM lParam)
{
// Only handle window destruction messages
switch(uMsg) {
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
return 0;
}
BOOL DoInit(HWND hWnd)
{
// Initialize Direct3D
InitD3D(&g_pD3D, &g_pD3DDevice, hWnd);
// Load the morphing water meshes
g_Water = new sMorphMesh();
LoadMorphingMesh(g_Water,
"..\\Data\\Water1.x",
"..\\Data\\Water2.x",
"..\\Data\\");
// Load the morphing dolphin meshes
g_Dolphin = new sMorphMesh();
LoadMorphingMesh(g_Dolphin,
"..\\Data\\Dolphin1.x",
"..\\Data\\Dolphin3.x",
"..\\Data\\");
// Create the backdrop
sBackdropVertex BackdropVerts[4] = {
{ 0.0f, 0.0, 1.0, 1.0f, 0.0f, 0.0f },
{ 640.0f, 0.0, 1.0, 1.0f, 1.0f, 0.0f },
{ 0.0f, 480.0, 1.0, 1.0f, 0.0f, 1.0f },
{ 640.0f, 480.0, 1.0, 1.0f, 1.0f, 1.0f }
};
g_pD3DDevice->CreateVertexBuffer(sizeof(BackdropVerts), D3DUSAGE_WRITEONLY, BACKDROPFVF, D3DPOOL_DEFAULT, &g_BackdropVB, NULL);
char *Ptr;
g_BackdropVB->Lock(0,0, (void**)&Ptr, 0);
memcpy(Ptr, BackdropVerts, sizeof(BackdropVerts));
g_BackdropVB->Unlock();
D3DXCreateTextureFromFile(g_pD3DDevice, "..\\Data\\Sky.bmp", &g_BackdropTexture);
// Create and enable a directional light
D3DLIGHT9 Light;
ZeroMemory(&Light, sizeof(Light));
Light.Type = D3DLIGHT_DIRECTIONAL;
Light.Diffuse.r = Light.Diffuse.g = Light.Diffuse.b = Light.Diffuse.a = 1.0f;
Light.Direction = D3DXVECTOR3(0.0f, -1.0f, 0.0f);
g_pD3DDevice->SetLight(0, &Light);
g_pD3DDevice->LightEnable(0, TRUE);
// Start playing an ocean sound
PlaySound("..\\Data\\Ocean.wav", NULL, SND_ASYNC | SND_LOOP);
return TRUE;
}
void DoShutdown()
{
// Stop playing an ocean sound
PlaySound(NULL, NULL, 0);
// Release Backdrop data
ReleaseCOM(g_BackdropVB);
ReleaseCOM(g_BackdropTexture);
// Delete meshes
delete g_Water; g_Water = NULL;
delete g_Dolphin; g_Dolphin = NULL;
// Release D3D objects
ReleaseCOM(g_pD3DDevice);
ReleaseCOM(g_pD3D);
}
void DoFrame()
{
static float DolphinXPos = 0.0f, DolphinZPos = 256.0f;
// Build the water morphing mesh using a time-based sine-wave scalar value
float WaterTimeFactor = (float)(timeGetTime()) * 0.001f;
float WaterScalar = ((float)sin(WaterTimeFactor) + 1.0f) * 0.5f;
BuildMorphMesh(g_Water, WaterScalar);
// Build the dolphin morphing mesh using a time-based scalar value
float DolphinTimeFactor = (float)(timeGetTime() % 501) / 250.0f;
float DolphinScalar = (DolphinTimeFactor<=1.0f)?DolphinTimeFactor:(2.0f-DolphinTimeFactor);
BuildMorphMesh(g_Dolphin, DolphinScalar);
// Calculate the angle of the dolphin's movement and
// reposition the dolphin if it's far enough underwater
float DolphinAngle = (float)(timeGetTime() % 6280) / 1000.0f * 3.0f;
if(sin(DolphinAngle) < -0.7f) {
DolphinXPos = (float)(rand()%1400) - 700.0f;
DolphinZPos = (float)(rand()%1500);
}
// Create and set the view transformation
D3DXMATRIX matView;
D3DXMatrixLookAtLH(&matView, &D3DXVECTOR3(0.0f, 170.0f, -1000.0f),
&D3DXVECTOR3(0.0f, 150.0f, 0.0f),
&D3DXVECTOR3(0.0f, 1.0f, 0.0f));
g_pD3DDevice->SetTransform(D3DTS_VIEW, &matView);
// Clear the device and start drawing the scene
g_pD3DDevice->Clear(NULL, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_RGBA(0,0,64,255), 1.0, 0);
if(SUCCEEDED(g_pD3DDevice->BeginScene())) {
// Draw the backdrop
g_pD3DDevice->SetFVF(BACKDROPFVF);
g_pD3DDevice->SetStreamSource(0, g_BackdropVB, 0, sizeof(sBackdropVertex));
g_pD3DDevice->SetTexture(0, g_BackdropTexture);
g_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
// Set identity matrix for world transformation
D3DXMATRIX matWorld;
D3DXMatrixIdentity(&matWorld);
g_pD3DDevice->SetTransform(D3DTS_WORLD, &matWorld);
// Enable lighting to draw water and dolpin
g_pD3DDevice->SetRenderState(D3DRS_LIGHTING, TRUE);
// Draw the water morphing mesh
DrawMesh(g_Water->ResultMesh);
// Draw the jumping dolphin
D3DXMatrixRotationZ(&matWorld, DolphinAngle - 1.57f);
matWorld._41 = DolphinXPos + (float)cos(DolphinAngle) * 256.0f;
matWorld._42 = (float)sin(DolphinAngle) * 512.0f;
matWorld._43 = DolphinZPos;
g_pD3DDevice->SetTransform(D3DTS_WORLD, &matWorld);
DrawMesh(g_Dolphin->ResultMesh);
// Turn off lighting
g_pD3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
// End the scene
g_pD3DDevice->EndScene();
}
// Present the scene to the user
g_pD3DDevice->Present(NULL, NULL, NULL, NULL);
}
void LoadMorphingMesh(sMorphMesh *Mesh,
char *SourceMesh,
char *TargetMesh,
char *TexturePath)
{
// Load the source and target meshes
LoadMesh(&Mesh->SourceMesh, g_pD3DDevice, SourceMesh, TexturePath);
LoadMesh(&Mesh->TargetMesh, g_pD3DDevice, TargetMesh, TexturePath);
// Reload the source mesh as a resulting morphed mesh container
LoadMesh(&Mesh->ResultMesh, g_pD3DDevice, SourceMesh, TexturePath);
// Determine if source mesh uses normals and calculate offset
if(Mesh->SourceMesh->MeshData.pMesh->GetFVF() & D3DFVF_NORMAL)
Mesh->NormalOffset = 3 * sizeof(float);
else Mesh->NormalOffset = 0;
// Get vertex buffer pitch
Mesh->VertexPitch = D3DXGetFVFVertexSize(Mesh->SourceMesh->MeshData.pMesh->GetFVF());
}
void BuildMorphMesh(sMorphMesh *Mesh, float Scalar)
{
// Lock mesh vertex buffers
char *SourcePtr, *TargetPtr, *ResultPtr;
Mesh->SourceMesh->MeshData.pMesh->LockVertexBuffer(D3DLOCK_READONLY, (void**)&SourcePtr);
Mesh->TargetMesh->MeshData.pMesh->LockVertexBuffer(D3DLOCK_READONLY, (void**)&TargetPtr);
Mesh->ResultMesh->MeshData.pMesh->LockVertexBuffer(0, (void**)&ResultPtr);
// Go through each vertex and interpolate coordinates
for(DWORD i=0;i<Mesh->SourceMesh->MeshData.pMesh->GetNumVertices();i++) {
// Get pointers to vertex data
sVertex *SourceVert = (sVertex*)SourcePtr;
sVertex *TargetVert = (sVertex*)TargetPtr;
sVertex *ResultVert = (sVertex*)ResultPtr;
// Get source coordinates and scale them
D3DXVECTOR3 vecSource = SourceVert->vecPos;
vecSource *= (1.0f - Scalar);
// Get target coordinates and scale them
D3DXVECTOR3 vecTarget = TargetVert->vecPos;
vecTarget *= Scalar;
// Store morphed coordinates
ResultVert->vecPos = vecSource + vecTarget;
// Handle interpolation of normals
if(Mesh->NormalOffset) {
SourceVert = (sVertex*)&SourcePtr[Mesh->NormalOffset];
TargetVert = (sVertex*)&TargetPtr[Mesh->NormalOffset];
ResultVert = (sVertex*)&ResultPtr[Mesh->NormalOffset];
// Get source coordinates and scale them
D3DXVECTOR3 vecSource = SourceVert->vecPos;
vecSource *= (1.0f - Scalar);
// Get target coordinates and scale them
D3DXVECTOR3 vecTarget = TargetVert->vecPos;
vecTarget *= Scalar;
// Store morphed coordinates
ResultVert->vecPos = vecSource + vecTarget;
}
// Go to next vertex
SourcePtr += Mesh->VertexPitch;
TargetPtr += Mesh->VertexPitch;
ResultPtr += Mesh->VertexPitch;
}
// Unlock buffers
Mesh->SourceMesh->MeshData.pMesh->UnlockVertexBuffer();
Mesh->TargetMesh->MeshData.pMesh->UnlockVertexBuffer();
Mesh->ResultMesh->MeshData.pMesh->UnlockVertexBuffer();
}