Coloring / Texturing Triangles in an ofMesh

Hi guys,

I’ve built an ofMesh using GL_TRIANGLE_STRIP and I’m trying to figure out how to apply a unique solid color (or texture) to each of the triangles in the mesh.

I tried ofMesh.addColor() but that appears to be setting the color of the vertices, which creates fuzzy gradients across the triangles which is not what I want.

Before I burden you guys with code, what’s the best way to approach this? I’m brand new to OpenGL so any advice would be terrifically welcome.

Here’s a screenshot of what my mesh currently looks like.
The triangles are connected by passing indices to ofMesh.addIndex() and I have a separate vector for the vertices that is sent to the vbo:

  
myVbo.setVertexData( &vertices[0], numVertices, GL_DYNAMIC_DRAW );  

thanks!

Turns out to shade triangles individually you have to draw them using GL_TRIANGLES as OpenGL lerps between the colors of the vertices when using TRIANGLE_STRIP or TRIANGLE_FAN which causes gradients instead of solid fills.

I rewrote my mesh giving each triangle three unique vertices which is probably less performant than using shared vertices but at least now I can shade each triangle independently.

![](http://forum.openframeworks.cc/uploads/default/2867/Screen Shot 2013-04-08 at 7.40.58 PM.png)

1 Like

After a number of iterations, here’s the mesh responding to input from a Leap and a video camera.

http://www.youtube.com/watch?v=J2bO8ns-eSo

Ah nice, it’s always good to see people work through problems on the forum so other people can learn from what you’re learning. Thanks for updating this!

1 Like

Hi Stephen!

Apologies for gravedigging but it looks like this is exactly the same problem i’ve run into - I’m needing a similar fix to prevent the linear gradient fill between the triangles.

Did you happen to still have the code for how you rewrote the mesh to draw the solid colours?

Thanks ever so much!
James.

Hi, this was a closed source project so I can’t share the code.

Just group your vertices into triangles and then have the three vertices of each triangle share the color that you want to fill it with. This will result in adjacent triangles having vertices with the same coordinates which sounds wasteful but AFAIK it’s the only way to get around having your geometry lerp between colors.

Hi oldfield,
I came across the same problem, and the way i managed to solve it is by creating a copy of the ofMesh in this manner:

ofMesh explode_mesh(ofMesh * inMesh) { 
			ofMesh temp = *inMesh; 
			ofMesh outMesh;
			outMesh.setMode(OF_PRIMITIVE_TRIANGLES);
			for (int i = 0; i < temp.getNumIndices() - 2; i += 3) {
				ofColor color((int)ofRandom(256.0f), (int)ofRandom(256.0f), (int)ofRandom(256.0f));
				outMesh.addVertex(temp.getVertex(temp.getIndex(i)));
				outMesh.addVertex(temp.getVertex(temp.getIndex(i + 1)));
				outMesh.addVertex(temp.getVertex(temp.getIndex(i + 2)));
				outMesh.addIndex(i);
				outMesh.addIndex(i + 1);
				outMesh.addIndex(i + 2);
				outMesh.addColor(color);
				outMesh.addColor(color);
				outMesh.addColor(color);
			}
			return outMesh;
		}

The above procedure assume that the input mesh has its indices assigned so as to generate the mesh triangles. What it does is it loops through all the triangles and duplicates its vertices. It then assigns a single color to each of the three vertex duplicates.

for example if i have a mesh, call it originalMesh, that i want to render each of its triangles in a different color, I do the following:

in ofApp.h

class ofApp : public ofBaseApp{
.
.
.
ofMesh originalMesh;  //mesh to be generated by some proceedure
ofMesh copyMesh;     //mesh to be rendered
.
.
.
};

in ofApp.cpp

ofApp::setup(){
.
.
.
originalMesh = some_mesh_generating_proceedure();   
//this should generate the mesh you want with each triangle identified with indices

copyMesh = explode_mesh(&mesh); 
//this is the mesh you want to render
.
.
.
}

ofApp::draw(){
.
.
.
copyMesh.draw();
.
.
.
}

Hope this helps!
Cheers,
AA

1 Like