Mesh morphing between two colors

Hey,

I would like to change the color of a mesh to a specific color and then return to the original color. My problem is that most of the vertices have different colors to start with.
I managed to change it to a specific color, but I cant find a way to return to the original color.
What would be a good way to morph back and forth between the two states of color?
Thank you! :slight_smile:

for(int i = 0; i < mesh.getNumColors(); i = i+10){
    
    mesh.getColors()[i];
    mesh.setColor(i, ofFloatColor(0.2, 0.3, 0.4));
    
//    Also tried something like this, but its not working:

//    ofColor c = mesh2.getColors()[i];
//    c.r = 0;  c.g = 0;  c.b = 0;
//    mesh2.addColor(c);
}

Dear Maedd,

I don’t know about meshes, so chances are I don’t understand.
But to me it seems to me you are already there! You would just have to store the original color in a variable to be able to swith between the 2 states:

// Code untested, just to get the idea!

vector <C> originalColor;
...
for(int i = 0; i < mesh.getNumColors(); i = i+10){
    // store the original color for later use
    originalColor.push_back(mesh.getColors()[i]);
    mesh.setColor(i, ofFloatColor(0.2, 0.3, 0.4));
}

// change back later
for(int i = 0; i < mesh.getNumColors(); i = i+10){
    mesh.setColor(i,originalColor[i]);
}

If you want to add a slow change of color it’s just maths. There’s multile ways of doing so. You might want to set variables:

  • a target value you set in the above loop
  • an actual value (which is the color of the vertice at a specific time, so might be a color “in between” the desired values)
  • then you calculate and set the step between the values in update() function.

Hope I understood correctly?

have a great day!
oe

Thank you! That’s what I was looking for.
I just got into vectors and I am having trouble passing multiple elements at the same time in the vector. I get the error "No matching member function for call to ‘push_back’ "

vector <float> originalColor[8];

// this works
originalColor[0].push_back(ofRandom(0.2));

// this doesnt
originalColor[0].push_back(mesh.getColors()[i]));

cout << mesh.getColors()[i]) << endl;  // gives me the r/g/b/a values 

As soon as I pass more elements (which I think are r/g/b/a) in the vector at the same time it doesnt work. I also tried out the code below which I found in another thread

originalColor.push_back(ofColor(ofRandom(0.0, 1.0), ofRandom(0.0, 1.0), ofRandom(0.0, 1.0)));

which gives the same error. What am I missing?

Thank you and also have a great day!

Hi Maedd,

there is two problems, both C++. First is a confusion between arrays and vectors. A vector would be defined like this:

vector <float> originalColor;

Note that there is no brackets. You can then push_back (= add on the end) as many float values to the vector as you like. Later (after filling them by push_back of course) you can access them like this:

originalColor[0];
originalColor[2];

The brackets mean that you access one specific value, above the first value (index 0) and the third value (index 2);

Acessing an Array will look the same. But defining an Array or list looks like this:

float originalColor[8];

Here (on defining) the curly brackets mean something different: the Array has a fixed number of Elements, here 8. So it is not flexible as a vector.

In your code you somehow mix both: you define a vector of Arrays of floats - or the like, but let’s not go further. Stick to one. As you do not know the number of elements, I’d advise stick to vectors.

Second Problem: actually you can push one specific type of variable/object in a vector: the type you defined the vector to hold. So you can push a float in a vector if you defined a vector of floats. Or you can push a color to a vector you defined to be a vector of colors.
So this should be working (please note that again there is no brackets on defining nore adding an object to the vector, only on reading one specific object/value):

// define a vector of floats
vector <float> originalColor;

// add a float to the end of the vector. Size of vector is 1 afterwards
originalColor.push_back(ofRandom(0.2));
// add another float to the end of vector. Size of vector is 2 afterwards
originalColor.push_back(ofRandom(0.2));

cout << originalColor[0] << "- " <<  originalColor[1]; 

Or you can do the same with int or string or ofColor…

// define a vector of ofFloatColor 
vector <ofFloatColor > originalColor;

// add a ofFloatColor to the end of the vector. Size of vector is 1 afterwards
originalColor.push_back( ofFloatColor ( ofRandom(0.0, 1.0), ofRandom(0.0, 1.0), ofRandom(0.0, 1.0) ) );
// add another ofFloatColor to the end of vector. Size of vector is 2 afterwards
originalColor.push_back( ofFloatColor ( ofRandom(0.0, 1.0), ofRandom(0.0, 1.0), ofRandom(0.0, 1.0) ) );

cout << originalColor[0].r << "/"  <<  originalColor[0].g << "/" << originalColor[0].b << " - "  << originalColor[1].r << "/" << originalColor[1].g << "/" << originalColor[1].b; 

Again all untested. Just 2 more remarks: ofColor takes 3 or 4 arguments from 0-255. ofFloatColor is more or less the same but uses 3 or 4 arguments from 0.0 to 1.0 (like you did in your code). So I changed it, otherwise all will be black (or neaaarly black) all the time.

Second thing is: you might not get an answer if you ask mere C++ questions in this forum. Do not get me wrong, you are always welcome to ask questions and we all want you to progress and prosper. Also it is difficult to distiguish sometimes. But just for you to know: all of the above has been pure C++, not openFrameworks. Can happen that nobody feels obliged to answer…

have a great day!
oe

1 Like