@@ -561,3 +561,218 @@ function initVertexBuffers(gl) {
561561
562562
563563### 移动、旋转和缩放
564+
565+
566+ ``` js
567+ const VSHADER_SOURCE =
568+ ' attribute vec4 a_Position;\n ' +
569+ ' uniform vec4 u_Translation;\n ' + // [!code ++]
570+ ' void main() {\n ' +
571+ ' gl_Position = a_Position + u_Translation;\n ' +
572+ ' }\n '
573+ const FSHADER_SOURCE =
574+ ' void main() {\n ' +
575+ ' gl_FragColor = vec4(1.0, 1.0, 0.0,1.0);\n ' +
576+ ' }\n '
577+
578+ // 在 x y z 方向上平移的距离
579+
580+ var Tx = 0.5 , Ty = 0.5 , Tz= 0.0 ; // [!code ++]
581+
582+ function main () {
583+ var canvas = document .getElementById (' webgl' )
584+ var gl = getWebGLContext (canvas)
585+ if (! gl) {
586+ console .error (' Failed to get the rendering context for WebGL' )
587+ return ;
588+ }
589+
590+ // 初始化着色器
591+ if (! initShaders (gl, VSHADER_SOURCE , FSHADER_SOURCE )) {
592+ console .error (' Failed to initialize shaders.' )
593+ return ;
594+ }
595+
596+ // 设置顶点着色器
597+ var n = initVertexBuffers (gl);
598+
599+
600+ var u_Translation = gl .getUniformLocation (gl .program , ' u_Translation' ); // [!code ++]
601+ gl .uniform4f (u_Translation, Tx, Ty, Tz, 0.0 ) // [!code ++]
602+
603+ if (n < 0 ) {
604+ console .error (' Failed to set the positions of the vertices' )
605+ return ;
606+ }
607+
608+ // // 获取attribut变量的存储位置
609+ var a_Position = gl .getAttribLocation (gl .program , ' a_Position' )
610+ //
611+ // var u_FragColor = gl.getUniformLocation(gl.program, 'u_FragColor')
612+ if (a_Position < 0 ) {
613+ console .error (' Failed to get the storage location of a_Position' )
614+ return ;
615+ }
616+ //
617+ // canvas.onmousedown = function (ev) {click(ev, gl, canvas, a_Position, u_FragColor)}
618+
619+
620+
621+ // gl.vertexAttrib3f(a_Position, 0.0, 0.0, 0.0)
622+
623+ // 设置canvas背景色
624+ gl .clearColor (0.0 ,0.0 ,0.0 ,1.0 )
625+
626+ // 清空canvas
627+ gl .clear (gl .COLOR_BUFFER_BIT );
628+
629+ // 绘制三个点
630+ gl .drawArrays (gl .TRIANGLE_FAN , 0 , n)
631+
632+
633+ }
634+
635+ function initVertexBuffers (gl ) {
636+ var vertices = new Float32Array ([
637+ - 0.5 , 0.5 , - 0.5 , - 0.5 , 0.5 , 0.5 , 0.5 , - 0.5
638+ ])
639+ var n = 4 // 点的个数
640+
641+ // 创建缓冲区对象
642+ var vertexBuffer = gl .createBuffer ();
643+ if (! vertexBuffer) {
644+ console .error (' Failed to create the buffer object' )
645+ return - 1 ;
646+ }
647+
648+ // 将缓冲区对象绑定到目标
649+ gl .bindBuffer (gl .ARRAY_BUFFER , vertexBuffer)
650+
651+ // 向缓冲区对象中写入数据
652+ gl .bufferData (gl .ARRAY_BUFFER , vertices, gl .STATIC_DRAW );
653+
654+ var a_Position = gl .getAttribLocation (gl .program , ' a_Position' );
655+
656+ // 将缓冲区对象分配给a_Position变量
657+ gl .vertexAttribPointer (a_Position, 2 , gl .FLOAT , false , 0 , 0 )
658+
659+ // 连接a_Position变量与分配给它的缓冲对象
660+ gl .enableVertexAttribArray (a_Position)
661+ return n
662+ }
663+
664+ ```
665+
666+ ![ 效果图] ( ./images/translation.png )
667+
668+
669+ ``` js
670+
671+ const VSHADER_SOURCE =
672+ ' attribute vec4 a_Position;\n ' +
673+ ' uniform float u_CosB, u_SinB;\n ' + // [!code ++]
674+ ' void main() {\n ' +
675+ ' gl_Position.x = a_Position.x * u_CosB - a_Position.y * u_SinB;\n ' + // [!code ++]
676+ ' gl_Position.y = a_Position.x * u_SinB + a_Position.y * u_CosB;\n ' + // [!code ++]
677+ ' gl_Position.z = a_Position.z;\n ' + // [!code ++]
678+ ' gl_Position.w = 1.0;\n ' + // [!code ++]
679+ ' }\n '
680+ const FSHADER_SOURCE =
681+ ' void main() {\n ' +
682+ ' gl_FragColor = vec4(1.0, 1.0, 0.0,1.0);\n ' +
683+ ' }\n '
684+
685+
686+ var ANGLE = 90.0 // [!code ++]
687+
688+ function main () {
689+ var canvas = document .getElementById (' webgl' )
690+ var gl = getWebGLContext (canvas)
691+ if (! gl) {
692+ console .error (' Failed to get the rendering context for WebGL' )
693+ return ;
694+ }
695+
696+ // 初始化着色器
697+ if (! initShaders (gl, VSHADER_SOURCE , FSHADER_SOURCE )) {
698+ console .error (' Failed to initialize shaders.' )
699+ return ;
700+ }
701+
702+ // 设置顶点着色器
703+ var n = initVertexBuffers (gl);
704+
705+ if (n < 0 ) {
706+ console .error (' Failed to set the positions of the vertices' )
707+ return ;
708+ }
709+
710+ var radian = Math .PI * ANGLE / 180.0 ; // [!code ++]
711+ var conB = Math .cos (radian); // [!code ++]
712+ var sinB = Math .sin (radian); // [!code ++]
713+
714+ var u_CosB = gl .getUniformLocation (gl .program , ' u_CosB' ); // [!code ++]
715+ var u_SinB = gl .getUniformLocation (gl .program , ' u_SinB' ); // [!code ++]
716+
717+ gl .uniform1f (u_CosB, conB) // [!code ++]
718+ gl .uniform1f (u_SinB, sinB) // [!code ++]
719+
720+
721+
722+ // // 获取attribut变量的存储位置
723+ var a_Position = gl .getAttribLocation (gl .program , ' a_Position' )
724+ //
725+ // var u_FragColor = gl.getUniformLocation(gl.program, 'u_FragColor')
726+ if (a_Position < 0 ) {
727+ console .error (' Failed to get the storage location of a_Position' )
728+ return ;
729+ }
730+ //
731+ // canvas.onmousedown = function (ev) {click(ev, gl, canvas, a_Position, u_FragColor)}
732+
733+
734+ // gl.vertexAttrib3f(a_Position, 0.0, 0.0, 0.0)
735+
736+ // 设置canvas背景色
737+ gl .clearColor (0.0 , 0.0 , 0.0 , 1.0 )
738+
739+ // 清空canvas
740+ gl .clear (gl .COLOR_BUFFER_BIT );
741+
742+ // 绘制三个点
743+ gl .drawArrays (gl .TRIANGLE_FAN , 0 , n)
744+
745+
746+ }
747+
748+ function initVertexBuffers (gl ) {
749+ var vertices = new Float32Array ([
750+ - 0.5 , 0.5 , - 0.5 , - 0.5 , 0.5 , 0.5 , 0.5 , - 0.5
751+ ])
752+ var n = 4 // 点的个数
753+
754+ // 创建缓冲区对象
755+ var vertexBuffer = gl .createBuffer ();
756+ if (! vertexBuffer) {
757+ console .error (' Failed to create the buffer object' )
758+ return - 1 ;
759+ }
760+
761+ // 将缓冲区对象绑定到目标
762+ gl .bindBuffer (gl .ARRAY_BUFFER , vertexBuffer)
763+
764+ // 向缓冲区对象中写入数据
765+ gl .bufferData (gl .ARRAY_BUFFER , vertices, gl .STATIC_DRAW );
766+
767+ var a_Position = gl .getAttribLocation (gl .program , ' a_Position' );
768+
769+ // 将缓冲区对象分配给a_Position变量
770+ gl .vertexAttribPointer (a_Position, 2 , gl .FLOAT , false , 0 , 0 )
771+
772+ // 连接a_Position变量与分配给它的缓冲对象
773+ gl .enableVertexAttribArray (a_Position)
774+ return n
775+ }
776+
777+ ```
778+ ![ 效果图] ( ./images/rotate.png )
0 commit comments