c - what is the best way for loading assets (images) into memory using opengl? -
this question not regarding specific platform , related opengl es.
my brother , creating simple 2d adventure game both new pure opengl programming , missing obvious.
we loading simple png texture game background. texture full hd (1920x1080) png file has size on disk of 2.7mb. once loaded in memory same file holds 9mb in memory.
here sample of code in charge of loading file.
int texture_gl_create(texture_t* texture) { int err; if (texture->id) { err = texture_gl_delete(texture); if (err) { return err; } } gl_check(glgentextures, 1, &texture->id); logd("texture has id: %d", texture->id); gl_check(glbindtexture, texture->target, texture->id); switch (texture->byte) { case 1: glpixelstorei(gl_pack_alignment, 1); break; case 2: glpixelstorei(gl_pack_alignment, 2); break; case 3: case 4: glpixelstorei(gl_pack_alignment, 4); break; } gl_check(gltexparameteri, texture->target, gl_texture_min_filter, gl_linear); gl_check(gltexparameteri, texture->target, gl_texture_mag_filter, gl_linear); gl_check(gltexparameteri, texture->target, gl_texture_wrap_s, gl_clamp_to_edge); gl_check(gltexparameteri, texture->target, gl_texture_wrap_t, gl_clamp_to_edge); gl_check(glteximage2d, texture->target, 0, texture->iformat, texture->width, texture->height, 0, texture->format, texture->type, texture->pixels); gl_check(glbindtexture, texture->target, 0); return 0; }
a simple look @ memory allocation shows call gltextimage2d loads 8mb memory !
from our understanding because call glteximage2d
loads raw file texture-target data memory. therefore there's 4 bytes per pixel (rgba) x 1920 x 1080 = 8.1 mb.
i can hardly believe how video game industry handles problem, there must better way load images memory.
my question therefore, what's best way load images in opengl (es) use little memory possible ?
use texture compression. these special compression algorithms pvrtc allow fast on-the-fly decompression in image pipeline, reducing video memory consumption , texture upload times.
when using glcompressedteximage2d need specify type of compression (internal format) used, , need figure out compression supported gpu by checking extensions.
Comments
Post a Comment