diff --git a/example-animatedpath/addons.make b/example-animatedpath/addons.make new file mode 100644 index 0000000..131d951 --- /dev/null +++ b/example-animatedpath/addons.make @@ -0,0 +1 @@ +ofxOpenVR diff --git a/example-animatedpath/bin/data/.gitkeep b/example-animatedpath/bin/data/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/example-animatedpath/bin/data/of.png b/example-animatedpath/bin/data/of.png new file mode 100644 index 0000000..9c88d67 Binary files /dev/null and b/example-animatedpath/bin/data/of.png differ diff --git a/example-animatedpath/icon.rc b/example-animatedpath/icon.rc new file mode 100644 index 0000000..7e26eb3 --- /dev/null +++ b/example-animatedpath/icon.rc @@ -0,0 +1,8 @@ +// Icon Resource Definition +#define MAIN_ICON 102 + +#if defined(_DEBUG) +MAIN_ICON ICON "icon_debug.ico" +#else +MAIN_ICON ICON "icon.ico" +#endif diff --git a/example-animatedpath/src/main.cpp b/example-animatedpath/src/main.cpp new file mode 100644 index 0000000..64b1275 --- /dev/null +++ b/example-animatedpath/src/main.cpp @@ -0,0 +1,12 @@ +#include "ofMain.h" +#include "ofApp.h" + +//======================================================================== +int main( ){ + ofGLWindowSettings settings; + settings.setGLVersion(4, 1); + settings.width = 1280; + settings.height = 720; + ofCreateWindow(settings); + ofRunApp(new ofApp()); +} diff --git a/example-animatedpath/src/ofApp.cpp b/example-animatedpath/src/ofApp.cpp new file mode 100644 index 0000000..d1db4c7 --- /dev/null +++ b/example-animatedpath/src/ofApp.cpp @@ -0,0 +1,214 @@ +#include "ofApp.h" + +#define STRINGIFY(A) #A + +//-------------------------------------------------------------- +void ofApp::setup(){ + + ofSetVerticalSync(false); + + // We need to pass the method we want ofxOpenVR to call when rending the scene + _openVR.setup(std::bind(&ofApp::render, this, std::placeholders::_1)); + _openVR.setDrawControllers(true); + + ofAddListener(_openVR.ofxOpenVRControllerEvent, this, &ofApp::controllerEvent); + + _light.setup(); + _light.setPosition(0, 0, 0); + _light.enable(); + + // Vertex shader source + string vertex; + + vertex = "#version 150\n"; + vertex += STRINGIFY( + uniform mat4 matrix; + + in vec4 position; + in vec2 texcoord; + + out vec2 texCoordVarying; + + void main() + { + texCoordVarying = texcoord; + gl_Position = matrix * position; + + } + ); + + // Fragment shader source + string fragment = "#version 150\n"; + fragment += STRINGIFY( + uniform sampler2DRect baseTex; + + in vec2 texCoordVarying; + + out vec4 fragColor; + + vec2 texcoord0 = texCoordVarying; + + void main() { + vec2 tx = texcoord0; + tx.y = 256.0 - tx.y; + vec4 image = texture(baseTex, tx); + fragColor = image; + } + ); + + // Shader + _shader.setupShaderFromSource(GL_VERTEX_SHADER, vertex); + _shader.setupShaderFromSource(GL_FRAGMENT_SHADER, fragment); + _shader.bindDefaults(); + _shader.linkProgram(); +} + +//-------------------------------------------------------------- +void ofApp::exit() { + + ofRemoveListener(_openVR.ofxOpenVRControllerEvent, this, &ofApp::controllerEvent); + + _openVR.exit(); +} + +//-------------------------------------------------------------- +void ofApp::update(){ + + _openVR.update(); + + float t = ofGetElapsedTimef() * 0.3; + + _cameraPos.x = cos( t ) * 100; + _cameraPos.z = sin( t ) * 100; + + ofMatrix4x4 hmdMatrix = _openVR.getCurrentHMDMatrix(); + ofQuaternion hmdRotation = hmdMatrix.getRotate(); + + ofMatrix4x4 newHmdMatrix; + newHmdMatrix = newHmdMatrix.newTranslationMatrix( -_cameraPos); + newHmdMatrix.rotate(hmdRotation); + + _openVR.setCurrentHMDMatrix(newHmdMatrix); +} + +//-------------------------------------------------------------- +void ofApp::draw(){ + + ofEnableLighting(); + ofEnableDepthTest(); + + _openVR.render(); + + _openVR.renderDistortion(); + + _openVR.drawDebugInfo(); +} + +//-------------------------------------------------------------- +void ofApp::render(vr::Hmd_Eye nEye) +{ + + ofMatrix4x4 currentViewMatrix = _openVR.getCurrentViewMatrix(nEye); + ofMatrix4x4 currentViewProjectionMatrix = _openVR.getCurrentViewProjectionMatrix(nEye); + ofMatrix4x4 hdmPoseMat = currentViewProjectionMatrix; + + ofPushMatrix(); + + ofMatrix4x4 currentProjMatrix = _openVR.getCurrentProjectionMatrix(nEye); + currentProjMatrix.scale(1.0, -1.0, 1.0); + + ofSetMatrixMode(OF_MATRIX_PROJECTION); + ofLoadMatrix(currentProjMatrix); + + ofSetMatrixMode(OF_MATRIX_MODELVIEW); + ofLoadMatrix(_openVR.getCurrentViewMatrix(nEye)); + + // boxes in space + + ofSeedRandom(0); + + for (int i = 0; i < 100; i++){ + + ofVec3f pos; + pos.x = ofRandom(-200, 200); + pos.y = ofRandom(-200, 200); + pos.z = ofRandom(-200, 200); + + float size = ofRandom(2, 20); + _box.set(size); + + ofFloatColor c; + c.r = ofRandomuf(); + c.g = ofRandomuf(); + c.b = ofRandomuf(); + + ofSetColor(c); + _box.setPosition(pos); + _box.draw(); + } + + ofPopMatrix(); +} + +//-------------------------------------------------------------- +void ofApp::controllerEvent(ofxOpenVRControllerEventArgs& args) +{ + 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; +} + +//-------------------------------------------------------------- +void ofApp::keyPressed(int key){ + + _openVR.toggleGrid(); +} + +//-------------------------------------------------------------- +void ofApp::keyReleased(int key){ + +} + +//-------------------------------------------------------------- +void ofApp::mouseMoved(int x, int y ){ + +} + +//-------------------------------------------------------------- +void ofApp::mouseDragged(int x, int y, int button){ + +} + +//-------------------------------------------------------------- +void ofApp::mousePressed(int x, int y, int button){ + +} + +//-------------------------------------------------------------- +void ofApp::mouseReleased(int x, int y, int button){ + +} + +//-------------------------------------------------------------- +void ofApp::mouseEntered(int x, int y){ + +} + +//-------------------------------------------------------------- +void ofApp::mouseExited(int x, int y){ + +} + +//-------------------------------------------------------------- +void ofApp::windowResized(int w, int h){ + +} + +//-------------------------------------------------------------- +void ofApp::gotMessage(ofMessage msg){ + +} + +//-------------------------------------------------------------- +void ofApp::dragEvent(ofDragInfo dragInfo){ + +} + diff --git a/example-animatedpath/src/ofApp.h b/example-animatedpath/src/ofApp.h new file mode 100644 index 0000000..71744d5 --- /dev/null +++ b/example-animatedpath/src/ofApp.h @@ -0,0 +1,40 @@ +#pragma once + +#include "ofMain.h" +#include "ofxOpenVR.h" + +//-------------------------------------------------------------- +class ofApp : public ofBaseApp { + +public: + void setup(); + void exit(); + void update(); + void draw(); + + void keyPressed(int key); + void keyReleased(int key); + void mouseMoved(int x, int y); + void mouseDragged(int x, int y, int button); + void mousePressed(int x, int y, int button); + void mouseReleased(int x, int y, int button); + void mouseEntered(int x, int y); + void mouseExited(int x, int y); + void windowResized(int w, int h); + void dragEvent(ofDragInfo dragInfo); + void gotMessage(ofMessage msg); + + void render(vr::Hmd_Eye nEye); + + void controllerEvent(ofxOpenVRControllerEventArgs& args); + +private: + + ofxOpenVR _openVR; + ofShader _shader; + + ofLight _light; + ofBoxPrimitive _box; + + ofVec3f _cameraPos; +}; diff --git a/src/ofxOpenVR.cpp b/src/ofxOpenVR.cpp index bd38f85..964230f 100644 --- a/src/ofxOpenVR.cpp +++ b/src/ofxOpenVR.cpp @@ -234,6 +234,20 @@ ofMatrix4x4 ofxOpenVR::getCurrentViewMatrix(vr::Hmd_Eye nEye) return matrix; } +//-------------------------------------------------------------- + +ofMatrix4x4 ofxOpenVR::getCurrentHMDMatrix() { + + return ofMatrix4x4(_mat4HMDPose.get()); +} + +//-------------------------------------------------------------- + +void ofxOpenVR::setCurrentHMDMatrix(ofMatrix4x4 & mat) { + + _mat4HMDPose.set(mat.getPtr()); +} + //-------------------------------------------------------------- ofMatrix4x4 ofxOpenVR::getControllerPose(vr::ETrackedControllerRole nController) { @@ -315,6 +329,24 @@ void ofxOpenVR::hideGrid(float transitionDuration) } } +//-------------------------------------------------------------- + +void ofxOpenVR::setNearClip(float nearClip) { + + _fNearClip = nearClip; + + setupCameras(); +} + +//-------------------------------------------------------------- + +void ofxOpenVR::setFarClip(float farClip){ + + _fFarClip = farClip; + + setupCameras(); +} + //-------------------------------------------------------------- bool ofxOpenVR::init() { @@ -1112,7 +1144,7 @@ void ofxOpenVR::renderDistortion() { glDisable(GL_DEPTH_TEST); glViewport(0, 0, ofGetWidth(), ofGetHeight()); - + glBindVertexArray(_unLensVAO); _lensShader.begin(); diff --git a/src/ofxOpenVR.h b/src/ofxOpenVR.h index f9ca8c3..f7f3649 100644 --- a/src/ofxOpenVR.h +++ b/src/ofxOpenVR.h @@ -65,6 +65,9 @@ class ofxOpenVR { ofMatrix4x4 getCurrentProjectionMatrix(vr::Hmd_Eye nEye); ofMatrix4x4 getCurrentViewMatrix(vr::Hmd_Eye nEye); + ofMatrix4x4 getCurrentHMDMatrix(); + void setCurrentHMDMatrix(ofMatrix4x4 & mat); + ofMatrix4x4 getControllerPose(vr::ETrackedControllerRole nController); bool isControllerConnected(vr::ETrackedControllerRole nController); @@ -81,6 +84,9 @@ class ofxOpenVR { void showGrid(float transitionDuration = 2.0f); void hideGrid(float transitionDuration = 2.0f); + void setNearClip(float near); + void setFarClip(float far); + ofEvent ofxOpenVRControllerEvent; private: