java - Drawing textured quads OpenGL not working -
i trying render group of textured quads.
i can colored quads render, not textured ones (screen comes empty.)
i using lwjgl , pngdecoder.
code initializing ogl:
gl11.glmatrixmode(gl11.gl_projection); gl11.glloadidentity(); gl11.glortho(0, 800, 0, 600, 1, -1); gl11.glmatrixmode(gl11.gl_modelview); gl11.glenable(gl11.gl_texture_2d);
code decoding image:
bytebuffer buffer = null; inputstream in = classloader.getsystemresourceasstream(filename); try { buffer = decodestreamtobuffer(in); } { in.close(); } return buffer;
my decodestreamtobuffer(inputstream in)
:
pngdecoder decoder; bytebuffer buf = null; try { decoder = new pngdecoder(in); buf = bytebuffer.allocatedirect(4*decoder.getwidth()*decoder.getheight()); decoder.decode(buf, decoder.getwidth()*4, format.rgba); buf.flip(); } catch (exception e) { e.printstacktrace(); } return buf;
my rendering code:
gl11.glclear(gl11.gl_color_buffer_bit | gl11.gl_depth_buffer_bit); gl11.glcolor3f(0.5f, 0.5f, 1.0f); //still there test color quads. // draw quad gl11.glpushmatrix(); gl11.gltranslatef(screencenter.getx(), screencenter.gety(), 0); gl11.glscalef(1f, 0.5f, 1f); gl11.glrotatef(camrotation, 0f, 0f, 1f); gl11.gltranslatef(-screencenter.getx(), -screencenter.gety(), 0); gl11.glbindtexture(gl11.gl_texture_2d, texture); gl11.glbegin(gl11.gl_texture_2d); gl11.gltexcoord2f(0.0f, 0.0f); gl11.glvertex2f(0, 0); gl11.gltexcoord2f(1.0f, 0.0f); gl11.glvertex2f(32, 0); gl11.gltexcoord2f(1.0f, 1.0f); gl11.glvertex2f(32, 32); gl11.gltexcoord2f(0.0f, 1.0f); gl11.glvertex2f(0, 32); gl11.glend(); gl11.glpopmatrix();
when leave texture binding out , change gl_texture_2d
gl_modelview
works... color instead of texture. making noob mistake here?
this wrong gl11.glbegin(gl11.gl_texture_2d);
the glbegin
method primitive mode
glenum
.
thereby either use:
gl_points
gl_lines
gl_line_strip
gl_line_loop
gl_triangles
gl_triangle_strip
gl_triangle_fan
gl_quads
gl_quad_strip
gl_polygon
the mode use specify rendering. in case code gl_quads
fix code need replace gl11.glbegin(gl11.gl_texture_2d);
gl11.glbegin(gl11.gl_quads);
.
extra
also take in mind glvertex
, glnormal
, gltexcoord
, etc. methods deprecated , should not used. suppose use vbos , shaders. though if learning opengl keep using deprecated methods since , easy use when learning!
Comments
Post a Comment