2014-08-27 70 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()

相关问题