2015-07-10 108 views
1

所以这就是我想要做的。我想用我的鼠标实现翻译,以便我翻译的对象将跟随我的鼠标移动,即如果我的鼠标光标移动了一定数量的像素,XI希望对象移动该确切相同的X.用glm翻译对象,使其跟随鼠标指针

到目前为止,我一直在使用glm来实现arcball,并且我得到了一个旋转矩阵。

我正在使用openGL和SDL,因此获取我的鼠标坐标并不困难。 我可以创建一个大小为3的向量与当前的鼠标,用鼠标移动事件中的坐标:

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) ; 

     } 
    } 

有了,我有一个转换矩阵,但不会让我翻译下面到底是什么鼠标光标最终做到了。

有人会对此有所了解吗?

此外,让我最后的投影矩阵,以便调用glMulMatrix(),我做的:

glm::matrixCompMult(translationMatrix,rotationMatrix); 

但是,当我这样做,无论是平移和旋转不工作。如果我简单地返回旋转矩阵并直接使用glMulMatrix(),则我的arcball的行为与预期相同,但是如果使用上面的代码,我只能看到我拥有的立方体的一个面,并且它不断改变其比例但不会旋转也不翻译。

至于我使用的绘图代码: //假设一些代码做是为了知道,应该做些什么翻译 matrixProjection = translationMatrix * mMatNow * scaleMatrix; glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glClear(GL_COLOR_BUFFER_BIT); glMultMatrixf(matrixProjection); //比例转换 glScalef(0.5,0.5,0.5); drawCube();

glFlush(); 
    SDL_GL_SwapBuffers(); 

,我得到的矩阵是这样的:首先 :

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

然后翻译后:

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

因此很明显,翻译确实发生,但我得到我的画这个怪异的结果:Translated image

原画:Original Drawing

+1

之前,如果你反对投票和投票关闭请说明原因。如果没有反馈,问题无法改进 – LBes

回答

1

你有你的鼠标骏马的位置和对象的要移动。

,让你的对象按照你的鼠标将其位置设置成鼠标的位置,最简单的方法:

一个办法:让我们说你有VEC 3(10,10,4)作为你的鼠标位置,然后简单地设置

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

(的yourse不写点) 作为对象矩阵。为此,需要鼠标位置不是窗口上的位置,而是世界上的位置。

另一种方式:在 变量在顶点着色器像这样添加:

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; 

} 

其中所述偏移是鼠标位置矢量。这种方法不会将对象附加到鼠标上,但会随之移动它。 同样对于你不想移动的每个对象,你都需要另一个着色器。

编辑: 如果你想旋转它指向你的courser的对象,最简单的方法是将该对象的中间作为一个向量并计算鼠标位置和对象向量之间的距离。然后你可以做一些数学来获得正确的角度并旋转物体。

EDIT2:

尝试以下操作:我也曾经有这样的翻译在我的矩阵就像平移后的相同的问题。这是因为他无论如何都覆盖了矩阵。 我做了什么: 首先,为了使它更容易,使用你的对象的位置做一个glm :: vec3。例如。 VEC3(0,0,0)。 然后画出你的四边形,它的中心位于屏幕的左上角。如果现在使用鼠标位置更新四方形的位置矢量,则鼠标将表示四边形的中心。 所以每帧你做到以下几点: 伪代码:

positionQuad.x = event.motion.x; 
positionQuad.y = 0; 
    positionQuad.z = event.motion.y; 
    translationMatrix = glm::translate(positionQuad); // Declare translationMatrix as global/class variable to have access anywhere. glm translate multiplies the translation vector (vec4(mX, 0, mZ, 1) with an identity matrix. This works because we initialized the quad at upper left corner 
    render(); // translationMatrix now is the model matrix. in render you still have to build the MVP matrix and draw it like always. 

随着旋转只需添加

glm::rotate(translationMatrix, DEGREES, glm::vec3(0,1,0)); //this changes the current model-matrix by rotating it by DEGREES degrees around the axis specified in the vec3. If you do it otherwise like translationMatrix= glm::rotate.... you would overwrite the Matrix and you wont have any translation anymore. 

翻译后呈现

+0

将尽快尝试,并让你知道它是如何发展的。无论如何,我可能会有一些问题^^ – LBes

+0

最终它并没有帮助,因为我想直接使用GLM中使用的函数。似乎有翻译功能,但它不起作用 – LBes

+1

它有多种原因。第一:如何计算鼠标X和Y?你是否获得屏幕坐标(例如523,445)还是获得增量? (1.456,0.235)。如果你得到增量,你不会真的移动对象,因为如果你不移动鼠标,位置会重置。另一件事,尝试yourMatrix = glm :: translate(vec3(mouseX,mouseY,0)); ,这是我如何做翻译。然后我旋转这样做:yourMatrix = glm :: rotate(yourMatrix,degrees,vec3(0,1,0))。如果你不这样做,你可能会覆盖你的矩阵,它不会再翻译。我也使用着色器来绘制MVP – Eskalior