2015-07-10 107 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