[ create a new paste ] login | about

Link: http://codepad.org/awYBBome    [ raw code | output | fork ]

C++, pasted on Nov 11:
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
#ifndef __WINH__
#define __WINH__
#include <Windows.h>
#endif

HWND g_Window;

LRESULT CALLBACK wndCallback (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {

	switch(msg) {
		case WM_DESTROY:
			PostQuitMessage(0);
			return 0;
	}

	return DefWindowProc(hWnd,msg,wParam,lParam);
}


int initWindow(UINT ui_Width,
				UINT ui_Height,
				const char *cc_Title) {


	WNDCLASSEX wndClass;

	wndClass.cbSize = sizeof(wndClass);
	wndClass.style = CS_HREDRAW;
	wndClass.lpfnWndProc = wndCallback;
	wndClass.cbClsExtra = NULL;
	wndClass.cbWndExtra = NULL;
	wndClass.hInstance = GetModuleHandle(NULL);
	wndClass.hIcon = NULL;
	wndClass.hCursor = NULL;
	wndClass.hbrBackground = NULL;
	wndClass.lpszMenuName = NULL;
	wndClass.lpszClassName = "Direct 3D";
	wndClass.hIconSm = NULL;

	if(!RegisterClassEx(&wndClass)) {

		return -1;

	}

	g_Window = CreateWindow("Direct 3D",cc_Title,WS_VISIBLE | WS_OVERLAPPEDWINDOW,0,0,ui_Width,ui_Height,NULL,NULL,GetModuleHandle(NULL),NULL);

	if(!g_Window) {
		return -2;
	}

	return 1;

}



________________________________________________________________________________


#include <iostream>
#include <d3d9.h>
#include <d3dx9.h>
#include <dEngine.h>
#include <time.h>

time_t times;
int Zufallszahl[3];

typedef struct _myVertex {

	D3DXVECTOR3 pos;
	D3DXVECTOR3 normal;
	DWORD diffuse;

} myVertex;

typedef struct _dice {

	D3DXVECTOR3 pos;
	D3DXVECTOR3 velo;
	D3DXVECTOR3 rotation;
	D3DXVECTOR3 rotationvelo;

	myVertex wuerfel[24];

} dice;

#define myFVF (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_DIFFUSE)
#define anzahlCubes 100
#define NULLPUNKT D3DXVECTOR3(0.0f,0.5f,0.0f);

PDIRECT3D9 d3d9_interface;
PDIRECT3DDEVICE9 d3d9_device;

PDIRECT3DVERTEXBUFFER9 vBuffer;
PDIRECT3DINDEXBUFFER9 iBuffer;

D3DMATERIAL9 wuerfelmat;
D3DLIGHT9 kerze;

myVertex *locker;
DWORD indizes[36] = {
	0,1,2,
	1,2,3,
	4,5,6,
	5,6,7,
	8,9,10,
	9,10,11,
	12,13,14,
	13,14,15,
	16,17,18,
	17,18,19,
	20,21,22,
	21,22,23
};
DWORD *indizvoid;

dice *wuerfels;

void initD3D();
void cleanD3D();

void RenderFunction(float dwTime);
void MoveFunction(float dwTime);

void makeCubes(int anzahl);

void lightMat();

int WINAPI WinMain(HINSTANCE hInstance,
					HINSTANCE hPrevInstance,
					LPSTR lpCmdLine,
					int nCmdShow) {
		MSG msg;
		DWORD dwTimeStart,dwTimeEnd;

		float timeDiff = 0.0f;

		initD3D();

		makeCubes(anzahlCubes);
		lightMat();

		d3d9_device->CreateVertexBuffer(sizeof(wuerfels[0].wuerfel),0,myFVF,D3DPOOL_MANAGED,&vBuffer,0);
		d3d9_device->CreateIndexBuffer(sizeof(indizes),0,D3DFMT_INDEX16,D3DPOOL_MANAGED,&iBuffer,0);

		iBuffer->Lock(0,0,(void**)&indizvoid,0);
		memcpy(indizvoid,indizes,sizeof(indizes));
		iBuffer->Unlock();

		d3d9_device->SetRenderState(D3DRS_DITHERENABLE,TRUE);
		d3d9_device->SetRenderState(D3DRS_CULLMODE,D3DCULL_NONE);

		d3d9_device->SetRenderState(D3DRS_AMBIENT,D3DCOLOR_XRGB(255,255,5));
		d3d9_device->SetRenderState(D3DRS_COLORVERTEX,false);
		d3d9_device->SetRenderState(D3DRS_LIGHTING,true);

		d3d9_device->SetLight(0,&kerze);
		d3d9_device->LightEnable(0,true);

		d3d9_device->SetMaterial(&wuerfelmat);

		ZeroMemory(&msg,sizeof(MSG));

		while(true) {
			dwTimeStart = timeGetTime();
			while(PeekMessage(&msg,g_Window,0,0,PM_REMOVE)) {

				TranslateMessage(&msg);
				DispatchMessage(&msg);

			}

			if(msg.message == WM_QUIT) break;

			MoveFunction(timeDiff);
			RenderFunction(timeDiff);

			dwTimeEnd = timeGetTime();
			timeDiff = (float) (dwTimeEnd - dwTimeStart) / 1000.0f;

		}


		cleanD3D();
		return msg.wParam;
}

void RenderFunction(float dwTime) {

	D3DXMATRIX rotX,rotY,rotZ;
	D3DXMATRIX translate,back;

	D3DXMATRIX view;

	D3DXMATRIX proj;

	d3d9_device->Clear(0,NULL,D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0),1.0f,0);
	d3d9_device->BeginScene();

	for(int i = 0; i < anzahlCubes; i++) {
		vBuffer->Lock(0,0,(void **)&locker,0);
		memcpy(locker,wuerfels[i].wuerfel,sizeof(wuerfels[i].wuerfel));
		vBuffer->Unlock();

		//Verschiebung und Rotation
		D3DXMatrixTranslation(&back,0.0f,-0.5f,0.0f);
		D3DXMatrixRotationX(&rotX,D3DXToRadian(180*wuerfels[i].rotation.x));
		D3DXMatrixRotationY(&rotY,D3DXToRadian(180*wuerfels[i].rotation.y));
		D3DXMatrixRotationZ(&rotZ,D3DXToRadian(180*wuerfels[i].rotation.z));
		D3DXMatrixTranslation(&translate,wuerfels[i].pos.x,wuerfels[i].pos.y+0.5f,wuerfels[i].pos.z+3.0f);

		//View-Transformation berechnen
		D3DXMatrixLookAtLH(&view,&D3DXVECTOR3(0.0f,0.0f,-10.0f),&D3DXVECTOR3(0.0f,0.0f,0.0f),&D3DXVECTOR3(0.0f,1.0f,0.0f));

		//Projektions-Transformation berechnen bei einer Bildgröße von 1200x800
		D3DXMatrixPerspectiveFovLH(&proj,D3DXToRadian(90),1200.0f/800.0f,0.1f,1000.0f);

		//Transformationen setzen
		d3d9_device->SetTransform(D3DTS_WORLD,&(back*rotX*rotY*rotZ*translate));
		d3d9_device->SetTransform(D3DTS_VIEW,&view);
		d3d9_device->SetTransform(D3DTS_PROJECTION,&proj);


		d3d9_device->SetStreamSource(0,vBuffer,0,sizeof(myVertex));
		d3d9_device->SetIndices(iBuffer);

		d3d9_device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,0,0,24,0,12);
	}

	d3d9_device->EndScene();
	d3d9_device->Present(NULL,NULL,NULL,NULL);

}

