为什么我的所有粒子都具有相同的旋转和颜色? (GLSL着色器)

时间:2014-08-27 14:58:16

标签: opengl geometry shader particles

与标题所示,我正在使用几何着色器在OpenGL中构建一个粒子系统,以便从点创建广告牌。一切似乎都没问题,但看起来所有粒子都共享第一个粒子的旋转和颜色。我当然检查了输入。 这是我当前的顶点和几何着色器:

顶点着色器:

#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();
}

我在这里做错了吗?

1 个答案:

答案 0 :(得分:0)

啊,我发现了导致我问题的原因。 从我还在使用glVertexAttribDivisor()时,我仍然有两次拨打glDrawArraysInstanced()