Miro Krsjak
Miro Krsjak

Reputation: 363

Fragment shader to process texture data and pass through

I'm trying to use an atomic counter in a fragment shader. Basically I'm displaying just a textured quad and need to make calculations on the texture data, and display the unaltered texture. I began with writing a simple pass-through fragment shader, but the displayed texture is plain color. Either i'm missing something, i'm not sure if to use also vertex shader, as i'm processing only texture, seemed obsolete to me.

Fragment shader code:

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

Rendering pipeline - using shaprGL, but it's easily readable:

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);

Upvotes: 1

Views: 1448

Answers (2)

Miro Krsjak
Miro Krsjak

Reputation: 363

I did figure it finally out, and if someone has the same problem, here is the vertex shader - note no min.version out vec4 texCoord; void main(void) { gl_Position = ftransform(); texCoord=gl_TextureMatrix[0] * gl_MultiTexCoord0; }

and the fragment shader - using version #440 to support atomic counters #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); }

Upvotes: 0

Nicol Bolas
Nicol Bolas

Reputation: 474406

uniform vec2 texture_coordinate;

Uniform values do not change from fragment to fragment; that's why they're called "uniform". You probably wanted in rather than uniform.

Granted, since you're using legacy features, you probably need to hook into the gl_TexCoord[] array. That's what the legacy vertex processor (since you're not using a VS) will spit out. Texture coordinate 0 will map to gl_TexCoord[0].

Upvotes: 1

Related Questions