【问题标题】:Fragment shader to process texture data and pass through片段着色器处理纹理数据并通过
【发布时间】:2016-12-15 20:57:03
【问题描述】:

我正在尝试在片段着色器中使用原子计数器。 基本上我只显示一个纹理四边形,需要对纹理数据进行计算,并显示未改变的纹理。 我从编写一个简单的直通片段着色器开始,但显示的纹理是纯色的。要么我遗漏了一些东西,我不确定是否也使用顶点着色器,因为我只处理纹理,对我来说似乎已经过时了。

片段着色器代码:

#version 420
 uniform vec2 texture_coordinate; uniform sampler2D my_color_texture;
 void main() {   
    gl_FragColor = texture2D(my_color_texture, texture_coordinate);
 }

渲染管道 - 使用 shaprGL,但易于阅读:

gl.Enable(OpenGL.GL_TEXTURE_2D);
String s = "my_color_texture"; //name of the texture variable in shader

gl.UseProgram(graycounter); // graycounter is a compiled/linked shader
GLint my_sampler_uniform_location = gl.GetUniformLocation(graycounter, s);

gl.ActiveTexture(OpenGL.GL_TEXTURE0);
gl.BindTexture(OpenGL.GL_TEXTURE_2D, ((dTexture)texture).texture); //texture id, working without shader on
gl.Uniform1ARB(my_sampler_uniform_location, 0);

gl.Begin(OpenGL.GL_QUADS); // show the rectangle
gl.Color(1.0f, 1.0f, 1.0f);
gl.TexCoord(0.0f, 0.0f); gl.Vertex(0.0f, 0.0f, 0.0f); // Bottom Left Of The Texture and Quad
gl.TexCoord(1.0f, 0.0f); gl.Vertex(w, 0.0f, 0.0f);  // Bottom Right Of The Texture and Quad
gl.TexCoord(1.0f, 1.0f); gl.Vertex(w, h, 0.0f);   // Top Right Of The Texture and Quad
gl.TexCoord(0.0f, 1.0f); gl.Vertex(0.0f, h, 0.0f);  // Top Left Of The Texture and Quad
gl.End();

gl.UseProgram(0);

【问题讨论】:

    标签: opengl glsl shader fragment-shader


    【解决方案1】:
    uniform vec2 texture_coordinate;
    

    统一值不会因片段而异;这就是为什么他们被称为“制服”。您可能想要in 而不是uniform

    当然,由于您使用的是旧版功能,因此您可能需要挂钩到 gl_TexCoord[] 数组。这就是传统的顶点处理器(因为你没有使用 VS)会吐出的东西。纹理坐标 0 将映射到 gl_TexCoord[0]

    【讨论】:

    • 其实,由于OP使用的是legacy GL,甚至可以不指定VS。
    • 我确实使用建议的顶点和片段着色器完成了渲染工作:VS void main(void) { gl_Position = ftransform(); gl_FrontColor = gl_Color; gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; FS:统一 sampler2D my_color_texture;在 vec2 texCoord; void main() { gl_FragColor = texture2D(my_color_texture,gl_TexCoord[0].xy);但是,为了能够使用原子计数器,需要使用glsl 420版本,并且我无法重写旧的全局变量:/
    【解决方案2】:

    我终于弄明白了,如果有人有同样的问题,这里是顶点着色器 - 注意没有 min.version out vec4 texCoord; void main(void) { gl_Position = ftransform(); texCoord=gl_TextureMatrix[0] * gl_MultiTexCoord0; }

    和片段着色器 - 使用版本 #440 来支持原子计数器 #version 440 layout (binding = 0, offset = 0) uniform atomic_uint g1; uniform sampler2D my_color_texture; in vec4 texCoord; out vec4 fragColour; void main() { fragColour = texture2D(my_color_texture,texCoord.xy); if (abs(fragColour.r*255 - 0)<1) atomicCounterIncrement(g1); }

    【讨论】:

      猜你喜欢
      • 2013-09-17
      • 2015-03-27
      • 1970-01-01
      • 2018-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-04
      相关资源
      最近更新 更多