zoukankan      html  css  js  c++  java
  • WebGL之点精灵的旋转(Rotation Sprite)

    上一篇已经把WebGL如何在点(Point)上添加图片进行了说明,并且直接提供了源代码,理解起来应该不难;
    矩形上面添加纹理,我们都知道是可以旋转的,那么点上面添加的纹理能不能进行旋转呢?答案是肯定的
    我们只需要建立一个旋转矩阵,这个旋转直接去操作Shader里面的gl_PointCoord,这样就能达到旋转纹理的目的;
    //顶点着色器
    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(" ")
    //片元着色器
    var FSHADER_SOURCE = [ 'precision mediump float;', "uniform sampler2D u_Sampler;", "uniform float u_Angle" "void main() {", " float x=gl_PointCoord.x-0.5;",//将顶点坐标进行偏移,使得旋转中心点位于(0.5,0.5) " float y=gl_PointCoord.y-0.5;",//将顶点坐标进行偏移,是的旋转中心点位于(0.5,0.5)
        " float nx=(cos(u_Angle) * x - sin(u_Angle ) * y);",//对x的旋转后的值
        
    " float ny = (sin( u_Angle ) * x + cos( u_Angle ) * y);",//对y的旋转后的值

        
    " gl_FragColor = texture2D( u_Sampler, vec2( nx+0.5,ny+0.5) );", //获取图片颜色

    "}"
    ].join(
    " ")

    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 u_Angle=gl.getUniformLocation(gl.program,'u_Angle');
    gl.uniform1f(u_Angle,
    3.14*0.25);

    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);
    }

     本代码依赖了几个js代码文件,链接: https://pan.baidu.com/s/1VloeqssKtr9J42nH4SH-eA 提取码: 2jww    自行下载

  • 相关阅读:
    b4a专用压缩库(国外免费)
    使用php从pc端下载apk到android手持终端并安装(比如把枪)
    快速搭建电子商务网站以及app
    【转】C#如何创建泛型类T的实例
    【转】C# 之泛型详解
    【转】Windows Server 2016 安装 IIS 服务时提示指定备用源路径
    C# json字符串转为对象
    【转】C#模拟http 发送post或get请求
    【转】Windows IIS注册asp 此操作系统版本不支持此选项 错误解决方法
    Webapi文件上传
  • 原文地址:https://www.cnblogs.com/wema/p/7084855.html
Copyright © 2011-2022 走看看