Alpha blending / transperency of a texture bound to quad - java lwjgl slick -


i need me this. can't figure out how alpha channel or blending works. image in .bmp format, loads perfectly. want specific color transparent, in paint color r255xg0xb255. i've been searching similar topics, found or tried seems mess stuff up, give me full black screen or make have magenta touch... else works fine code... should maybe switch .png? solve issue? have pros or cons if use png or bmp?

        // initialization code opengl     glmatrixmode(gl_projection);     glloadidentity();     glortho(0, height, width, 0, 1, -1);     glmatrixmode(gl_modelview);     glenable(gl_texture_2d);      glclearcolor(1.0f, 0.0f, 1.0f, 0.0f);      glenable(gl_blend);     glblendfunc(gl_src_alpha, gl_one_minus_src_alpha);       while (!display.iscloserequested()) {         // render         glclear(gl_color_buffer_bit);          this.drawmaptoscreen();         this.drawcreaturetoscreen();          display.update();         display.sync(60);     }      display.destroy(); }  private void drawcreaturetoscreen() {     texture tex3 = spriteloader.loadtexture("player");     color.magenta.bind();     tex3.bind();     glbegin(gl_quads);     gltexcoord2f(0, 0);     glvertex2i((height-32)/2,(width-32)/2);     gltexcoord2f(1, 0);          glvertex2i((height-32)/2 +32,(width-32)/2);     gltexcoord2f(1, 1);     glvertex2i((height-32)/2 +32,(width-32)/2 +32);     gltexcoord2f(0, 1);     glvertex2i((height-32)/2,(width-32)/2 +32);     glend();              } 

what looking when color 1.0f, 0.0f, 1.0f (in eyes of opengl: full red, no green, full blue) have alpha (transparency) channel zero, pixel drawn transparent. unfortunately, there no way in opengl, unless use shaders, , believe me using shaders far messier solution propose: doing "pre-multiplied alpha" in image editor. means: doesn't depend on format use - .png or .bmp both fine, , both support alpha channel. alpha transparency; in opengl, you'll dealing floats lot, i'll use floats here. alpha fourth color channel; have red, green, blue, , alpha. alpha controls transparency (as float) being multiplied other channels: if alpha 0.0f, color transparent, if alpha 1.0f, opaque. sum up: in editor, must make sure area want transparent transparent in editor. alpha blending, still must enabled transparency whatsoever: preferred blend mode follows:

glenable(gl_blend); glblendfunc(gl_src_alpha, gl_one_minus_src_alpha); 

now, blending technique making objects appear transparent, among other things. in case, you'll want use blend function above: use gldisable(gl_blend) disable blending occurring after enabling it. blend function above mixes preexisting colors , colors rendered in such way object rendered overlapping another, after said other, makes appear "top" , rendered object transparent relative "bottom" object.

tl;dr:

  • make sure area want transparent transparent in image editor. opengl cannot make specific colors have alpha values of 0.0f unless use needlessly complex shader.
  • use blending setup glenable(gl_blend); glblendfunc(gl_src_alpha, gl_one_minus_src_alpha); enable blending. make sure top object rendered after bottom object.

Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -