2012-02-17 45 views
2

我目前正試圖設置一個GPU皮膚(與glsl),但它不工作的方式,我會:)實際上它根本不工作。我的網消失的時候我嘗試這個GLSL代碼:Glsl皮膚障礙,誰可以跳呢?

layout(location = 0) in vec3 vertexPos; 
layout(location = 1) in vec2 vertexUv; 
layout(location = 2) in vec3 vertexNor; 
layout(location = 5) in ivec4 joints_influences; 
layout(location = 6) in vec4 weights_influences; 

uniform mat4 ViewProj, View, Proj, Model; 
out vec3  vertexPosEye; 
out vec3  vertexNorEye; 

const int  MAX_INFLUENCES = 4; 
const int  MAX_BONES = 50; 
uniform mat4 animation_matrices[MAX_BONES]; 
uniform mat4 inv_bind_matrices[MAX_BONES]; 

void main() 
{ 
    vertexPosEye = (View * Model * vec4(vertexPos, 1)).xyz;  // Position 
    vertexNorEye = (View * Model * vec4(vertexNor, 0)).xyz;  // Normal matrix 

    vec4 final_v = vec4(0, 0, 0, 0); 

    for (int i = 0; i < MAX_INFLUENCES; i++) 
    { 
     vec4 v =  vec4(vertexPos, 1) 
        * inv_bind_matrices[joints_influences[i]] 
        * animation_matrices[joints_influences[i]] 
        * weights_influences[i]; 

     final_v += v; 
    } 
    gl_Position = ViewProj * Model * final_v; 
} 

當我試試這個:

gl_Position = ViewProj * Model * vertexPos; 

我的網又回來了:)但沒有動畫了,當然......

這裏是我的應用(C++)代碼時,我設置VBO屬性:

// Vertex position 
glGenBuffers(1, &buffer[0]); 
glBindBuffer(GL_ARRAY_BUFFER, buffer[0]); 
glBufferData(GL_ARRAY_BUFFER, vertices.pos.size() * sizeof(bVector3), &vertices.pos[0], GL_STATIC_DRAW); 

// Ibid for uv, normals, tangents and bitangents. 

// Skinning : joints index 
glGenBuffers(1, &buffer[5]); 
glBindBuffer(GL_ARRAY_BUFFER, buffer[5]); 
glBufferData(GL_ARRAY_BUFFER, vertices.joints.size() * sizeof(SkinningJoints), &vertices.joints[0], GL_STATIC_DRAW); 

// Skinning : weights 
glGenBuffers(1, &buffer[6]); 
glBindBuffer(GL_ARRAY_BUFFER, buffer[6]); 
glBufferData(GL_ARRAY_BUFFER, vertices.weights.size() * sizeof(SkinningWeights), &vertices.weights[0], GL_STATIC_DRAW); 

// Indices 
glGenBuffers(1, &buffer[7]); 
glBindBuffer(GL_ARRAY_BUFFER, buffer[7]); 
glBufferData(GL_ARRAY_BUFFER, vertices.indices.size() * sizeof(bUshort), &vertices.indices[0], GL_STATIC_DRAW); 

在主循環:

glEnableVertexAttribArray(0); 
glBindBuffer(GL_ARRAY_BUFFER, m->GetBuffer(0)); 
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0)); 

glEnableVertexAttribArray(for uv, normals, tangents and bitangents)... 

glEnableVertexAttribArray(5); 
glBindBuffer(GL_ARRAY_BUFFER, m->GetBuffer(5)); 
glVertexAttribPointer(5, 4, GL_INT, GL_FALSE, 0, BUFFER_OFFSET(0)); 

glEnableVertexAttribArray(6); 
glBindBuffer(GL_ARRAY_BUFFER, m->GetBuffer(6)); 
glVertexAttribPointer(6, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0)); 

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m->GetBuffer(7)); 
glDrawElements(GL_TRIANGLES, m->vertices.indices.size(), GL_UNSIGNED_SHORT, BUFFER_OFFSET(0)); 

這裏是我的RenderingVertices結構(Barr的recomendations後):

struct RenderingVertices 
{ 
    // std::vector<Vec3> 
    vVec3      pos, nor, tang, btan; 
    vVec2      uv; 
    vUshort      indices; 

    vector<SkinningJoints>  joints; 
    vector<SkinningWeights>  weights; 
}; 

這裏是我的SkinningJoints結構:

struct SkinningJoints 
{ 
    int   j[MAX_BONES_PER_VERT]; 

    SkinningJoints(Vertex::Weights weights[MAX_BONES_PER_VERT]) 
    { 
     for (bUint i = 0; i < MAX_BONES_PER_VERT; i++) 
      j[i] = weights[i].jid; 
    } 
}; 

我SkinningWeights結構幾乎是一樣的,與數組float而不是int。

現在,當我嘗試調試關節指數,權重值和最終頂點顏色,這裏是我得到:

// Shader 
color_debug = joints_influences; 

http://www.images-host.fr/view.php?img=00021800pop.jpg

color_debug = weights_influences; 

http://www.images-host.fr/view.php?img=00021800pop2.jpg

另一個有趣事情,當我嘗試這個:

vec4 pop = vec4(vertexPos, 1) * animation_matrices[1] * inv_bind_matrices[1] * 1.0; 
gl_Position = ViewProj * Model * pop; 

我的所有網格實際上是旋轉的,這意味着我的制服animation_matrices是好的。

任何人都可以看到我在這裏做錯了嗎?

回答

2

您是否嘗試調試皮膚屬性?將頂點權重輸出爲顏色,以便確認您具有有意義的值?如果一切都是黑色的,你會知道去哪裏看。

通過快速瀏覽RenderingVertices,我可以發現第一個問題。你正在向GL傳遞一個Vector指針,我不認爲這是你想要做的。

大多數情況下,您會將蒙皮影響限制爲每個頂點4個關節/重量配對。所以你可以用一個簡單的數組(即SkinningJoints關節[4];)。

+0

這是我所得到的,當我把「在vec4 weights_influences佈局(位置= 6);」作爲顏色。 http://www.images-host.fr/view.php?img=17021756pop.jpg – 2012-02-17 16:57:15

4

我終於搞定了。對於那些誰可能有興趣,這裏是我做錯了:

當我送關節指數陣列GLSL,而不是做這樣的:

glEnableVertexAttribArray(5); 
glBindBuffer(GL_ARRAY_BUFFER, m->GetBuffer(5)); 
glVertexAttribPointer(5, 4, GL_INT, GL_FALSE, 0, BUFFER_OFFSET(0)); 

我需要做的是:

glEnableVertexAttribArray(5); 
glBindBuffer(GL_ARRAY_BUFFER, m->GetBuffer(5)); 
glVertexAttribIPointer(5, 4, GL_INT, 0, BUFFER_OFFSET(0)); 

你必須仔細觀察以找出差異。我不需要調用glVertexAttribPointer(),而需要調用glVertexAttribIPointer(),因爲關節索引是int

希望這會幫助別人一天。