2014-08-27 124 views
-1

就像標題所暗示的那樣,我在OpenGL中使用幾何着色器來構建粒子系統,以從點創建廣告牌。一切似乎沒問題,但它看起來像所有的粒子共享第一個粒子的旋轉和顏色。我已經檢查了當然的輸入。 這裏是我當前的頂點和幾何着色器:爲什麼我的所有粒子都有相同的旋轉和顏色? (GLSL着色器)

頂點着色器:

#version 330 core 

layout(location=0) in vec4 in_position; 
layout(location=2) in float in_angle; 
layout(location=3) in vec4 in_color; 

uniform mat4 P; 
uniform mat4 V; 

out VertexData 
{ 
    vec4 color; 
    float angle; 
} vertex; 

void main() 
{ 
    vertex.color = in_color; 
    vertex.angle = in_angle; 
    gl_Position = in_position; 
} 

幾何着色器:

#version 330 

layout (points) in; 
layout (triangle_strip, max_vertices = 4) out; 

uniform mat4 V; 
uniform mat4 P; 

in VertexData 
{ 
    vec4 color; 
    float angle; 
} vertex[]; 

out vec4 fragColor; 
out vec2 texcoord; 

mat4 rotationMatrix(vec3 axis, float angle) 
{ 
    axis = normalize(axis); 
    float s = sin(angle); 
    float c = cos(angle); 
    float oc = 1.0 - c; 
    return mat4(oc * axis.x * axis.x + c, oc * axis.x * axis.y - axis.z * s, oc * axis.z * axis.x + axis.y * s, 0.0, 
    oc * axis.x * axis.y + axis.z * s, oc * axis.y * axis.y + c, oc * axis.y * axis.z - axis.x * s, 0.0, 
    oc * axis.z * axis.x - axis.y * s, oc * axis.y * axis.z + axis.x * s, oc * axis.z * axis.z + c, 0.0, 
    0.0, 0.0, 0.0, 1.0); 
} // http://www.neilmendoza.com/glsl-rotation-about-an-arbitrary-axis/ 

    void main() 
{ 
    mat4 R = rotationMatrix(vec3(0,0,1), vertex[0].angle); 
    vec4 pos = V * vec4(gl_in[0].gl_Position.xyz, 1.0); 
    float size = gl_in[0].gl_Position.w; 

    texcoord = vec2(0.0, 0.0); 
    gl_Position = P * (pos + vec4(texcoord, 0, 0) * R * size); 
    fragColor = vertex[0].color; 
    EmitVertex(); 

    texcoord = vec2(1.0, 0.0); 
    gl_Position = P * (pos + vec4(texcoord, 0, 0) * R * size); 
    fragColor = vertex[0].color; 
    EmitVertex(); 

    texcoord = vec2(0.0, 1.0); 
    gl_Position = P * (pos + vec4(texcoord, 0, 0) * R * size); 
    fragColor = vertex[0].color; 
    EmitVertex(); 

    texcoord = vec2(1.0, 1.0); 
    gl_Position = P * (pos + vec4(texcoord, 0, 0) * R * size); 
    fragColor = vertex[0].color; 
    EmitVertex(); 

    EndPrimitive(); 
} 

我是不是做錯了這裏?

+0

當你說你已經檢查了輸入,你是怎麼做到的?你有沒有嘗試嘗試沒有幾何着色器的點基元?這可能與您的頂點緩衝區的錯誤步長/大小一樣簡單,導致每個附加頂點的未定義結果。 – 2014-08-27 17:15:58

+0

我的意思是我檢查了我傳遞給GPU的數組中的值。根據你的建議,我剛剛嘗試拿出幾何着色器並將它們繪製爲點基元,問題依然存在。感謝那個建議,我將不得不考慮緩衝。 – shrekleton 2014-08-27 18:01:27

回答

0

啊我發現了什麼導致我的問題。 我仍然有兩個電話glVertexAttribDivisor(),從我還在使用glDrawArraysInstanced()

相關問題