golkeeper's picture

Texturing problem (opengl 3.2)

I have a problem with texturing in OpenGL 3.2. Geometry appears absolutely black (color 0,0,0,1). Specificaton says :
"If a fragment shader uses a sampler whose associated texture object is not complete,
as defined in section 3.8.12, the texture image unit will return (R; G;B;A)
= (0; 0; 0; 1)."
Words about texture completeness :
"For one-, two-, or three-dimensional textures and one- or two-dimensional array
textures, a texture is complete if the following conditions all hold true:
The set of mipmap arrays levelbase through q (where q is defined in the
Mipmapping discussion of section 3.8.9) were each specified with the same
internal format.
The dimensions of the arrays follow the sequence described in the Mipmapping
discussion of section 3.8.9.
levelbase <= levelmax
Each dimension of the levelbase array is positive.
If the internal format of the arrays is integer (see (see table 3.12),
TEXTURE_MAG_FILTER must be NEAREST and TEXTURE_MIN_FILTER
must be NEAREST or NEAREST_MIPMAP_NEAREST."

Code I use for loading textures is based on corresponding example in Open TK documentation

	GL.ActiveTexture(TextureUnit.Texture0);
	GL.BindTexture(TextureTarget.Texture2D, handle);		
	GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.LinearMipmapNearest);
	GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
	GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureBaseLevel, 0);
	GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMaxLevel, 0);
	GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinLod, -1000);			
	GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMaxLod, 1000);			
	Bitmap bmp=new Bitmap(filename);
	BitmapData bmp_data=bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
	GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba32f, bmp_data.Width, bmp_data.Height, 0,
		OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, bmp_data.Scan0);
	bmp.UnlockBits(bmp_data);

What completeness condition it does not resolve?
IsTexture(handle) returns true
Texture dimensions is 512X512
Texture seems to be loaded correctly, GL.GetTexImage(); retrievs image equvalent to original
I try to use GL.GenerateMipmap(GenerateMipmapTarget.Texture2D), and variate texture parameters, and to set parameters after TexImage2D(), it changes nothing.
Also there is TEXTURE_LOD_BIAS parameter in specs, cann't locate it in OpenTK.

OnRenderFrame callback contents

protected override void OnRenderFrame(FrameEventArgs e)
			{
			GL.Viewport(0, 0, Width, Height);
			GL.Clear(ClearBufferMask.ColorBufferBit|ClearBufferMask.DepthBufferBit);
			GL.ActiveTexture(TextureUnit.Texture0);//not nec
			GL.BindTexture(TextureTarget.Texture2D, square.Tex.Handle);//not necessary
			square.Draw(ref s.Camera.Proj);
			SwapBuffers();
			}

Draw() does

	GL.UseProgram(ProgramID);//Use program BEFORE uniforming!
	GL.UniformMatrix4(ProjUL, false, ref projection);
	GL.UniformMatrix4(ModVUL, false, ref modelview);
	GL.Uniform1(TexSUL,tex.Handle);

and passes vertixes to OpenGL

Fragment shader is
#version 150
precision highp float;
uniform sampler2D uf_texture;
smooth in vec4 vcolor;
smooth in vec2 tpos;
out vec4 gl_FragColor;
void main(void)
{
gl_FragColor=texture(uf_texture,tpos,0.0);
}";

texture coordinates is correct in fragment shader, I examine it using gl_FragColor = vec4(tpos,0,1);

There no GLErrors,or warnings in shaders compilation logs. I use Windows Server 2008 x86, .Net Framework 3.5sp1, OpenTk 1beta-2, latest Nvidia drivers 190.62, videoadapter is Geforce 8400MG. What it could be?

Thanks for answers, exuse me for bad english


Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
golkeeper's picture

ooops, may be the matter is sampler must be uniformed by texture unit number, not texture object(

EDIT Sorry, GL.Uniform1(TexSUL,(int)TextureUnit.Texture0) is solution, but question about TEXTURE_LOD_BIAS still remains

zahirtezcan's picture

Actually you need to send texture unit as pure index such that, TextureUnit.Texture0 has some integr value but you need to send (yourUnit - TextureUnit.Texture0). So, uniform value spans the range [0, MAX_TEXTURE_UNITS(or something like that)]

golkeeper's picture

Oh, I see, thx. GL.Uniform1(TexSUL,(int)TextureUnit.Texture0) works too, it seems OpenGL assumes int values not corresponding to TextureUnit enum as Texture0. Also TEXTURE_LOD_BIAS is a parameter for TexParameter function and for removed from core profile TexEnv function. TextureEnvParameter enum contain TextureLodBias, while TextureParameterName (TexParameter argument type) doesn't. Is it all right?

zahirtezcan's picture
golkeeper wrote:

Oh, I see, thx. GL.Uniform1(TexSUL,(int)TextureUnit.Texture0) works too, it seems OpenGL assumes int values not corresponding to TextureUnit enum as Texture0.

Actually, it is undefined, so driver dependent. My nVidia drivers sets any undefined uniform to 0(zero). So, yes any non-set or wrong-set (I assume your driver gives an InvalidValue GLError but you are using release version of OpenTK) uniform defaults to zero for my drivers.

golkeeper wrote:

Also TEXTURE_LOD_BIAS is a parameter for TexParameter function and for removed from core profile TexEnv function. TextureEnvParameter enum contain TextureLodBias, while TextureParameterName (TexParameter argument type) doesn't. Is it all right?

TexEnv parameters are deprecated in 3.2. But TextureparameterName should contain TextureLodBias. Please check version 1.0-beta2. If it does not contain please report an issue, theFiddler tracks these problems via reported issues