-
Notifications
You must be signed in to change notification settings - Fork 1
/
NeroPlatform.cs
120 lines (103 loc) · 4.1 KB
/
NeroPlatform.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
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using NeroOS.Cameras;
using NeroOS.Drawing;
using NeroOS.Apps;
using NeroOS.Sim;
namespace NeroOS
{
public class NeroPlatform : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
Canvas canvas;
StereoRig cameraRig;
KeyboardState lastKeyState;
List<IProgram> apps;
public NeroPlatform()
{
graphics = new GraphicsDeviceManager(this);
graphics.PreferredBackBufferWidth = 640;
graphics.PreferredBackBufferHeight = 480;
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
canvas = new Canvas(this.GraphicsDevice);
cameraRig = new StereoRig();
cameraRig.LoadCalibrationFile("Config/intrinsics.yml", "Config/extrinsics.yml");
apps = new List<IProgram>();
apps.Add(new GlowFingerApp());
apps.Add(new DragNDropApp());
for (int i = 0; i < apps.Count; i++)
{
apps[i].OnCreate();
}
base.Initialize();
}
protected override void Update(GameTime gameTime)
{
float elapsedTime = (float)gameTime.ElapsedGameTime.Milliseconds / 1000.0f;
cameraRig.Update();
BoundingSphere[] detectedPoints = cameraRig.GetDetectionPoints();
for (int i = 0; i < apps.Count; i++)
{
apps[i].OnUpdate(elapsedTime);
if (detectedPoints != null)
{
//List<BoundingSphere> intersectedPoints = new List<BoundingSphere>();
for (int j = 0; j < detectedPoints.Length; j++)
{
if (apps[i].InteractCollision(detectedPoints[j]))
{
//intersectedPoints.Add(detectedPoints[j]);
apps[i].OnInteract(detectedPoints);
break;
}
}
//if(intersectedPoints.Count > 0)
// apps[i].OnInteract(intersectedPoints.ToArray());
}
}
KeyboardState currKeyState = Keyboard.GetState();
if(currKeyState.IsKeyDown(Keys.Space) && lastKeyState.IsKeyUp(Keys.Space))
{
Console.WriteLine("Reversing matrix!");
cameraRig.ReverseMatrix();
}
lastKeyState = currKeyState;
base.Update(gameTime);
}
void RenderApps(VirtualCamera camera, RenderLayer renderLayer)
{
GraphicsDevice device = canvas.GetDevice();
DepthStencilBuffer dsOld = device.DepthStencilBuffer;
device.DepthStencilBuffer = canvas.GetDepthStencil();
device.SetRenderTarget(0, renderLayer.RenderTarget);
device.SetRenderTarget(1, renderLayer.GlowTarget);
device.Clear(Color.TransparentBlack);
device.RenderState.DepthBufferEnable = true;
device.RenderState.DepthBufferWriteEnable = true;
device.RenderState.DepthBufferFunction = CompareFunction.LessEqual;
device.SetVertexShaderConstant(CanvasShaderConstants.VC_MODELVIEW, camera.ViewProjection);
for (int i = 0; i < apps.Count; i++)
{
apps[i].OnRender(canvas);
}
device.SetRenderTarget(0, null);
device.SetRenderTarget(1, null);
device.DepthStencilBuffer = dsOld;
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
cameraRig.RenderCameraRig(canvas);
RenderApps(cameraRig.virtualCameraA, cameraRig.renderLayerA);
RenderApps(cameraRig.virtualCameraB, cameraRig.renderLayerB);
cameraRig.CompositeFinalImage(canvas);
base.Draw(gameTime);
}
}
}