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
2 changes: 1 addition & 1 deletion SharedCode/DraggablePoints.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,4 @@ class DraggablePoints : public SelectablePoints {
}
}
}
};
};
9 changes: 8 additions & 1 deletion SharedCode/Mapamok.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,25 @@ class Mapamok {
modelMatrix = ofxCv::makeMatrix(rvec, tvec);
calibrationReady = true;
}
void begin() {
void begin(ofRectangle viewPort) {
if(calibrationReady) {
glPushMatrix();
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glMatrixMode(GL_MODELVIEW);
ofPushView();
/* in intrinsics you need to comment out the following line cause its broken and causing issues...i.e leaking GL state all over the place. need to be like this:
void Intrinsics::loadProjectionMatrix(float nearDist, float farDist, ofVec2f viewportOffset) const {
//glViewport(viewportOffset.x, viewportOffset.y, imageSize.width, imageSize.height);
*/
ofViewport(viewPort.x, viewPort.y, viewPort.width, viewPort.height, true);
intrinsics.loadProjectionMatrix(.1, 10);
ofxCv::applyMatrix(modelMatrix);
}
}
void end() {
if(calibrationReady) {
ofPopView();
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
Expand Down
22 changes: 22 additions & 0 deletions SharedCode/Projector.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// Projector.h
// mapamok
//
// Created by dantheman on 5/18/14.
//
//


#pragma once
#include "ofMain.h"
#include "Mapamok.h"
#include "DraggablePoints.h"



class Projector{
public:
ofRectangle viewPort;
Mapamok mapamok;
DraggablePoints referencePoints;
};
13 changes: 11 additions & 2 deletions SharedCode/SelectablePoints.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@ class SelectablePoints : public EventWatcher {
set<unsigned int> selected;

float clickRadiusSquared;

ofRectangle mViewPort;
public:
SelectablePoints()
:clickRadiusSquared(0) {
}
unsigned int size() {
return points.size();
}
void setViewPort(ofRectangle viewport){
mViewPort = viewport;
}
void add(const ofVec2f& v) {
points.push_back(DraggablePoint());
points.back().position = v;
Expand All @@ -35,7 +38,7 @@ class SelectablePoints : public EventWatcher {
bool shift = ofGetKeyPressed(OF_KEY_SHIFT);
bool hitAny = false;
for(int i = 0; i < size(); i++) {
bool hit = points[i].isHit(mouse, clickRadiusSquared);
bool hit = points[i].isHit(mouse-mViewPort.getTopLeft(), clickRadiusSquared);
if(hit && !hitAny) {
if(!points[i].selected) {
points[i].selected = true;
Expand All @@ -58,10 +61,16 @@ class SelectablePoints : public EventWatcher {
}
}
void draw(ofEventArgs& args) {
ofPushView();
ofViewport(mViewPort.x, mViewPort.y, mViewPort.width, mViewPort.height, true);
ofSetupScreenPerspective();
//ofTranslate(mViewPort.getTopLeft());
ofPushStyle();
ofSetColor(255, 0, 255);
for(int i = 0; i < size(); i++) {
points[i].draw(clickRadiusSquared);
}
ofPopStyle();
ofPopView();
}
};
2 changes: 2 additions & 0 deletions mapamok/mapamok.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@
C66C6414C8B86FDB99ED3B70 /* core.hpp */ = {isa = PBXFileReference; explicitFileType = sourcecode.c.h; fileEncoding = 30; name = core.hpp; path = ../../../addons/ofxOpenCv/libs/opencv/include/opencv2/core/core.hpp; sourceTree = SOURCE_ROOT; };
C72403EDCB7DCAEC60D1DCDD /* aiVersion.h */ = {isa = PBXFileReference; explicitFileType = sourcecode.c.h; fileEncoding = 30; name = aiVersion.h; path = ../../../addons/ofxAssimpModelLoader/libs/assimp/include/aiVersion.h; sourceTree = SOURCE_ROOT; };
C76DE5C29BDBD2CAA1DD0021 /* ofxCvContourFinder.cpp */ = {isa = PBXFileReference; explicitFileType = sourcecode.cpp.cpp; fileEncoding = 30; name = ofxCvContourFinder.cpp; path = ../../../addons/ofxOpenCv/src/ofxCvContourFinder.cpp; sourceTree = SOURCE_ROOT; };
C8777F691929298A00A0FC70 /* Projector.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Projector.h; sourceTree = "<group>"; };
C878437FC072E5D613F2495A /* aiTexture.h */ = {isa = PBXFileReference; explicitFileType = sourcecode.c.h; fileEncoding = 30; name = aiTexture.h; path = ../../../addons/ofxAssimpModelLoader/libs/assimp/include/aiTexture.h; sourceTree = SOURCE_ROOT; };
CB6E898FB329C46EC3DEF461 /* aiMatrix4x4.h */ = {isa = PBXFileReference; explicitFileType = sourcecode.c.h; fileEncoding = 30; name = aiMatrix4x4.h; path = ../../../addons/ofxAssimpModelLoader/libs/assimp/include/aiMatrix4x4.h; sourceTree = SOURCE_ROOT; };
CBDE84185E2969BA4AB209FC /* general.h */ = {isa = PBXFileReference; explicitFileType = sourcecode.c.h; fileEncoding = 30; name = general.h; path = ../../../addons/ofxOpenCv/libs/opencv/include/opencv2/flann/general.h; sourceTree = SOURCE_ROOT; };
Expand Down Expand Up @@ -571,6 +572,7 @@
2710864C1863A94000509AE4 /* EventWatcher.h */,
2710864D1863A94000509AE4 /* SelectablePoints.h */,
278CD16E19007D9700C464A8 /* Mapamok.h */,
C8777F691929298A00A0FC70 /* Projector.h */,
);
name = SharedCode;
path = ../../SharedCode;
Expand Down
173 changes: 101 additions & 72 deletions mapamok/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,12 @@
#include "ofAppGLFWWindow.h"
#include "ofxAssimpModelLoader.h"
#include "ofxUI.h"

#include "Projector.h"
#include "Mapamok.h"
#include "DraggablePoints.h"
#include "MeshUtils.h"

class ReferencePoints : public DraggablePoints {
public:
void draw(ofEventArgs& args) {
ofPushStyle();
ofSetColor(ofColor::red);
DraggablePoints::draw(args);
ofPopStyle();
}
};


class ofApp : public ofBaseApp {
public:
Expand All @@ -39,11 +31,12 @@ class ofApp : public ofBaseApp {
bool saveButton = false;
float backgroundBrightness = 0;
bool useShader = false;

vector<Projector> mProjectors;
ofVboMesh mesh, cornerMesh;
ofEasyCam cam;
ReferencePoints referencePoints;

ofRectangle mViewPortLeft;
ofRectangle mViewPortRight;
ofRectangle mViewPort;
void setup() {
ofSetWindowTitle("mapamok");
ofSetVerticalSync(true);
Expand All @@ -56,10 +49,30 @@ class ofApp : public ofBaseApp {
cam.setDistance(1);
cam.setNearClip(.1);
cam.setFarClip(10);
cam.setVFlip(false);

referencePoints.setClickRadius(8);
referencePoints.enableControlEvents();
// referencePoints.enableDrawEvent();
//640 * 480 * 2 with a 160 pixel overlap
//Left Projector Position and Full Texture Size
mViewPortLeft = ofRectangle(0, 0, 1280, 1000);
//Right Projector Posiiton
mViewPortRight = ofRectangle(1280, -200, 1280, 1000);
mViewPort = ofRectangle(0, 0, 1280, 1000);
mProjectors.assign(2, Projector());


mProjectors[0].viewPort = mViewPortLeft;
mProjectors[0].referencePoints.setClickRadius(5);
mProjectors[0].referencePoints.setViewPort(mViewPortLeft);
mProjectors[0].referencePoints.enableControlEvents();
mProjectors[0].referencePoints.enableDrawEvent();



mProjectors[1].viewPort = mViewPortRight;
mProjectors[1].referencePoints.setClickRadius(5);
mProjectors[1].referencePoints.setViewPort(mViewPortRight);
mProjectors[1].referencePoints.enableControlEvents();
mProjectors[1].referencePoints.enableDrawEvent();
}
enum {
RENDER_MODE_FACES = 0,
Expand Down Expand Up @@ -138,55 +151,52 @@ class ofApp : public ofBaseApp {
}
}
void drawEdit() {
cam.begin();
ofPushStyle();
ofSetColor(255, 128);
mesh.drawFaces();
ofPopStyle();
cam.end();

ofMesh cornerMeshImage = cornerMesh;
// should only update this if necessary
project(cornerMeshImage, cam, ofGetWindowRect());

// if this is a new mesh, create the points
// should use a "dirty" flag instead allowing us to reset manually
if(cornerMesh.getNumVertices() != referencePoints.size()) {
referencePoints.clear();
for(int i = 0; i < cornerMeshImage.getNumVertices(); i++) {
referencePoints.add(ofVec2f(cornerMeshImage.getVertex(i)));
for(int i = 0; i < mProjectors.size(); i++){
cam.begin(mProjectors[i].viewPort);
ofPushStyle();
ofSetColor(255, 128);
mesh.drawFaces();
ofPopStyle();
cam.end();

ofMesh cornerMeshImage = cornerMesh;
// should only update this if necessary
project(cornerMeshImage, cam, mViewPort);

// if this is a new mesh, create the points
// should use a "dirty" flag instead allowing us to reset manually
if(cornerMesh.getNumVertices() != mProjectors[i].referencePoints.size()) {
mProjectors[i].referencePoints.clear();
for(int j = 0; j < cornerMeshImage.getNumVertices(); j++) {
mProjectors[i].referencePoints.add(ofVec2f(cornerMeshImage.getVertex(j)));
}
}
}

// if the calibration is ready, use the calibration to find the corner positions

// otherwise, update the points
for(int i = 0; i < referencePoints.size(); i++) {
DraggablePoint& cur = referencePoints.get(i);
if(!cur.hit) {
cur.position = cornerMeshImage.getVertex(i);
} else {
ofLine(cur.position, cornerMeshImage.getVertex(i));
// if the calibration is ready, use the calibration to find the corner positions
// otherwise, update the points
for(int j = 0; j < mProjectors[i].referencePoints.size(); j++) {
DraggablePoint& cur = mProjectors[i].referencePoints.get(j);
if(!cur.hit) {
cur.position = cornerMeshImage.getVertex(j);
} else {
ofLine(cur.position, cornerMeshImage.getVertex(j));
}
}
}

// should be a better way to do this
ofEventArgs args;
referencePoints.draw(args);

// calculating the 3d mesh
vector<ofVec2f> imagePoints;
vector<ofVec3f> objectPoints;
for(int i = 0; i < referencePoints.size(); i++) {
DraggablePoint& cur = referencePoints.get(i);
if(cur.hit) {
imagePoints.push_back(cur.position);
objectPoints.push_back(cornerMesh.getVertex(i));

// calculating the 3d mesh
vector<ofVec2f> imagePoints;
vector<ofVec3f> objectPoints;
for(int j = 0; j < mProjectors[i].referencePoints.size(); j++) {
DraggablePoint& cur = mProjectors[i].referencePoints.get(j);
if(cur.hit) {
imagePoints.push_back(cur.position);
objectPoints.push_back(cornerMesh.getVertex(j));
}
}
// should only calculate this when the points are updated
mProjectors[i].mapamok.update(mProjectors[i].viewPort.width, mProjectors[i].viewPort.height, imagePoints, objectPoints);
}

// should only calculate this when the points are updated
mapamok.update(ofGetWidth(), ofGetHeight(), imagePoints, objectPoints);
}
void draw() {
ofBackground(backgroundBrightness);
Expand All @@ -195,18 +205,26 @@ class ofApp : public ofBaseApp {
if(editToggle) {
drawEdit();
}

if(mapamok.calibrationReady) {
mapamok.begin();
if(editToggle) {
ofSetColor(255, 128);
} else {
ofSetColor(255);
for(int i = 0; i < mProjectors.size(); i++){
if(mProjectors[i].mapamok.calibrationReady) {
mProjectors[i].mapamok.begin(mProjectors[i].viewPort);

if(editToggle) {
ofSetColor(255, 128);
} else {
ofSetColor(255);
}
mesh.draw();

mProjectors[i].mapamok.end();
}
mesh.draw();
mapamok.end();
}

ofPushStyle();
ofNoFill();
ofSetColor(255, 0, 255);
ofRect(mViewPortLeft);
ofRect(mViewPortRight);
ofPopStyle();
ofDrawBitmapString(ofToString((int) ofGetFrameRate()), 10, ofGetHeight() - 40);
}
void loadModel(string filename) {
Expand All @@ -226,6 +244,17 @@ class ofApp : public ofBaseApp {
centerAndNormalize(meshes[i], cornerMin, cornerMax);
}


mesh.clearColors();
int numVerts = mesh.getNumVertices();
for(int i= 0; i < numVerts; i++){
if(i <= numVerts/2){
mesh.addColor(ofFloatColor(0, 1., 1.));
}else{
mesh.addColor(ofFloatColor(1., 0., 1.));
}
}

// merge
// another good metric for finding corners is if there is a single vertex touching
// the wall of the bounding box, that point is a good control point
Expand Down Expand Up @@ -262,6 +291,6 @@ class ofApp : public ofBaseApp {

int main() {
ofAppGLFWWindow window;
ofSetupOpenGL(&window, 1280, 720, OF_WINDOW);
ofSetupOpenGL(&window, 1280, 480, OF_WINDOW);
ofRunApp(new ofApp());
}