WebGL之点上添加图片(using texture on point)

Posted wema

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WebGL之点上添加图片(using texture on point)相关的知识,希望对你有一定的参考价值。

var VSHADER_SOURCE =[    
        "attribute vec4 a_Position;",                            
        "uniform mat4 u_ProjMatrix;",
        "uniform float u_PointSize;",
        "void main() {",
            "    gl_PointSize = u_PointSize;",
            "    gl_Position = u_ProjMatrix * a_Position;",
        "}"

    ].join("\n")
var FSHADER_SOURCE =
[
        "precision mediump float;",    
        "uniform sampler2D u_Sampler;",               
        "void main() {",     
     "  gl_FragColor = texture2D(u_Sampler,vec2(gl_PointCoord.x,1.0 - gl_PointCoord.y));",
"}" ].join("\n") function main() { var canvas = document.getElementById(‘webgl‘); var gl = getWebGLContext(canvas); if (!gl) { console.log(‘Failed to get the rendering context for WebGL‘); return; } if (!initShaders(gl, VSHADER_SOURCE, FSHADER_SOURCE)) { console.log(‘Failed to intialize shaders.‘); return; } var projMatrix4=new Matrix4(); projMatrix4.setOrtho(-1,1,-1,1,0.0,1.0); var u_projMatrix=gl.getUniformLocation(gl.program,‘u_ProjMatrix‘); gl.uniformMatrix4fv(u_projMatrix,false,projMatrix4.elements); var position=gl.getAttribLocation(gl.program,‘a_Position‘); gl.vertexAttrib3f(position,0.0,0.0,0.0); gl.clearColor(0, 0, 0, 1); gl.clear(gl.COLOR_BUFFER_BIT); loadImg(gl); } function loadImg(gl){ var image=new Image(); image.onload=function(){ drawPic(gl,image) } image.src="pic.png"; } function drawPic(gl,image){ var width=image.width; var height=image.height; var pointSize=gl.getUniformLocation(gl.program,"u_PointSize"); gl.uniform1f(pointSize,Math.max(width,height)); var texture=gl.createTexture(); //gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL,1); gl.activeTexture(gl.TEXTURE0); gl.bindTexture(gl.TEXTURE_2D,texture); gl.texParameteri(gl.TEXTURE_2D,gl.TEXTURE_MAG_FILTER,gl.NEAREST); gl.texParameteri(gl.TEXTURE_2D,gl.TEXTURE_MIN_FILTER,gl.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.uniform1i(gl.getUniformLocation(gl.program,"u_Sampler"),0); gl.texImage2D(gl.TEXTURE_2D,0,gl.RGBA,gl.RGBA,gl.UNSIGNED_BYTE,image); gl.clear(gl.COLOR_BUFFER_BIT); gl.drawArrays(gl.POINTS,0,1); gl.bindTexture(gl.TEXTURE_2D, null); }

 


以上是关于WebGL之点上添加图片(using texture on point)的主要内容,如果未能解决你的问题,请参考以下文章

WebGL— FrameBuffer,RenderBuffer,Texture区别

WebGL笔记:贴图(笔记终结)

WebGL之点精灵的旋转(Rotation Sprite)

WebGL 创建纹理

iOS开发之点触textView编辑区关闭键盘

WebGL-五