Skip to content

Commit

Permalink
added static functions to get final position & rotation of headset (w…
Browse files Browse the repository at this point in the history
…hich will be compatible with non-VR location). removed reliance on TempVars.
  • Loading branch information
phr00t committed Jul 10, 2015
1 parent ba4a0be commit 5e160f4
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 18 deletions.
26 changes: 23 additions & 3 deletions src/jmevr/app/VRApplication.java
Expand Up @@ -9,6 +9,8 @@
import com.jme3.input.KeyInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.queue.RenderQueue;
import com.jme3.renderer.queue.RenderQueue.Bucket;
Expand All @@ -29,7 +31,7 @@ public class VRApplication extends SimpleApplication{

private static OpenVR VRhardware;
private static OpenVRViewManager VRappstate;
private static VRGuiNode primaryGuiNode;
private static VRApplication mainApp;
private static Spatial observer;
private static final ArrayList<VRInput> VRinput = new ArrayList<>();

Expand All @@ -54,7 +56,7 @@ public void simpleRender(RenderManager renderManager) {
public VRApplication() {
super();
guiNode = new VRGuiNode();
primaryGuiNode = (VRGuiNode)guiNode;
mainApp = this;

// we are going to use OpenVR now, not the Oculus Rift
// OpenVR does support the Rift
Expand Down Expand Up @@ -87,7 +89,7 @@ private static void initVRinput() {
}

public static VRGuiNode getVRGuiNode(){
return primaryGuiNode;
return (VRGuiNode)mainApp.guiNode;
}

public static Spatial getObserver() {
Expand All @@ -98,6 +100,24 @@ public static void setObserver(Spatial observer) {
VRApplication.observer = observer;
}

public static Quaternion getFinalOberserverRotation() {
if( VRappstate == null ) {
if( VRApplication.observer == null ) {
return mainApp.getCamera().getRotation();
} else return VRApplication.observer.getWorldRotation();
}
return VRappstate.getFinalRotation();
}

public static Vector3f getFinalOberserverPosition() {
if( VRappstate == null ) {
if( VRApplication.observer == null ) {
return mainApp.getCamera().getLocation();
} else return VRApplication.observer.getWorldTranslation();
}
return VRappstate.getFinalPosition();
}

@Override
public void simpleInitApp() {
// run this function before OVRAppState gets initialized to force
Expand Down
39 changes: 25 additions & 14 deletions src/jmevr/state/OpenVRViewManager.java
Expand Up @@ -10,6 +10,7 @@
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Matrix4f;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector2f;
import com.jme3.math.Vector3f;
import com.jme3.post.Filter;
Expand Down Expand Up @@ -147,6 +148,10 @@ private void setupVRScene(){
app.getViewPort().attachScene(distortionScene);
}

//final & temp values for camera calculations
private final Matrix4f tempMat = new Matrix4f();
private final Vector3f finalPosition = new Vector3f();
private final Quaternion finalRotation = new Quaternion();

@Override
public void update(float tpf) {
Expand All @@ -163,22 +168,20 @@ public void update(float tpf) {
VRApplication.getVRHardware().updatePose();
Matrix4f posAndRot = ((OpenVR)VRApplication.getVRHardware()).getPositionAndOrientation();
// combine the two for each eye
TempVars tempVars = TempVars.get();
// left eye
tempVars.tempMat4.set(leftMatrix);
tempVars.tempMat4.multLocal(posAndRot);
tempVars.tempMat4.multLocal(transformMatrix);
tempVars.tempMat4.toTranslationVector(tempVars.vect1);
tempVars.tempMat4.toRotationQuat(tempVars.quat1);
camLeft.setFrame(tempVars.vect1, tempVars.quat1);
tempMat.set(leftMatrix);
tempMat.multLocal(posAndRot);
tempMat.multLocal(transformMatrix);
tempMat.toTranslationVector(finalPosition);
tempMat.toRotationQuat(finalRotation);
camLeft.setFrame(finalPosition, finalRotation);
// right eye
tempVars.tempMat4.set(rightMatrix);
tempVars.tempMat4.multLocal(posAndRot);
tempVars.tempMat4.multLocal(transformMatrix);
tempVars.tempMat4.toTranslationVector(tempVars.vect1);
tempVars.tempMat4.toRotationQuat(tempVars.quat1);
camRight.setFrame(tempVars.vect1, tempVars.quat1);
tempVars.release();
tempMat.set(rightMatrix);
tempMat.multLocal(posAndRot);
tempMat.multLocal(transformMatrix);
tempMat.toTranslationVector(finalPosition);
tempMat.toRotationQuat(finalRotation);
camRight.setFrame(finalPosition, finalRotation);

// update GUI position?
VRGuiNode vrgn = VRApplication.getVRGuiNode();
Expand All @@ -187,6 +190,14 @@ public void update(float tpf) {
}
}

public Vector3f getFinalPosition() {
return finalPosition;
}

public Quaternion getFinalRotation() {
return finalRotation;
}

private void setupCamerasAndViews() {
camLeft = app.getCamera().clone();
float origWidth = camLeft.getWidth();
Expand Down
2 changes: 1 addition & 1 deletion src/jmevr/util/MeshUtil.java
Expand Up @@ -32,7 +32,7 @@ public static Mesh setupDistortionMesh(int eye) {
float texcoordB[] = new float[(int) (m_iLensGridSegmentCountV * m_iLensGridSegmentCountH) * 2];

int vertPos = 0, coordPos = 0;

float Xoffset = eye == 0 ? -1f : 0;
for (int y = 0; y < m_iLensGridSegmentCountV; y++) {
for (int x = 0; x < m_iLensGridSegmentCountH; x++) {
Expand Down

0 comments on commit 5e160f4

Please sign in to comment.