A question about how drawing happens

Hi there,
I have a tupid question about how drawing happens in openFrameworks, and I feel I’m missing some bit of info. I have the following function

void ofApp::drawMesh(){
ofMesh texture;
texture.setMode(OF_PRIMITIVE_TRIANGLES);

faces = mesh.getUniqueFaces();
 for (int i = 0; i < faces.size(); i++)
 {
     ofMeshFace face = faces[i];
     ofPoint center = (face.getVertex(0) + face.getVertex(1) + face.getVertex(2))/3;
     texture.addVertex(face.getVertex(0));
     texture.addTexCoord(face.getVertex(0));
     texture.addVertex(face.getVertex(1));
     texture.addTexCoord(face.getVertex(1));
     texture.addVertex(face.getVertex(2));
     texture.addTexCoord(face.getVertex(2));
      ofEnableAlphaBlending();
     ofEnableBlendMode(OF_BLENDMODE_ADD);
     ofSetColor(pixels.getColor(center.x, center.y), 100);
     ofTriangle(face.getVertex(0), face.getVertex(1), face.getVertex(2));
     wood.bind();
     ofSetColor(255, 255, 255, 50);
     texture.draw();
     wood.unbind();
 }

}

Here wood is an ofImage object, which I have loaded with a wood texture. I would expect this to happens: to each face of the mesh, it draws a triangle with color obtained from the ofPixel object pixels, with an alpha value of 200, and to this it adds another triangle filled with the texture wood, at given coordinates. What happens, instead, is that I can only see the colored triangles, or, if I do the binding and mesh drawing before, I see only the wood textures. Is this expected, and if so, why, or am I missing something?
Also, can I color the mesh faces without color interpolation? This would spare the drawing of triangles as I do here…
Best,
Alessandro

you shouldn’t be drawing the mesh on every cycle of the loop like that, not sure what you are trying to do but it should look something like:

void ofApp::drawMesh(){
    ofMesh texture;
    texture.setMode(OF_PRIMITIVE_TRIANGLES);

    faces = mesh.getUniqueFaces();
    for (int i = 0; i &lt; faces.size(); i++)
    {
         ofMeshFace face = faces[i];
         ofPoint center = (face.getVertex(0) + face.getVertex(1) + face.getVertex(2))/3;
         texture.addVertex(face.getVertex(0));
         texture.addTexCoord(face.getVertex(0));
         texture.addColor(ofColor((pixels.getColor(center.x, center.y), 100));
         texture.addVertex(face.getVertex(1));
         texture.addTexCoord(face.getVertex(1));
         texture.addColor(ofColor((pixels.getColor(center.x, center.y), 100));
         texture.addVertex(face.getVertex(2));
         texture.addTexCoord(face.getVertex(2));
         texture.addColor(ofColor((pixels.getColor(center.x, center.y), 100));
    }

    ofEnableAlphaBlending();
    ofEnableBlendMode(OF_BLENDMODE_ADD);
    wood.bind();
    ofSetColor(255, 255, 255, 50);
    texture.draw();
    wood.unbind();
}

Hi Arturo,
you are right about the drawing outside the loop, but this doesn’t do what I have in mind. Basically, I have a mesh from a Delauney decomposition, I fill each triangle with the color of an image I have loaded, at the position of the center of the triangle. This works pretty well. Now, I would like the triangle faces to have some texture, in this case woodish, still preserving the color.
Something like this, basically

Best,
Alessandro

adding color and texture coordinates as i posted should work but you can’t control how “predominant” is the texture. the best way to do this is by using a shader to control how much of the texture and the colors are applied and how they are mixed

Ah, I see. I’ll think about it more, then, one more reason to try to learn how to use shaders :smile: .
Thanks for the help.
Best,
Alessandro

@mimetik yeah checkout the multiTextureShaderExample!

Thanks for the tip!

Alessandro