Skip to content

Instantly share code, notes, and snippets.

@smukkejohan
Created January 11, 2014 02:47
Show Gist options
  • Save smukkejohan/8366353 to your computer and use it in GitHub Desktop.
Save smukkejohan/8366353 to your computer and use it in GitHub Desktop.
Ribbon 3d
#include "testApp.h"
#include "ofxStereoCamera.h"
#include <OpenGL/OpenGL.h>
// Syphon together with 3D primitive and NoFill does not run
const int width = 1280;
const int height = 720;
ofxStereoCamera<ofEasyCam> cam;
vector<ofVec3f> points;
//--------------------------------------------------------------
void testApp::setup()
{
ofSetFrameRate(60);
ofSetVerticalSync(true);
ofBackground(0);
// There is some clash between cam setup and 3d primitive rendering. I fixed it by commenting out shaders then putting them in again.
// It seems to happen only in mode 3 line by line
cam.setup(ofGetWidth(), ofGetHeight());
cam.setScale(1, -1, 1);
cam.setPhysicalFocusDistance(120);
cam.setFocusDistance(50);
cam.setNearClip(0.1);
cam.setOutputMode(ofxStereoCameraOutputMode::SIDE_BY_SIDE);
/*float s = 1000;
for (int i = 0; i < 3000; i++)
{
ofVec3f v;
v.x = ofRandom(-s, s);
v.y = ofRandom(-s, s);
v.z = ofRandom(-s, s);
points.push_back(v);
}*/
leftOutputServer.setName("Left");
rightOutputServer.setName("Right");
sbsOutputServer.setName("Side By Side");
parameters.setName("Stereo");
parameters.add(boxOneZ.set("Box1z",0.0,0.0,1.0));
parameters.add(boxTwoZ.set("Box2z",0.0,0.0,1.0));
gui.setup(parameters);
sync.setup((ofParameterGroup&)gui.getParameter(),9001,"localhost",9000);
}
//--------------------------------------------------------------
void testApp::update()
{
sync.update();
cam.setFocusDistance(cam.getGlobalPosition().length());
cam.update();
ofVec3f sumOfAllPoints(0,0,0);
for(unsigned int i = 0; i < points.size(); i++){
points[i].z -= 4;
sumOfAllPoints += points[i];
}
center = sumOfAllPoints / points.size();
}
void testApp::drawScene()
{
glPushMatrix();
ofFill();
//ofDrawBox(200, 0, boxOneZ.get()*200-100, 240, 400, 30);
//ofDrawBox(-200, 0, boxTwoZ.get()*200-100, 240, 400, 30);
ofSetColor(255);
//do the same thing from the first example...
ofMesh mesh;
mesh.setMode(OF_PRIMITIVE_TRIANGLE_STRIP);
for(unsigned int i = 1; i < points.size(); i++){
//find this point and the next point
ofVec3f thisPoint = points[i-1];
ofVec3f nextPoint = points[i];
//get the direction from one to the next.
//the ribbon should fan out from this direction
ofVec3f direction = (nextPoint - thisPoint);
//get the distance from one point to the next
float distance = direction.length();
//get the normalized direction. normalized vectors always have a length of one
//and are really useful for representing directions as opposed to something with length
ofVec3f unitDirection = direction.normalized();
//find both directions to the left and to the right
ofVec3f toTheLeft = unitDirection.getRotated(-90, ofVec3f(0,0,1));
ofVec3f toTheRight = unitDirection.getRotated(90, ofVec3f(0,0,1));
//use the map function to determine the distance.
//the longer the distance, the narrower the line.
//this makes it look a bit like brush strokes
float thickness = ofMap(distance, 0, 60, 20, 2, true);
//calculate the points to the left and to the right
//by extending the current point in the direction of left/right by the length
ofVec3f leftPoint = thisPoint+toTheLeft*thickness;
ofVec3f rightPoint = thisPoint+toTheRight*thickness;
//add these points to the triangle strip
mesh.addVertex(ofVec3f(leftPoint.x, leftPoint.y, leftPoint.z));
mesh.addVertex(ofVec3f(rightPoint.x, rightPoint.y, rightPoint.z));
}
//end the shape
mesh.draw();
glPopMatrix();
}
//--------------------------------------------------------------
void testApp::draw()
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
ofSetColor(255);
cam.draw(0, 0, ofGetWidth(), ofGetHeight());
ofSetLineWidth(2);
ofFill();
if(showGrid){
glDisable(GL_DEPTH_TEST);
for(int x = 0; x < ofGetWidth(); x+=20){
for(int y = 0; y < ofGetWidth(); y+=20){
if(fmodf(x+y+.0,40) > 0){
ofSetColor(255);
} else {
ofSetColor(0);
}
ofRect(x, y, 20, 20);
}
}
}
cam.beginLeft();
drawScene();
cam.endLeft();
cam.beginRight();
drawScene();
cam.endRight();
//leftOutputServer.publishTexture(&cam.getLeftTexture());
//rightOutputServer.publishTexture(&cam.getRightTexture());
sbsOutputServer.publishScreen();
//cam.getLeftFbo()->draw(0, 0, ofGetWidth()/2, ofGetHeight()/2);
string str;
str += "[1] SIDE_BY_SIDE\n";
str += "[2] CROSS_EYED\n";
str += "[3] LINE_BY_LINE\n";
str += "[4] ANAGLYPH\n\n";
str += "[5] PARALLEL\n";
str += "[6] TOE_IN\n";
ofSetColor(255);
//ofDrawBitmapString(str, 10, 20);
//gui.draw();
}
//--------------------------------------------------------------
void testApp::keyPressed(int key)
{
if (key == '1')
{
cam.setOutputMode(ofxStereoCameraOutputMode::SIDE_BY_SIDE);
}
else if (key == '2')
{
cam.setOutputMode(ofxStereoCameraOutputMode::CROSS_EYED);
}
else if (key == '3')
{
cam.setOutputMode(ofxStereoCameraOutputMode::LINE_BY_LINE);
}
else if (key == '4')
{
cam.setOutputMode(ofxStereoCameraOutputMode::ANAGLYPH);
}
if (key == '5')
{
cam.setViewMode(ofxStereoCameraViewMode::PARALLEL);
}
else if (key == '6')
{
cam.setViewMode(ofxStereoCameraViewMode::TOE_IN);
}
if (key == 'f')
{
ofToggleFullscreen();
}
if (key == 'g')
{
showGrid = !showGrid;
}
if (key == 'd')
{
showDots =! showDots;
}
}
//--------------------------------------------------------------
void testApp::keyReleased(int key)
{
}
//--------------------------------------------------------------
void testApp::mouseMoved(int x, int y)
{
ofVec3f mousePoint(x - ofGetWidth()/2,y-ofGetHeight()/2, 400);
points.push_back(mousePoint);
}
//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button)
{
}
//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button)
{
}
//--------------------------------------------------------------
void testApp::mouseReleased(int x, int y, int button)
{
}
//--------------------------------------------------------------
void testApp::windowResized(int w, int h)
{
}
//--------------------------------------------------------------
void testApp::gotMessage(ofMessage msg)
{
}
//--------------------------------------------------------------
void testApp::dragEvent(ofDragInfo dragInfo)
{
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment