c++ - Odd results using glTexImage2D -



c++ - Odd results using glTexImage2D -

i've been trying figure out how glteximage2d works , seeing odd results pretty clear-cut code. code draws rough circle 256*256 length unsigned array , sends info out become texture. texture displayed turning out variations of reddish , orange no matter combinations select within image creation loop:

unsigned* info = new unsigned[256*256]; (int y = 0; y < 256; ++y) (int x = 0; x < 256; ++x) if ((x - 100)*(x - 100) + (y - 156)*(y - 156) < 75*75) data[256*y + x] = ((156 << 24) | (256 << 16) | (156 << 8) | (200 << 0)); else data[256*y + x] = 0; // i'd expect transparent , above transparent , green, it's reddish somehow. glbindtexture(gl_texture_2d, texid); gltexparameteri(gl_texture_2d, gl_texture_min_filter, gl_linear); gltexparameteri(gl_texture_2d, gl_texture_mag_filter, gl_linear); glteximage2d(gl_texture_2d, 0, gl_rgba8, 256, 256, 0, gl_rgba, gl_unsigned_byte, (glvoid*)data);

opengl options:

glenable(gl_texture_2d); glshademodel(gl_smooth); glclearcolor(0.0f, 0.0f, 0.0f, 0.5f); glcleardepth(1.0f); glenable(gl_depth_test); gldepthfunc(gl_lequal); glhint(gl_perspective_correction_hint, gl_nicest); //glblendfunc(gl_src_alpha, gl_one); //glenable(gl_blend); //gldisable(gl_cull_face); glgentextures(1, &leaf[0]); createleaf(leaf[0]); // createleaf(gluint& texid) posted exclusively above

the rest of code nil display texture on single quad in window. (x64 win7)

edit: tried rickard's solution , i'm still getting violet circle.

glteximage2d(gl_texture_2d, 0, gl_rgba8, 256, 256, 0, gl_rgba, gl_unsigned_byte, (glvoid*)data);

first positive things. utilize sized internal format (gl_rgba8, rather gl_rgba). good; maintain doing that. have clear understanding of difference between internal format (gl_rgba8) , pixel transfer format (gl_rgba). good.

the problem this. told opengl info stream of unsigned bytes. it's not stream of unsigned bytes; it's stream of unsigned integers. that's how declared data, that's how filled data. why lying opengl?

the problem colors. 1 of color values:

((156 << 24) | (256 << 16) | (156 << 8) | (200 << 0))

first, 256 not valid color. 256 in hex 0x100, 2 bytes, not one.

the unsigned integer is:

0x9d009cc8

if these intended rgba colors in order, reddish 0x9d, greenish 0x00, bluish 0x9c, , alpha 0xc8.

now, because you're working on little-endian computer, 4 bytes stored flipped, this:

0xc89c009d

when tell opengl pretend byte array (which not), losing little-endian conversion. opengl sees byte array starting 0xc8, reddish value. , on.

you need tell opengl you're doing: you're storing 4 8-bit unsigned values in single unsigned 32-bit integer. this, utilize following:

glteximage2d(gl_texture_2d, 0, gl_rgba8, 256, 256, 0, gl_rgba, gl_unsigned_int_8_8_8_8, (glvoid*)data);

the gl_unsigned_int_8_8_8_8 says you're feeding opengl array of unsigned 32-bit integers (which are). first 8-bits of 32-bit integer red, sec green, 3rd blue, , 4th alpha.

so, prepare code, need this:

gluint* info = new gluint[256*256]; //use opengl's types (int y = 0; y < 256; ++y) (int x = 0; x < 256; ++x) if ((x - 100)*(x - 100) + (y - 156)*(y - 156) < 75*75) data[256*y + x] = ((0x9c << 24) | (0xff << 16) | (0x9c << 8) | (0xc8 << 0)); else data[256*y + x] = 0; // i'd expect transparent , above transparent , green, it's reddish somehow. glbindtexture(gl_texture_2d, texid); gltexparameteri(gl_texture_2d, gl_texture_min_filter, gl_linear); gltexparameteri(gl_texture_2d, gl_texture_mag_filter, gl_linear); gltexparameteri(gl_texture_2d, gl_texture_base_level, 0); //always set base of operations , max mipmap levels of texture. gltexparameteri(gl_texture_2d, gl_texture_max_level, 0); glteximage2d(gl_texture_2d, 0, gl_rgba8, 256, 256, 0, gl_rgba, gl_unsigned_int_8_8_8_8, (glvoid*)data);

// i'd expect transparent , above transparent , green, it's reddish somehow.

alpha doesn't mean transparent; means nil @ unless give meaning. alpha represents transparency if utilize blending , set blending mode causes low alpha create things transparent. otherwise, means nil @ all.

c++ opengl

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

c# - Can ProtoBuf-Net deserialize to a flat class? -

javascript - Change element in each JQuery tab to dynamically generated colors -