2016-01-21 84 views
0

所以我想爲我的批處理渲染系統翻譯CPU上的頂點。我試圖複製glsl,但它不起作用。 (該模型沒有顯示出來)C++ glm Opengl glm :: mat4轉換和旋轉glm :: vec4

glm::vec4 off = glm::vec4(0, 0, 0, 1); 

off = Util::createTransform(offset, glm::vec3(0, 45, 0)) * off; //translated the vertex by the offset(supplied by the function) and rotates by 45 degrees on the Y axis 

for (int i = 0; i < Tvertex.size(); i++) { 
    Tvertex[i] *= glm::vec3(off.x, off.y, off.z); //I think its here I might have messed up? 
} 

這裏是 「的Util :: createTransform」 功能:

glm::mat4 Util::createTransform(glm::vec3 pos, glm::vec3 rot) { 
    glm::mat4 trans = glm::mat4(1.0); 
    trans = glm::rotate(trans, glm::radians(rot.x), glm::vec3(1, 0, 0)); 
    trans = glm::rotate(trans, glm::radians(rot.y), glm::vec3(0, 1, 0)); 
    trans = glm::rotate(trans, glm::radians(rot.z), glm::vec3(0, 0, 1)); 
    trans = glm::translate(trans, pos); 
    return trans; 
} 

那麼,在哪裏我搞砸了?

回答

0

Util::createTransform()返回glm::mat4,而你只需要的是矩陣的最右列,並將其存儲在一個glm::vec4

您正試圖創建一個表示旋轉和平移組成的變換矩陣。此操作不能由單個vec4表示。您可以單獨使用翻譯,然後只需將相同的矢量添加到所有頂點以轉換偏移即可。但是,隨着旋轉 - 或除翻譯之外的其他轉換 - 您將需要完整的矩陣。

由於glm使用與舊的「固定功能」GL相同的約定,所以必須使用Matrix *向量乘法順序將轉換矩陣應用於頂點。所以你的代碼應該是這樣的:

glm::mat4 off = Util::createTransform(offset, glm::vec3(0, 45, 0)) * off; //translated the vertex by the offset(supplied by the function) and rotates by 45 degrees on the Y axis 

for (int i = 0; i < Tvertex.size(); i++) { 
    Tvertex[i] = off * Tvertex[i]; 
} 
0

如何:

// I think its here I might have messed up? 
Tvertex[i] *= glm::vec3(off.x, off.y, off.z); 

// I think that might be what you wanted: 
Tvertex[i] += glm::vec3(off.x, off.y, off.z);