Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions example-animatedpath/addons.make
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ofxOpenVR
Empty file.
Binary file added example-animatedpath/bin/data/of.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions example-animatedpath/icon.rc
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions example-animatedpath/src/main.cpp
Original file line number Diff line number Diff line change
@@ -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());
}
214 changes: 214 additions & 0 deletions example-animatedpath/src/ofApp.cpp
Original file line number Diff line number Diff line change
@@ -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){

}

40 changes: 40 additions & 0 deletions example-animatedpath/src/ofApp.h
Original file line number Diff line number Diff line change
@@ -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;
};
34 changes: 33 additions & 1 deletion src/ofxOpenVR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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()
{
Expand Down Expand Up @@ -1112,7 +1144,7 @@ void ofxOpenVR::renderDistortion()
{
glDisable(GL_DEPTH_TEST);
glViewport(0, 0, ofGetWidth(), ofGetHeight());

glBindVertexArray(_unLensVAO);
_lensShader.begin();

Expand Down
6 changes: 6 additions & 0 deletions src/ofxOpenVR.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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<ofxOpenVRControllerEventArgs> ofxOpenVRControllerEvent;

private:
Expand Down