Skip to content

Commit de881bd

Browse files
committed
added initial implementations of VRSelector and VRCamera
1 parent e78c17e commit de881bd

File tree

9 files changed

+214
-10
lines changed

9 files changed

+214
-10
lines changed

debug/apps/vrcube/src/main/java/vrcube/MainActivity.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
import android.os.Bundle;
44

5-
import processing.vr.PVR;
5+
import processing.vr.VRActivity;
66
import processing.core.PApplet;
77

8-
public class MainActivity extends PVR {
8+
public class MainActivity extends VRActivity {
99
@Override
1010
public void onCreate(Bundle savedInstanceState) {
1111
super.onCreate(savedInstanceState);
Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,68 @@
11
package vrcube;
22

33
import processing.core.PApplet;
4-
import processing.vr.*;
4+
import processing.vr.*;
55

66
public class Sketch extends PApplet {
7+
float boxSize = 140;
8+
VRCamera vrcam;
9+
VRSelector vrsel;
710

11+
public void settings() {
12+
fullScreen(VR);
13+
}
14+
15+
public void setup() {
16+
vrcam = new VRCamera(this);
17+
vrsel = new VRSelector(this);
18+
}
19+
20+
public void draw() {
21+
vrsel.update();
22+
background(120);
23+
translate(width/2, height/2);
24+
lights();
25+
drawGrid();
26+
drawAim();
27+
}
28+
29+
void drawGrid() {
30+
for (int i = 0; i < 4; i++) {
31+
for (int j = 0; j < 4; j++) {
32+
float x = map(i, 0, 3, -350, +350);
33+
float y = map(j, 0, 3, -350, +350);
34+
pushMatrix();
35+
translate(x, y);
36+
rotateY(millis()/1000.0f);
37+
if (vrsel.hit(boxSize)) {
38+
strokeWeight(5);
39+
stroke(0xFF2FB1EA);
40+
if (mousePressed) {
41+
fill(0xFF2FB1EA);
42+
} else {
43+
fill(0xFFE3993E);
44+
}
45+
} else {
46+
noStroke();
47+
fill(0xFFE3993E);
48+
}
49+
box(boxSize);
50+
popMatrix();
51+
}
52+
}
53+
}
54+
55+
void drawAim() {
56+
vrcam.begin();
57+
stroke(47, 177, 234, 150);
58+
strokeWeight(50);
59+
point(0, 0, 100);
60+
vrcam.end();
61+
}
62+
63+
64+
65+
/*
866
public void settings() {
967
fullScreen(STEREO);
1068
}
@@ -14,9 +72,10 @@ public void setup() { }
1472
public void draw() {
1573
background(157);
1674
lights();
17-
translate(width/2, height/2);
75+
translate(width / 2, height / 2);
1876
rotateX(frameCount * 0.01f);
19-
rotateY(frameCount * 0.01f);
77+
rotateY(frameCount * 0.01f);
2078
box(350);
2179
}
80+
*/
2281
}

mode/libraries/vr/src/processing/vr/VRActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/*
44
Part of the Processing project - http://processing.org
55
6-
Copyright (c) 2016 The Processing Foundation
6+
Copyright (c) 2016-19 The Processing Foundation
77
88
This library is free software; you can redistribute it and/or
99
modify it under the terms of the GNU Lesser General Public
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package processing.vr;
2+
3+
import processing.core.PApplet;
4+
5+
public class VRCamera {
6+
protected PApplet parent;
7+
8+
public VRCamera(PApplet parent) {
9+
this.parent = parent;
10+
}
11+
12+
public void begin() {
13+
parent.pushMatrix();
14+
parent.eye();
15+
}
16+
17+
public void end() {
18+
parent.popMatrix();
19+
}
20+
}

mode/libraries/vr/src/processing/vr/VRGraphics.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/*
44
Part of the Processing project - http://processing.org
55
6-
Copyright (c) 2016 The Processing Foundation
6+
Copyright (c) 2016-19 The Processing Foundation
77
88
This library is free software; you can redistribute it and/or
99
modify it under the terms of the GNU Lesser General Public

mode/libraries/vr/src/processing/vr/VRGraphicsMono.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/*
44
Part of the Processing project - http://processing.org
55
6-
Copyright (c) 2016 The Processing Foundation
6+
Copyright (c) 2016-19 The Processing Foundation
77
88
This library is free software; you can redistribute it and/or
99
modify it under the terms of the GNU Lesser General Public

mode/libraries/vr/src/processing/vr/VRGraphicsStereo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/*
44
Part of the Processing project - http://processing.org
55
6-
Copyright (c) 2016 The Processing Foundation
6+
Copyright (c) 2016-19 The Processing Foundation
77
88
This library is free software; you can redistribute it and/or
99
modify it under the terms of the GNU Lesser General Public
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2+
3+
/*
4+
Part of the Processing project - http://processing.org
5+
6+
Copyright (c) 2019 The Processing Foundation
7+
8+
This library is free software; you can redistribute it and/or
9+
modify it under the terms of the GNU Lesser General Public
10+
License as published by the Free Software Foundation, version 2.1.
11+
12+
This library is distributed in the hope that it will be useful,
13+
but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
Lesser General Public License for more details.
16+
17+
You should have received a copy of the GNU Lesser General
18+
Public License along with this library; if not, write to the
19+
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20+
Boston, MA 02111-1307 USA
21+
*/
22+
23+
package processing.vr;
24+
25+
import processing.core.PApplet;
26+
import processing.core.PMatrix3D;
27+
import processing.core.PVector;
28+
29+
public class VRSelector {
30+
protected PApplet parent;
31+
32+
protected PVector dir = new PVector();
33+
protected PVector cam = new PVector();
34+
35+
protected PMatrix3D eyeMat = new PMatrix3D();
36+
protected PMatrix3D objMat = new PMatrix3D();
37+
38+
protected PVector front = new PVector();
39+
protected PVector objCam = new PVector();
40+
protected PVector objFront = new PVector();
41+
protected PVector objDir = new PVector();
42+
43+
protected PVector hit = new PVector();
44+
45+
public VRSelector(PApplet parent) {
46+
this.parent = parent;
47+
}
48+
49+
public void update() {
50+
parent.getEyeMatrix(eyeMat);
51+
cam.set(eyeMat.m03, eyeMat.m13, eyeMat.m23);
52+
dir.set(eyeMat.m02, eyeMat.m12, eyeMat.m22);
53+
PVector.add(cam, dir, front);
54+
}
55+
56+
public boolean hit(PMatrix3D mat, float boxSize) {
57+
objMat.set(mat);
58+
return hitImpl(boxSize);
59+
}
60+
61+
public boolean hit(float boxSize) {
62+
parent.getObjectMatrix(objMat);
63+
return hitImpl(boxSize);
64+
}
65+
66+
protected boolean hitImpl(float boxSize) {
67+
objMat.mult(cam, objCam);
68+
objMat.mult(front, objFront);
69+
PVector.sub(objFront, objCam, objDir);
70+
PVector boxMin = new PVector(-boxSize/2, -boxSize/2, -boxSize/2);
71+
PVector boxMax = new PVector(+boxSize/2, +boxSize/2, +boxSize/2);
72+
return intersectsLine(objCam, objDir, boxMin, boxMax, 0, 1000, hit);
73+
}
74+
75+
protected boolean intersectsLine(PVector orig, PVector dir,
76+
PVector minPos, PVector maxPos, float minDist, float maxDist, PVector hit) {
77+
PVector bbox;
78+
PVector invDir = new PVector(1/dir.x, 1/dir.y, 1/dir.z);
79+
80+
boolean signDirX = invDir.x < 0;
81+
boolean signDirY = invDir.y < 0;
82+
boolean signDirZ = invDir.z < 0;
83+
84+
bbox = signDirX ? maxPos : minPos;
85+
float txmin = (bbox.x - orig.x) * invDir.x;
86+
bbox = signDirX ? minPos : maxPos;
87+
float txmax = (bbox.x - orig.x) * invDir.x;
88+
bbox = signDirY ? maxPos : minPos;
89+
float tymin = (bbox.y - orig.y) * invDir.y;
90+
bbox = signDirY ? minPos : maxPos;
91+
float tymax = (bbox.y - orig.y) * invDir.y;
92+
93+
if ((txmin > tymax) || (tymin > txmax)) {
94+
return false;
95+
}
96+
if (tymin > txmin) {
97+
txmin = tymin;
98+
}
99+
if (tymax < txmax) {
100+
txmax = tymax;
101+
}
102+
103+
bbox = signDirZ ? maxPos : minPos;
104+
float tzmin = (bbox.z - orig.z) * invDir.z;
105+
bbox = signDirZ ? minPos : maxPos;
106+
float tzmax = (bbox.z - orig.z) * invDir.z;
107+
108+
if ((txmin > tzmax) || (tzmin > txmax)) {
109+
return false;
110+
}
111+
if (tzmin > txmin) {
112+
txmin = tzmin;
113+
}
114+
if (tzmax < txmax) {
115+
txmax = tzmax;
116+
}
117+
if ((txmin < maxDist) && (txmax > minDist)) {
118+
hit.x = orig.x + txmin * dir.x;
119+
hit.y = orig.y + txmin * dir.y;
120+
hit.z = orig.z + txmin * dir.z;
121+
return true;
122+
}
123+
return false;
124+
}
125+
}

mode/libraries/vr/src/processing/vr/VRSurface.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/*
44
Part of the Processing project - http://processing.org
55
6-
Copyright (c) 2016 The Processing Foundation
6+
Copyright (c) 2016-19 The Processing Foundation
77
88
This library is free software; you can redistribute it and/or
99
modify it under the terms of the GNU Lesser General Public

0 commit comments

Comments
 (0)