How to rotate ofQuaternion by 180 degrees around Z axis?

Hi, I’m trying to find a way to flip the orientation of the lighting as if the window is displayed upside down.
I simply tried to multiply ofQuaternion(180, ofVec3f(0,0,1)) to the current orientation. But it seems to be not working correctly on some situations.

Here’s my test code that flips the current orientation of the lighting whenever key is pressed. And moving the mouse changes the degrees.

In ofApp.h

ofLight light;
float degrees;
bool shouldFlip;

In ofApp.cpp

//--------------------------------------------------------------
void ofApp::setup(){

    ofEnableDepthTest();
    light.setDirectional();
    degrees = 120;
    shouldFlip = false;
}

//--------------------------------------------------------------
void ofApp::update(){

    ofQuaternion rotate = ofQuaternion(degrees, ofVec3f(0,1,0));
    if (shouldFlip)
        rotate *= ofQuaternion(180, ofVec3f(0,0,1)); //flip orientation
    light.setOrientation(rotate);
}

//--------------------------------------------------------------
void ofApp::draw(){

    ofEnableLighting();
    light.enable();
    ofDrawSphere(ofGetWidth()/2, ofGetHeight()/2, 100);
    light.disable();
    ofDisableLighting();
}

//--------------------------------------------------------------
void ofApp::keyPressed(int key){
    shouldFlip = !shouldFlip; //toggle flip
}

//--------------------------------------------------------------
void ofApp::keyReleased(int key){

}

//--------------------------------------------------------------
void ofApp::mouseMoved(int x, int y){

    degrees = ((float)x / (float)ofGetWidth()) * 180;
    cout << degrees << endl;
}

If I flip the orientation, the orientation of the lighting is not reliable and doesn’t move smoothly.
Can someone please explain why this happens and how to fix it?
Thank you so much in advance!

EDIT : If I change rotate *= ofQuaternion(180, ofVec3f(0,0,1)); to rotate *= ofQuaternion(180.017, ofVec3f(0,0,1));, it seems to work fine but I don’t understand why and I’m not sure if it’s safe.

Actually, tour code is flipping the light every time the update function get called when shouldFlip == true. Is this what you want?

Yes, I want to flip/unflip the orientation of the lighting as if the window is rotated upside down.

So if shouldFlip is set to true, it should flip the lighting from original orientation.

But this flip of 180 degree is happening 60 times per second when shouldFlip is true. Is this what you want?
If you want to flip and unflip when the mouse is clicked, I would add a listener to the event MousePressed, and I would flip just once, when the event is fired. Your code for the rotation looks correct to me.

Hey Thanks.
I just tried the same code in the nightly build and it worked without the problem.
So I’m guessing this was v0.9.8’s bug which is fixed now.