webgl复习笔记——三角形平移、旋转(1)

平移

每个点都加一个变量

x' = x + Tx;
y' = y + Ty;




    let v_shader = `
    attribute vec4 a_position;
    uniform vec4 u_translation;
    void main(){
        gl_Position = a_position + u_translation;
    }
    `;
......
    initShaders(webgl, v_shader, f_shader);

    let n = initBuffer();

    let u_fragColor = webgl.getUniformLocation(webgl.program, "u_fragColor")
    webgl.uniform4f(u_fragColor, 1, 0, 1, 1);

    let tx = .2,ty = .5,tz = 0;
    let u_translation = webgl.getUniformLocation(webgl.program,"u_translation");
    webgl.uniform4f(u_translation,tx,ty,tz,0.0);

    webgl.clearColor(0, 0, 0, 1.0);
    webgl.clear(webgl.COLOR_BUFFER_BIT);
    webgl.drawArrays(webgl.TRIANGLE_FAN, 0, n);

旋转


    let v_shader = `
    attribute vec4 a_position;
    uniform float u_cosb,u_sinb;
    void main(){
        gl_Position.x = (a_position).x * u_cosb -(a_position).y*u_sinb;
        gl_Position.y = (a_position).x * u_sinb +(a_position).y*u_cosb;
        gl_Position.z = a_position.z;
        gl_Position.w = a_position.w;
    }
    `;

........
    var angle = 90.0;
    var radian = Math.PI * angle / 180.0;//弧度
    var cosb = Math.cos(radian);
    var sinb = Math.sin(radian);

    var u_cosb = webgl.getUniformLocation(webgl.program,'u_cosb');
    var u_sinb = webgl.getUniformLocation(webgl.program,'u_sinb');
    webgl.uniform1f(u_cosb,cosb);
    webgl.uniform1f(u_sinb,sinb);

    webgl.clearColor(0, 0, 0, 1.0);

posted on   老豆浆  阅读(466)  评论(1编辑  收藏  举报

编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示