c++ - Translating the object with glm so that it follows the mouse cursor -


so here want do. want implement translation mouse object translating follow mouse movement i.e if mouse cursor moves amount of pixels x want object move exact same amount x.

so far rotations have been using glm implement arcball , rotation matrix that.

i using opengl , sdl getting mouse coordinates not difficult. can create vector of size 3 current mouse coordinates during mouse motion event with:

while (sdl_pollevent(&event))     {         switch (event.type)         {             case  sdl_mousemotion:              glm::vec3 vectranslation(event.motion.x, event.motion.y, 0);             glm::mat4 translationmatrix ;             glm::translate(translationmatrix, vectranslation) ;          }     } 

with have translation matrix, not me translation following mouse cursor did in end.

would have insights on it?

besides, final projection matrix call glmulmatrix(), do:

glm::matrixcompmult(translationmatrix,rotationmatrix); 

but when so, both translation , rotation don't work. if return rotation matrix , use directly glmulmatrix(), arcball behaves expected, if use code above, can see 1 face of cube have , keeps on changing proportions never rotates nor translates.

as code use drawing: //assume code done know if , translations should done matrixprojection = translationmatrix * mmatnow * scalematrix ; glmatrixmode(gl_modelview); glloadidentity(); glclear(gl_color_buffer_bit); glmultmatrixf(matrixprojection); // scaling transfomation glscalef(0.5,0.5,0.5); drawcube();

    glflush();      sdl_gl_swapbuffers();  

the matrix like: first:

1 ; 0 ; 0 ; 0 ; 0 ; 1 ; 0 ; 0 ; 0 ; 0 ; 1 ; 0 ; 0 ; 0 ; 0 ; 1 ;

then after translation:

1 ; 0 ; 0 ; 0 ; 0 ; 1 ; 0 ; 0 ; 0 ; 0 ; 1 ; -25 ; 0 ; 0 ; 0 ; 1 ;

so apparently translation happen, weird result on drawing:translated image

original drawing: original drawing

you have position of mouse-courser , of object want move.

the easiest way let object follow mouse set position position of mouse:

one way: lets have vec3(10, 10, 4) mouse position, set

. . . 10 . . . 10 . . . 4 0 0 0 1 

(of yourse dont write dots) objects matrix. neccessary mouse position not location on window location in world.

another way: add in variable in vertex-shader this:

layout(location = 0) in vec3 inputposition; layout(location = 1) in vec3 inputoffset; layout(location = 2) in vec4 inputcolor;  out vec4 color;  uniform mat4 worldmatrix; uniform mat4 viewmatrix; uniform mat4 projectionmatrix;  void main(void) {     gl_position = worldmatrix * vec4(inputposition + inputoffset, 1.0f);     gl_position = viewmatrix * gl_position;     gl_position = projectionmatrix * gl_position;      color = inputcolor;  } 

where offset mouse-position vector. approach doesnt attach object mouse moves it. every object dont want move need shader.

edit: if want rotate object points courser, easiest way take middle of object vector , calculate distance between mouse position , object vector. can math right angle , rotate object.

edit2:

try following: once had problem matrix before translation same after translation. because did overwrite matrix anyhow. did: first, make easier, make glm::vec3 position of object. eg. vec3(0,0,0). draw quad it's center @ left upper corner of screen. if update position vector of quad mouse position, mouse respresents center of quad. every frame following: pseudo code:

    positionquad.x = event.motion.x;  positionquad.y = 0;     positionquad.z = event.motion.y;     translationmatrix = glm::translate(positionquad); // declare translationmatrix global/class variable have access anywhere. glm translate multiplies translation vector (vec4(mx, 0, mz, 1) identity matrix. works because initialized quad @ upper left corner     render(); // translationmatrix model matrix. in render still have build mvp matrix , draw always. 

with rotation add

glm::rotate(translationmatrix, degrees, glm::vec3(0,1,0)); //this changes current model-matrix rotating degrees degrees around axis specified in vec3. if otherwise translationmatrix= glm::rotate.... overwrite matrix , wont have translation anymore. 

after translation , before render


Comments