02-20-2014 04:58 PM
using OculusRift.Oculus;
cameraRotation = Matrix.CreateFromQuaternion(OculusClient.GetPredictedOrientation());
cameraRotation = Matrix.CreateFromQuaternion(OculusClient.GetPredictedOrientation()) * bodyWorldMatrix;
OculusClient oculusClient;
Effect oculusRiftDistortionShader;
RenderTarget2D renderTargetLeft;
RenderTarget2D renderTargetRight;
Texture2D renderTextureLeft;
Texture2D renderTextureRight;
float scaleImageFactor;
float fov_x;
float fov_d;
int IPD = 0;
public static float aspectRatio;
float yfov;
float viewCenter;
float eyeProjectionShift;
float projectionCenterOffset;
Matrix projCenter;
Matrix projLeft;
Matrix projRight;
Matrix viewLeft;
Matrix viewRight;
float halfIPD;
oculusClient = new OculusClient();
scaleImageFactor = 0.71f;
graphics.PreferredBackBufferWidth = (int)Math.Ceiling(Settings.resolutionX / scaleImageFactor);
graphics.PreferredBackBufferHeight = (int)Math.Ceiling(Settings.resolutionY / scaleImageFactor);
oculusRiftDistortionShader = Content.Load<Effect>("Shaders/OculusRift");
aspectRatio = (float)(OculusClient.GetScreenResolution().X * 0.5f / (float)(OculusClient.GetScreenResolution().Y));
fov_d = OculusClient.GetEyeToScreenDistance();
fov_x = OculusClient.GetScreenSize().Y * scaleImageFactor;
yfov = 2.0f * (float)Math.Atan(fov_x / fov_d);
camera = new Camera(yfov, aspectRatio);
renderTargetLeft = new RenderTarget2D(GraphicsDevice, graphics.PreferredBackBufferWidth / 2, graphics.PreferredBackBufferHeight);
renderTargetRight = new RenderTarget2D(GraphicsDevice, graphics.PreferredBackBufferWidth / 2, graphics.PreferredBackBufferHeight);
OculusClient.SetSensorPredictionTime(0, 0.03f);
UpdateResolutionAndRenderTargets();
private int viewportWidth;
private int viewportHeight;
private Microsoft.Xna.Framework.Rectangle sideBySideLeftSpriteSize;
private Microsoft.Xna.Framework.Rectangle sideBySideRightSpriteSize;
private void UpdateResolutionAndRenderTargets()
{
if (viewportWidth != GraphicsDevice.Viewport.Width || viewportHeight != GraphicsDevice.Viewport.Height)
{
viewportWidth = GraphicsDevice.Viewport.Width;
viewportHeight = GraphicsDevice.Viewport.Height;
sideBySideLeftSpriteSize = new Microsoft.Xna.Framework.Rectangle(0, 0, viewportWidth / 2, viewportHeight);
sideBySideRightSpriteSize = new Microsoft.Xna.Framework.Rectangle(viewportWidth / 2, 0, viewportWidth / 2, viewportHeight);
}
}
viewCenter = OculusClient.GetScreenSize().X * 0.25f;
eyeProjectionShift = viewCenter - OculusClient.GetLensSeparationDistance() * 0.5f;
projectionCenterOffset = 4.0f * eyeProjectionShift / OculusClient.GetScreenSize().X;
projCenter = camera.projectionMatrix;
projLeft = Matrix.CreateTranslation(projectionCenterOffset, 0, 0) * projCenter;
projRight = Matrix.CreateTranslation(-projectionCenterOffset, 0, 0) * projCenter;
halfIPD = OculusClient.GetInterpupillaryDistance() * 0.5f;
viewLeft = Matrix.CreateTranslation(halfIPD, 0, 0) * camera.viewMatrix;
viewRight = Matrix.CreateTranslation(-halfIPD, 0, 0) * camera.viewMatrix;
GraphicsDevice.SetRenderTarget(renderTargetLeft);
GraphicsDevice.Clear(Microsoft.Xna.Framework.Color.Black);
View = viewLeft;
Projection = projLeft;
//Here we draw everything
GraphicsDevice.SetRenderTarget(renderTargetRight);
GraphicsDevice.Clear(Microsoft.Xna.Framework.Color.Black);
View = viewRight;
Projection = projRight;
//Here we draw everything AGAIN
GraphicsDevice.SetRenderTarget(null);
renderTextureLeft = (Texture2D)renderTargetLeft;
renderTextureRight = (Texture2D)renderTargetRight;
GraphicsDevice.Clear(Microsoft.Xna.Framework.Color.Black);
//Set the four Distortion params of the oculus
oculusRiftDistortionShader.Parameters["distK0"].SetValue(oculusClient.DistK0);
oculusRiftDistortionShader.Parameters["distK1"].SetValue(oculusClient.DistK1);
oculusRiftDistortionShader.Parameters["distK2"].SetValue(oculusClient.DistK2);
oculusRiftDistortionShader.Parameters["distK3"].SetValue(oculusClient.DistK3);
oculusRiftDistortionShader.Parameters["imageScaleFactor"].SetValue(scaleImageFactor);
oculusRiftDistortionShader.Parameters["drawLeftLens"].SetValue(true);
spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.Opaque, null, null, null, oculusRiftDistortionShader);
spriteBatch.Draw(renderTextureLeft, sideBySideLeftSpriteSize, Microsoft.Xna.Framework.Color.White);
spriteBatch.End();
oculusRiftDistortionShader.Parameters["drawLeftLens"].SetValue(false);
spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.Opaque, null, null, null, oculusRiftDistortionShader);
spriteBatch.Draw(renderTextureRight, sideBySideRightSpriteSize, Microsoft.Xna.Framework.Color.White);
spriteBatch.End();
bool resetOk;
if ((currentState.Buttons.X == ButtonState.Pressed && previousState.Buttons.X != ButtonState.Pressed))
{
resetOk = OculusClient.ResetSensorOrientation(0);
}
03-20-2014 11:10 AM
05-14-2014 05:46 PM
08-04-2014 10:45 AM
08-27-2014 08:26 AM