So I am trying to map an image to an arbitrarily shaped polygon, based on small “slits” or pieces of an image.
In processing I did this dividing the image into smaller pieces and defining it in parts for each vertex.
beginShape();
texture(tb); //<--- previously defined PImage
//------------------------------------------------
vertex(x, y, texture_x, texture_y);
//(continue for each vertex)
//------------------------------------------------
endShape();
It is relatively easy to do ofBeginShape() etc in OF, but I’m confused as to how to do the texture mapping part. ofTexture() is a 3d-related class, there’s ofMesh() and ofVbo() (from looking at the 3D primitives example these seem to be closer to what I need)? Not sure how to achieve this, the documentation is a bit sparce in places.
oh yes sorry forgot to mention that you have to bind the texture. you can also do img.bind() without the getTextureReference()
ofMesh or even better ofVboMesh should be slightly faster than of3dPrimitive because of3dPrimitive always uploads a transformation matrix but unless you are drawing tons of them the difference should be insignificant
I am using ofMesh::plane() to create a mesh and I’m not sure how to link it to a set of ofxBox2d joints properly.
//setup
//setting up mesh
img.loadImage("sample_image/ghost_03__02.png");
mesh = mesh.plane(img.width, img_ghost.height, 1, 12);
//setting up 12 circles and connect joints
//(c and j are vectors with circles and joints)
for (int i=0; i<12; i++)
{
ofxBox2dCircle circle;
circle.setPhysics(3.0, 0.53, 0.1);
circle.setup(the_world.getWorld(), ofGetWidth()/2, 100+(i*30), 15);
c.push_back(circle);
}
// now connect each circle with a joint, first one to last.
for (int i=0; i<c.size(); i++)
{
if(i > 0)
{
ofxBox2dJoint joint;
joint.setup(the_world.getWorld(), c[i-1].body, c[i].body);
joint.setLength(50);
j.push_back(joint);
if(i == c.size()-1)
{
ofxBox2dJoint joint2;
joint2.setup(the_world.getWorld(), c[0].body, c[i].body);
joint2.setLength(50);
j.push_back(joint2);
}
}
}
//in update, I set the vertices to the positions of the circles:
for(int i = 0; i < c.size(); i++)
{
tm.setVertex(i, ofPoint(c[i].getPosition().x, c[i].getPosition().y));
}
this obviously doesn’t yield what I need, here is the box2d circles and joints, as expected:
sorry got ahead of myself, idk how plane() is defining the vertices and indices. Looked at the function briefly but was a bit confused, decided to just do it myself:
//m is the mesh
//"y fragment"
int y_fr = img.height/5;
//number of vertices
int n_v = 12;
//define the vertices
for(int i = 0; i < n_v/2; i++)
{
m.addVertex(ofPoint(0, y_fr*i));
m.addTexCoord(ofPoint(0,y_fr*i));
m.addVertex(ofPoint(img.width, y_fr*i));
m.addTexCoord(ofPoint(img.width,y_fr*i));
}
//setup the indices
for(int i = 0; i < n_v - 2; i++)
{
m.addIndex(i);
m.addIndex(i + 1);
m.addIndex(i + 2);
}
//initial circles
for (int i=0; i<n_v/2; i++)
{
ofxBox2dCircle circle;
circle.setPhysics(3.0, 0.53, 0.1);
circle.setup(the_world.getWorld(), 50, 50+ i*(y_fr*.5), 30);
c.push_back(circle);
ofxBox2dCircle circle_2;
circle_2.setPhysics(3.0, 0.53, 0.1);
circle_2.setup(the_world.getWorld(), 50 + (img.width), 50 + i*(y_fr*.5) , 30);
c.push_back(circle_2);
}
// now connect each circle with a joint
for (int i=0; i<c.size()-2; i+=2)
{
ofxBox2dJoint joint;
joint.setup(the_world.getWorld(), c[i].body, c[i+1].body);
joint.setLength(50);
j.push_back(joint);
if(i != 10)
{
ofxBox2dJoint joint1;
joint1.setup(the_world.getWorld(), c[i].body, c[i+2].body);
joint1.setLength(50);
j.push_back(joint1);
ofxBox2dJoint joint2;
joint2.setup(the_world.getWorld(), c[i+1].body, c[i+3].body);
joint2.setLength(50);
j.push_back(joint2);
}
}