【问题标题】:Why do all my particles share the same rotation and color? (GLSL Shaders)为什么我的所有粒子都共享相同的旋转和颜色? (GLSL 着色器)
【发布时间】:2014-08-27 14:58:15
【问题描述】:

正如标题所示,我正在 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();
}

我在这里做错了吗?

【问题讨论】:

  • 当你说你检查了输入,你是怎么做到的?您是否尝试过不使用几何着色器的点图元?这可能就像顶点缓冲区的错误步幅/大小一样简单,导致每个额外顶点的未定义结果。
  • 我的意思是我检查了传递给 gpu 的数组中的值。根据您的建议,我刚刚尝试取出几何着色器并将它们绘制为点图元,同样的问题仍然存在。感谢您的建议,我将不得不研究缓冲。

标签: opengl geometry shader particles


【解决方案1】:

啊,我发现是什么导致了我的问题。 从我还在使用glDrawArraysInstanced() 时起,我仍然有两个电话给glVertexAttribDivisor()

【讨论】:

    猜你喜欢
    • 2013-11-25
    • 1970-01-01
    • 2018-11-30
    • 1970-01-01
    • 1970-01-01
    • 2011-05-21
    • 2014-08-17
    • 2011-10-04
    • 1970-01-01
    相关资源
    最近更新 更多