Skip to content

Instantly share code, notes, and snippets.

@diska
Created March 10, 2019 17:24
Show Gist options
  • Select an option

  • Save diska/4762fe79d1b57c21a37a2abd9e884ec1 to your computer and use it in GitHub Desktop.

Select an option

Save diska/4762fe79d1b57c21a37a2abd9e884ec1 to your computer and use it in GitHub Desktop.
WebGL1.0でtextureを生成して使うサンプル。意味はともかく一応動く。
<h1>WebGLTexture</h1>
<hr/><canvas id="CNVS" width="256px" height="256px"></canvas><hr/>
<script>
var cx=CNVS.getContext("webgl");
var data=new Uint8Array(256*256*4);for(var i=0; i<256*256*4; i++)data[i]=Math.random()*0xff;
with(cx){
var tx=createTexture();
var fb=createFramebuffer();
bindTexture(TEXTURE_2D, tx);
texParameteri(TEXTURE_2D, TEXTURE_MIN_FILTER, NEAREST);
texParameteri(TEXTURE_2D, TEXTURE_MAG_FILTER, NEAREST);
texImage2D(TEXTURE_2D,0, RGBA, 256,256,0,RGBA, UNSIGNED_BYTE,data);
bindFramebuffer(FRAMEBUFFER, fb);
framebufferTexture2D(FRAMEBUFFER, COLOR_ATTACHMENT0, TEXTURE_2D, tx, 0);
console.log(`fb:${checkFramebufferStatus(FRAMEBUFFER)}`);
bindFramebuffer(FRAMEBUFFER, null);
}
cx.clearColor(.1,.3,0,1);cx.clear(0x4000);
with(cx){
var vsrc=`void main(){gl_PointSize=256.;gl_Position=vec4(0,0,0,1);}`;
var fsrc=`uniform sampler2D tx;
void main(){gl_FragColor=texture2D(tx, gl_PointCoord.xy);}`;
var vs=createShader(VERTEX_SHADER); shaderSource(vs, vsrc); compileShader(vs);
var fs=createShader(FRAGMENT_SHADER); shaderSource(fs, fsrc); compileShader(fs);
var pg=createProgram(); attachShader(pg, vs); attachShader(pg, fs);
linkProgram(pg);
console.log(`vs:${getShaderInfoLog(vs)}`);
console.log(`fs:${getShaderInfoLog(fs)}`);
console.log(`pg:${getProgramInfoLog(pg)}`);
useProgram(pg); deleteShader(vs); deleteShader(fs);
}
cx.drawArrays(0, 0,1);
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment