Mipmapping causes 3D texture problems - FIXED

Started by Martijn, June 26, 2022, 11:55:07

Previous topic - Next topic


Hi I am trying to implement 3D textures for my terrains. However I am having a troubles with it. I suspect generating mipmaps is the cause of it.

The texture only renders closeby:

I changed my shader to output the texture coordinates as a color value, they seem to be okay:
outColour = vec4(texcoord.x, texcoord.y, texcoord.z, 1);

When I get close with the camera the textures distort:

So I disabled my mipmap code and voila, it must be mipmapping causing the distortion.

This is my texture loading / mipmapping code:
// Bind the texture
			glBindTexture(type, apiID);
			glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
			if (type == TYPE_TEXTURE_2D)
				glTexImage2D(type, 0, GL_RGBA, decoder.getWidth(), decoder.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, buf);
			if (type == TYPE_TEXTURE_3D)
				GL14.glTexImage3D(type, 0, GL_RGBA, decoder.getWidth(), decoder.getHeight(), layers, 0, GL_RGBA, GL_UNSIGNED_BYTE, buf);
			glTexParameteri(type, GL11.GL_TEXTURE_WRAP_S, (renderMode == RENDER_TEXTURE_CLAMP_EDGE) ? GL12.GL_CLAMP_TO_EDGE : GL11.GL_REPEAT);
			if (type == TYPE_TEXTURE_2D || type == TYPE_TEXTURE_3D)
				glTexParameteri(type, GL11.GL_TEXTURE_WRAP_T, (renderMode == RENDER_TEXTURE_CLAMP_EDGE) ? GL12.GL_CLAMP_TO_EDGE : GL11.GL_REPEAT);
			if (type == TYPE_TEXTURE_3D)
				glTexParameteri(type, GL14.GL_TEXTURE_WRAP_R, (renderMode == RENDER_TEXTURE_CLAMP_EDGE) ? GL12.GL_CLAMP_TO_EDGE : GL11.GL_REPEAT);
			glTexParameteri(type, GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST); //<--- DEBUG - disabling mipmap
			glTexParameteri(type, GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);

			/*  <- DEBUG - mipmapping causes distortion on 3D textures? 
			glTexParameterf(type, GL12.GL_TEXTURE_MIN_LOD, -1000f);
			glTexParameterf(type, GL12.GL_TEXTURE_MAX_LOD, 1000f);
			glTexParameterf(type, GL14.GL_TEXTURE_LOD_BIAS, -1f);
			glBindTexture(type, 0);

TYPE_TEXTURE_2D and 3D are equal to GL_TEXTURE_2D and 3D. What am I doing wrong?


I fixed the problem by changing GL_TEXTURE_3D to GL_TEXTURE_2D_ARRAY. Now mipmapping works.