ofMesh Primitive Types

Hi, I’m trying to do something quite simple;

At the moment I’m creating a Mesh comprised of a grid of Vec3f points, and applying a live video texture to it. I’d like to deform the mesh in different ways, but the structure of the mesh is not what I’m expecting.

In the list of types for ofMesh::setMode() I can’t find anything that will construct a plain grid structure - ie. all the points are joined together relative to the closest points. Instead the closest thing I can find is OF_PRIMITIVE_TRIANGLE_FAN - but this creates a strange (fan) effect - with all of the points somehow linking to the first indexed point (see picture);

What am I missing?

If you take a look at the Mesh from Camera example, there is a simple mesh set up in the beginning of it. It doesn’t appear to have a setMode. If you want, you can rewrite the code to include a call to getMode to find out what it is using.

You can see the example online here: https://github.com/openframeworks/openFrameworks/blob/master/examples/3d/meshFromCameraExample/src/ofApp.cpp

I hope that helps.

This is just generating a plain mesh without a texture though - I’m struggling to find anything in the examples or on Github that illustrates a simple 2D/3D mesh with an image texture - ie. the standard style of QC or processing demo with flag moving in the wind.

I’ve just rewritten my patch to generate the mesh in a triangular fan pattern instead of in columns / rows. It looks fine before any deformation;

But is very chaotic when I try to deform it at all - it’s not a safe structure of mesh…

Here is it with a few simple deformations (for example, mouse position bulge);

I suppose my question is - how on earth can I make a textured ofMesh in the style of a flag? :slight_smile:

Also apologies for the gross looking image - it’s a USB microscope and a bit of bread!


Perhaps you can use ofPlanePrimitive to create the mesh. It is handy beause it can compute the texture coords.
In this example, I use the OF_PRIMITIVE_TRIANGLES mode to initialize the mesh. This create vertices in this order:
0 1 2
3 4 5
6 7 8


class ofApp : public ofBaseApp{

	void setup();
	void update();
	void draw();

	ofVideoGrabber cam;
	ofPlanePrimitive plane;
	int planeWidth;
	int planeHeight;
	int planeGridSize;
	int planeColumsNbr;
	int planeRowsNbr;
	vector< ofVec3f > baseVertices;


void ofApp::setup()
	cam.setDeviceID( 0 ) ;
	cam.setup( 800, 600 ) ;

	planeWidth = cam.getWidth() ;
	planeHeight = cam.getHeight() ;
	planeGridSize = 20 ;
	planeColumsNbr = planeWidth / planeGridSize ;
	planeRowsNbr = planeHeight / planeGridSize ;
	plane.set( planeWidth, planeHeight, planeColumsNbr, planeRowsNbr, OF_PRIMITIVE_TRIANGLES ) ;
	plane.mapTexCoords( 0, 0, planeWidth, planeHeight ) ;
	baseVertices = plane.getMesh().getVertices() ;

void ofApp::update()
	cam.update() ;

	ofMesh & mesh = plane.getMesh() ;
	ofVec3f v ;
	float offset ;
	float angle = ofGetElapsedTimef() * 2.f ;

	for( int col = 0 ; col < planeColumsNbr ; ++col )
		float offset = 50.f * sin( angle + ( float( col ) / float( planeColumsNbr ) ) * 5.f ) ;

		for( int row = 0 ; row < planeRowsNbr ; ++row )
			int index = row * planeColumsNbr + col ;
			v.set( baseVertices[ index ] ) ;
			v.y += offset ;
			mesh.setVertex( index, v ) ;

void ofApp::draw()
	ofPushMatrix() ;
	ofTranslate( ofGetWidth() * 0.5f, ofGetHeight() * 0.5f ) ;

	ofSetColor( 255 ) ;
	cam.getTexture().bind() ;
	plane.draw() ;
	cam.getTexture().unbind() ;

	ofSetColor( 0, 255, 0, 50 ) ;
	plane.drawWireframe() ;

	ofPopMatrix() ;

src.zip (1.4 KB)

I hope that helps. But you can thanks me even if it doesn’t ! :wink:

Thank you! That’s the ticket! I also got my existing mesh working by adding triangles as well as vertices, after taking a look at the grid format in ofxPiMapper