Binding a specific Texture Level/Mip-Map

Started by
7 comments, last by Stephan Picker 10 years, 9 months ago

Hey Guys,

I'm trying to bind a specific Level of an offscreen-render-texture to let's say texture target 0.

Now the default way of binding a texture doesn't allow that, so this is my workaround.
It's ugly, inconsistent and I don't like it.


public void bind(int textureId) {
    glActiveTexture(GL_TEXTURE0 + textureId);
    glBindTexture(GL_TEXTURE_2D, textureHandle);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, baseLevel);
} 

So I found this function "glBindImageTexture", which I still dont' understand.


public void bind(int textureId, int level) {
    glBindImageTexture(textureId, textureHandle, level, false, 0, GL_READ_ONLY, GL_RGBA32F);
}


Is this what I want? Do I use it right? Also I can't use formats like GL_RGB32F - without the Alpha channel.
All I get is a grey Rect, when I render this texture with "glBindImageTexture".

Advertisement

This will bias your textures to use mip maps 2 levels closer to level 0 (it will use 2x higher resolution compared to what it should be using).

GLfloat bias = -2.0;
glTexEnvfv(GL_TEXTURE_FILTER_CONTROL, GL_TEXTURE_LOD_BIAS, &bias);

glTexParameter takes these as inputs, you can push the lowest and highest resolution you want to use. I assume if they are = , it will only use that mip-map level no matter what.

GL_TEXTURE_MIN_LOD,
GL_TEXTURE_MAX_LOD

You can also write a shader and call texture2dLod(texture, uv_coords, mipMapLevel)

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal

It may also depend what you want to do. I don't believe that you can pick a specific level to render to, but the above makes it so you can choose which level to actually sample and texture from.

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal

Ok, now I tried these functions:


texelFetch(texture, coord, level);
textureLod(texture, coord, level);

and I also set my filters via "glTexParameter()" to


MAG -> GL_LINEAR
MIN -> GL_LINEAR_MIPMAP_NEAREST

but all I get is black pixels.

I'am absolutely shure that my textures are all-right, because when I render them with with:


glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, level);

It views the right level. (I have different colors on each level of my test texture).


		OpenGL.setTexture(0, texture); //(texture unit, textureHandle)
		OpenGL.setProgram(blitProgram);

		blitProgram.setInt("level", level);

		OpenGL.setDepthTest(false);

		glBegin(GL_QUADS);
		glVertexAttrib2f(1, 0.0f, 0.0f);
		glVertexAttrib3f(0, left, bottom, 0.0f);
		glVertexAttrib2f(1, 1.0f, 0.0f);
		glVertexAttrib3f(0, right, bottom, 0.0f);
		glVertexAttrib2f(1, 1.0f, 1.0f);
		glVertexAttrib3f(0, right, top, 0.0f);
		glVertexAttrib2f(1, 0.0f, 1.0f);
		glVertexAttrib3f(0, left, top, 0.0f);
		glEnd();

		OpenGL.setDepthTest(true);

#version 420 core

in  vec2 passCoord;

out vec3 outColor;

uniform sampler2D source;
uniform int       level;

void main()
{
	outColor = textureLod(source, passCoord, level).rgb + vec3(0.1f);
}

#version 420 core

in vec3 inPos;
in vec2 inCoord;
out vec2 passCoord;

void main()
{
	gl_Position = vec4(inPos, 1.0f);

	passCoord = inCoord;
}

Thanks in advance

I missed it. If you can set it to view the proper levels what is your issue? Sampling them from a shader? If you want to use textureLod, try it without changing the texture parameters. I have used a shader to pick a specific texture level without changing texture parameters.

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal

Sry, I ment it works when rendering with "texture()" [without Lod] and chaching its base level but it doesn't when using "textureLod" without the baselevel midification. Tomorrow I will try commenting out all parameter changes and see what happens.

Tomorrow I will try commenting out all parameter changes and see what happens.

Doesn't Work.

I also just read that you can only use "textureLod" in vertex buffers. Isn't that nonsense?

No you can only use textureLod , and not texture in vertex shaders. Because you dont use mip-map/ anisotropic filtering when sampling a texture in a vertex shader.

You can though use textureLod in a pixel shader.

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal

... ok, but still - as long as I use "textureLod" all I get is black pixels ...

This topic is closed to new replies.

Advertisement