opengl - 点精灵渲染问题

发布于 2024-11-29 15:48:05 字数 1468 浏览 1 评论 0原文

我正在尝试渲染点精灵,但我得到了点。问题出在哪里? (通过 glUniform3f 改变颜色)

顶点着色器:

private static String vertexShader =
"#version 330" + "\n" +
"layout (location = 0) in vec4 position;" + "\n" +
"uniform mat4 pMatrix;" + "\n" +
"uniform mat4 mMatrix;" + "\n" +
"void main()" + "\n" +
"{" + "\n" +
"gl_Position = pMatrix * mMatrix * position;" + "\n" +
"}";

片段着色器:

private static String fragmentShader =
"#version 330" + "\n" +
"out vec4 vFragColor;" + "\n" +
"uniform vec3 Color;" + "\n" +
"uniform vec3 lightDir;" + "\n" +
"void main()" + "\n" +
"{" + "\n" +
"vec3 N;" + "\n" +
"N.xy = gl_PointCoord* 2.0 - vec2(1.0);" + "\n" +    
"float mag = dot(N.xy, N.xy);" + "\n" +
"if (mag > 1.0) discard;" + "\n" +   
"N.z = sqrt(1.0-mag);" + "\n" +
"float diffuse = max(0.0, dot(lightDir, N));" + "\n" +
"vFragColor = vec4(Color,1) * diffuse;" + "\n" +
"}";

渲染:

gl.glUseProgram(shaderProgramID);
gl.glUniformMatrix4fv(projectionMatrixUniform, 1, false, projectionMatrix, 0);
gl.glUniformMatrix4fv(modelViewMatrixUniform, 1, false, modelViewMatrix, 0);
gl.glUniform3f(colorUniform, 1.0f, 0.0f, 0.0f);
gl.glUniform3f(lightDirUniform, 0.0f, 0.0f, 1.0f);
gl.glBindBuffer(GL3.GL_ARRAY_BUFFER, vbo[0]);
gl.glEnableVertexAttribArray(0);
gl.glVertexAttribPointer(0, 4, GL3.GL_FLOAT, false, 0, 0);
gl.glDrawArrays(GL3.GL_POINTS, 0, n_particles);
gl.glDisableVertexAttribArray(0);
gl.glUseProgram(0);

I'm trying to render point sprites but I get points. Where is the problem ? (changing a color via glUniform3f works)

Vertex shader:

private static String vertexShader =
"#version 330" + "\n" +
"layout (location = 0) in vec4 position;" + "\n" +
"uniform mat4 pMatrix;" + "\n" +
"uniform mat4 mMatrix;" + "\n" +
"void main()" + "\n" +
"{" + "\n" +
"gl_Position = pMatrix * mMatrix * position;" + "\n" +
"}";

Fragment shader:

private static String fragmentShader =
"#version 330" + "\n" +
"out vec4 vFragColor;" + "\n" +
"uniform vec3 Color;" + "\n" +
"uniform vec3 lightDir;" + "\n" +
"void main()" + "\n" +
"{" + "\n" +
"vec3 N;" + "\n" +
"N.xy = gl_PointCoord* 2.0 - vec2(1.0);" + "\n" +    
"float mag = dot(N.xy, N.xy);" + "\n" +
"if (mag > 1.0) discard;" + "\n" +   
"N.z = sqrt(1.0-mag);" + "\n" +
"float diffuse = max(0.0, dot(lightDir, N));" + "\n" +
"vFragColor = vec4(Color,1) * diffuse;" + "\n" +
"}";

Rendering:

gl.glUseProgram(shaderProgramID);
gl.glUniformMatrix4fv(projectionMatrixUniform, 1, false, projectionMatrix, 0);
gl.glUniformMatrix4fv(modelViewMatrixUniform, 1, false, modelViewMatrix, 0);
gl.glUniform3f(colorUniform, 1.0f, 0.0f, 0.0f);
gl.glUniform3f(lightDirUniform, 0.0f, 0.0f, 1.0f);
gl.glBindBuffer(GL3.GL_ARRAY_BUFFER, vbo[0]);
gl.glEnableVertexAttribArray(0);
gl.glVertexAttribPointer(0, 4, GL3.GL_FLOAT, false, 0, 0);
gl.glDrawArrays(GL3.GL_POINTS, 0, n_particles);
gl.glDisableVertexAttribArray(0);
gl.glUseProgram(0);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

