当前位置:网站首页>webgl_ Graphic transformation (rotation, translation, zoom)
webgl_ Graphic transformation (rotation, translation, zoom)
2022-07-07 15:39:00 【Vegetable chicken on the road】
translation
Translate a triangle , You need to measure each component of the vertex coordinates (x and y), Add the triangle on the corresponding axis ( Such as X Axis or Y Axis ) Up translation distance . such as , Will point p(x, y, z) Translation to p’(x’, y’, z’), stay X Axis 、Y Axis 、Z The translation distance in the three directions of the axis is Tx,Ty,Tz, among Tz by 0, As shown in the figure below .
Then on the corresponding component of the coordinate , Add these directly T value , You can be sure p’ It's the coordinates of , As shown below .
x’= x +Tx
y’ = y + Ty
z’=z+Tz
We only need to add a constant to each component of vertex coordinates in the shader to realize the above equation . obviously , This is a vertex by vertex operation , The above modification should occur in vertex shaders , Not in the slice shader .
When modifying the code , Translate the distance Tx、Ty、Tz The value of is passed into the vertex shader , Then they are added to the corresponding components of the vertex coordinates , Assigned to it again gl_Position. Let's take a look at the modified example program .
rotate
Rotation is slightly more complicated than translation , To describe a rotation , You must specify :
● Rotation axis ( The graph will rotate around the axis of rotation ).
● Direction of rotation ( Direction : Clockwise or counter clockwise ).
● Rotation Angle ( The angle through which the figure rotates ).
set sth. up p(x, y, z) rotate β The angle then becomes a point p ‘(x ’ , y ‘,z’): First of all, the rotation is around Z It's done on the axis , therefore z The coordinates will not change , Can be ignored directly ﹔ then ,x Coordinates and y The situation of coordinates is a little complicated , Here's the picture :
In the figure 3.22 in ,r Is from the origin to the point p Distance of , and α yes X The axis rotates to the point p The angle of . Use these two variables to calculate the point p Coordinates of , As shown below :
x = rcosα
y = rsinα
Similarly , You can use r、α、β To show a little p’ Coordinates of , After a series of changes, we get the final equation , As shown below :
x’= r cos (α +β)
y’= r sin (α +β)
===>
x’ = r (cosα cos β - sinα sin β)
y’ = r (sinα cos β + cosα sin β)
===>
x’= x cos β - y sin β
y’= x sin β + y cos β
z’=z
Again , For rotation operation , Also operate on vertex shaders , The modification procedure is as follows :
Above , In defining variables u_cosB and u_sinB when , Why uniform Variable , Because the values of these two variables are independent of vertices .
Transformation matrix : rotate
If the figure is rotated, after the complex operation of Translation , If we calculate as shown above , It's going to be complicated , So we introduce the transformation matrix , It is also the basis for subsequent graphic transformation operations .
Suppose there is a matrix as follows , Homovector (x,y,z), After multiplication, we get a new vector (x’,y’,z’):( Be careful : The multiplication of matrix and vector does not conform to the distribution rate of multiplication ,AxB It's not equal to BxA Of )
In this case , New vector obtained by multiplying matrix and vector , The three components are x’、y’、z, The values are as follows . Be careful , Only when the number of columns of the matrix is equal to the number of rows of the vector , Then you can multiply the two .
x’ = ax + by + cz
y’ = dx + ey + fz
z’ = gx + hy + iz
Now? , To understand how matrices replace mathematical expressions , Next, compare the matrix equation with the mathematical expression .
Bring the results into , You can get a new vector (x’,y’,z’) as follows :
This matrix is called the transformation matrix , Because it will be the vector on the right (x, y,z)“ Transformation ” For the vector on the left (x ', y ‘, z’). The transformation of the transformation matrix above is a rotation , So this matrix can also be called rotation matrix .
4*4 The rotation matrix of
Transformation matrix : translation
Transformation matrix : The zoom
We store each element of the matrix in an array , But the problem is : The matrix is two-dimensional , Its elements are arranged in rows and columns , And the array is one-dimensional , Its elements are just arranged in a line . here , We can store matrix elements in an array in two ways : By row main order and by column main order ,webGL and OpenGL equally , Matrix elements are stored in the array in the main order of columns . Here's the picture :
See here, you should have a certain understanding of the changes in graphics , Then use these changes to achieve complex transformation effects .
边栏推荐
- 【跟着江科大学Stm32】STM32F103C8T6_PWM控制直流电机_代码
- Starting from 1.5, build a microservice framework link tracking traceid
- 知否|两大风控最重要指标与客群好坏的关系分析
- 简述keepalived工作原理
- [quick start of Digital IC Verification] 26. Ahb-sramc of SystemVerilog project practice (6) (basic points of APB protocol)
- [server data recovery] a case of RAID data recovery of a brand StorageWorks server
- What are PV and UV? pv、uv
- 【目标检测】YOLOv5跑通VOC2007数据集
- 从 1.5 开始搭建一个微服务框架链路追踪 traceId
- Ctfshow, information collection: web10
猜你喜欢
【数字IC验证快速入门】22、SystemVerilog项目实践之AHB-SRAMC(2)(AMBA总线介绍)
There is a cow, which gives birth to a heifer at the beginning of each year. Each heifer has a heifer at the beginning of each year since the fourth year. Please program how many cows are there in the
Ctfshow, information collection: web8
Ctfshow, information collection: web2
【數字IC驗證快速入門】20、SystemVerilog學習之基本語法7(覆蓋率驅動...內含實踐練習)
Ctfshow, information collection: web14
[quickstart to Digital IC Validation] 20. Basic syntax for system verilog Learning 7 (Coverage Driven... Including practical exercises)
MySQL bit type resolution
【兰州大学】考研初试复试资料分享
【深度学习】语义分割实验:Unet网络/MSRC2数据集
随机推荐
TypeScript 发布 4.8 beta 版本
Ctfshow, information collection: web12
Points for attention in porting gd32 F4 series programs to gd32 F3 series
有一头母牛,它每年年初生一头小母牛。每头小母牛从第四个年头开始,每年年初也生一头小母牛。请编程实现在第n年的时候,共有多少头母牛?
【深度学习】语义分割实验:Unet网络/MSRC2数据集
"Baidu Cup" CTF competition 2017 February, web:include
[deep learning] semantic segmentation experiment: UNET network /msrc2 dataset
大表delete删数据导致数据库异常解决
leetcode 241. Different ways to add parentheses design priority for operational expressions (medium)
Unity's ASE realizes cartoon flame
[markdown grammar advanced] make your blog more exciting (IV: set font style and color comparison table)
Gd32 F3 pin mapping problem SW interface cannot be burned
Guangzhou Development Zone enables geographical indication products to help rural revitalization
HPDC smart base Talent Development Summit essay
Connecting FTP server tutorial
Do you know the relationship between the most important indicators of two strong wind control and the quality of the customer base
[quickstart to Digital IC Validation] 20. Basic syntax for system verilog Learning 7 (Coverage Driven... Including practical exercises)
什么是数据泄露
Comparable and comparator of sorting
【搞船日记】【Shapr3D的STL格式转Gcode】