void MoveFunction(float dwTime) {

	for(int i = 0; i < anzahlCubes; i++) {

		wuerfels[i].pos += wuerfels[i].velo * dwTime;
		wuerfels[i].rotation += wuerfels[i].rotationvelo * dwTime;

	}

}

void makeCubes(int anzahl) {

	wuerfels = new dice[anzahl];
	//Random
	time(&times);
	srand((unsigned int)time);
	for(int i = 0; i < anzahl; i++) {

		//////////////////////////KOORDINATEN//////////////////////////

		//Vorne
		wuerfels[i].wuerfel[0].pos = D3DXVECTOR3(-0.5f,0.5f,-0.5f);	//Oben-Links
		wuerfels[i].wuerfel[1].pos = D3DXVECTOR3(0.5f,0.5f,-0.5f);		//Oben-Rechts
		wuerfels[i].wuerfel[2].pos = D3DXVECTOR3(-0.5f,-0.5f,-0.5f);	//Unten-Links
		wuerfels[i].wuerfel[3].pos = D3DXVECTOR3(0.5f,-0.5f,-0.5f);	//Unten-Rechts

		//Hinten
		wuerfels[i].wuerfel[4].pos = D3DXVECTOR3(-0.5f,0.5f,0.5f);	//Oben-Links
		wuerfels[i].wuerfel[5].pos = D3DXVECTOR3(0.5f,0.5f,0.5f);	//Oben-Rechts
		wuerfels[i].wuerfel[6].pos = D3DXVECTOR3(-0.5f,-0.5f,0.5f);	//Unten-Links
		wuerfels[i].wuerfel[7].pos = D3DXVECTOR3(0.5f,-0.5f,0.5f);	//Unten-Rechts

		//Oben
		wuerfels[i].wuerfel[8].pos = D3DXVECTOR3(-0.5f,0.5f,0.5f);	//Vorne-Links
		wuerfels[i].wuerfel[9].pos = D3DXVECTOR3(0.5f,0.5f,0.5f);		//Vorne-Rechts
		wuerfels[i].wuerfel[10].pos = D3DXVECTOR3(-0.5f,0.5f,-0.5f);	//Hinten-Links
		wuerfels[i].wuerfel[11].pos = D3DXVECTOR3(0.5f,0.5f,-0.5f);	//Hinten-Rechts

		//Unten
		wuerfels[i].wuerfel[12].pos = D3DXVECTOR3(-0.5f,-0.5f,0.5f);	//Vorne-Links
		wuerfels[i].wuerfel[13].pos = D3DXVECTOR3(0.5f,-0.5f,0.5f);	//Vorne-Rechts
		wuerfels[i].wuerfel[14].pos = D3DXVECTOR3(-0.5f,-0.5f,-0.5f);	//Hinten-Links
		wuerfels[i].wuerfel[15].pos = D3DXVECTOR3(0.5f,-0.5f,-0.5f);	//Hinten-Rechts

		//Rechts
		wuerfels[i].wuerfel[16].pos = D3DXVECTOR3(0.5f,0.5f,0.5f);	//Oben-Vorne 
		wuerfels[i].wuerfel[17].pos = D3DXVECTOR3(0.5f,0.5f,-0.5f);	//Oben-Hinten
		wuerfels[i].wuerfel[18].pos = D3DXVECTOR3(0.5f,-0.5f,0.5f);	//Unten-Vorne
		wuerfels[i].wuerfel[19].pos = D3DXVECTOR3(0.5f,-0.5f,-0.5f);	//Unten-Hinten


		//Links
		wuerfels[i].wuerfel[20].pos = D3DXVECTOR3(-0.5f,0.5f,0.5f);	//Oben-Vorne 
		wuerfels[i].wuerfel[21].pos = D3DXVECTOR3(-0.5f,0.5f,-0.5f);	//Oben-Hinten
		wuerfels[i].wuerfel[22].pos = D3DXVECTOR3(-0.5f,-0.5f,0.5f);	//Unten-Vorne
		wuerfels[i].wuerfel[23].pos = D3DXVECTOR3(-0.5f,-0.5f,-0.5f);	//Unten-Hinten
		

		//////////////////////////NORMALENVEKTOREN//////////////////////////

		//Vorne
		wuerfels[i].wuerfel[0].normal = D3DXVECTOR3(0.0f,0.0f,-1.0f);	//Oben-Links
		wuerfels[i].wuerfel[1].normal = D3DXVECTOR3(0.0f,0.0f,-1.0f);	//Oben-Rechts
		wuerfels[i].wuerfel[2].normal = D3DXVECTOR3(0.0f,0.0f,-1.0f);	//Unten-Links
		wuerfels[i].wuerfel[3].normal = D3DXVECTOR3(0.0f,0.0f,-1.0f);	//Unten-Rechts

		//Hinten
		wuerfels[i].wuerfel[4].normal = D3DXVECTOR3(0.0f,0.0f,1.0f);	//Oben-Links
		wuerfels[i].wuerfel[5].normal = D3DXVECTOR3(0.0f,0.0f,1.0f);	//Oben-Rechts
		wuerfels[i].wuerfel[6].normal = D3DXVECTOR3(0.0f,0.0f,1.0f);	//Unten-Links
		wuerfels[i].wuerfel[7].normal = D3DXVECTOR3(0.0f,0.0f,1.0f);	//Unten-Rechts

		//Oben
		wuerfels[i].wuerfel[8].normal = D3DXVECTOR3(0.0f,1.0f,0.0f);	//Vorne-Links
		wuerfels[i].wuerfel[9].normal = D3DXVECTOR3(0.0f,1.0f,0.0f);	//Vorne-Rechts
		wuerfels[i].wuerfel[10].normal = D3DXVECTOR3(0.0f,1.0f,0.0f);	//Hinten-Links
		wuerfels[i].wuerfel[11].normal = D3DXVECTOR3(0.0f,1.0f,0.0f);	//Hinten-Rechts

		//Unten
		wuerfels[i].wuerfel[12].normal = D3DXVECTOR3(0.0f,-1.0f,0.0f);//Vorne-Links
		wuerfels[i].wuerfel[13].normal = D3DXVECTOR3(0.0f,-1.0f,0.0f);//Vorne-Rechts
		wuerfels[i].wuerfel[14].normal = D3DXVECTOR3(0.0f,-1.0f,0.0f);//Hinten-Links
		wuerfels[i].wuerfel[15].normal = D3DXVECTOR3(0.0f,-1.0f,0.0f);//Hinten-Rechts

		//Rechts
		wuerfels[i].wuerfel[16].normal = D3DXVECTOR3(1.0f,0.0f,0.0f);	//Oben-Vorne 
		wuerfels[i].wuerfel[17].normal = D3DXVECTOR3(1.0f,0.0f,0.0f);	//Oben-Hinten
		wuerfels[i].wuerfel[18].normal = D3DXVECTOR3(1.0f,0.0f,0.0f);	//Unten-Vorne
		wuerfels[i].wuerfel[19].normal = D3DXVECTOR3(1.0f,0.0f,0.0f);	//Unten-Hinten


		//Links
		wuerfels[i].wuerfel[20].normal = D3DXVECTOR3(-1.0f,0.0f,0.0f);//Oben-Vorne 
		wuerfels[i].wuerfel[21].normal = D3DXVECTOR3(-1.0f,0.0f,0.0f);//Oben-Hinten
		wuerfels[i].wuerfel[22].normal = D3DXVECTOR3(-1.0f,0.0f,0.0f);//Unten-Vorne
		wuerfels[i].wuerfel[23].normal = D3DXVECTOR3(-1.0f,0.0f,0.0f);//Unten-Hinten

		Zufallszahl[0] = rand();
		Zufallszahl[1] = rand();
		Zufallszahl[2] = rand();

		for(int j = 0; j < 24; j++) 
			wuerfels[i].wuerfel[j].diffuse = D3DCOLOR_XRGB((Zufallszahl[0]%255)+1,(Zufallszahl[2]%255+1),(Zufallszahl[1]%255+1));


		////////////Velocity,Rotation etc.////////////
		wuerfels[i].pos			= NULLPUNKT;
		wuerfels[i].velo			= D3DXVECTOR3(((float)(Zufallszahl[0]%20)+1)/10,0.0f,-1.0f);

		wuerfels[i].rotation		= D3DXVECTOR3(0.0f,0.0f,0.0f);
		wuerfels[i].rotationvelo	= D3DXVECTOR3(3.0f,2.0f,1.0f);

	}

}

