Bind texture to a deformed rectangular

Hello,

I have a texture (220x484) loaded into GPU memory.
I also have a ofPath, which represent a rectangular which can be slightly deformed.
What I need is to apply (bind) a texture to the vertices of my path in a straight forward way:

upper left corner of texture to upper left vertice of ofPath
lower left corner of texture to lower left vertice of ofPath
lower right corner of texture to lower right vertice of ofPath
upper right corner of texture to upper right vertice of ofPath

basically I need to apply a texture on a 2D object.

I looked up in ofBook, but I only found chapter about 3D objects.

Thanks
M

Hi,
Do you really need an ofPath?

ofImage img ;
ofMesh mesh ;
void ofApp::setup()
{
    img.load( "img.jpg" ) ;
    float x = 50.f ;
    float y = 25.f ;
    float w = img.getWidth() ;
    float h = img.getHeight() ;
    mesh.setMode( OF_PRIMITIVE_TRIANGLE_STRIP ) ;

    mesh.addVertex( ofVec3f( x, y ) ) ;
    mesh.addVertex( ofVec3f( x + w, y ) ) ;
    mesh.addVertex( ofVec3f( x, y + h ) ) ;
    mesh.addVertex( ofVec3f( x + w, y + h ) ) ;

    mesh.addTexCoord( ofVec2f( 0.f, 0.f ) ) ;
    mesh.addTexCoord( ofVec2f( w, 0.f ) ) ;
    mesh.addTexCoord( ofVec2f( 0.f, h ) ) ;
    mesh.addTexCoord( ofVec2f( w, h ) ) ;
}

void ofApp::draw()
{
    img.getTexture().bind();
    mesh.draw();
    img.getTexture().unbind();
}

Hello,

Thanks for a quick answer.

Yes, I found a thread talking about Meshes and did pretty much the same thing, but for now I get black screen… will check again to see what am I doing wrong and compare to your code above.

M

You can use an ofPath too if you like. ofPath has the .getTesselation() method which will return an ofMesh for the ofPath. If I remember right, the texcoords are also included. Having oF calculate the ofMesh for a shape can be helpful, especially for shapes with concave or more complex curves.