Skip to content

Instantly share code, notes, and snippets.

@patakk
Created October 23, 2017 12:11
Show Gist options
  • Save patakk/83981429c0d904fbf3b72b1f628f7e01 to your computer and use it in GitHub Desktop.
Save patakk/83981429c0d904fbf3b72b1f628f7e01 to your computer and use it in GitHub Desktop.
#include "ofApp.h"
#include "ARAnchorManager.h"
//--------------------------------------------------------------
ofApp :: ofApp (ARSession * session){
this->session = session;
cout << "creating ofApp" << endl;
}
ofApp::ofApp(){}
//--------------------------------------------------------------
ofApp :: ~ofApp () {
cout << "destroying ofApp" << endl;
}
//--------------------------------------------------------------
void ofApp::setup() {
ofBackground(127);
int fontSize = 8;
if (ofxiOSGetOFWindow()->isRetinaSupportedOnDevice())
fontSize *= 2;
font.load("fonts/mono0755.ttf", fontSize);
processor = ARProcessor::create(session);
processor->setup();
// processor->anchorController->addAnchor(ImageMesh());
//copyFBO.allocate(ofGetWindowWidth(),ofGetWindowHeight(),GL_RGBA);
}
//--------------------------------------------------------------
void ofApp::update(){
processor->update();
processor->updatePlanes();
}
ofCamera camera;
//--------------------------------------------------------------
void ofApp::draw() {
ofEnableAlphaBlending();
ofDisableDepthTest();
processor->drawFrame();
ofEnableDepthTest();
ARFrame * currentFrame = session.currentFrame;
ofMatrix4x4 cameraMatrix;
if (currentFrame) {
// Create a transform with a translation of 0.2 meters in front of the camera
matrix_float4x4 translation = matrix_identity_float4x4;
translation.columns[3].z = -0.2;
matrix_float4x4 transform = matrix_multiply(currentFrame.camera.transform, translation);
cameraMatrix = ARCommon::toMat4(transform); // u biti ovo je matrica kamere pomnozena sa ovom translacijom od -0.2 metra
// ispisujemo taj red (u biti stupac) koji se odnosi na translaciju)
cout << cameraMatrix.getRowAsVec4f(3)[0] << " " << cameraMatrix.getRowAsVec4f(3)[1] << " " << cameraMatrix.getRowAsVec4f(3)[2] << " " << cameraMatrix.getRowAsVec4f(3)[3] << " " << endl << endl;
}
/*
// This loops through all of the added anchors.
processor->anchorController->loopPlaneAnchors([=](PlaneAnchorObject obj,int index) -> void {
processor->updatePlanes();
cout << index << " " << obj.width << " " << obj.height << endl;
camera.begin();
processor->setARCameraMatrices();
ofMatrix4x4 mat = ofMatrix4x4(obj.transform);
//mat.translate(0,0,0*(1000+500*sin(ofGetFrameNum()*0.1))*0.0001);
//mat.rotate(90, 0, 1, 0);
ofPushMatrix();
ofMultMatrix(mat);
ofSetColor(255);
ofRotate(90,1,0,0);
//ofScale(0.0001, 0.0001);
for(int k = 0; k < 400; k++){
ofPushMatrix();
ofFill();
ofSetColor(102,216,254,100);
ofTranslate(obj.position.x/2 + ofRandom(obj.width)*0.2,obj.position.y/2 + ofRandom(obj.height)*0.2, -ofRandom(0.25));
//ofDrawRectangle(-obj.position.x/2,-obj.position.z/2,0,obj.width,obj.height);
ofRotate(ofRandom(90), ofRandom(1), ofRandom(1), ofRandom(1));
ofDrawCircle(0,0,0,0.02);
//ofSetColor(102,0,0,100);
ofFill();
ofSetColor(255,255,255);
ofPopMatrix();
}
ofPopMatrix();
camera.end();
});
*/
processor->anchorController->loopAnchors([=](ARObject obj,int index) -> void {
processor->updatePlanes();
camera.begin();
processor->setARCameraMatrices();
ofMatrix4x4 objectMatrix = ofMatrix4x4(obj.modelMatrix);
//mat.translate(0,0,0*(1000+500*sin(ofGetFrameNum()*0.1))*0.0001);
//mat.rotate(90, 0, 1, 0);
ofPushMatrix();
float x1 = cameraMatrix.getRowAsVec4f(3)[0];
float y1 = cameraMatrix.getRowAsVec4f(3)[1];
float z1 = cameraMatrix.getRowAsVec4f(3)[2];
float x2 = objectMatrix.getRowAsVec4f(3)[0];
float y2 = objectMatrix.getRowAsVec4f(3)[1];
float z2 = objectMatrix.getRowAsVec4f(3)[2];
ofFill();
//ofDrawCircle(0,0,0,0.02);
// tu interpoliram izmedu pozicije kamere i objekta
for(float p = 0; p < 1.0; p += 1.0/200){
float xx = x1 + (x2-x1)*p;
float yy = y1 + (y2-y1)*p;
float zz = z1 + (z2-z1)*p;
ofSetColor(255,255-255*p);
ofDrawSphere(xx,yy+p*0.01*cos(p*30+ofGetFrameNum()*0.1),zz,0.001);
}
ofSetColor(255,255,255);
ofPopMatrix();
ofPushMatrix();
ofMultMatrix(objectMatrix);
ofSetColor(255);
//ofRotate(90,1,0,0);
//ofScale(0.0001, 0.0001);
for(int k = 0; k < 1; k++){
ofPushMatrix();
ofFill();
ofSetColor(102,216,254,100);
//ofTranslate(obj.position.x/2 + ofRandom(obj.width)*0.2,obj.position.y/2 + ofRandom(obj.height)*0.2, -ofRandom(0.25));
//ofDrawRectangle(-obj.position.x/2,-obj.position.z/2,0,obj.width,obj.height);
//ofRotate(ofRandom(90), ofRandom(1), ofRandom(1), ofRandom(1));
//ofDrawRectangle(0,0,0,0.6, 0.1);
//ofSetColor(102,0,0,100);
ofFill();
ofSetColor(255,255,255);
ofPopMatrix();
}
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;
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);
}
//--------------------------------------------------------------
void ofApp::exit() {
//
}
//--------------------------------------------------------------
void ofApp::touchDown(ofTouchEventArgs &touch){
// this will quickly build up memory so clear when we hit the max number of iamges.
//if(images.size() >= maxImages){
// images.clear();
//}
//ofFbo fbo;
//fbo.allocate(ofGetWindowWidth(), ofGetWindowHeight(),GL_RGBA);
//fbo.begin();
//ofClear(0,255);
//processor->getCameraTexture().draw(0,0,ofGetWindowWidth(),ofGetWindowHeight());
//fbo.end();
//images.push_back(fbo);
processor->anchorController->addAnchor();
}
//--------------------------------------------------------------
void ofApp::touchMoved(ofTouchEventArgs &touch){
}
//--------------------------------------------------------------
void ofApp::touchUp(ofTouchEventArgs &touch){
}
//--------------------------------------------------------------
void ofApp::touchDoubleTap(ofTouchEventArgs &touch){
}
//--------------------------------------------------------------
void ofApp::lostFocus(){
}
//--------------------------------------------------------------
void ofApp::gotFocus(){
}
//--------------------------------------------------------------
void ofApp::gotMemoryWarning(){
}
//--------------------------------------------------------------
void ofApp::deviceOrientationChanged(int newOrientation){
}
//--------------------------------------------------------------
void ofApp::touchCancelled(ofTouchEventArgs& args){
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment