-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathview.cpp
359 lines (309 loc) · 14 KB
/
view.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
#include "view.hpp"
int gRndm = 1; //rozhoduje o barve pozadi... 0=cerna, 1=bila
bool bFScreen=false;
LPDIRECT3D9 g_pD3D = NULL; // Used to create the D3DDevice
float v_X, v_Y, v_Z, vt_X, vt_Y, vt_Z;
D3DXVECTOR3* vTranslace = new D3DXVECTOR3(0.f, 0.f, 0.f);
bool v_XY = true;
float CamRotRad = iPatra*iObsah*9000.f; //78000
float RotUp=D3DX_PI/18.3f, CamRot = -D3DX_PI; //D3DX_PI/2.5;
float CamRotVel = 0.0f;
float fVel=iObsah * 50.8; //5000
float fFOV = D3DX_PI/3;
D3DXVECTOR3 velocity[10];
//-----------------------------------------------------------------------------
// Name: InitD3D()
// Desc: Initializes Direct3D
//-----------------------------------------------------------------------------
HRESULT InitD3D( HWND hWnd, LPDIRECT3DDEVICE9& g_pd3dDevice, D3DXMATRIX* treeMatrix )
{
// Create the D3D object.
if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
return E_FAIL;
// Set up the structure used to create the D3DDevice
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory( &d3dpp, sizeof( d3dpp ) );
if(bFScreen)
d3dpp.Windowed = false; //fullscreeen
else
d3dpp.Windowed = true;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8;
g_pd3dDevice = NULL;
// Create the D3DDevice
if( FAILED(g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_HARDWARE_VERTEXPROCESSING,
&d3dpp, &g_pd3dDevice ) ) )
{
return E_FAIL;
if(FAILED(g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_HARDWARE_VERTEXPROCESSING,
&d3dpp, &g_pd3dDevice )))
{
if(FAILED(g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_HARDWARE_VERTEXPROCESSING,
&d3dpp, &g_pd3dDevice )))
{
if( FAILED(g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_REF, hWnd,
D3DCREATE_HARDWARE_VERTEXPROCESSING,
&d3dpp, &g_pd3dDevice ) ) )
{
if(FAILED(g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_HARDWARE_VERTEXPROCESSING,
&d3dpp, &g_pd3dDevice )))
{
if(FAILED(g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_FORCE_DWORD, hWnd,
D3DCREATE_HARDWARE_VERTEXPROCESSING,
&d3dpp, &g_pd3dDevice )))
{
}
}
}
}
}
}
// Turn off culling, so we see the front and back of the triangle
g_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_CCW );
// Turn off D3D lighting, since we are providing our own vertex colors
g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE );
//TMatX = new D3DXMATRIX[iObsah];
if( g_pD3D != NULL )
g_pD3D->Release();
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: Render()
// Desc: Draws the scene
//-----------------------------------------------------------------------------
VOID render(const LPDIRECT3DDEVICE9& g_pd3dDevice, int* Pocet, byte * Keys, float* axs, D3DXMATRIX* treeMatrix, LPDIRECT3DVERTEXBUFFER9* treeVertexBuffers)
{
// Clear the backbuffer to a black color
if( gRndm==1 )
g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB( 255, 255, 255 ), 1.0f, 0 );
else
g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB( 0, 0, 0 ), 1.0f, 0 );
// Begin the scene
if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )
{
g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
//CamRotRad = 20.f;
float x = 0.f;
float y = 0.f;
// For our world matrix, we will just rotate the object about the y-axis.
//D3DXMATRIX matWorld= TMatX[iWorld];
// Set up the rotation matrix to generate 1 full rotation (2*PI radians)
// every 1000 ms. To avoid the loss of precision inherent in very high
// floating point numbers, the system time is modulated by the rotation
// period before conversion to a radian angle.
if (v_XY && (v_X - 0.1f) > 0.0f)
{
v_X -= 0.1f;
v_Y += 0.1f;
}
else if (v_XY && (v_X - 0.1f) <= 0.0f)
{
v_XY = false;
}
else if (!v_XY && (v_X + 0.1f) < 100.0f)
{
v_X += 0.1f;
v_Y -= 0.1f;
}
else if(!v_XY && (v_X + 0.1f) >= 100.0f)
{
v_XY = true;
}
//KBD
// if (Keys[DIK_W] & 0x80)
// CamRotRad -= fVel;
// //vt_Z -= 0.1;
// if (Keys[DIK_S] & 0x80)
// CamRotRad += fVel;
if (Keys[DIK_ADD] & 0x80)
fVel += iObsah*0.42;
//vt_Z -= 0.1;
if (Keys[DIK_SUBTRACT] & 0x80)
fVel -= iObsah*0.42;
//vt_Z += 0.1;
// if (Keys[DIK_A] & 0x80) // ad otocili pohled
// CamRot = D3DX_PI/2;
// if (Keys[DIK_D] & 0x80)
// CamRot = 2*D3DX_PI;
// if (Keys[DIK_A] & 0x80)
// v_X += fVel;
// if (Keys[DIK_D] & 0x80)
// v_X -+ fVel;
// if (Keys[DIK_Q] & 0x80)
// vt_Y -= fVel;
// if (Keys[DIK_E] & 0x80)
// vt_Y += fVel;
if (Keys[DIK_1] & 0x80)
CamRotVel -= 0.001;
if (Keys[DIK_2] & 0x80)
CamRotVel += 0.001;
if (Keys[DIK_3] & 0x80)
CamRot -= D3DX_PI/27;
if (Keys[DIK_4] & 0x80)
CamRot += D3DX_PI/27;
if (Keys[DIK_5] & 0x80)
CamRotVel = 0.f;
if (Keys[DIK_DOWN] & 0x80)
RotUp -= D3DX_PI/32;
if (Keys[DIK_UP] & 0x80)
RotUp += D3DX_PI/32;
if (Keys[DIK_INSERT] & 0x80)
{
}
D3DXMATRIX mRotateVX, mRotateVY;
D3DXMATRIX mRotateVZ;
// Rotate the view
// axY+= D3DX_PI/180;
// D3DXMatrixRotationX(&mRotateVX,RotUp);
D3DXMatrixRotationX(&mRotateVX,axs[1] * 0.0005f); //mys nahoru dolu
// D3DXMatrixRotationX(&mRotateVX,axs[0] * 0.001f);
D3DXMatrixRotationZ(&mRotateVZ, axs[2] *0.001f); //mys okolo
D3DXVECTOR3* At = new D3DXVECTOR3( 0.0f, 0.0f, 0.0f );
//At = XMVector3Transform( At, mRotateVX);
//At = XMVector3Transform( At, mRotateVY);
if(CamRot <= 2 * 3.14159265358979323846)
CamRot += CamRotVel*3.14159265358979323846/180;
else
CamRot = 0.f;
// D3DXMATRIX mRotateVY;
D3DXMatrixRotationY(&mRotateVY, -3.14159265358979323846/2 + CamRot + axs[0] * D3DX_PI/11520);
//CamRot = 3.14159265358979323846/2;
D3DXMATRIX mRotateW;
D3DXMatrixRotationX(&mRotateW, -3.14159265358979323846/2);
D3DXMATRIX g_View;
D3DXVECTOR3* Eye = new D3DXVECTOR3( cos(CamRot) * CamRotRad, sin(CamRot) * CamRotRad, 0.0f);
// D3DXVECTOR3 vTranslace = *(new D3DXVECTOR3( cos(CamRot) * CamRotRad + cos((float)axs[0] * D3DX_PI/11520),
// vt_Y,
// sin(CamRot) * CamRotRad +sin((float)axs[0] * D3DX_PI/11520)+ vt_X ));
// D3DXVECTOR3* Eye = new D3DXVECTOR3( cos(CamRot) * CamRotRad, sin(CamRot) * CamRotRad, 0.0f);
// D3DXVECTOR3* Eye = vTranslace;
// D3DXMATRIX posun;
// D3DXMatrixTranslation(&posun, , , );
D3DXVECTOR3* Up = new D3DXVECTOR3( 1.0f, 0.0f, 0.0f);
D3DXMatrixLookAtLH( &g_View, Eye, At, Up );
D3DXMATRIX mScale;
D3DXMatrixScaling(&mScale, 0.5f, 0.5f, 0.5f );
// D3DXMATRIX mOrbitionx;
// D3DXMatrixRotationY(&mOrbitionx, (velocity[1].x) / 4);
// D3DXMATRIX mOrbition;
// D3DXMatrixRotationX(&mOrbition, velocity[1].x);
// D3DXMATRIX mTranslation;
// D3DXMatrixTranslation(&mTranslation, 0.f,-(3.f + y), 0.f);
// D3DXMATRIX mTranslationx;
// D3DXMatrixTranslation(&mTranslationx,-(-1.f + y/3),0.f, 0.f);
//g_World1 = mScale * (mTranslation * mOrbition) * mTranslationx * mOrbitionx;
// XMMATRIX translate = XMMatrixTranslation(vt_X, vt_Y, vt_Z);
D3DXMATRIX translate;
// D3DXMatrixTranslation(&translate, cos(CamRot) * CamRotRad + cos((float)axs[0] * D3DX_PI/11520) , vt_Y, sin(CamRot) * CamRotRad +sin((float)axs[0] * D3DX_PI/11520)+ vt_X);
// D3DXMatrixTranslation(&translate, 0.0f,0.0f,0.0f);
D3DXMatrixTranslation(&translate, vTranslace->x, vTranslace->y, vTranslace->z);
g_View = translate * mRotateVZ * mRotateVY * mRotateVX; //potrebuji k zjisteni at
if (Keys[DIK_W] & 0x80)
{
// CamRotRad -= fVel;
HANDLE hstdout = GetStdHandle( STD_OUTPUT_HANDLE );
SetConsoleTextAttribute( hstdout, 0xC );
clearScreen();
cout << "x: " << g_View._13 << "\n"; // base
cout << "y: " << g_View._23 << "\n";
cout << "z: " << g_View._33 << "\n"; // base
D3DXVECTOR3* vZaxis = new D3DXVECTOR3( g_View._13,
g_View._23,
g_View._33 ); // toto je presne smer, kterym hledim
//i.e.: zaxis = normal(Eye - At)
(*vTranslace) += (*vZaxis) * -fVel;
// std::cout << "&g_View = " << g_View << "\n";
}
if (Keys[DIK_S] & 0x80)
{
clearScreen();
cout << "x: " << g_View._13 << "\n"; // base
cout << "y: " << g_View._23 << "\n";
cout << "z: " << g_View._33 << "\n"; // base
// CamRotRad += fVel;
D3DXVECTOR3* vZaxis = new D3DXVECTOR3( g_View._13,
g_View._23,
g_View._33 ); // toto je presne smer, kterym hledim
//i.e.: zaxis = normal(Eye - At)
(*vTranslace) += (*vZaxis) * fVel;
}
if (Keys[DIK_Q] & 0x80)
{
D3DXVECTOR3 *vKolmice = new D3DXVECTOR3 ( 0.f, -1.f, 0.f );
(*vTranslace) += (*vKolmice) * fVel;
}
if (Keys[DIK_E] & 0x80)
{
D3DXVECTOR3 *vKolmice = new D3DXVECTOR3 ( 0.f, 1.f, 0.f );
(*vTranslace) += (*vKolmice) * fVel;
}
if (Keys[DIK_A] & 0x80)
{
D3DXVECTOR3 *vKolmice = new D3DXVECTOR3 ( g_View._33, 0.f, -g_View._13 );
(*vTranslace) += (*vKolmice) * fVel;
}
if (Keys[DIK_D] & 0x80)
{
D3DXVECTOR3 *vKolmice = new D3DXVECTOR3 ( -g_View._33, 0.f, g_View._13 );
(*vTranslace) += (*vKolmice) * fVel;
}
g_View = translate * mRotateVZ * mRotateVY * mRotateVX;
//FLOAT fAngle = iTime * ( 2.0f * D3DX_PI ) / 10000.0f;
//D3DXMatrixRotationX( &matWorld, -D3DX_PI/2);
// Set up our view matrix. A view matrix can be defined given an eye point,
// a point to lookat, and a direction for which way is up. Here, we set the
// eye five units back along the z-axis and up three units, look at the
// origin, and define "up" to be in the y-direction.
//D3DXVECTOR3 vEyePt( 10.0f, 50.0f,-50.0f );
//D3DXVECTOR3 vLookatPt( 0.0f, 150.0f, 0.0f );
//D3DXVECTOR3 vUpVec( 0.0f, 1.0f, 0.0f );
D3DXMATRIX g_World1 = treeMatrix[0] * mScale * mRotateW;// * translate;
//g_pd3dDevice->SetTransform( D3DTS_WORLD, &g_World1 );
//g_pd3dDevice->SetTransform( D3DTS_VIEW, &g_View );
// For the projection matrix, we set up a perspective transform (which
// transforms geometry from 3D view space to 2D viewport space, with
// a perspective divide making objects smaller in the distance). To build
// a perpsective transform, we need the field of view (1/4 pi is common),
// the aspect ratio, and the near and far clipping planes (which define at
// what distances geometry should be no longer be rendered).
//g_Projection = XMMatrixPerspectiveFovLH( XM_PIDIV2, width / (FLOAT)height, 1.f, 102000.0f );
//g_Projection = XMMatrixPerspectiveFovLH( 120.0f, width / (FLOAT)height, 0.000001f, 1000000000000000000000.0f );
//CBChangeOnResize cbChangesOnResize;
//cbChangesOnResize.mProjection = XMMatrixTranspose( g_Projection );
//g_pImmediateContext->UpdateSubresource( g_pCBChangeOnResize, 0, NULL, &cbChangesOnResize, 0, 0 );
//CamRotRad = 2.f;
D3DXMATRIX matProj;
D3DXMatrixPerspectiveFovLH( &matProj, fFOV, width / (FLOAT)height, 1.0f, 102000000.0f );
g_pd3dDevice->SetTransform( D3DTS_VIEW, &g_View );
g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
//g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
for(int i = 0; i < iObsah; i++)
{
// Setup the world, view, and projection Matrices
//SetupMatrices(i);
D3DXMATRIX g_World1 = treeMatrix[i] * mScale * mRotateW;// * translate;
g_pd3dDevice->SetTransform( D3DTS_WORLD, &g_World1 );
// Render the vertex buffer contents
g_pd3dDevice->SetStreamSource( 0, treeVertexBuffers[i], 0, sizeof( CUSTOMVERTEX ) );
g_pd3dDevice->DrawPrimitive(
D3DPT_POINTLIST, 0, Pocet[i] );
}
// End the scene
g_pd3dDevice->EndScene();
}
g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
// Present the backbuffer contents to the display
}
void clearScreen()
{
HANDLE hOut;
COORD Position;
hOut = GetStdHandle(STD_OUTPUT_HANDLE);
Position.X = 0;
Position.Y = 0;
SetConsoleCursorPosition(hOut, Position);
}