Skip to content

Commit c417d77

Browse files
committed
WIP - Ability to set the VR camera position. There are some issues still (eg. properly apply the transformation on the Vive controllers matrices) - see PR #4 for more info #4
1 parent a302190 commit c417d77

10 files changed

Lines changed: 258 additions & 0 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,5 @@ example-simple/bin/*
3535
!example-simple/bin/data
3636
example-drawing/bin/*
3737
!example-drawing/bin/data
38+
example-animatedpath/bin/*
39+
!example-animatedpath/bin/data

example-animatedpath/addons.make

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ofxOpenVR

example-animatedpath/bin/data/.gitkeep

Whitespace-only changes.
58.3 KB
Loading

example-animatedpath/icon.rc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Icon Resource Definition
2+
#define MAIN_ICON 102
3+
4+
#if defined(_DEBUG)
5+
MAIN_ICON ICON "icon_debug.ico"
6+
#else
7+
MAIN_ICON ICON "icon.ico"
8+
#endif

example-animatedpath/src/main.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include "ofMain.h"
2+
#include "ofApp.h"
3+
4+
//========================================================================
5+
int main( ){
6+
ofGLWindowSettings settings;
7+
settings.setGLVersion(4, 1);
8+
settings.width = 1280;
9+
settings.height = 720;
10+
ofCreateWindow(settings);
11+
ofRunApp(new ofApp());
12+
}

example-animatedpath/src/ofApp.cpp

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
#include "ofApp.h"
2+
3+
#define STRINGIFY(A) #A
4+
5+
//--------------------------------------------------------------
6+
void ofApp::setup(){
7+
8+
ofSetVerticalSync(false);
9+
10+
// We need to pass the method we want ofxOpenVR to call when rending the scene
11+
_openVR.setup(std::bind(&ofApp::render, this, std::placeholders::_1));
12+
_openVR.setDrawControllers(true);
13+
14+
ofAddListener(_openVR.ofxOpenVRControllerEvent, this, &ofApp::controllerEvent);
15+
16+
_light.setup();
17+
_light.setPosition(0, 0, 0);
18+
_light.enable();
19+
20+
for (int i = 0; i < 50; i++) {
21+
Cube* cube = new Cube();
22+
23+
float size = 2;
24+
25+
ofVec3f pos;
26+
pos.x = ofRandom(-20, 20);
27+
pos.y = ofRandom(-20, 20);
28+
pos.z = ofRandom(-20, 20);
29+
30+
cube->box.set(size);
31+
cube->box.setPosition(pos);
32+
33+
ofFloatColor c;
34+
c.r = ofRandomuf();
35+
c.g = ofRandomuf();
36+
c.b = ofRandomuf();
37+
38+
cube->color.set(c);
39+
40+
_cubes.push_back(cube);
41+
}
42+
}
43+
44+
//--------------------------------------------------------------
45+
void ofApp::exit() {
46+
47+
ofRemoveListener(_openVR.ofxOpenVRControllerEvent, this, &ofApp::controllerEvent);
48+
49+
_openVR.exit();
50+
}
51+
52+
//--------------------------------------------------------------
53+
void ofApp::update(){
54+
55+
_openVR.update();
56+
57+
float t = ofGetElapsedTimef() * 0.3;
58+
59+
_cameraPos.x = cos( t ) * 6;
60+
_cameraPos.z = sin( t ) * 6;
61+
62+
_openVR.applyTranslation(_cameraPos);
63+
}
64+
65+
//--------------------------------------------------------------
66+
void ofApp::draw(){
67+
68+
ofEnableLighting();
69+
ofEnableDepthTest();
70+
71+
_openVR.render();
72+
_openVR.renderDistortion();
73+
74+
ofDisableDepthTest();
75+
_openVR.drawDebugInfo();
76+
}
77+
78+
//--------------------------------------------------------------
79+
void ofApp::render(vr::Hmd_Eye nEye)
80+
{
81+
ofPushView();
82+
ofSetMatrixMode(OF_MATRIX_PROJECTION);
83+
ofLoadMatrix(_openVR.getCurrentProjectionMatrix(nEye));
84+
ofSetMatrixMode(OF_MATRIX_MODELVIEW);
85+
ofMatrix4x4 currentViewMatrixInvertY = _openVR.getCurrentViewMatrix(nEye);
86+
currentViewMatrixInvertY.scale(1.0f, -1.0f, 1.0f);
87+
ofLoadMatrix(currentViewMatrixInvertY);
88+
89+
// boxes in space
90+
for (int i = 0; i < 50; i++){
91+
ofSetColor(_cubes[i]->color);
92+
_cubes[i]->box.draw();
93+
}
94+
95+
ofPopView();
96+
}
97+
98+
//--------------------------------------------------------------
99+
void ofApp::controllerEvent(ofxOpenVRControllerEventArgs& args)
100+
{
101+
cout << "ofApp::controllerEvent > role: " << (int)args.controllerRole << " - event type: " << (int)args.eventType << " - button type: " << (int)args.buttonType << " - x: " << args.analogInput_xAxis << " - y: " << args.analogInput_yAxis << endl;
102+
}
103+
104+
//--------------------------------------------------------------
105+
void ofApp::keyPressed(int key){
106+
107+
_openVR.toggleGrid();
108+
}
109+
110+
//--------------------------------------------------------------
111+
void ofApp::keyReleased(int key){
112+
113+
}
114+
115+
//--------------------------------------------------------------
116+
void ofApp::mouseMoved(int x, int y ){
117+
118+
}
119+
120+
//--------------------------------------------------------------
121+
void ofApp::mouseDragged(int x, int y, int button){
122+
123+
}
124+
125+
//--------------------------------------------------------------
126+
void ofApp::mousePressed(int x, int y, int button){
127+
128+
}
129+
130+
//--------------------------------------------------------------
131+
void ofApp::mouseReleased(int x, int y, int button){
132+
133+
}
134+
135+
//--------------------------------------------------------------
136+
void ofApp::mouseEntered(int x, int y){
137+
138+
}
139+
140+
//--------------------------------------------------------------
141+
void ofApp::mouseExited(int x, int y){
142+
143+
}
144+
145+
//--------------------------------------------------------------
146+
void ofApp::windowResized(int w, int h){
147+
148+
}
149+
150+
//--------------------------------------------------------------
151+
void ofApp::gotMessage(ofMessage msg){
152+
153+
}
154+
155+
//--------------------------------------------------------------
156+
void ofApp::dragEvent(ofDragInfo dragInfo){
157+
158+
}
159+

example-animatedpath/src/ofApp.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#pragma once
2+
3+
#include "ofMain.h"
4+
#include "ofxOpenVR.h"
5+
6+
//--------------------------------------------------------------
7+
class ofApp : public ofBaseApp {
8+
9+
public:
10+
void setup();
11+
void exit();
12+
void update();
13+
void draw();
14+
15+
void keyPressed(int key);
16+
void keyReleased(int key);
17+
void mouseMoved(int x, int y);
18+
void mouseDragged(int x, int y, int button);
19+
void mousePressed(int x, int y, int button);
20+
void mouseReleased(int x, int y, int button);
21+
void mouseEntered(int x, int y);
22+
void mouseExited(int x, int y);
23+
void windowResized(int w, int h);
24+
void dragEvent(ofDragInfo dragInfo);
25+
void gotMessage(ofMessage msg);
26+
27+
void render(vr::Hmd_Eye nEye);
28+
29+
void controllerEvent(ofxOpenVRControllerEventArgs& args);
30+
31+
private:
32+
33+
ofxOpenVR _openVR;
34+
35+
struct Cube
36+
{
37+
ofBoxPrimitive box;
38+
ofFloatColor color;
39+
};
40+
41+
vector<Cube*> _cubes;
42+
43+
ofLight _light;
44+
glm::vec3 _cameraPos;
45+
};

src/ofxOpenVR.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "ofxOpenVR.h"
2+
#include <glm/gtx/matrix_decompose.hpp>
23

34
#define STRINGIFY(A) #A
45

@@ -231,6 +232,34 @@ glm::mat4x4 ofxOpenVR::getCurrentViewMatrix(vr::Hmd_Eye nEye)
231232
return matV;
232233
}
233234

235+
//--------------------------------------------------------------
236+
void ofxOpenVR::applyTranslation(glm::vec3 & vec)
237+
{
238+
// Decompose the HMD's matrix in order to get the rotation component
239+
//glm::vec3 scale;
240+
//glm::quat rotation;
241+
//glm::vec3 translation;
242+
//glm::vec3 skew;
243+
//glm::vec4 perspective;
244+
245+
//glm::decompose(_mat4HMDPose, scale, rotation, translation, skew, perspective);
246+
//rotation = glm::conjugate(rotation);
247+
248+
//// Create a new matrix from the HDM's rotation
249+
//glm::mat4x4 newHmdMatrix = glm::mat4_cast(rotation);
250+
251+
//glm::vec3 hdmTrans = translation + vec;
252+
253+
// Apply the camera's translation
254+
glm::mat4x4 newHmdMatrix = glm::translate(_mat4HMDPose, vec);
255+
256+
//glm::vec3 contTrans = glm::vec3(-vec.z, -vec.x, .0f);
257+
_mat4LeftControllerPose = glm::translate(_mat4LeftControllerPose, -vec);
258+
_mat4RightControllerPose = glm::translate(_mat4RightControllerPose, -vec);
259+
260+
_mat4HMDPose = newHmdMatrix;
261+
}
262+
234263
//--------------------------------------------------------------
235264
glm::mat4x4 ofxOpenVR::getControllerPose(vr::ETrackedControllerRole nController)
236265
{

src/ofxOpenVR.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ class ofxOpenVR {
6464
glm::mat4x4 getCurrentProjectionMatrix(vr::Hmd_Eye nEye);
6565
glm::mat4x4 getCurrentViewMatrix(vr::Hmd_Eye nEye);
6666

67+
void applyTranslation(glm::vec3 & vec);
68+
6769
glm::mat4x4 getControllerPose(vr::ETrackedControllerRole nController);
6870
bool isControllerConnected(vr::ETrackedControllerRole nController);
6971

0 commit comments

Comments
 (0)