A simple cout << endl fix a bug. What is happening?

#1

Hi,
I’ve got a really strange behavior. My program works as expected with this code:

void Object::updateMatrix() const
	{
		mat = glm::translate( glm::mat4(), glm::vec3( x, y, 0.f ) ) ;
		mat = glm::rotate( mat, rotation, glm::vec3( 0.f, 0.f, 1.f ) ) ;
		mat = glm::scale( mat, glm::vec3( scaleX, scaleY, 1.f ) ) ;
		mat = glm::translate( mat, glm::vec3( -anchorX, -anchorY, 0.f ) ) ;
		cout << endl;
		imat = glm::inverse( mat ) ;
		needMatrixUpdate = false ;
	}

But when I remove the cout line, the program run like if the line marked with a [*] has no effect:

void Object::updateMatrix() const
	{
		mat = glm::translate( glm::mat4(), glm::vec3( x, y, 0.f ) ) ;
		mat = glm::rotate( mat, rotation, glm::vec3( 0.f, 0.f, 1.f ) ) ;
		mat = glm::scale( mat, glm::vec3( scaleX, scaleY, 1.f ) ) ;
		mat = glm::translate( mat, glm::vec3( -anchorX, -anchorY, 0.f ) ) ; // [*]
		imat = glm::inverse( mat ) ;
		needMatrixUpdate = false ;
	}

Another way to fix it is:

void Object::updateMatrix() const
	{
		mat = glm::translate( glm::mat4(), glm::vec3( x, y, 0.f ) ) ;
		mat = glm::rotate( mat, rotation, glm::vec3( 0.f, 0.f, 1.f ) ) ;
		auto mat2 = glm::scale( mat, glm::vec3( scaleX, scaleY, 1.f ) ) ;
		mat = glm::translate( mat2, glm::vec3( -anchorX, -anchorY, 0.f ) ) ;
		imat = glm::inverse( mat ) ;
		needMatrixUpdate = false ;
	}

I can’t figure what is happening here. Where is the bug ? in OF ? in gcc ? in glm ? between the keyboard and the chair ? (yeah, probably the last one)

I have tried to reproduce this bug in a minimal application, but it disappear when I try to isolate it. If you want to have a look, here’s the thing I’m currently coding, this is not so big, the problematic method is in c2d/Object.cpp, line 167
src.zip (8,6 Ko)

The right behavior is that an object must be selected on mouse click. If you delete the line 173 the bug appear.
I’m puzzled, so any thought is welcome :slight_smile:

#2

I ran into similar problems with the previous lines too. Now I do:

	void Object::updateMatrix() const
	{
		auto mat1 = glm::translate( glm::mat4(), glm::vec3( x, y, 0.f ) ) ;
		auto mat2 = glm::rotate( mat1, rotation, glm::vec3( 0.f, 0.f, 1.f ) ) ;
		auto mat3 = glm::scale( mat2, glm::vec3( scaleX, scaleY, 1.f ) ) ;
		mat = glm::translate( mat3, glm::vec3( -anchorX, -anchorY, 0.f ) ) ;
		imat = glm::inverse( mat ) ;
		needMatrixUpdate = false ;
	}

which solve the problem but I still doesn’t understand what’s happening.