c# - Simple Example of How to Draw a Square With Texture Using Tao.Framework -
i'm trying build simple test of opengl
using tao.framework
. need draw square texture. far came this:
private void simpleopenglcontrol1_paint(object sender, painteventargs e) { simpleopenglcontrol1.initializecontexts(); gl.glclearcolor(0f, 0f, 0f, 0f); gl.glclear(gl.gl_color_buffer_bit); gl.glortho(-1, 2, -1, 2, 1, -1); // open image var filename = @"c:\mypath\image.bmp"; var bmp = new bitmap(filename); var bmpdata = bmp.lockbits( new rectangle(0, 0, bmp.width, bmp.height), imagelockmode.readonly, pixelformat.format24bpprgb); // texture gl.glteximage2d(gl.gl_texture_2d, 0, (int)gl.gl_rgb8, bmp.width, bmp.height, 0, gl.gl_bgr_ext, gl.gl_unsigned_byte, bmpdata.scan0); gl.glbindtexture(gl.gl_texture_2d, 0); // draw square. gl.glbegin(gl.gl_polygon); gl.glvertex2f(0, 0); gl.glvertex2f(0, 1); gl.glvertex2f(1, 1); gl.glvertex2f(1, 0); gl.glend(); }
the square rendered in expected shape, without texture. i'm new in opengl
, more in tao.framework
. how can fix it? or how correct way add texture?
edited
of @j-p i'm try this:
//... gl.glenable(gl.gl_texture_2d); // draw square. gl.glbegin(gl.gl_polygon); gl.gltexcoord2f(0, 0); gl.glvertex2f(0, 0); gl.gltexcoord2f(0, 1); gl.glvertex2f(0, 1); gl.gltexcoord2f(1, 1); gl.glvertex2f(1, 1); gl.gltexcoord2f(1, 0); gl.glvertex2f(1, 0); gl.glend();
but square keep without texture.
you miss @ least
gl.enable(gl.texture2d);
which not enabled default.
also have generate new texture slot calling:
int texid; gl.glgentextures(1, texid); //where 1 count, , texid new texture slot in gpu
then have bind texture slot further operation applied.
gl.glbindtexture(gl.gl_texture_2d, texid);
note calling gl.glbindtexture(gl.gl_texture_2d, 0); disable current texture.
now further operation directed texture so, can pass bitmap data texture slot calling:
gl.glteximage2d(gl.gl_texture_2d, 0, (int)gl.gl_rgb8, bmp.width, bmp.height, 0, gl.gl_bgr_ext, gl.gl_unsigned_byte, bmpdata.scan0);
if want set texture parameters texgen , clamping, , many other things, here, example:
gl.gltexparameteri(gl.gl_texture_2d, gl.gl_texture_min_filter, gl.gl_linear); gl.gltexparameteri(gl.gl_texture_2d, gl.gl_texture_mag_filter, gl.gl_linear);
your vertex declaration should include texture coordinate or have set texture coordinate generation active.
gl.texcoord(0,1); gl.glvertex2f(0, 0); gl.texcoord(0,0); gl.glvertex2f(0, 1); gl.texcoord(1,0); gl.glvertex2f(1, 1); gl.texcoord(1,1); gl.glvertex2f(1, 0);
if want coordinate automaticaly generated can omit gl.texcoord call , enable via:
gl.glenable(gl.gl_texture_gen_s); //enable texture coordinate generation gl.glenable(gl.gl_texture_gen_t); gl.gltexparameteri(gl.gl_texture_2d, gl.gl_texture_wrap_s, gl.gl_clamp); gl.gltexparameteri(gl.gl_texture_2d, gl.gl_texture_wrap_t, gl.gl_clamp);
don't forget unlock bitmap data (even if garbage collector must):
bmp.unlock(bmpdata);
Comments
Post a Comment