Skip to content

Commit e8d912a

Browse files
committed
added setNear/Far to VRCamera class
1 parent 799d8f7 commit e8d912a

File tree

2 files changed

+160
-4
lines changed

2 files changed

+160
-4
lines changed

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

Lines changed: 104 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
package vrcube;
22

33
import processing.core.PApplet;
4+
import processing.core.PMatrix3D;
5+
import processing.core.PVector;
46
import processing.vr.*;
57

68
public class Sketch extends PApplet {
79
float boxSize = 140;
810
VRCamera vrcam;
9-
VRSelector vrsel;
11+
Selector vrsel;
1012

1113
public void settings() {
1214
fullScreen(VR);
1315
}
1416

1517
public void setup() {
1618
vrcam = new VRCamera(this);
17-
vrsel = new VRSelector(this);
19+
vrsel = new Selector(this);
20+
// vrcam.setNear(1000);
21+
// vrcam.setFar(1100);
1822
}
1923

2024
public void draw() {
@@ -53,14 +57,110 @@ void drawGrid() {
5357
}
5458

5559
void drawAim() {
56-
vrcam.begin();
60+
vrcam.sticky();
5761
stroke(47, 177, 234, 150);
5862
strokeWeight(50);
5963
point(0, 0, 100);
60-
vrcam.end();
64+
vrcam.noSticky();
6165
}
6266

67+
class Selector {
68+
protected PApplet parent;
6369

70+
protected PVector dir = new PVector();
71+
protected PVector cam = new PVector();
72+
73+
protected PMatrix3D eyeMat = new PMatrix3D();
74+
protected PMatrix3D objMat = new PMatrix3D();
75+
76+
protected PVector front = new PVector();
77+
protected PVector objCam = new PVector();
78+
protected PVector objFront = new PVector();
79+
protected PVector objDir = new PVector();
80+
81+
protected PVector hit = new PVector();
82+
83+
public Selector(PApplet parent) {
84+
this.parent = parent;
85+
}
86+
87+
public void update() {
88+
parent.getEyeMatrix(eyeMat);
89+
cam.set(eyeMat.m03, eyeMat.m13, eyeMat.m23);
90+
dir.set(eyeMat.m02, eyeMat.m12, eyeMat.m22);
91+
PVector.add(cam, dir, front);
92+
}
93+
94+
public boolean hit(PMatrix3D mat, float boxSize) {
95+
objMat.set(mat);
96+
return hitImpl(boxSize);
97+
}
98+
99+
public boolean hit(float boxSize) {
100+
parent.getObjectMatrix(objMat);
101+
return hitImpl(boxSize);
102+
}
103+
104+
protected boolean hitImpl(float boxSize) {
105+
objMat.mult(cam, objCam);
106+
objMat.mult(front, objFront);
107+
PVector.sub(objFront, objCam, objDir);
108+
PVector boxMin = new PVector(-boxSize/2, -boxSize/2, -boxSize/2);
109+
PVector boxMax = new PVector(+boxSize/2, +boxSize/2, +boxSize/2);
110+
return intersectsLine(objCam, objDir, boxMin, boxMax, 0, 1000, hit);
111+
}
112+
113+
protected boolean intersectsLine(PVector orig, PVector dir,
114+
PVector minPos, PVector maxPos, float minDist, float maxDist, PVector hit) {
115+
PVector bbox;
116+
PVector invDir = new PVector(1/dir.x, 1/dir.y, 1/dir.z);
117+
118+
boolean signDirX = invDir.x < 0;
119+
boolean signDirY = invDir.y < 0;
120+
boolean signDirZ = invDir.z < 0;
121+
122+
bbox = signDirX ? maxPos : minPos;
123+
float txmin = (bbox.x - orig.x) * invDir.x;
124+
bbox = signDirX ? minPos : maxPos;
125+
float txmax = (bbox.x - orig.x) * invDir.x;
126+
bbox = signDirY ? maxPos : minPos;
127+
float tymin = (bbox.y - orig.y) * invDir.y;
128+
bbox = signDirY ? minPos : maxPos;
129+
float tymax = (bbox.y - orig.y) * invDir.y;
130+
131+
if ((txmin > tymax) || (tymin > txmax)) {
132+
return false;
133+
}
134+
if (tymin > txmin) {
135+
txmin = tymin;
136+
}
137+
if (tymax < txmax) {
138+
txmax = tymax;
139+
}
140+
141+
bbox = signDirZ ? maxPos : minPos;
142+
float tzmin = (bbox.z - orig.z) * invDir.z;
143+
bbox = signDirZ ? minPos : maxPos;
144+
float tzmax = (bbox.z - orig.z) * invDir.z;
145+
146+
if ((txmin > tzmax) || (tzmin > txmax)) {
147+
return false;
148+
}
149+
if (tzmin > txmin) {
150+
txmin = tzmin;
151+
}
152+
if (tzmax < txmax) {
153+
txmax = tzmax;
154+
}
155+
if ((txmin < maxDist) && (txmax > minDist)) {
156+
hit.x = orig.x + txmin * dir.x;
157+
hit.y = orig.y + txmin * dir.y;
158+
hit.z = orig.z + txmin * dir.z;
159+
return true;
160+
}
161+
return false;
162+
}
163+
}
64164

65165
/*
66166
public void settings() {
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
27+
public class VRCamera {
28+
protected PApplet parent;
29+
protected VRGraphics graphics;
30+
31+
public VRCamera(PApplet parent) {
32+
if (parent.g instanceof VRGraphics) {
33+
this.parent = parent;
34+
this.graphics = (VRGraphics)(parent.g);
35+
} else {
36+
System.err.println("The VR camera can only be created when the VR renderer is in use");
37+
}
38+
}
39+
40+
public void sticky() {
41+
parent.pushMatrix();
42+
parent.eye();
43+
}
44+
45+
public void noSticky() {
46+
parent.popMatrix();
47+
}
48+
49+
public void setNear(float near) {
50+
graphics.defCameraNear = near;
51+
}
52+
53+
public void setFar(float far) {
54+
graphics.defCameraFar = far;
55+
}
56+
}

0 commit comments

Comments
 (0)