void lightMat() {


	wuerfelmat.Ambient.r = 0.2f;
	wuerfelmat.Ambient.g = 0.0f;
	wuerfelmat.Ambient.b = 0.0f;
	wuerfelmat.Ambient.a = 0.9f;

	wuerfelmat.Diffuse.r = 0.8f;
	wuerfelmat.Diffuse.g = 0.0f;
	wuerfelmat.Diffuse.b = 0.0f;
	wuerfelmat.Diffuse.a = 1.0f;

	wuerfelmat.Specular.r = 0.0f;
	wuerfelmat.Specular.g = 1.0f;
	wuerfelmat.Specular.b = 0.0f;
	wuerfelmat.Specular.a = 0.5f;

	wuerfelmat.Power = 1.0f;

	kerze.Type = D3DLIGHT_DIRECTIONAL;

	kerze.Diffuse.r = 0.6f;
	kerze.Diffuse.g = 0.6f;
	kerze.Diffuse.b = 0.6f;
	kerze.Diffuse.a = 1.0f;

	kerze.Specular.r = 1.0f;
	kerze.Specular.g = 1.0f;
	kerze.Specular.b = 1.0f;
	kerze.Specular.a = 1.0f;
	
	kerze.Direction = D3DXVECTOR3(1.0f,0.0f,0.0f);
}

