Hi,

How to Mask a image on ofMesh?

I’m trying to put an masked image on ofMesh. to use with box2d ofxBox2dPolygon.

1 Like

See this example of using `ofMesh` with images, transparency, etc.

https://github.com/bakercp/ExperimentalMedia/tree/master/Week9/UnicornSchmear

2 Likes

Thank you very much for quick replay.
I tested simple this code want to add full size image in meshes.

image get distorted… could you help how can I fix image distortion.

thank you.

``````	//Pyramid's base vertices
ofPoint v0 = ofPoint( -200, -100, 0 );
ofPoint v1 = ofPoint( 200, -100, 0 );
ofPoint v2 = ofPoint(  0, 200, 0 );
//Pyramid's top vertex
ofPoint v3 = ofPoint( 0, 0, 0 );

mesh.setupIndicesAuto();	//Set up indices

//Set up a texture coordinates for all the vertices
int w = image.getWidth();
int h = image.getHeight();

mesh.addTexCoord( ofPoint( w/2.0, h/2.0 ) );	//v3
mesh.addTexCoord( ofPoint( w/2.0, 0 ) );		//v2
mesh.addTexCoord( ofPoint( 0, h ) );		//v0

mesh.addTexCoord( ofPoint( w/2.0, h/2.0  ) );	//v3
mesh.addTexCoord( ofPoint( w, h ) );		//v1
mesh.addTexCoord( ofPoint( w/2.0, 0 ) );		//v2

mesh.addTexCoord( ofPoint( w/2.0, h/2.0  ) );	//v3
mesh.addTexCoord( ofPoint( 0, h ) );		//v0
mesh.addTexCoord( ofPoint( w, h ) );		//v1
``````

Thank you for great sample code. I’m getting closer… but still texture distorted. I may miss understood about TexCoord.

``````void testApp::setup(){

mesh.setMode(OF_PRIMITIVE_TRIANGLE_STRIP);
int   nPts  = 8;
int r = 100;
int cx = 200;
int cy = 200;

float scale = r / image.getWidth();

for (int i=0; i<nPts; i++) {
float n = ofMap(i, 0, nPts-1, 0.0, TWO_PI);
float x = cos(n);
float y = sin(n);
float d = ofRandom(-r/2, r/2);
pts.push_back(ofPoint(cx + (x * r + d), cy + (y * r + d)));
}
setNormals( mesh );			//Set normals to the surface
light.enable();
``````

}

//--------------------------------------------------------------
void testApp::update(){

}

//--------------------------------------------------------------
void testApp::draw(){
ofEnableDepthTest(); //Enable z-buffering

``````//Set a background
ofBackgroundGradient( ofColor( 255 ), ofColor( 128 ) );

ofPushMatrix();						//Store the coordinate system

mesh.clearVertices();
int minx, miny = 1000;
int maxx,maxy = 0;
for (int i=0; i<pts.size(); i++) {
if(pts[i].x < minx) {
minx = pts[i].x;
}

if(maxx < pts[i].x) {
maxx = pts[i].x;
}

if(pts[i].y < miny) {
miny = pts[i].y;
}

if(maxy < pts[i].y ) {
maxy = pts[i].y;
}
}

ofPoint center = ofPoint((maxx - minx) / 2.0, (maxy - miny) / 2.0);
for (int i=0; i<pts.size(); i++) {
}

int imagew = image.width;
int imageh = image.height;
ofPoint imageCenter = ofPoint(imagew/2.0, imageh/2.0);

for (int i=0; i<pts.size(); i++) {
int x = ofMap(pts[i].x, minx, maxx,  0, imagew);
int y = ofMap(pts[i].y, miny, maxy, 0, imageh);
}
int x = ofMap(pts.front().x, minx, maxx,  0, imagew);
int y = ofMap(pts.front().y, miny, maxy, 0, imageh);

ofSetColor( 255, 255, 255 );		//Set color
image.bind();						//Use image's texture for drawing
mesh.draw();						//Draw mesh
image.unbind();						//End using image's texture

ofPopMatrix();						//Restore the coordinate system
``````

}

//--------------------------------------------------------------
//Universal function which sets normals for the triangle mesh
void setNormals( ofMesh &mesh ){

``````//The number of the vertices
int nV = mesh.getNumVertices();

//The number of the triangles
int nT = mesh.getNumIndices() / 3;

vector<ofPoint> norm( nV );			//Array for the normals

//Scan all the triangles. For each triangle add its
//normal to norm's vectors of triangle's vertices
for (int t=0; t<nT; t++) {

//Get indices of the triangle t
int i1 = mesh.getIndex( 3 * t );
int i2 = mesh.getIndex( 3 * t + 1 );
int i3 = mesh.getIndex( 3 * t + 2 );

//Get vertices of the triangle
const ofPoint &v1 = mesh.getVertex( i1 );
const ofPoint &v2 = mesh.getVertex( i2 );
const ofPoint &v3 = mesh.getVertex( i3 );

//Compute the triangle's normal
ofPoint dir = ( (v2 - v1).crossed( v3 - v1 ) ).normalized();

//Accumulate it to norm array for i1, i2, i3
norm[ i1 ] += dir;
norm[ i2 ] += dir;
norm[ i3 ] += dir;
}

//Normalize the normal's length
for (int i=0; i<nV; i++) {
norm[i].normalize();
}

//Set the normals to mesh
mesh.clearNormals();