Skip to content

Instantly share code, notes, and snippets.

View larsberg's full-sized avatar
💭
Big up to my github brethren

Lars Berg larsberg

💭
Big up to my github brethren
View GitHub Profile
@larsberg
larsberg / ClosestPointOnLine for openframeworks
Last active May 19, 2021 00:28
closest point on line in OF.
//closest point on line
//http://paulbourke.net/geometry/pointlineplane/
ofVec3f closestPointOnLine(ofVec3f p, ofVec3f l0, ofVec3f l1 )
{
if(l0 == l1) return ofVec3f();
float u = (((p.x-l0.x) * (l1.x-l0.x)) + ((p.y-l0.y) * (l1.y-l0.y)) + ((p.z-l0.z) * (l1.z-l0.z))) / l1.distanceSquared( l0 );
if( u < 0.0f )
return l0;
@larsberg
larsberg / exportFBOImage
Created June 24, 2014 18:25
save OF fbo to image
void exportFbo(ofFbo& _fbo)
{
auto path = ofSystemSaveDialog("fbo.png", "save as");
ofShortPixels pixels;
_fbo.readToPixels(pixels);
ofSaveImage(pixels, path.getPath());
}
if (!document.styleSheets.length) document.head.appendChild(document.createElement('style'));
var sheet = document.styleSheets[document.styleSheets.length - 1];
var rules = {};
function cssRule(selector, styles) {
var index;
if (selector in rules) {
index = rules[selector];
sheet.deleteRule(index);
} else {
index = rules[selector] = sheet.cssRules.length;
@larsberg
larsberg / OFSimpleCullExample
Last active June 2, 2017 11:56
simple culling example for openframeworks
...
void ofApp::draw()
{
ofEnableAlphaBlending();
//pass 1
glEnable(GL_CULL_FACE);
glCullFace(GL_FRONT);
@larsberg
larsberg / Svg2Mesh
Created August 1, 2014 21:53
OF svg to ofMesh
static void addSvgToMesh(string dir, ofMesh& m)
{
ofxSVG svg;
svg.load(dir);
for ( auto i=0; i<svg.getNumPath(); i++ )
{
auto& path = svg.getPathAt(i);
path.simplify();
@larsberg
larsberg / RandomPointsOnMesh
Last active August 29, 2015 14:04
Evenly distributed randome points on an ofMesh
#pragma once
#include "ofMain.h"
namespace RandomPointsOnMesh
{
static float areaOfTriangle(ofVec3f p0, ofVec3f p1, ofVec3f p2)
{
return (p2 - p1).cross(p0 - p1).length() * .5;
@larsberg
larsberg / Mesh2Points.h
Created August 4, 2014 20:49
evenly distributed random points on a mesh
//
// Mesh2Points.h
//
// Created by lars berg on 8/1/14.
//
#pragma once
#include "ofMain.h"
@larsberg
larsberg / pointOnSphere
Last active August 29, 2015 14:08
get ofVec3f on sphere using
//theta in [0,TWO_PI), phi in [0,PI], and where radius in [0,infty)
static ofVec3f pointOnSphere(float theta, float phi, float radius = 50)
{
ofVec3f p;
p.x = radius * cos(theta) * sin(phi);
p.y = radius * cos(phi);
p.z = radius * sin(theta) * sin(phi);
return p;
@larsberg
larsberg / createOFGeometries
Last active August 29, 2015 14:08
OF create geometries
static void makeScreenQuad(ofMesh& m, float w=2, float h=2)
{
w *= .5;
h *= .5;
m.clear();
m.addVertex(ofVec3f(-w,-h,0));
m.addVertex(ofVec3f( w,-h,0));
m.addVertex(ofVec3f( w, h,0));
m.addVertex(ofVec3f(-w, h,0));
// 1D gaussian kernel
// pretty much ripped from here: http://www.apileofgrains.nl/blur-filters-c/
vector<float> gaussianKernel( int radius, float weight = 1.)
{
int mem_amount = (radius*2)+1;
vector<float> kernel( (radius*2) + 1 );
float twoRadiusSquaredRecip = 1.0 / (2.0 * radius * radius);
float sqrtTwoPiTimesRadiusRecip = 1.0 / (sqrt(2.0 * PI) * radius);