Skip to content

Commit

Permalink
added initial implementations of VRSelector and VRCamera
Browse files Browse the repository at this point in the history
  • Loading branch information
codeanticode committed Jul 1, 2019
1 parent e78c17e commit de881bd
Show file tree
Hide file tree
Showing 9 changed files with 214 additions and 10 deletions.
4 changes: 2 additions & 2 deletions debug/apps/vrcube/src/main/java/vrcube/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import android.os.Bundle;

import processing.vr.PVR;
import processing.vr.VRActivity;
import processing.core.PApplet;

public class MainActivity extends PVR {
public class MainActivity extends VRActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand Down
65 changes: 62 additions & 3 deletions debug/apps/vrcube/src/main/java/vrcube/Sketch.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,68 @@
package vrcube;

import processing.core.PApplet;
import processing.vr.*;
import processing.vr.*;

public class Sketch extends PApplet {
float boxSize = 140;
VRCamera vrcam;
VRSelector vrsel;

public void settings() {
fullScreen(VR);
}

public void setup() {
vrcam = new VRCamera(this);
vrsel = new VRSelector(this);
}

public void draw() {
vrsel.update();
background(120);
translate(width/2, height/2);
lights();
drawGrid();
drawAim();
}

void drawGrid() {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
float x = map(i, 0, 3, -350, +350);
float y = map(j, 0, 3, -350, +350);
pushMatrix();
translate(x, y);
rotateY(millis()/1000.0f);
if (vrsel.hit(boxSize)) {
strokeWeight(5);
stroke(0xFF2FB1EA);
if (mousePressed) {
fill(0xFF2FB1EA);
} else {
fill(0xFFE3993E);
}
} else {
noStroke();
fill(0xFFE3993E);
}
box(boxSize);
popMatrix();
}
}
}

void drawAim() {
vrcam.begin();
stroke(47, 177, 234, 150);
strokeWeight(50);
point(0, 0, 100);
vrcam.end();
}



/*
public void settings() {
fullScreen(STEREO);
}
Expand All @@ -14,9 +72,10 @@ public void setup() { }
public void draw() {
background(157);
lights();
translate(width/2, height/2);
translate(width / 2, height / 2);
rotateX(frameCount * 0.01f);
rotateY(frameCount * 0.01f);
rotateY(frameCount * 0.01f);
box(350);
}
*/
}
2 changes: 1 addition & 1 deletion mode/libraries/vr/src/processing/vr/VRActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/*
Part of the Processing project - http://processing.org
Copyright (c) 2016 The Processing Foundation
Copyright (c) 2016-19 The Processing Foundation
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
Expand Down
20 changes: 20 additions & 0 deletions mode/libraries/vr/src/processing/vr/VRCamera.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package processing.vr;

import processing.core.PApplet;

public class VRCamera {
protected PApplet parent;

public VRCamera(PApplet parent) {
this.parent = parent;
}

public void begin() {
parent.pushMatrix();
parent.eye();
}

public void end() {
parent.popMatrix();
}
}
2 changes: 1 addition & 1 deletion mode/libraries/vr/src/processing/vr/VRGraphics.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/*
Part of the Processing project - http://processing.org
Copyright (c) 2016 The Processing Foundation
Copyright (c) 2016-19 The Processing Foundation
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
Expand Down
2 changes: 1 addition & 1 deletion mode/libraries/vr/src/processing/vr/VRGraphicsMono.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/*
Part of the Processing project - http://processing.org
Copyright (c) 2016 The Processing Foundation
Copyright (c) 2016-19 The Processing Foundation
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
Expand Down
2 changes: 1 addition & 1 deletion mode/libraries/vr/src/processing/vr/VRGraphicsStereo.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/*
Part of the Processing project - http://processing.org
Copyright (c) 2016 The Processing Foundation
Copyright (c) 2016-19 The Processing Foundation
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
Expand Down
125 changes: 125 additions & 0 deletions mode/libraries/vr/src/processing/vr/VRSelector.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */

/*
Part of the Processing project - http://processing.org
Copyright (c) 2019 The Processing Foundation
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation, version 2.1.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
Boston, MA 02111-1307 USA
*/

package processing.vr;

import processing.core.PApplet;
import processing.core.PMatrix3D;
import processing.core.PVector;

public class VRSelector {
protected PApplet parent;

protected PVector dir = new PVector();
protected PVector cam = new PVector();

protected PMatrix3D eyeMat = new PMatrix3D();
protected PMatrix3D objMat = new PMatrix3D();

protected PVector front = new PVector();
protected PVector objCam = new PVector();
protected PVector objFront = new PVector();
protected PVector objDir = new PVector();

protected PVector hit = new PVector();

public VRSelector(PApplet parent) {
this.parent = parent;
}

public void update() {
parent.getEyeMatrix(eyeMat);
cam.set(eyeMat.m03, eyeMat.m13, eyeMat.m23);
dir.set(eyeMat.m02, eyeMat.m12, eyeMat.m22);
PVector.add(cam, dir, front);
}

public boolean hit(PMatrix3D mat, float boxSize) {
objMat.set(mat);
return hitImpl(boxSize);
}

public boolean hit(float boxSize) {
parent.getObjectMatrix(objMat);
return hitImpl(boxSize);
}

protected boolean hitImpl(float boxSize) {
objMat.mult(cam, objCam);
objMat.mult(front, objFront);
PVector.sub(objFront, objCam, objDir);
PVector boxMin = new PVector(-boxSize/2, -boxSize/2, -boxSize/2);
PVector boxMax = new PVector(+boxSize/2, +boxSize/2, +boxSize/2);
return intersectsLine(objCam, objDir, boxMin, boxMax, 0, 1000, hit);
}

protected boolean intersectsLine(PVector orig, PVector dir,
PVector minPos, PVector maxPos, float minDist, float maxDist, PVector hit) {
PVector bbox;
PVector invDir = new PVector(1/dir.x, 1/dir.y, 1/dir.z);

boolean signDirX = invDir.x < 0;
boolean signDirY = invDir.y < 0;
boolean signDirZ = invDir.z < 0;

bbox = signDirX ? maxPos : minPos;
float txmin = (bbox.x - orig.x) * invDir.x;
bbox = signDirX ? minPos : maxPos;
float txmax = (bbox.x - orig.x) * invDir.x;
bbox = signDirY ? maxPos : minPos;
float tymin = (bbox.y - orig.y) * invDir.y;
bbox = signDirY ? minPos : maxPos;
float tymax = (bbox.y - orig.y) * invDir.y;

if ((txmin > tymax) || (tymin > txmax)) {
return false;
}
if (tymin > txmin) {
txmin = tymin;
}
if (tymax < txmax) {
txmax = tymax;
}

bbox = signDirZ ? maxPos : minPos;
float tzmin = (bbox.z - orig.z) * invDir.z;
bbox = signDirZ ? minPos : maxPos;
float tzmax = (bbox.z - orig.z) * invDir.z;

if ((txmin > tzmax) || (tzmin > txmax)) {
return false;
}
if (tzmin > txmin) {
txmin = tzmin;
}
if (tzmax < txmax) {
txmax = tzmax;
}
if ((txmin < maxDist) && (txmax > minDist)) {
hit.x = orig.x + txmin * dir.x;
hit.y = orig.y + txmin * dir.y;
hit.z = orig.z + txmin * dir.z;
return true;
}
return false;
}
}
2 changes: 1 addition & 1 deletion mode/libraries/vr/src/processing/vr/VRSurface.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/*
Part of the Processing project - http://processing.org
Copyright (c) 2016 The Processing Foundation
Copyright (c) 2016-19 The Processing Foundation
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
Expand Down

0 comments on commit de881bd

Please sign in to comment.