0
votes

Weird issue. Seems like my GLSL program is behaving different than logic and the spec suggest. Here's the vert shader:

#version 120

uniform mat4 q;
varying vec2 TextureCoord;

void main()
{
  gl_FrontColor = gl_Color;
  gl_TexCoord[0] = gl_MultiTexCoord0;
  vec4 p = q*gl_Vertex;
  TextureCoord = vec2(.01f,.01f);
  gl_Position= gl_ModelViewProjectionMatrix*p;
  gl_PointSize = gl_Normal.x / -p.z;
}

And here's the frag shader:

#version 120

uniform sampler2D theTex;
varying vec2 TextureCoord;

void main()
{
  vec2 realTexCoord = ( gl_TexCoord[0].st*vec2(0.024f,0.024f)) + TextureCoord;
  gl_FragColor = gl_Color*texture2D(theTex, realTexCoord);

}

The wrinkle is this. If I drop the + TextureCoord in the frag shader and replace it with some constant vec2 like (.1f,.1f) everything's fine, I see the texture. But like this, I just get weird garbage color noise. This is GL_POINTS with smooth points and texture coordinate replacing on. I'm using the TexCoord[0].st because gl_PointCoord is always zero...

What it looks like is happening is junk values are somehow getting into my varying in between. But how would this happen? And if these values are somehow being 'interpolated' between the vertex and fragment shaders, what sort of logic is applied?

I tried sending the values in on a uniform, but it too is always zero.

I'm using LWJGL.

2
Are you actually checking your shader link and compile for errors in the infolog? I didn't think it was legal to sample gl_TexCoord in the fragment shader.Tim
yup, I am. It's fine but for this garbling. I'm using some older hardware, but still...user1086498

2 Answers

0
votes

What is the GPU HW? Have You tried installing latest drivers for it? Have You tried running Your code on GPU from different vendor? This maybe the case when varyings are not correctly linked between shaders - this may happen on some buggy/old OpenGL implementations.

Have you tried same with GL_TRIANGLE_STRIP? It may be only GL_POINTS topology buggy here. Btw. texturing GL_POINTS is a strange idea for me.

Varyings are interpolated through primitives according to current gl_Position. For GL_POINTS there should be no interpolation, as there is one vertice per assembled primitive.

0
votes

I've just had some similar problems with an old ATI card.

You could try calling glTexEnvf( GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE ); in your main program - that fixed gl_PointCoord for me.

I'm not sure if this would work for you (I don't know why it worked for me...) but my varying garbling problem was fixed by adding the line _coord = gl_TexCoord[0].xy; to the end of my fragment shader (_coord is an otherwise unused vec2 and I didn't call gl_TexCoord anywhere else at all). I found this suggested on a forum, but for the life of me I can't find it again!