void initD3D() {

	D3DPRESENT_PARAMETERS d3dpp;

	d3d9_interface = Direct3DCreate9(D3D_SDK_VERSION);
	//Direct3D-Abzählung

	//MessageBox(NULL,acModes,"Die VideoModi für Adapter 1",MB_OK);

	if(initWindow(1200,800,"Direct3D-Fenster") == 1) {

		ZeroMemory(&d3dpp,sizeof(D3DPRESENT_PARAMETERS));

		d3dpp.BackBufferHeight  = 1200;
		d3dpp.BackBufferWidth = 800;
		d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
		d3dpp.BackBufferCount = 1;
		d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;
		d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
		d3dpp.hDeviceWindow = g_Window;
		d3dpp.Windowed = true;
		d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;

		if(FAILED(d3d9_interface->CreateDevice(0,D3DDEVTYPE_HAL,NULL,D3DCREATE_MIXED_VERTEXPROCESSING,&d3dpp,&d3d9_device))) {
			MessageBox(NULL,"Deviceerstellung hat nicht funktioniert","Nö",MB_OK);
		
		}
			
	d3d9_device->SetFVF(myFVF);

	}
}

void cleanD3D() {

	vBuffer->Release();
	iBuffer->Release();
	d3d9_interface->Release();
	d3d9_device->Release();

}


Output:
1
2
3
4
5
6
Line 20: error: Windows.h: No such file or directory
Line 17: error: d3d9.h: No such file or directory
Line 18: error: d3dx9.h: No such file or directory
Line 20: error: dEngine.h: No such file or directory
Line 7: error: 'HWND' does not name a type
compilation terminated due to -Wfatal-errors.


Create a new paste based on this one


Comments: