Color and dimensions of ofSpherePrimitive

I am creating primitives using user input for the atributes (location, size, color, etc).

I could easily make a prism, using the following code

ofBoxPrimitive* box = new ofBoxPrimitive();
box->set(w, h, d);
box->setPosition(x, y, z);
//box->rotate(rand() % 360, 1.0, 0.0, 0.0);
//box->rotate(rand() % 360, 0, 1.0, 0.0);
//box->rotate(rand() % 360, 0, 0.0, 1.0);
for (int i = 0; i < 6; i++)
{
    box->setSideColor(i, fillCol);
}

But as for the ofSpherePrimitive, I can’t find how to set the color to something else than white, and the size to something irregular (like a 3D ellipse? I don’t know what’s the word).

I figured out I should use textures to give the sphere a color, but I can’t get it to work.

What is the recommended method to color and stretch an ofSpherePrimitive, or should I use another tye of object entirely? If yes, which one?

Hi Kaito

Usually the color is set using ofSetColor(ofColor::thecoloryouwant); instead of using box->setSideColor(…). I think setSideColor is a handy function to draw colored cubes. @arturo maybe it’s misleading?

If you want to color the ofSpherePrimitive “sphere”:

void ofApp::draw()
{
    ofSetColor(ofColor::blue); // paint blue
    sphere.draw();
}

If you want to stretch it like an [ellipsoid] (https://en.wikipedia.org/wiki/Ellipsoid) then you can use ofScale(x,y,z).

To stretch it horizontally for example.

void ofApp::draw()
{
    ofSetColor(ofColor::blue);
    ofScale(2,1,1); // stretch in the x axis
    sphere.draw();
}

If you are going to apply ofSetColor and ofScale then you might want to look into ofPushMatrix() and ofPushStyle() to apply the effects only to the 3d objects you want

Considering the fact that I have my own primitive class, containing the primitive object and a couple parameters that the user chose when creating it, and I have my own draw function, it would probably be pretty easy to add a ofScale and ofColor in that method and putting them back to normal after calling the “real” draw().

Thanks a lot I’ll try it out!

@Hennio I’m also having troubles coloring a sphere created with the ofSpherePrimitive class. I can’t find any class method which sets the color of the object. The way you suggested is a global color setting, isnt it? I am not sure if this isa good idea, and also I would like to be able to build several spheres all with different colours. Therefore it should be possible in my opinion to set the color of the individual ofSpherePrmitive object.
Actually from the of3dPrimitive classes I only saw that the ofBoxPrimitive has a method for setting color (setSideColor() ).
So since I cant find a method to color primitive 3D objects I only thought of creating my custom mesh in form of a sphere, use some Triangle mode and then the ofMesh method addColor(). But that it has to be somehow like that, when supposedly there are already classes for primitive shapes I dont understand.
I later tried to use the method getMesh() when using the ofSpherePrimitive class and then apply addColor() and this is what actually worked @Kaito_Kid

Would appreciate any help and advice - I dont know what I’m missing - coloring a created prmitive shape cant be that hard I guess and I would have assumed there are class methods for setting colors. But as I said, setting a global color drawing mode is not the way to go for me …

Hey @thomasprade1 , the of3dPrimitive and related classes (like ofSpherePrimitive) have an ofMesh inside of them. You can use .addColor() to add a color for each vertex in the mesh by looping over them:

    ofSpherePrimitive sphere;
    sphere.set(100.f, 10);
    ofColor color = ofColor(255, 0, 127);
    for(size_t i{0}; i < sphere.getMesh().getVertices().size(); ++i)
    {
        sphere.getMesh().addColor(color);
    }

There is a .setColor() method if you want to change an existing color for a vertex (instead of adding a new one). You can also use .addColors() to add whole std::vector of ofFloatColor to the mesh.

Also, don’t be afraid to start a new thread. Sometimes this is easier and better than adding to an older one.