OpenGL transparent texture rendering oddly -


i'm trying write code render arbitrary image on arbitrary polygon. program needs support image transparency. have chosen use opengl render said polygons. i'm developing on windows machine, i'm using glew handle opengl functions happened after opengl 1.1. graphics hardware supports opengl 2.1. using stb_image load images disk ram. getting rather odd results drawing.

when render image onto polygon using gl_decal in gltexenv, texture's alpha channel dictates blending between color of texture , color of polygon it's drawn on to. expect, it's not behavior like. so, changed gl_decal gl_replace, said use texture's alpha value when blending color buffer. after enabling blending, weird results mentioned above. when alpha value of image want draw equal 0, random colors in (supposedly) transparent part. problem same 1 mentioned in transparent png texture in cocos2d using opengl iphone, on windows 7 , not ios.

does know why getting these results , can not these results?

image rendered gltexenvf( gl_texture_env, gl_texture_env_mode, gl_decal );: http://www.majhost.com/gallery/dagonecelstraun/others/helpneeded/gl_decal.png note red text , red background on kitten image. should be.

image rendered gltexenvf( gl_texture_env, gl_texture_env_mode, gl_replace );: http://www.majhost.com/gallery/dagonecelstraun/others/helpneeded/gl_replace.png note text no longer red. odd. note kitten's background not meaningful.

initialization code, in graphics2d.cpp:

graphics2d::graphics2d() { glenable( gl_texture_2d ); glenable( gl_blend ); glblendfunc( gl_src_alpha, gl_one_minus_src_alpha ); } 

image loading code, in graphics2d.cpp:

int graphics2d::loadimage( std::string imagename ) { file *file = fopen( imagename.c_str(), "rb" );  //open filename read-only binary file gluint texture = 0; int x, y, comp; //actual x-dimension, y-dimension, , number of byter per pixel in actual file glubyte *data = stbi_load_from_file( file, &x, &y, &comp, 0 ); fclose( file );  glenable( gl_texture_2d );  glgentextures( 1, &texture ); glbindtexture( gl_texture_2d, texture ); glpixelstorei( gl_unpack_alignment, 1 ); gltexparameteri( gl_texture_2d, gl_texture_wrap_s, gl_clamp ); gltexparameteri( gl_texture_2d, gl_texture_wrap_t, gl_clamp ); gltexparameteri( gl_texture_2d, gl_texture_mag_filter, gl_nearest ); gltexparameteri( gl_texture_2d, gl_texture_min_filter, gl_linear );  if( comp == 3 ) {     glteximage2d( gl_texture_2d, 0, gl_rgb, x, y, 0, gl_rgb, gl_unsigned_byte, data ); } else if( comp == 4 ) {     glteximage2d( gl_texture_2d, 0, gl_rgba, x, y, 0, gl_rgba, gl_unsigned_byte, data ); }  imagedata d; d.id = texture; d.x = x; d.y = y; d.a = comp == 4; images.push_back( d );  return texture; } 

image drawing code, in graphics2d.cpp:

void graphics2d::drawimage( int imageid, int x, int y ) { imagedata curdata; for( imagedata &id : images ) {     if( id.id == imageid ) {         curdata = id;     } } int maxx = x + curdata.x; int maxy = y + curdata.y;  if( curdata.a ) {            gltexenvf( gl_texture_env, gl_texture_env_mode, gl_replace ); } else {      gltexenvf( gl_texture_env, gl_texture_env_mode, gl_decal ); }  glbindtexture( gl_texture_2d, imageid ); glbegin( gl_quads );     gltexcoord2i( 0, 0 );     glvertex3i( x, -y, -1 );     gltexcoord2i( 1, 0 );     glvertex3i( maxx, -y, -1 );     gltexcoord2i( 1, 1 );     glvertex3i( maxx, -maxy, -1 );     gltexcoord2i( 0, 1 );     glvertex3i( x, -maxy, -1 ); glend(); } 

finally, code called main program, in main.cpp:

void paint( hdc hdc ) { glclear( gl_color_buffer_bit | gl_depth_buffer_bit ); glmatrixmode( gl_modelview ); glpushmatrix(); glloadidentity();  //graphics.drawimage( catjpg, 0, 0 ); graphics.setcolor( 255, 0, 0 );    //graphics object of type graphics2d graphics.drawimage( catpng, 250, 250 ); graphics.drawtext( "hello world", 50, 50 );  glpopmatrix(); swapbuffers( hdc ); } 

let me explain little data structures i've used: imagedata struct int opengl texture id, int texture's width (stored in x), int texture's height (stored in y), , bool tell me if texture has alpha channel or not (stored in a). statement if( curdata.a ) evaluates true if texture has alpha channel.

it's worth noting literally thing changed between first , second images is, in image drawing function, changed 'gltexenvf( gl_texture_env, gl_texture_env_mode, gl_replace );' (what , in second image) 'gltexenvf( gl_texture_env, gl_texture_env_mode, gl_decal );' (what used , in first image).


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -