javascript - Blank texture in webGL -
i have simple quad, , simple shader (see below). load image needed texture, process it, uniform place in shader program, , send it, way has been explained in "learning webgl" examples. tested , used webgl inspector see textures i've been using , problem quad whole black. in fragment shader, line:
gl_fragcolor = texture2d( usampler, vuv);
actually sets fragment color (0,0,0,1). it's "blank" texture, or all-black alpha equals 1 texture, not same image i'm trying attach.
if encountered similar problem , knows how fix it, please tell me. it's done in chrome, --allow-file-access-from-files flag, html page, js/webgl code , image local, tested on server, no results.
vertex:
attribute vec3 avertexposition; attribute vec2 auv; uniform mat4 upmatrix; uniform mat4 umvmatrix; varying vec2 vuv; void main() { vuv = auv; gl_position = upmatrix * umvmatrix * vec4(avertexposition, 1.0); }
fragment:
uniform sampler2d usampler; varying vec2 vuv; void main() { gl_fragcolor = texture2d( usampler, vuv) }
texture loading , attaching:
var tex = new chaos.texture().load2d("ch.jpg"); var mat = new chaos.material().fromscript("v1", "f1"); mat.addtexture("usampler", tex);
loading function:
load2d: function(url) { function handletextureloaded(image, texture, gl) { gl.bindtexture(gl.texture_2d, texture); gl.teximage2d(gl.texture_2d, 0, gl.rgba, gl.rgba, gl.unsigned_byte, image); gl.texparameteri(gl.texture_2d, gl.texture_mag_filter, gl.linear); gl.texparameteri(gl.texture_2d, gl.texture_min_filter, gl.linear_mipmap_nearest); gl.texparameteri(gl.texture_2d, gl.texture_wrap_s, gl.clamp_to_edge); gl.texparameteri(gl.texture_2d, gl.texture_wrap_t, gl.clamp_to_edge); gl.generatemipmap(gl.texture_2d); gl.bindtexture(gl.texture_2d, null); } var tex, im, gl = chaos.__r.context; tex = gl.createtexture(); im = new image(); im.src = url; im.onload = function() { handletextureloaded(im, tex, gl); } return tex; },
and addtexture:
addtexture: function(name, texture) { this.maps[name] = texture; this.locunif(name); }
in render function there's part:
for(var key in mate.maps) { gl.activetexture(gl.texture0 + tex_count); // problem int+string, don't @ gl.bindtexture(gl.texture_2d, mate.maps[key]); gl.uniform1i(shaderprogram.unif[key], tex_count); tex_count++; }
i declared property .isready on texture, , put on 'true' in end of image's onload callback function. in render function check if .isready , send gpu. if animation running, visible in second or third frame, depending on image size , time takes loaded. problem used local variable buffered arrays, discarded after first frame rendered (because buffer data once). fine now.
if encounteres similar problem blank texture, check if right values being sent gl.uniform1i, check binded texture actual webgl texture object , check how attributes going.
Comments
Post a Comment