Combining meshes with different colours and keeping colours

I’m trying to figure this out and think I’m close but still can’t get it to work.

Say I have two spheres, one black, one red and I want to combine them into one mesh but keeping their colours. I can use sphere1.append(sphere2) which works well but the colours of the first override the second so the second colour info is lost. My thinking has been to get the vertices size of the second sphere and then with the newly appended mesh iterate through those vertices and try and colour each one but it seems setColor is for for setting the colour of the items in the colour vertex list and there doesn’t seem to be a way to set the colour of a vertex directly after it’s been created?

I know I’m missing something here so any help would be appreciated.

I guess I could make an entirely new mesh from the combined meshes and then iterate through that, see if they vertices match the second sphere and colour accordingly?

Are you perhaps adding together two spheres that have the same exact coordinates? If you were using ofMesh::Sphere(...) to create each sphere and then appending them together with sphere1.append(sphere2), then you’d have only the colors from sphere2 visible. They would be drawn directly over sphere1.

If you shift the coordinates of the vertices of one of the spheres, then you’d be able to see both colors. (You’d need to set the primitive mode to triangles when creating your spheres, otherwise you’d end up with a stray face between the last face of sphere1 and the first face of sphere2.)

Thanks Mike for the suggestion but after shifting the x of the second sphere it still gets drawn in the first color. One thing I should mention is there is a bit of the second colour on the second sphere but then the rest is the original sphere colour. It looks to me like the color indices are not mapping correctly.

@mikewesthad One other thing - I’m creating the spheres with ofPrimitiveSphere and then using getMesh() to convert to a mesh. Don’t know if that would cause a problem?

@bjdawes Here’s one way to do it, written in ofSketch style (which is easy to translate into whatever IDE you are using). It’s not necessarily the most efficient way to do things, but efficiency would depend on what you are trying to do.

ofMesh sphere1;
ofMesh sphere2;

void setup() {
    // sphere2: red sphere
	sphere1 = ofMesh::sphere(75, 12, OF_PRIMITIVE_TRIANGLES);
	sphere1.setColorForIndices(0, sphere1.getNumIndices(), ofColor::red);
    
    // sphere1: blue sphere
	sphere2 = ofMesh::sphere(75, 12, OF_PRIMITIVE_TRIANGLES);
	sphere2.setColorForIndices(0, sphere1.getNumIndices(), ofColor::blue);
	
	// Shift sphere2 100 units to the right
	for (int i=0; i<sphere2.getNumVertices(); i++) {
	    sphere2.getVerticesPointer()[i] += ofVec3f(100, 0, 0);    
	}
	
	sphere1.append(sphere2);
}

void draw() {
    ofBackground(0);
    ofPushMatrix();
        ofTranslate(ofGetWidth()/2-50, ofGetHeight()/2);
    	sphere1.drawWireframe();
    ofPopMatrix();
}

I’m not sure why ofSpherePrimitive would be different, but I’ll take a look now.

2 Likes

@bjdawes So the same strategy works for me for ofSpherePrimitive - maybe you could share the code that isn’t working and I could take a peek?

Re: the color sharing between the two spheres - that sounds like it would be due to using OF_PRIMITIVE_TRIANGLE_STRIP as the mode for the meshes.

@mikewesthad thanks Mike - very enlightening, will give this a try tomorrow. And ofSketch looks great!

@mikewesthad - just tried it and it worked perfectly. Wasn’t using colorForIndices! Thanks again - awesome stuff.

1 Like

Ahhhh, I see - yeah that’ll do it. Glad it’s working!