-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStereoControl.cs
More file actions
777 lines (638 loc) · 34.9 KB
/
StereoControl.cs
File metadata and controls
777 lines (638 loc) · 34.9 KB
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
using System;
using System.Diagnostics;
using System.Windows.Forms;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System.Runtime.InteropServices;
namespace Stereo
{
class StereoControl : GraphicsDeviceControl
{
#region Constructor
//public void Menu()
//{
//}
#endregion
#region Class Variables
// Our Basic effect
BasicEffect effect;
// Content stuff
ContentManager content;
Texture2D backgroundTexture;
Rectangle mainFrame;
SpriteBatch spriteBatch;
SpriteFont font;
Form2 stereoCopy;
// Collection for game components
GameComponentCollection Components;
// Store a list of primitive models, plus which one is currently selected.
List<GeometricPrimitive> primitives = new List<GeometricPrimitive>();
// Our camera used for this example
FreeCamera camera;
// Some cool stuff
RasterizerState wireFrame; // to draw wireFrames
bool drawBoundingSphere = false; // expose the boundingSpheres
bool inspectionMode = false; // activate inspection Mode
int inspectedPrim = 0; // selected Primitive to inspect
MouseState prevMouseState, currMouseState;
// to get the new sizes we use the following
float size; // a float to set the new size
float size2; // a float to set other sizes
bool needsUpdate; // a bool to see if we need to add a new one
GeometricPrimitive gp; // A geometric primitive to get the type of prim we're inspecting
// The names of each primitive, these will appear right above a pointed primitive
static readonly string[] ModelFilenames = new string[]{
"Esfera",
"Cubo",
"Cilindro",
"Toroide",
"Cilindro eliptico",
"Cilindro hiperbolico",
};
// The cursor is used to tell what the user's pointer/mouse is over. The cursor
// is moved with the left thumbstick. On windows, the mouse can be used as well.
Cursor cursor;
// Stereoscopy hell yeah
RenderTarget2D renderTarget;
Texture2D resolvedTexture;
#endregion
#region Timestep Fixing
Stopwatch stopWatch;
readonly TimeSpan TargetElapsedTime = TimeSpan.FromTicks(TimeSpan.TicksPerSecond / 60);
readonly TimeSpan MaxElapsedTime = TimeSpan.FromTicks(TimeSpan.TicksPerSecond / 10);
TimeSpan accumulatedTime;
TimeSpan lastTime;
void Tick(object sender, EventArgs e)
{
TimeSpan currentTime = stopWatch.Elapsed;
TimeSpan elapsedTime = currentTime - lastTime;
lastTime = currentTime;
if (elapsedTime > MaxElapsedTime)
{
elapsedTime = MaxElapsedTime;
}
accumulatedTime += elapsedTime;
bool updated = false;
while (accumulatedTime >= TargetElapsedTime)
{
Update();
accumulatedTime -= TargetElapsedTime;
updated = true;
}
if (updated)
{
Invalidate();
}
}
// Ticking function
void TickWhileIdle(object sender, EventArgs e)
{
NativeMethods.Message message;
while (!NativeMethods.PeekMessage(out message, IntPtr.Zero, 0, 0, 0))
{
Tick(sender, e);
}
}
#endregion
#region Initialize
/// <summary>
/// Initializes the control.
/// </summary>
protected override void Initialize()
{
// Initialize our custom Component Collection
Components = new GameComponentCollection();
// Initialize RasterizerState to Enable wireFrames
wireFrame = new RasterizerState()
{
FillMode = FillMode.WireFrame,
CullMode = CullMode.None,
};
// BackBuffer size and stuff
GraphicsDevice.PresentationParameters.BackBufferHeight = 480;
GraphicsDevice.PresentationParameters.BackBufferWidth = 640;
//GraphicsDevice.RasterizerState = wireFrame; // Optional, here for debugging purposes and stuff
// Create our BasicEffect.
effect = new BasicEffect(GraphicsDevice);
//effect.VertexColorEnabled = true;
effect.EnableDefaultLighting();
// Content stuff
content = new ContentManager(Services, "Content");
// Set our root directory
content.RootDirectory = "Content";
// use new spriteBatch
spriteBatch = new SpriteBatch(GraphicsDevice);
font = content.Load<SpriteFont>(@"fonts\hudFont");
backgroundTexture = content.Load<Texture2D>(@"Texturas\bg");
mainFrame = new Rectangle(0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);
// Load the primitives we want to be drawn
// Load the primitives
primitives.Add(new SpherePrimitive(GraphicsDevice));
primitives.Add(new CubePrimitive(GraphicsDevice));
primitives.Add(new CylinderPrimitive(GraphicsDevice));
primitives.Add(new TorusPrimitive(GraphicsDevice));
primitives.Add(new EllipticalCylinder(GraphicsDevice));
primitives.Add(new HyperbollicCylinder(GraphicsDevice));
// Initialize the renderer for our bounding spheres
BoundingSphereRenderer.Initialize(GraphicsDevice, 45);
// Add a new camera to our scene
camera = new FreeCamera( GraphicsDevice );
// Attach it as a game component
Components.Add(camera);
// Add a new cursor to our scene
cursor = new Cursor(GraphicsDevice, content, spriteBatch);
Components.Add(cursor);
// Set up the mouse and stuff
Mouse.WindowHandle = this.Handle;
// Go through every component and execute their Initialize method
foreach (WinFormComponent component in Components)
{
component.Initialize();
}
// Start the animation timer.
stopWatch = Stopwatch.StartNew();
// Start the size modifier
size = 1.0f;
size2 = 1.0f;
// Get us some stereoscopy going pl0x
PresentationParameters pp = GraphicsDevice.PresentationParameters;
renderTarget = new RenderTarget2D(GraphicsDevice, pp.BackBufferWidth, pp.BackBufferHeight, false, GraphicsDevice.DisplayMode.Format, pp.DepthStencilFormat );
// J00 LET'S PUT OUR NEW WINFORM HERE CUZ WAI NOT
stereoCopy = new Form2();
stereoCopy.Show();
// Hook the idle event to constantly redraw our animation.
Application.Idle += TickWhileIdle;
}
#endregion
#region Draw
/// <summary>
/// Draws the control.
/// </summary>
protected override void Draw()
{
// Get us some stereoscopy going pl0x
PresentationParameters pp = GraphicsDevice.PresentationParameters;
renderTarget = new RenderTarget2D(GraphicsDevice, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height, false, GraphicsDevice.DisplayMode.Format, pp.DepthStencilFormat);
// lazy hack to expand bg texture
mainFrame.Width = GraphicsDevice.Viewport.Width;
mainFrame.Height = GraphicsDevice.Viewport.Height;
// stereo here we go baby
GraphicsDevice.SetRenderTarget( renderTarget );
// Clear the Graphics Device to render the scene
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.Opaque);
//spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.Additive);
//spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend,
// null, null, null, null, transformMatrix);
spriteBatch.Draw(backgroundTexture, mainFrame,
Color.White);
spriteBatch.End();
//we reset the states modified by the spritebatch
GraphicsDevice.BlendState = BlendState.Opaque;
GraphicsDevice.DepthStencilState = DepthStencilState.Default;
//Depending on the 3D content
GraphicsDevice.SamplerStates[0] = SamplerState.LinearWrap;
GraphicsDevice.RasterizerState = wireFrame; // optional turn on if you want
// Spin the primitives according to how much time has passed.
float time = (float)stopWatch.Elapsed.TotalSeconds;
// Lulz done to get that weird spinning rotation
float yaw = time * 0.7f;
float pitch = time * 0.8f;
float roll = time * 0.9f;
// Set the color
Color color = Color.Red;
// Set transform matrices via the camera
effect.View = camera.viewMatrix;
effect.Projection = camera.projectionMatrix;
/// <summary>
/// This part just sets some values for the basic shader to work and stuff
/// </summary>
effect.DiffuseColor = color.ToVector3();
effect.Alpha = color.A / 255.0f;
effect.GraphicsDevice.DepthStencilState = DepthStencilState.Default;
if (color.A < 255)
{
// Set renderstates for alpha blended rendering.
effect.GraphicsDevice.BlendState = BlendState.AlphaBlend;
}
else
{
// Set renderstates for opaque rendering.
effect.GraphicsDevice.BlendState = BlendState.Opaque;
}
// If we are in inspection Mode
if (inspectionMode)
{
// Focus on one primitive and draw it
DrawInspectedPrimitive( effect, camera.viewMatrix, camera.projectionMatrix, time );
}
else
{
// Reset the size modifier
size = 1.0f;
// Draw them prims
DrawPrimitives(effect, camera.viewMatrix, camera.projectionMatrix, time);
// Draw text here because we are too lazy to check what states are not reset and stuff
spriteBatch.Begin();
// Draw fonts
spriteBatch.DrawString(font, "Haz clic sobre alguna primitiva",
new Vector2(((GraphicsDevice.Viewport.Width) / 2) - 155, 10), Color.White, 0, Vector2.Zero,
1, SpriteEffects.None, 1);
spriteBatch.End();
}
// Go through every DrawableComponent and Draw() them (if drawable)
for (int i = 0; i < Components.Count; i++)
{
WinFormComponent gc = (WinFormComponent)Components[i];
if ((gc is DrawableWinFormComponent) &&
((DrawableWinFormComponent)gc).Visible)
{
((DrawableWinFormComponent)gc).Draw(stopWatch);
}
}
// if there is an intersection and the user has pressed the left mouse button
// we need to go into inspection mode
currMouseState = Mouse.GetState();
// Stop inspection mode
if (currMouseState.RightButton == Microsoft.Xna.Framework.Input.ButtonState.Pressed &&
prevMouseState.RightButton != Microsoft.Xna.Framework.Input.ButtonState.Pressed)
{
// Do stuff, like going into inspection mode
if (inspectionMode)
inspectionMode = false;
}
// Set the previousMouseState to be the currentMouseState so the above code works
prevMouseState = currMouseState;
/// Go through every Component attached and Update() them
foreach (WinFormComponent component in Components)
{
component.Update(stopWatch);
}
// end of stereo rendering stuff
GraphicsDevice.SetRenderTarget( null );
resolvedTexture = renderTarget;
// Draw it!
spriteBatch.Begin();
spriteBatch.Draw(resolvedTexture, Vector2.Zero, Color.White);
spriteBatch.End();
// do it for form2
stereoCopy.stereoCopy1.backgroundTexture = resolvedTexture;
}
#endregion
#region HelperFunctions
/// <summary>
/// This function draws every primitive on screen, takes 2 matrices, the current time, and does magic
/// to render the boundingSpheres around every primitive
/// </summary>
/// <param name="effect">The effect we're using to draw the primitives</param>
/// <param name="viewMatrix">The current view matrix as given by the camera</param>
/// <param name="projectionMatrix">The projection matrix from the camera</param>
/// <param name="time">The GameTime</param>
void DrawPrimitives( BasicEffect effect, Matrix viewMatrix, Matrix projectionMatrix, float time )
{
// Lulz done to get that weird spinning rotation
float yaw = time * 0.7f;
float pitch = time * 0.8f;
float roll = time * 0.9f;
// Move every primitive next to each other <--- to be erased soon <--- LIE!
float pos = -5.0f;
foreach (GeometricPrimitive primitive in primitives)
{
// Rotate them for t3h lulz
primitive.Transformation.Rotate = new Vector3(yaw, pitch, roll) * 30.0f;
// Offset each primitive by a factor
primitive.Transformation.Translate = new Vector3(pos, 0.0f, 0.0f);
// Update the world matrix by this factor
effect.World = primitive.Transformation.Matrix;
// Draw the primitive
primitive.Draw(effect);
// Update this factor
pos += 2.0f;
if (drawBoundingSphere)
{
// We already have the center position of each primitive even if they move accros the world
// by using the translate vector of each primitive as a center for each boundingSphere, we
// can just easily create a new sphere, take it shawn
BoundingSphere sphere = new BoundingSphere(primitive.Transformation.Translate, 0.5f);
// draw the sphere with our renderer
BoundingSphereRenderer.Draw(sphere, viewMatrix, projectionMatrix);
}
}
// We loop again for every primitive, why? because we now want to be drawing some text above each primitive
// using spriteBatch(), since spriteBatch changes some states in the graphicsDevice, we need to reset some
// parameters to draw 3D again, since initializing, ending, resetting uses up cycles, we only want to do this
// once, not every time we go through a primitive
// Begin the SpriteBatch
spriteBatch.Begin();
// If the cursor is over a model, we'll draw its name. To figure out if
// the cursor is over a model, we'll use cursor.CalculateCursorRay. That
// function gives us a world space ray that starts at the "eye" of the
// camera, and shoots out in the direction pointed to by the cursor.
Ray cursorRay = cursor.CalculateCursorRay(projectionMatrix, viewMatrix);
// Initialize a variable
int i = 0;
// Loop through the primitives
foreach (GeometricPrimitive primitive in primitives)
{
// Gimme a big enough sphere
BoundingSphere sphere = new BoundingSphere(primitive.Transformation.Translate, 0.5f);
// To check for intersection
if (sphere.Intersects(cursorRay) != null)
{
// now we know that we want to draw the model's name. We want to
// draw the name a little bit above the model: but where's that?
// SpriteBatch.DrawString takes screen space coordinates, but the
// model's position is stored in world space.
// we'll use Viewport.Project, which will project a world space
// point into screen space. We'll project the vector (0,0,0) using
// the model's world matrix, and the view and projection matrices.
// that will tell us where the model's origin is on the screen.
Vector3 screenSpace = GraphicsDevice.Viewport.Project(
Vector3.Zero, camera.projectionMatrix, camera.viewMatrix,
primitive.Transformation.Matrix);
// we want to draw the text a little bit above that, so we'll use
// the screen space position - 60 to move up a little bit. A better
// approach would be to calculate where the top of the model is, and
// draw there. It's not that much harder to do, but to keep the
// sample easy, we'll take the easy way out.
Vector2 textPosition =
new Vector2(screenSpace.X, screenSpace.Y - 60);
// we want to draw the text centered around textPosition, so we'll
// calculate the center of the string, and use that as the origin
// argument to spriteBatch.DrawString. DrawString automatically
// centers text around the vector specified by the origin argument.
Vector2 stringCenter =
font.MeasureString(ModelFilenames[i]) / 2;
// to make the text readable, we'll draw the same thing twice, once
// white and once black, with a little offset to get a drop shadow
// effect.
// first we'll draw the shadow...
Vector2 shadowOffset = new Vector2(1, 1);
spriteBatch.DrawString(font, ModelFilenames[i],
textPosition + shadowOffset, Color.Black, 0.0f,
stringCenter, 1.0f, SpriteEffects.None, 0.0f);
// ...and then the real text on top.
spriteBatch.DrawString(font, ModelFilenames[i],
textPosition, Color.White, 0.0f,
stringCenter, 1.0f, SpriteEffects.None, 0.0f);
// if there is an intersection and the user has pressed the left mouse button
// we need to go into inspection mode
currMouseState = Mouse.GetState();
if ( currMouseState.LeftButton == Microsoft.Xna.Framework.Input.ButtonState.Pressed &&
prevMouseState.LeftButton != Microsoft.Xna.Framework.Input.ButtonState.Pressed)
{
// Do stuff, like going into inspection mode
if (!inspectionMode)
{
inspectionMode = true;
inspectedPrim = i;
// Upcast our inspected primitive to use this info later
gp = (GeometricPrimitive)primitives[inspectedPrim];
}
}
// Set the previousMouseState to be the currentMouseState so the above code works
prevMouseState = currMouseState;
}
// add up i
i++;
}
// End the spriteBatch, duh
spriteBatch.End();
}
/// <summary>
/// This helper function takes a BoundingSphere and a transform matrix, and
/// returns a transformed version of that BoundingSphere.
/// </summary>
/// <param name="sphere">the BoundingSphere to transform</param>
/// <param name="world">how to transform the BoundingSphere.</param>
/// <returns>the transformed BoundingSphere/</returns>
private static BoundingSphere TransformBoundingSphere(BoundingSphere sphere, Matrix transform)
{
BoundingSphere transformedSphere;
// the transform can contain different scales on the x, y, and z components.
// this has the effect of stretching and squishing our bounding sphere along
// different axes. Obviously, this is no good: a bounding sphere has to be a
// SPHERE. so, the transformed sphere's radius must be the maximum of the
// scaled x, y, and z radii.
// to calculate how the transform matrix will affect the x, y, and z
// components of the sphere, we'll create a vector3 with x y and z equal
// to the sphere's radius...
Vector3 scale3 = new Vector3(sphere.Radius, sphere.Radius, sphere.Radius);
// then transform that vector using the transform matrix. we use
// TransformNormal because we don't want to take translation into account.
scale3 = Vector3.TransformNormal(scale3, transform);
// scale3 contains the x, y, and z radii of a squished and stretched sphere.
// we'll set the finished sphere's radius to the maximum of the x y and z
// radii, creating a sphere that is large enough to contain the original
// squished sphere.
transformedSphere.Radius = Math.Max(scale3.X, Math.Max(scale3.Y, scale3.Z));
// transforming the center of the sphere is much easier. we can just use
// Vector3.Transform to transform the center vector. notice that we're using
// Transform instead of TransformNormal because in this case we DO want to
// take translation into account.
transformedSphere.Center = Vector3.Transform(sphere.Center, transform);
return transformedSphere;
}
/// <summary>
/// This helper function checks to see if a ray will intersect with a model.
/// The model's bounding spheres are used, and the model is transformed using
/// the matrix specified in the worldTransform argument.
/// </summary>
/// <param name="ray">the ray to perform the intersection check with</param>
/// <param name="sphere">the sphere to perform the intersection check with. </param>
/// <returns>true if the ray intersects the sphere.</returns>
private static bool RayIntersectsSphere(Ray ray, BoundingSphere sphere)
{
if (sphere.Intersects(ray) != null)
{
return true;
}
// If we've gotten this far, This means that there was no collision,
// and we should return false.
return false;
}
void DrawInspectedPrimitive(BasicEffect effect, Matrix viewMatrix, Matrix projectionMatrix, float time )
{
// reset the update variable
needsUpdate = false;
// Check for user input, if the user presses a key
KeyboardState keys = Keyboard.GetState();
if (keys.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.R))
{
// Add to the size value
size += 0.1f;
needsUpdate = true;
}
else if (keys.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.F))
{
// Add to the size value
size -= 0.1f;
if (size < 0.0f)
size = 0.0f;
needsUpdate = true;
}
else if (keys.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.T))
{
// Add to the size value
size2 += 0.1f;
needsUpdate = true;
}
else if (keys.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.G))
{
// Add to the size value
size2 -= 0.1f;
if (size < 0.0f)
size = 0.0f;
needsUpdate = true;
}
// if we have changed the size of our primitive
if (needsUpdate)
{
// Dispose of the primitive
primitives[inspectedPrim].Dispose();
// Remove it from the list
primitives.RemoveAt(inspectedPrim);
// Find out what our primitive was is and re add it with a new size value
if (gp is SpherePrimitive)
primitives.Insert(inspectedPrim, new SpherePrimitive(GraphicsDevice, size, 12));
else if (gp is CubePrimitive)
primitives.Insert(inspectedPrim, new CubePrimitive(GraphicsDevice, size));
else if (gp is CylinderPrimitive)
primitives.Insert(inspectedPrim, new CylinderPrimitive(GraphicsDevice, size, size, 16));
else if (gp is TorusPrimitive)
primitives.Insert(inspectedPrim, new TorusPrimitive(GraphicsDevice, size, size2, 16));
else if (gp is HyperbollicCylinder)
primitives.Insert(inspectedPrim, new HyperbollicCylinder(GraphicsDevice, size, size, size, 16));
else if (gp is EllipticalCylinder)
primitives.Insert(inspectedPrim, new EllipticalCylinder(GraphicsDevice, size, size * 2, size, 16));
}
// Lulz done to get that weird spinning rotation
float yaw = time * 0.7f;
float pitch = time * 0.8f;
float roll = time * 0.9f;
// Rotate them for t3h lulz
primitives[inspectedPrim].Transformation.Rotate = new Vector3(yaw, pitch, roll) * 30.0f;
// We need to take the primitive from its position, and bring it in front of the camera, wherever that is
// so we compare each component and add or substract the prim's position accordingly
// First for the X position
//if ((int)camera.cameraPosition.X < primitives[inspectedPrim].Transformation.Translate.X)
// primitives[inspectedPrim].Transformation.Translate = new Vector3(primitives[inspectedPrim].Transformation.Translate.X - 0.25f, primitives[inspectedPrim].Transformation.Translate.Y, primitives[inspectedPrim].Transformation.Translate.Z);
//else if ((int)camera.cameraPosition.X > primitives[inspectedPrim].Transformation.Translate.X)
// primitives[inspectedPrim].Transformation.Translate = new Vector3(primitives[inspectedPrim].Transformation.Translate.X + 0.25f, primitives[inspectedPrim].Transformation.Translate.Y, primitives[inspectedPrim].Transformation.Translate.Z);
if ( camera.cameraPosition.X < primitives[inspectedPrim].Transformation.Translate.X ||
camera.cameraPosition.X > primitives[inspectedPrim].Transformation.Translate.X )
primitives[inspectedPrim].Transformation.Translate = new Vector3(camera.cameraPosition.X, primitives[inspectedPrim].Transformation.Translate.Y, primitives[inspectedPrim].Transformation.Translate.Z);
// Then the Y position
//if ((int)camera.cameraPosition.Y < primitives[inspectedPrim].Transformation.Translate.Y)
// primitives[inspectedPrim].Transformation.Translate = new Vector3(primitives[inspectedPrim].Transformation.Translate.X, primitives[inspectedPrim].Transformation.Translate.Y - 0.25f, primitives[inspectedPrim].Transformation.Translate.Z);
//else if ((int)camera.cameraPosition.Y > primitives[inspectedPrim].Transformation.Translate.Y)
// primitives[inspectedPrim].Transformation.Translate = new Vector3(primitives[inspectedPrim].Transformation.Translate.X, primitives[inspectedPrim].Transformation.Translate.Y + 0.25f, primitives[inspectedPrim].Transformation.Translate.Z);
if (camera.cameraPosition.Y < primitives[inspectedPrim].Transformation.Translate.Y ||
camera.cameraPosition.Y > primitives[inspectedPrim].Transformation.Translate.Y)
primitives[inspectedPrim].Transformation.Translate = new Vector3(primitives[inspectedPrim].Transformation.Translate.X, camera.cameraPosition.Y, primitives[inspectedPrim].Transformation.Translate.Z);
// Finally the Z position
if ((int)camera.cameraPosition.Z + 10.0f < primitives[inspectedPrim].Transformation.Translate.Z)
primitives[inspectedPrim].Transformation.Translate = new Vector3(primitives[inspectedPrim].Transformation.Translate.X, primitives[inspectedPrim].Transformation.Translate.Y, primitives[inspectedPrim].Transformation.Translate.Z - 0.25f);
else if ((int)camera.cameraPosition.Z - 10.0f > primitives[inspectedPrim].Transformation.Translate.Z)
primitives[inspectedPrim].Transformation.Translate = new Vector3(primitives[inspectedPrim].Transformation.Translate.X, primitives[inspectedPrim].Transformation.Translate.Y, primitives[inspectedPrim].Transformation.Translate.Z + 0.25f);
// Update the world matrix by this factor
effect.World = primitives[inspectedPrim].Transformation.Matrix;
// Draw the primitive
primitives[inspectedPrim].Draw(effect);
// Present the 2D graphics
// Present this primtive's formula
// Draw text here because we are too lazy to check what states are not reset and stuff
spriteBatch.Begin();
// Draw fonts
if (gp is SpherePrimitive)
{
// draw text example
spriteBatch.DrawString(font, "Esfera",
new Vector2(((GraphicsDevice.Viewport.Width) / 2) - 27 , 10), Color.White, 0, Vector2.Zero,
1, SpriteEffects.None, 1);
spriteBatch.DrawString(font, "x^2 + y^2 + z^2 = " + (size / 2) + "^2",
new Vector2(((GraphicsDevice.Viewport.Width) / 2) - 110, 50), Color.White, 0, Vector2.Zero,
1, SpriteEffects.None, 1);
needsUpdate = true;
spriteBatch.DrawString(font, "Haz click derecho para regresar",
new Vector2(((GraphicsDevice.Viewport.Width) / 2) - 155, (GraphicsDevice.Viewport.Height - 35)),
Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 1);
}
else if (gp is CubePrimitive)
{
// draw text example
spriteBatch.DrawString(font, "Cubo",
new Vector2(((GraphicsDevice.Viewport.Width) / 2) - 18, 10), Color.White, 0, Vector2.Zero,
1, SpriteEffects.None, 1);
spriteBatch.DrawString(font, "Haz click derecho para regresar",
new Vector2(((GraphicsDevice.Viewport.Width) / 2) - 155, (GraphicsDevice.Viewport.Height - 35)),
Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 1);
}
else if (gp is CylinderPrimitive)
{
// draw text example
spriteBatch.DrawString(font, "Cilindro",
new Vector2(((GraphicsDevice.Viewport.Width) / 2) - 36, 10), Color.White, 0, Vector2.Zero,
1, SpriteEffects.None, 1);
spriteBatch.DrawString(font, "x^2 + y^2 = " + (size / 2) + "^2",
new Vector2(((GraphicsDevice.Viewport.Width) / 2) - 80, 50), Color.White, 0, Vector2.Zero,
1, SpriteEffects.None, 1);
needsUpdate = true;
spriteBatch.DrawString(font, "Haz click derecho para regresar",
new Vector2(((GraphicsDevice.Viewport.Width) / 2) - 155, (GraphicsDevice.Viewport.Height - 35)),
Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 1);
}
else if (gp is TorusPrimitive)
{
// draw text here
spriteBatch.DrawString(font, "Toroide",
new Vector2(((GraphicsDevice.Viewport.Width) / 2) - 36, 10), Color.White, 0, Vector2.Zero,
1, SpriteEffects.None, 1);
spriteBatch.DrawString(font, "["+(size - size2/2)+"-sqrt(x^2+y^2)]^2 + z^2 = " + (size2 / 2) + "^2",
new Vector2(((GraphicsDevice.Viewport.Width) / 2) - 160, 50), Color.White, 0, Vector2.Zero,
1, SpriteEffects.None, 1);
needsUpdate = true;
spriteBatch.DrawString(font, "Haz click derecho para regresar",
new Vector2(((GraphicsDevice.Viewport.Width) / 2) - 155, (GraphicsDevice.Viewport.Height - 35)),
Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 1);
}
else if (gp is HyperbollicCylinder)
{
// draw text example
spriteBatch.DrawString(font, "Cilindro hiperbolico",
new Vector2(((GraphicsDevice.Viewport.Width) / 2) - 90, 10), Color.White, 0, Vector2.Zero,
1, SpriteEffects.None, 1);
spriteBatch.DrawString(font, "(x^2 / "+ (size*2) +"^2) - (y^2 / "+ size +"^2) = 1",
new Vector2(((GraphicsDevice.Viewport.Width) / 2) - 145, 50), Color.White, 0, Vector2.Zero,
1, SpriteEffects.None, 1);
needsUpdate = true;
spriteBatch.DrawString(font, "Haz click derecho para regresar",
new Vector2(((GraphicsDevice.Viewport.Width) / 2) - 155, (GraphicsDevice.Viewport.Height - 35)),
Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 1);
}
else if (gp is EllipticalCylinder)
{
// draw text example
spriteBatch.DrawString(font, "Cilindro eliptico",
new Vector2(((GraphicsDevice.Viewport.Width) / 2) - 78, 10), Color.White, 0, Vector2.Zero,
1, SpriteEffects.None, 1);
spriteBatch.DrawString(font, "(x^2 / " + (size*2) + "^2) + (y^2 / " + size + "^2) = 1",
new Vector2(((GraphicsDevice.Viewport.Width) / 2) - 145, 50), Color.White, 0, Vector2.Zero,
1, SpriteEffects.None, 1);
needsUpdate = true;
spriteBatch.DrawString(font, "Haz click derecho para regresar",
new Vector2(((GraphicsDevice.Viewport.Width) / 2) - 155, (GraphicsDevice.Viewport.Height - 35)),
Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 1);
}
spriteBatch.End();
// Present the buttons that can affect this particular prim
// yawn, meibi later?
// Reset states
// actually, don't, cause they are already being reset up there, so yeah
}
#endregion
}
}