diff --git a/example-basic/src/ofApp.h b/example-basic/src/ofApp.h index a644b5e..6d71c4e 100644 --- a/example-basic/src/ofApp.h +++ b/example-basic/src/ofApp.h @@ -27,28 +27,16 @@ class ofApp : public ofxiOSApp { void gotMemoryWarning(); void deviceOrientationChanged(int newOrientation); - - ofVbo vbo; - - + vector < matrix_float4x4 > mats; + vector anchors; + ofCamera camera; ofTrueTypeFont font; - - //CVOpenGLESTextureRef createTextureFromPixelBuffer(CVPixelBufferRef pixelBuffer,int planeIndex); - - - + ofImage img; + // ====== AR STUFF ======== // ARSession * session; - ARRef processor; - - /* - int32_t cameraWidth; - int32_t cameraHeight; - - */ - - ofImage img; + }; diff --git a/example-basic/src/ofApp.mm b/example-basic/src/ofApp.mm index 8f72426..32115b9 100644 --- a/example-basic/src/ofApp.mm +++ b/example-basic/src/ofApp.mm @@ -17,15 +17,6 @@ void logSIMD(const simd::float4x4 &matrix) output << std::endl; } -ofMatrix4x4 matFromSimd(const simd::float4x4 &matrix){ - ofMatrix4x4 mat; - mat.set(matrix.columns[0].x,matrix.columns[0].y,matrix.columns[0].z,matrix.columns[0].w, - matrix.columns[1].x,matrix.columns[1].y,matrix.columns[1].z,matrix.columns[1].w, - matrix.columns[2].x,matrix.columns[2].y,matrix.columns[2].z,matrix.columns[2].w, - matrix.columns[3].x,matrix.columns[3].y,matrix.columns[3].z,matrix.columns[3].w); - return mat; -} - //-------------------------------------------------------------- ofApp :: ofApp (ARSession * session){ this->session = session; @@ -51,48 +42,20 @@ ofMatrix4x4 matFromSimd(const simd::float4x4 &matrix){ font.load("fonts/mono0755.ttf", fontSize); - - processor = ARProcessor::create(session); - processor->setup(); - - - } -vector < matrix_float4x4 > mats; //-------------------------------------------------------------- void ofApp::update(){ processor->update(); - mats.clear(); - - if (session.currentFrame){ - NSInteger anchorInstanceCount = session.currentFrame.anchors.count; - - for (NSInteger index = 0; index < anchorInstanceCount; index++) { - ARAnchor *anchor = session.currentFrame.anchors[index]; - - // Flip Z axis to convert geometry from right handed to left handed - matrix_float4x4 coordinateSpaceTransform = matrix_identity_float4x4; - coordinateSpaceTransform.columns[2].z = -1.0; - - matrix_float4x4 newMat = matrix_multiply(anchor.transform, coordinateSpaceTransform); - mats.push_back(newMat); - logSIMD(newMat); - //anchorUniforms->modelMatrix = matrix_multiply(anchor.transform, coordinateSpaceTransform); - } - } - } - -ofCamera camera; //-------------------------------------------------------------- void ofApp::draw() { ofEnableAlphaBlending(); @@ -108,49 +71,36 @@ ofMatrix4x4 matFromSimd(const simd::float4x4 &matrix){ camera.begin(); processor->setARCameraMatrices(); - for (int i = 0; i < mats.size(); i++){ + for (int i = 0; i < session.currentFrame.anchors.count; i++){ + ARAnchor * anchor = session.currentFrame.anchors[i]; + + // note - if you need to differentiate between different types of anchors, there is a + // "isKindOfClass" method in objective-c that could be used. For example, if you wanted to + // check for a Plane anchor, you could put this in an if statement. + // if([anchor isKindOfClass:[ARPlaneAnchor class]]) { // do something if we find a plane anchor} + // Not important for this example but something good to remember. + ofPushMatrix(); - //mats[i].operator=(const simd_float4x4 &) - ofMatrix4x4 mat; - mat.set(mats[i].columns[0].x, mats[i].columns[0].y,mats[i].columns[0].z,mats[i].columns[0].w, - mats[i].columns[1].x, mats[i].columns[1].y,mats[i].columns[1].z,mats[i].columns[1].w, - mats[i].columns[2].x, mats[i].columns[2].y,mats[i].columns[2].z,mats[i].columns[2].w, - mats[i].columns[3].x, mats[i].columns[3].y,mats[i].columns[3].z,mats[i].columns[3].w); + ofMatrix4x4 mat = ARCommon::convert(anchor.transform); ofMultMatrix(mat); - + ofSetColor(255); ofRotate(90,0,0,1); - float aspect = ARCommon::getNativeAspectRatio(); - img.draw(-aspect/8,-0.125,aspect/4,0.25); - - + img.draw(-0.025 / 2, -0.025 / 2,0.025,0.025); + + ofPopMatrix(); } - + camera.end(); } } ofDisableDepthTest(); // ========== DEBUG STUFF ============= // - int w = MIN(ofGetWidth(), ofGetHeight()) * 0.6; - int h = w; - int x = (ofGetWidth() - w) * 0.5; - int y = (ofGetHeight() - h) * 0.5; - int p = 0; - - x = ofGetWidth() * 0.2; - y = ofGetHeight() * 0.11; - p = ofGetHeight() * 0.035; - - //ofSetColor(ofColor::black); - font.drawString("frame num = " + ofToString( ofGetFrameNum() ), x, y+=p); - font.drawString("frame rate = " + ofToString( ofGetFrameRate() ), x, y+=p); - font.drawString("screen width = " + ofToString( ofGetWidth() ), x, y+=p); - font.drawString("screen height = " + ofToString( ofGetHeight() ), x, y+=p); - - + processor->debugInfo.drawDebugInformation(font); + } @@ -161,8 +111,18 @@ ofMatrix4x4 matFromSimd(const simd::float4x4 &matrix){ //-------------------------------------------------------------- void ofApp::touchDown(ofTouchEventArgs &touch){ - - processor->anchorController->addAnchor(); + if (session.currentFrame){ + ARFrame *currentFrame = [session currentFrame]; + + matrix_float4x4 translation = matrix_identity_float4x4; + translation.columns[3].z = -0.2; + matrix_float4x4 transform = matrix_multiply(currentFrame.camera.transform, translation); + + // Add a new anchor to the session + ARAnchor *anchor = [[ARAnchor alloc] initWithTransform:transform]; + + [session addAnchor:anchor]; + } } //-------------------------------------------------------------- diff --git a/example-basic/src/viewcontrollers/MyAppViewController.mm b/example-basic/src/viewcontrollers/MyAppViewController.mm index cb0129f..5b7ba2d 100644 --- a/example-basic/src/viewcontrollers/MyAppViewController.mm +++ b/example-basic/src/viewcontrollers/MyAppViewController.mm @@ -19,7 +19,7 @@ - (void)loadView { [super loadView]; ARCore::SFormat format; - format.enablePlaneTracking().enableLighting(); + format.enableLighting(); self.session = ARCore::generateNewSession(format); diff --git a/src/ARAnchorManager.mm b/src/ARAnchorManager.mm index 28f2d3f..186194e 100644 --- a/src/ARAnchorManager.mm +++ b/src/ARAnchorManager.mm @@ -16,7 +16,9 @@ _onPlaneAdded = nullptr; } - ARAnchorManager::ARAnchorManager(ARSession * session):shouldUpdatePlanes(false){ + ARAnchorManager::ARAnchorManager(ARSession * session): + shouldUpdatePlanes(false), + maxTrackedPlanes(0){ this->session = session; } diff --git a/src/ARCam.h b/src/ARCam.h index 86ba855..7e28a7b 100644 --- a/src/ARCam.h +++ b/src/ARCam.h @@ -154,6 +154,10 @@ namespace ARCore { //! draws the camera frame. void draw(); + //! Draws a scaled version of the camera image based on the properties passed in. + //! Will default to class properties if no new values are passed as parameters. + void drawScaled(int x=0,int y=0, float w = 0, float h = 0); + void updatePlaneTexCoords(); //! Allows you to force an orientation to make it easier to lock your device diff --git a/src/ARCam.mm b/src/ARCam.mm index ef0cf31..6e4b8a2 100644 --- a/src/ARCam.mm +++ b/src/ARCam.mm @@ -98,7 +98,25 @@ cameraDimensions.y = y; } - void ARCam::draw(){ + void ARCam::drawScaled(int x, int y, float w, float h){ + + + if(x == 0){ + x = xShift; + } + + if(y == 0){ + y = yShift; + } + + if(w == 0){ + w = cameraDimensions.x; + } + + if(h == 0){ + h = cameraDimensions.y; + } + if(needsPerspectiveAdjustment){ // Adjust drawing as necessary . switch(UIDevice.currentDevice.orientation){ @@ -145,6 +163,46 @@ } + void ARCam::draw(){ + cameraConvertShader.begin(); + + switch(UIDevice.currentDevice.orientation){ + case UIDeviceOrientationFaceUp: + cameraConvertShader.setUniform1i("isPortraitOrientation", true); + break; + case UIDeviceOrientationFaceDown: + break; + + case UIDeviceOrientationUnknown: + + cameraConvertShader.setUniform1i("isPortraitOrientation", true); + break; + case UIDeviceOrientationPortraitUpsideDown: + + cameraConvertShader.setUniform1i("isPortraitOrientation", true); + break; + + case UIDeviceOrientationPortrait: + + cameraConvertShader.setUniform1i("isPortraitOrientation", true); + + break; + + case UIDeviceOrientationLandscapeLeft: + + cameraConvertShader.setUniform1i("isPortraitOrientation", false); + break; + + case UIDeviceOrientationLandscapeRight: + + cameraConvertShader.setUniform1i("isPortraitOrientation", false); + break; + } + + vMesh.draw(GL_TRIANGLE_STRIP, 0, 16); + cameraConvertShader.end(); + } + //! Sets the x and y position of where the camera image is placed. void ARCam::setCameraImagePosition(float xShift,float yShift){ this->xShift = xShift; diff --git a/src/ARDebugUtils.h b/src/ARDebugUtils.h index dafcfe1..ccabd4f 100644 --- a/src/ARDebugUtils.h +++ b/src/ARDebugUtils.h @@ -65,7 +65,7 @@ namespace ARDebugUtils { break; case ARTrackingStateReasonInsufficientFeatures: - font.drawString("Tracking state: Bad!",0,y + 100); + font.drawString("Tracking state: Bad!",x,y + 100); break; }