|
| 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 | + |
0 commit comments