ofTexture on a generative mesh

Hello, I’m trying to add a texture to a mesh that is composed by many cylinders. It looks more or less like this https://github.com/edap/ofxLSystem/raw/master/img/green.png.
If it was only one cylinder, it should not be a problem, but, as the mesh is composed by many cylinder, i do not know how to calculate the text coord. In order to know where is the 1.0 point of mesh I should know how big the texture is, but when i start to draw the mesh i don’t know the final dimension of it. I would like to add a small texture that will be repeated all over the mesh.
I’ve read this thread Help in applying texture to a circular ofMesh, but i can not apply i to my mesh.
Any suggestion is appreciated.

I’m still stuck with this problem. I’ve tried to add a texture but i can not see it when i draw the mesh. In my App.cpp file I’ve the following code

    ofImage bark;
    bark.loadImage("texture1.png");
    bark.getTextureReference().setTextureWrap(GL_REPEAT, GL_REPEAT);
    bark.getTextureReference().setTextureMinMagFilter(GL_NEAREST, GL_NEAREST);

This is the code that generate the Cylinder starting from 2 given ofNode

void ofxLSTube::generate(ofMesh& mesh, const ofxLSBranch branch, const int radius, const int scaledRadius){
    ofMatrix4x4 beginMatrix = branch.begin.getGlobalTransformMatrix();
    ofMatrix4x4 endMatrix = branch.end.getGlobalTransformMatrix();

    // Cylinder body
    int first = mesh.getNumVertices();
    for (int i = 0; i < resolution; i++){
        // if it is the last face, close it where the first face
        // was started
        if(i == resolution -1){
            mesh.addIndex(first+(i*2));
            mesh.addIndex(first);
            mesh.addIndex(first+1);

            mesh.addIndex(first+1);
            mesh.addIndex(first+(i*2)+1);
            mesh.addIndex(first+(i*2));
        }else{
            mesh.addIndex(first+(i*2));
            mesh.addIndex(first+(i*2)+2);
            mesh.addIndex(first+(i*2)+3);

            mesh.addIndex(first+(i*2)+3);
            mesh.addIndex(first+(i*2)+1);
            mesh.addIndex(first+(i*2));
        }
    }
    
    for (int i = 0; i < resolution; i++){
        //calculate x and y component
        float theta = 2.0f * 3.1415926f * float(i) / float(resolution);
        float x = radius * cosf(theta);
        float y = radius * sinf(theta);
        ofVec3f circleBottom = ofVec3f(x, y, 0.0);
        ofVec3f direction = (ofVec3f() + circleBottom).getNormalized();

        float xTop = scaledRadius * cosf(theta);
        float yTop = scaledRadius * sinf(theta);
        ofVec3f circleTop = ofVec3f(xTop, yTop, 0.0);
        ofVec2f tcoord;

        // bottom
        tcoord.y = 0.f;
        tcoord.x = ofMap(i, 0, resolution, 0.f, 1.f);
        mesh.addVertex(circleBottom * beginMatrix);
        mesh.addNormal(direction * beginMatrix.getRotate());
        //top
        tcoord.y = 1.f;
        tcoord.x = ofMap(i, 0, resolution, 0.f, 1.f);
        mesh.addVertex(circleTop * endMatrix);
        mesh.addNormal(direction * endMatrix.getRotate());
    }

    // Cylinder cap
    int topMiddlePoint = mesh.getNumVertices();
    ofVec3f topDir = branch.end.getPosition().getNormalized();
    mesh.addVertex(branch.end.getGlobalPosition());
    mesh.addNormal(topDir * endMatrix.getRotate());
    ofVec2f tcoord;
    tcoord.x = 0.5;
    tcoord.y = 0.5;
    mesh.addTexCoord(tcoord);

    for (int i = 0; i < resolution; i++){
        if (i == (resolution-1)) {
            //closing triangle
            mesh.addIndex(topMiddlePoint);
            mesh.addIndex(topMiddlePoint+ i + 1);
            mesh.addIndex(topMiddlePoint+1);
        } else {
            //indices
            mesh.addIndex(topMiddlePoint);
            mesh.addIndex(topMiddlePoint+ i + 1);
            mesh.addIndex(topMiddlePoint+ i + 2);
        }
        //add vertex
        float theta = 2.0f * 3.1415926f * float(i) / float(resolution);
        float x = scaledRadius * cosf(theta);
        float y = scaledRadius * sinf(theta);
        tcoord.x = ofMap(x, -scaledRadius, +scaledRadius, 0.f, 1.f);
        tcoord.y = ofMap(y, -scaledRadius, +scaledRadius, 0.f, 1.f);

        ofVec3f circleTemp = ofVec3f(x, y, 0.0);
        mesh.addVertex(circleTemp * branch.end.getGlobalTransformMatrix());
        mesh.addNormal(topDir * endMatrix.getRotate());
        mesh.addTexCoord(tcoord);
    }
}

Someone has an idea about what I’m doing wrong?

This is the texture I’m using, it is 512x512 px.

It seems that in order to repeat a texture i’ve to know the exact dimension of the mesh, to calculate how many times to repeat the texture.

Is there a way to repeat the texture without knowing the final size of the mesh?

It may not be visible from your code snippet but did you enable normalized texture coordinates? Otherwise of expects your coordinates to correspond to your image size.

http://openframeworks.cc/documentation/gl/ofTexture/#!show_ofEnableNormalizedTexCoords

The problem is that it is not possible to add correct texture coordinates without knowing the final dimension of the mesh. I’m now trying to figure out a kind of “boundary box” in which the generative mesh will probably evolve. After this i will have a max width on the x axis and a max height on the y axis. I will use these 2 values and the current point in the mesh iteration process to find my texture coordinates.