libgdx icon indicating copy to clipboard operation
libgdx copied to clipboard

Feature request: add sRGB internal format of texture

Open Kright opened this issue 7 years ago • 3 comments

Internal format sRGB is same as RGB, but openGL automatically does gamma-correction when reading colors from texture. This is used in HDR rendering.

Way to add this:

code in GLTexure.uploadImageData:

Gdx.gl.glTexImage2D(target, miplevel, pixmap.getGLInternalFormat(), pixmap.getWidth(), pixmap.getHeight(), 0, pixmap.getGLFormat(), pixmap.getGLType(), pixmap.getPixels());

So, getGLInternalFormat is returned by Pixmap:

public int getGLInternalFormat () {
	return toGlFormat(format);
}

but toGlFormat never returns sRGB:

public static int toGlFormat (int format) {
	switch (format) {
	case GDX2D_FORMAT_ALPHA:
		return GL20.GL_ALPHA;
	case GDX2D_FORMAT_LUMINANCE_ALPHA:
		return GL20.GL_LUMINANCE_ALPHA;
	case GDX2D_FORMAT_RGB888:
	case GDX2D_FORMAT_RGB565:
		return GL20.GL_RGB;
	case GDX2D_FORMAT_RGBA8888:
	case GDX2D_FORMAT_RGBA4444:
		return GL20.GL_RGBA;
	default:
		throw new GdxRuntimeException("unknown format: " + format);
	}
}

As I think, one can add a few new formats or/and special flag to the Texture constructor.

P.S. Now I am achieving this by overriding Pixmap.getGLInternalFormat() method and returning something like GL30.GL_SRGB8_ALPHA8 or GL30.GL_SRGB8

Kright avatar Aug 26 '18 11:08 Kright

If you want this functionality, the best way is to submit a pull request yourself.

EtK2000 avatar Nov 03 '18 23:11 EtK2000

Another cleaner way to have sRGB texture is to do something like this, given a pixmap :

TextureData data = new GLOnlyTextureData(pixmap.getWidth(), pixmap.getHeight(), 0, GL30.GL_SRGB8, GL20.GL_RGB, GL20.GL_UNSIGNED_BYTE);
texture = new Texture(data);
texture.draw(pixmap, 0, 0);

or if you have an alpha channel in the pixmap :

new GLOnlyTextureData(pixmap.getWidth(), pixmap.getHeight(), 0, GL30.GL_SRGB8_ALPHA8, GL20.GL_RGBA, GL20.GL_UNSIGNED_BYTE);

If we want to implement it in libgdx more conveniently, the best way would be to have an option in Texture constructor. Pixmap and Gdx2DPixmap shouldn't be concerned by this.

mgsx-dev avatar Jun 24 '21 15:06 mgsx-dev

I was able to enable/disable gamma correction using following code before rendering.

if (gammaCorrected)
     Gdx.gl30.glEnable(org.lwjgl.opengl.GL30.GL_FRAMEBUFFER_SRGB);
else
     Gdx.gl30.glDisable(org.lwjgl.opengl.GL30.GL_FRAMEBUFFER_SRGB);

abdallabushnaq avatar Jul 27 '24 07:07 abdallabushnaq