Dear Paul,
Thanks a lot for your reply, this really helped me to get back on the right tracks !
I tried to derive a minimal example that illustrates what i was looking for, objects positions combined from translations, rotations and scalings, with an axis placed at the center of screen (see integral code below).
I wonder why the toLocal function works when ones deactivate the homogenous coordinates while the inverse function toScreen needs it (at least that’s what i found).
vec2 toScreen( mat4 mat, vec2 posLocal ) {
return vec2( mat * vec4( posLocal, 0, 1 ) ); // homogeneous coords !
}
Also, I am using both the modelMatrix and the viewMatrix - it appeared to me that it was simpler this way if you want to combine several transformations on the modelMatrix.
Anyway, here is the simplest code i could derive, thanks again.
#include "cinder/app/App.h"
#include "cinder/app/RendererGl.h"
#include "cinder/gl/gl.h"
using namespace ci; using namespace ci::app; using namespace std;
class Trs : public App {
public:
float angle = 0;
float angle2 = 0;
vec2 toLocal( mat4 mat, vec2 posScreen ) { // to object Space
return vec2( glm::inverse( mat ) * vec4( posScreen, 0, 0 ) ); // deactivate homogeneous coordinates !
}
void draw() {
gl::clear();
auto rect = Rectf(0, 0, 20, 10); // the shape to draw
gl::pushViewMatrix();
gl::setViewMatrix(glm::translate(gl::getViewMatrix(), vec3( vec2(getWindowSize()) * 0.5f, 0)));
gl::pushModelMatrix();
// translation to the center of the rectangle
vec2 pivot = rect.getCenter();
mat4 mat = glm::translate( vec3( -pivot, 0) );
gl::setModelMatrix( mat );
gl::drawStrokedRect(rect);
// combine a translation and then a rotation
pivot = - toLocal( mat, vec2(100, 0) );
angle += 0.1;
mat = glm::toMat4( glm::angleAxis( angle, vec3( 0, 0, 1 ) ) )
* glm::translate( vec3( -pivot, 0) );
gl::setModelMatrix( mat );
gl::drawStrokedRect(rect);
// combine same translation with scale and other rotation
angle2 += 0.05;
mat = glm::toMat4( glm::angleAxis( angle2, vec3( 0, 0, 1 ) ) )
* glm::scale( vec3(2, 2, 1) )
* glm::translate( vec3( -pivot, 0) );
gl::setModelMatrix( mat );
gl::drawSolidRect(rect);
gl::popModelMatrix();
gl::popViewMatrix();
}
};
CINDER_APP( Trs, RendererGl)