High Level Frag Shader Help

I’m having a problem with the most basic fragment shader using a texture. I’m just trying to get the fragment shader to pass through the texture (as a test), but it just renders black. I suspect that I am missing a step. I am not using a vertex shader.

There are 2 versions of the shader: one written in the high level language and one written in the low level language. I have tested each of them independently. The low level shader works perfectly and passes the texture through. The high level shader is returning black. I know the high level shader is being called because if I replace the texture2D call with a constant value vec4(1.0,0.5,1.0,1.0) it works fine. I also have been diligently checking for errors using glGetInfoLogARB, and there are no errors for either of the shaders.

I’m omitting all the setup code in the snippet below because it is quite lengthy, but I am quite certain that it is correct because it is properly calling the shader code. And the texture must be ok because it works with the low level shader fine.

Following is my main rendering code:

		glBindTexture(GL_TEXTURE_2D, textureID);
		GLint texArb = glGetUniformLocationARB(shaderProgram, "tex");
		glUniform1iARB(texArb, 0);

			glTexCoord2f( inRect->left, inRect->bottom );
			glVertex2f( outRect->left, outRect->bottom );
			glTexCoord2f( inRect->right, inRect->bottom );
			glVertex2f( outRect->right, outRect->bottom );
			glTexCoord2f( inRect->right, inRect->top );
			glVertex2f( outRect->right, outRect->top );
			glTexCoord2f( inRect->left, inRect->top );
			glVertex2f( outRect->left, outRect->top );

Here is the high level shader:

	static const GLcharARB *testShader =
		"uniform sampler2D tex;
		"void main(void) {
		"	gl_FragColor = texture2D(tex, gl_TexCoord[0].st);

And here is the low level version (which works fine):

	static const GLcharARB *testShaderLow =
		"TEX result.color, fragment.texcoord[0], texture[0], RECT;

Can anyone see what I’m missing? Why would the texture2D function be returning black?

Thanks for any help. It must be something basic that I’ve overlooked.

Steven Walker

Your GLSL shader uses 2D textures, your ARB_fragment_program shader uses RECT textures.

Most likely your texture coordinates are in the [0…width]x[0…height] range (required for RECTANGLE textures) so the ARB_fragment_program works correctly.

Update your GLSL shader to use sampler2DRect and texture2DRect and it should work fine.

Thank you so much! That was it. I just changed the types to 2DRects and it works perfectly now.


The official and also working uniform and function names are samplerRect and textureRect from the ARB_texture_rectangle extension.

Originally posted by Relic:
The official and also working uniform and function names are samplerRect and textureRect from the ARB_texture_rectangle extension.
That was actually a bug in the spec (see the revision history of the latest spec: http://oss.sgi.com/projects/ogl-sample/registry/ARB/texture_rectangle.txt.) ).

The correct names are indeed sampler2DRect and texture2DRect.

Argh! I missed the spec update. Thanks, now the reserved and official names finally agree.

Hi, I’m having the exact same problem as he is with GLSL fragment shaders, but when I tried to change sampler2D to sampler2dRect and texture2D to texture2DRect, I got the following shader compiler errors:

ERROR: 0:1: 'sampler2DRect' : Reserved word.
ERROR: 0:1: 'sampler2DRect' : syntax error parse error  

What should that tell me?
3D Lab’s ShaderGen works fine on my card, as does the standard brick example from the Orange Book, so my guess is there might be something else I’m not doing. I tried to copy his code above as exactly as possible.

gluonconcerto - What hardware are you using?

Maybe something wrong about your hardware and drivers

I am using 6200A,works perfectly now

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.