/
RealProg.cs
349 lines (295 loc) · 11.4 KB
/
RealProg.cs
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
using System;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using _3DAPI;
using _3DAPI.Physics;
namespace WPSampleGame
{
class RealProg
{
static PhysicalObject collisiontester;
static World physicsworld = new World();
static bool flip = false;
static Texture2D rtex;
public RealProg(Renderer _renderer)
{
renderer = _renderer;
renderer.cameraPosition.Z = -15;
Shader myder = renderer.createBasicShader();
//Uncomment for AABB collisions
// physicsworld.testtype = CollisionTestType.AABB;
myder.Draw();
Bitmap mmap = new Bitmap("pic.jpg");
Texture2D mtex = renderer.createTextureFromBitmap(mmap);
mtex.Draw();
rtex = renderer.createTexture(512, 512);
rtex.Draw();
Mesh[] meshes = Primitives.LoadMesh("playercube.obj", flip);
collisiontester = new PhysicalObject(meshes[0].meshverts, 5, CollisionType.Dynamic, physicsworld);
collisiontester.Position = new Vector3D(-5, 0, 0);
mainmesh = meshes[0];
foreach (Mesh mesh in meshes)
{
VertexBuffer tbuff = renderer.CreateVertexBuffer(mesh.meshverts, mesh.meshtexas, mesh.meshnorms);
rotatingbuffer = tbuff;
if (mesh.bitmap != null)
{
Console.WriteLine("BITMAP RENDER");
Texture2D tt = renderer.createTextureFromBitmap(mesh.bitmap);
tt.Draw();
}
tbuff.Draw();
}
mtex.Draw();
Mesh cube = Primitives.LoadMesh("playercube.obj", flip)[0];
theobject = new PhysicalObject(cube.meshverts, 9, CollisionType.Dynamic, physicsworld);
collisiontester.ownedVBO = rotatingbuffer;
theobject.ownedVBO = renderer.CreateVertexBuffer(cube.meshverts, cube.meshtexas, cube.meshnorms);
theobject.ownedVBO.Draw();
physicsworld.physicalobjects.Add(theobject);
physicsworld.physicalobjects.Add(collisiontester);
Mesh anothercube = Primitives.LoadMesh("playercube.obj", flip)[0];
PhysicalObject mobject = new PhysicalObject(anothercube.meshverts.Clone() as Vector3D[], 1, CollisionType.Dynamic, physicsworld);
mobject.ownedVBO = renderer.CreateVertexBuffer(anothercube.meshverts, anothercube.meshtexas, anothercube.meshnorms);
physicsworld.physicalobjects.Add(mobject);
mobject.ownedVBO.Draw();
mobject.Position = new Vector3D(30, 0, 0);
//Set physics properties
theobject.Weight = 1;
collisiontester.Weight = 1;
//collisiontester.Velocity = new Vector3D(.05f, 0, 0);
theobject.Position = new Vector3D(15, 0, 0);
theobject.IsCube = true;
theobject.Weight = 9999999;
mobject.IsCube = true;
//End physics properties
physicsworld.Start();
physicsworld.physicsUpdateFrame += new System.Threading.ThreadStart(physicsworld_physicsUpdateFrame);
System.Threading.Thread mthread = new System.Threading.Thread(thetar);
mthread.Start();
#if PHONE
renderer.defaultTouchpad.onTouchMoved += new TouchEvent(defaultTouchpad_onTouchMoved);
renderer.defaultTouchpad.onTouchFound += new TouchEvent(defaultTouchpad_onTouchFound);
renderer.defaultTouchpad.onTouchLost += new TouchEvent(defaultTouchpad_onTouchLost);
#endif
#if !PHONE
renderer.defaultKeyboard.onKeyDown += new keyboardeventargs(defaultKeyboard_onKeyDown);
renderer.defaultKeyboard.onKeyUp += new keyboardeventargs(defaultKeyboard_onKeyUp);
renderer.defaultMouse.onMouseMove += new mouseEvent(defaultMouse_onMouseMove);
renderer.defaultMouse.onMouseDown += new mouseEvent(defaultMouse_onMouseDown);
#endif
}
void defaultTouchpad_onTouchLost(int touchpoint, int x, int y)
{
touches.Remove(touchpoint);
if (touches.Count == 0)
{
left = false;
right = false;
up = false;
down = false;
}
}
void defaultTouchpad_onTouchFound(int touchpoint, int x, int y)
{
touches.Add(touchpoint);
if (touches.Count == 2)
{
defaultMouse_onMouseDown(MouseButton.Right, 0, 0);
}
}
List<int> touches = new List<int>();
void defaultTouchpad_onTouchMoved(int touchpoint, int x, int y)
{
if (x > 120)
{
left = true;
}
else
{
left = false;
}
if (x < 50)
{
right = true;
}
else
{
right = false;
}
if (y > 120)
{
down = true;
}
else
{
down = false;
}
if (y < 50)
{
up = true;
}
else
{
up = false;
}
}
static void physicsworld_physicsUpdateFrame()
{
renderer.SetRenderTarget(rtex, collisiontester.Position, new Vector3D(180, 0, 0));
}
static void defaultMouse_onMouseDown(MouseButton btn, int x, int y)
{
lock (physicsworld.physicalobjects)
{
Mesh tmesh = Primitives.LoadMesh("playercube.obj", flip)[0];
PhysicalObject mobject = new PhysicalObject(tmesh.meshverts, 1, CollisionType.Dynamic, physicsworld);
mobject.IsCube = true;
mobject.ownedVBO = renderer.CreateVertexBuffer(tmesh.meshverts, tmesh.meshtexas, tmesh.meshnorms);
mobject.ownedVBO.Draw();
physicsworld.physicalobjects.Add(mobject);
mobject.Position = renderer.cameraPosition;
if (btn == MouseButton.Left)
{
mobject.Velocity = Vector3D.ComputeRotation(renderer.worldRotation);
}
else
{
mobject.Weight = 3f;
}
}
}
static Vector2D prevval = new Vector2D(0, 0);
static bool move = false;
static void defaultMouse_onMouseMove(MouseButton btn, int x, int y)
{
if (prevval.X == 0)
{
prevval.X = x;
prevval.Y = y;
}
if (move)
{
renderer.worldRotation.Y += (prevval.X - x) / 10;
renderer.worldRotation.X += (prevval.Y - y) / 10;
//Windows Phone doesn't support setting the cursor position
#if !PHONE
System.Windows.Forms.Cursor.Position = new Point((int)prevval.X, (int)prevval.Y);
#endif
move = false;
}
else
{
move = true;
}
}
static bool ctrl = false;
static void defaultKeyboard_onKeyUp(string KeyName)
{
if (KeyName.ToLower().Contains("control"))
{
ctrl = false;
}
if (KeyName.ToLower().Contains("up"))
{
up = false;
}
if (KeyName.ToLower().Contains("down"))
{
down = false;
}
if (KeyName.ToLower().Contains("left"))
{
left = false;
}
if (KeyName.ToLower().Contains("right"))
{
right = false;
}
}
static void defaultKeyboard_onKeyDown(string KeyName)
{
if (KeyName.ToLower().Contains("control"))
{
ctrl = true;
}
if (!ctrl)
{
if (KeyName.ToLower().Contains("up"))
{
up = true;
}
if (KeyName.ToLower().Contains("down"))
{
down = true;
}
if (KeyName.ToLower().Contains("left"))
{
left = true;
}
if (KeyName.ToLower().Contains("right"))
{
right = true;
}
if (KeyName.ToLower().Contains("f10"))
{
renderer.Dispose();
isrunning = false;
}
}
}
static Mesh mainmesh;
static bool isrunning = true;
static Renderer renderer;
static bool left = false;
static bool right = false;
static bool up = false;
static bool down = false;
static void thetar()
{
renderer.cameraPosition.Z -= 5f;
while (isrunning)
{
//theobject.ownedVBO.rotation.X += .01f;
Vector3D prevpos = renderer.cameraPosition;
//rotatingbuffer.rotation.Y = MathHelpers.DegreesToRadians(-90);
//rotatingbuffer.rotation.Y += .01f;
Vector3D cameraPosition = new Vector3D(0, 0, 0);
Vector3D direction = Vector3D.ComputeRotation(new Vector3D(renderer.worldRotation.X, renderer.worldRotation.Y, renderer.worldRotation.Z));
direction.Y *= -1;
Vector3D direction2 = Vector3D.ComputeRotation(new Vector3D(renderer.worldRotation.X, renderer.worldRotation.Y - 90, renderer.worldRotation.Z));
if (left)
{
cameraPosition.X -= .1f * direction2.X;
cameraPosition.Z -= .1f * direction2.Z;
}
if (right)
{
cameraPosition.X += .1f * direction2.X;
cameraPosition.Z += .1f * direction2.Z;
}
if (up)
{
cameraPosition.Z += .1f * direction.Z;
cameraPosition.X += .1f * direction.X;
}
if (down)
{
cameraPosition.Z -= .1f * direction.Z;
cameraPosition.X -= .1f * direction.X;
}
// collisiontester.Rotation = rotatingbuffer.rotation;
renderer.cameraPosition += cameraPosition;
if (collisiontester.Contains(renderer.cameraPosition))
{
renderer.cameraPosition = prevpos;
}
System.Threading.Thread.Sleep(10);
}
}
static PhysicalObject theobject;
static VertexBuffer rotatingbuffer;
}
}