国际总奸 2024-12-06 15:48:05

为什么不简单地使用几何着色器来完成它呢?
您只需将变换后的顶点位置传递给几何着色器,然后让几何着色器输出该点周围的四个顶点即可。
它更加灵活和稳定。

示例:

顶点着色器:

void main()
{
  gl_TexCoord[0] = gl_MultiTexCoord0;

  // Output vertex position
  gl_Position = gl_ModelViewMatrix * gl_Vertex;
}

几何着色器:

#extension GL_EXT_geometry_shader4 : enable

uniform float particle_size;

void main()
{
    float halfsize = particle_size * 0.5;

    gl_TexCoord[0] = gl_TexCoordIn[0][0];
    gl_FrontColor = gl_FrontColorIn[0];

     // Vertex 4
    gl_TexCoord[0].st = vec2(1.0,1.0);
    gl_Position = gl_PositionIn[0];
    gl_Position = gl_ProjectionMatrix  * gl_Position;
    gl_Position.xy += vec2(halfsize, halfsize);
    EmitVertex();

    // Vertex 3
    gl_TexCoord[0].st = vec2(1.0,-1.0);
    gl_Position = gl_PositionIn[0];
    gl_Position = gl_ProjectionMatrix  * gl_Position;
    gl_Position.xy += vec2(halfsize, -halfsize);
    EmitVertex();

    // Vertex 2
    gl_TexCoord[0].st = vec2(-1.0,1.0);
    gl_Position = gl_PositionIn[0];
    gl_Position = gl_ProjectionMatrix  * gl_Position;
    gl_Position.xy += vec2(-halfsize, halfsize);
    EmitVertex();

    // Vertex 1
    gl_TexCoord[0].st = vec2(-1.0,-1.0);
    gl_Position = gl_PositionIn[0];
    gl_Position = gl_ProjectionMatrix  * gl_Position;
    gl_Position.xy += vec2(-halfsize, -halfsize);
    EmitVertex();



    EndPrimitive();
}

Why don't you simply do it using a geometry shader?
You simply pass the transformed vertex position to the geometry shader and let the geometry shader output four vertices around that point and you're done.
It's much more flexible and stable.

Example:

Vertex shader:

void main()
{
  gl_TexCoord[0] = gl_MultiTexCoord0;

  // Output vertex position
  gl_Position = gl_ModelViewMatrix * gl_Vertex;
}

Geometry shader:

#extension GL_EXT_geometry_shader4 : enable

uniform float particle_size;

void main()
{
    float halfsize = particle_size * 0.5;

    gl_TexCoord[0] = gl_TexCoordIn[0][0];
    gl_FrontColor = gl_FrontColorIn[0];

     // Vertex 4
    gl_TexCoord[0].st = vec2(1.0,1.0);
    gl_Position = gl_PositionIn[0];
    gl_Position = gl_ProjectionMatrix  * gl_Position;
    gl_Position.xy += vec2(halfsize, halfsize);
    EmitVertex();

    // Vertex 3
    gl_TexCoord[0].st = vec2(1.0,-1.0);
    gl_Position = gl_PositionIn[0];
    gl_Position = gl_ProjectionMatrix  * gl_Position;
    gl_Position.xy += vec2(halfsize, -halfsize);
    EmitVertex();

    // Vertex 2
    gl_TexCoord[0].st = vec2(-1.0,1.0);
    gl_Position = gl_PositionIn[0];
    gl_Position = gl_ProjectionMatrix  * gl_Position;
    gl_Position.xy += vec2(-halfsize, halfsize);
    EmitVertex();

    // Vertex 1
    gl_TexCoord[0].st = vec2(-1.0,-1.0);
    gl_Position = gl_PositionIn[0];
    gl_Position = gl_ProjectionMatrix  * gl_Position;
    gl_Position.xy += vec2(-halfsize, -halfsize);
    EmitVertex();



    EndPrimitive();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文