.addVertex in ofMesh not recognizing an ofPoint as parameter

I’m trying to use ofPoint objects set in setup() as ofMesh vertices, but they won’t be recognized as parameters of .addVertex()

more precisely (weirdly) all but the first .addVertex() appear to be recognizing the ofPoint objects.

do you know a way to fix this or to use variables as .addVertex() parameters?

thx

void DownGrad::setup(ofPoint corUPleft, ofPoint corUPright, ofPoint corDOWleft, ofPoint corDOWright)
{
    
    corUPleft.set(0, 0);
    corUPright.set(ofGetWidth(), 0);
    corDOWleft.set(0, 500);
    corDOWright.set(ofGetWidth(), 500);
}

//--------------------------------------------------------------
void DownGrad::draw(){
    
    // ofMesh function from Zach: https://forum.openframeworks.cc/t/gradient-fills-for-rects/18184/2
    ofMesh bottleft;
    bottleft.setMode(OF_PRIMITIVE_TRIANGLE_STRIP);
    bottleft.addVertex( corUPleft );
    bottleft.addColor(ofColor(0,0,0,0));
    bottleft.addVertex( corUPright );
    bottleft.addColor(ofColor(0,0,0,0));
    bottleft.addVertex( ofPoint(0,500) );
    bottleft.addColor(ofColor::blue);
    bottleft.addVertex( ofPoint(ofGetWidth(),500) );
    bottleft.addColor(ofColor::blue);
    bottleft.draw();

}



you have a scope problem with that.
the DownGrad::setup function is setting the parameters it has been passed, but these are copies of whatever you are passing to that functions, thus these will get destroyed once the function ends. on top of this, the function’s parameters have the same name as your class variables, which are confusing you, although it is very clearly established how such thing behaves within C++. google for “C++ function argument pass by value or reference” for an explanation.

Considering that you want to modify those class variables you should either remove all the parameters from DownGrad::setup or do something like

void DownGrad::setup(ofPoint _corUPleft, ofPoint _corUPright, ofPoint _corDOWleft, ofPoint _corDOWright)
{
    corUPleft = _corUPleft;
    corUPright = _corUPright;
    corDOWleft = _corDOWleft;
    corDOWright = _corDOWright;
}

and you pass to this functions the desired values when calling it.

thanks, roy!

i tried removing all the parameters –only defining the ofPoints in my .h file and then setting them in the setup() for later use in the draw()

void DownGrad::setup()
{
    
    corUPleft.set(0, 0);
    corUPright.set(ofGetWidth(), 0);
    corDOWleft.set(0, 500);
    corDOWright.set(ofGetWidth(), 500);
    
    xLeft = 0;
    xRight = ofGetWidth() ;
    yUP = 0;
    yDOWN = 500;

i also tried defining some variables (xLeft and so on) in the .h file, then assigning them a value in setup() as seen above, and finally passing those variables inside the ofPoint() inside the .addVertex() of the ofMesh in draw()

void DownGrad::draw(){

    ofMesh bottleft;
    bottleft.setMode(OF_PRIMITIVE_TRIANGLE_STRIP);
    bottleft.addVertex( ofPoint(xLeft, yUP) );
    bottleft.addColor(ofColor(0,0,0,0));
    bottleft.addVertex( ofPoint(xRight, yUP) );
    bottleft.addColor(ofColor(0,0,0,0));
    bottleft.addVertex( ofPoint(xLeft, yDOWN) );
    bottleft.addColor(ofColor::blue);
    bottleft.addVertex( ofPoint(xRight, yDOWN) );
    bottleft.addColor(ofColor::blue);
    bottleft.draw();

}

but neither option worked :confused:

i don’t know what am i still doing wrong. i skipped trying to pass the ofPoints (the corUPleft and so on) as references (using &).

any further thoughts on this?

THat should work.
I created a new, empty project in ProjectGenerator and pasted the following into the draw function

void ofApp::draw(){
    
    float xLeft = 0;
    float xRight = ofGetWidth() ;
    float yUP = 0;
    float yDOWN = 500;
    
    ofMesh bottleft;
    bottleft.setMode(OF_PRIMITIVE_TRIANGLE_STRIP);
    bottleft.addVertex( ofPoint(xLeft, yUP) );
    bottleft.addColor(ofColor(0,0,0,0));
    bottleft.addVertex( ofPoint(xRight, yUP) );
    bottleft.addColor(ofColor(0,0,0,0));
    bottleft.addVertex( ofPoint(xLeft, yDOWN) );
    bottleft.addColor(ofColor::blue);
    bottleft.addVertex( ofPoint(xRight, yDOWN) );
    bottleft.addColor(ofColor::blue);
    bottleft.draw();

}

This is the result.


Is this what you are expecting? is this what you are getting?
If not, then, are you doing anythhing else with those variables inside the DownGrad class?

it’s working now!

following an example app, i set the variables in DownGrad::DownGrad()

DownGrad::DownGrad()
{

    xLeft = 0;
    xRight = ofGetWidth() ;
    yUP = 0;
    yDOWN = 500;
    
}

and then only initialized (?) them in setup()

void DownGrad::setup(float _yUP, float _yDOWN, float _xLeft, float _xRight){
    
    xLeft = _xLeft;
    xRight = _xRight ;
    yUP = _yUP;
    yDOWN = _yDOWN;

}

yes! that’s what i was expecting and now also getting.

i think i had to do it slightly differently (the instantiation?) because i’m not working this code in the offApp.cpp but in a “side” module (sorry for my improvised vocabulary):

Captura de Pantalla 2022-01-04 a la(s) 17.03.23

well it would depend on how you are instatiating and calling the instances different methods if it matters or not.
for example if you use ofGetWidth() in the constructor and use a regular instance that value might not work because ofGetWidth() gets the correct value once the window has been setup which is most probably after the instances constructors were called.

There is no need to set the values twice, in the constructor and in setup function, considering that you are calling the setup function.