当前位置:网站首页>【我的OpenGL学习进阶之旅】关于OpenGL的着色器的向量和矩阵分类的访问方式: xyzw/rgba/stpq以及数组下标
【我的OpenGL学习进阶之旅】关于OpenGL的着色器的向量和矩阵分类的访问方式: xyzw/rgba/stpq以及数组下标
2022-06-30 06:47:00 【字节卷动】
一、向量的单独分量的表达方式
向量的单独分量可以用两种方式访问:
- 使用 “
.
”运算符
。 - 使用
数组下标
。
二、命名方案
2.1 三种组合访问方式
根据组成向量的分量数量,每个分量可以通过下面三种组合访问
{x,y,z,w}
通常代表了一个空间坐标(x,y,z,w)
,你可以分别使用.x、.y、.z和.w
来获取它们的第1、2、3、4
个分量。{r,g,b,a}
通常代表了一个颜色(r,g,b,a)
,你可以分别使用.r、.g、.b和.a
来获取它们的第1、2、3、4
个分量。{s,t,p,q}
通常代表了一个纹理坐标(s,t,p,q)
,你可以分别使用.s、.t、.p和.q
来获取它们的第1、2、3、4
个分量。
2.2 组合方式的意义
三种不同命名方案的原因是向量可以互换地用于表示数学上的向量、颜色和纹理坐标
。
x/r/s
分量总是引用向量的第1
个分量。y/g/t
分量总是引用向量的第2
个分量。z/b/p
分量总是引用向量的第3
个分量。w/a/q
分量总是引用向量的第4
个分量。
不同的命名约定只是未了方便。
2.3 组合方式注意事项
但是,不能在访问向量时混合使用命名约定(换言之,不能才有.xgr
这样的引用方法,因为一次只能使用一种命名约定)
三、使用 “.” 运算符
使用".
" 运算符时,可以在操作中重新排列向量的分量。
下面是一个例子。
vec3 myVec3 = vec3(0.0,1.0,2.0); // myVec3 = {0.0,1.0,2.0}
vec3 temp;
temp = myVec3.xyz; // temp = {0.0,1.0,2.0}
temp = myVec3.xxx; // temp = {0.0,0.0,0.0}
temp = myVec3.zyx; // temp = {2.0,1.0,0.0}
四、使用数组下标"[]"运算符访问
除了使用".
" 运算符时,使用数组下标"[]
"运算符访问。
在数组下标中,元素[0]
对应于x
,元素[1]
对应于y
,等等。
矩阵被看成由一些向量组成。例如,mat2
可以看做两个vec2
,mat3
可以看做3个vec3
,等等。
对于矩阵,单独的列可以用数组下标运算符"[]
"选择,然后每个向量可以通过向量访问行为来访问。
下面展示一些访问矩阵的例子:
mat4 myMat4 = mat4(1.0); // Initialize diagonal to 1.0 (identity)
vex4 col0 = myMat4[0]; // Get col0 vector out of the matrix
float m1_1 = myMat4[1][1]; // Get element at [1][1] in matrix
float m2_2 = myMat4[2].z; // Get element at [2][2] in matrix
边栏推荐
猜你喜欢
RT thread migration to s5p4418 (III): static memory pool management
Principle: webmvcconfigurer and webmvcconfigurationsupport pit avoidance Guide
tomorrow! "Mobile cloud Cup" competition air publicity will start!
【json-tutorial】第一章学习笔记
1.9 - 存储器的分类
Four tips in numpy
Porting RT thread to s5p4418 (II): dynamic memory management
First experience of Galaxy Kirin
RT thread Kernel Implementation (V): timer
Gazebo model modification
随机推荐
Four ways to create multithreads
Fastapi learning Day1
Thread safe solutions, communication between threads (classic examples of producers and consumers)
List in set (2)
Why does the verification code not refresh when clicked
Problems and solutions of creating topic messages in ROS
1.9 - Cache
Porting RT thread to s5p4418 (V): thread communication
Record one time of Tencent Test Development Engineer's automation interface test practice experience
Collection and method of traversing collection elements (1)
Huawei full-scale Daniel shared the 598 page full-color Manual of network protocols for the first time
ftplib+ tqdm 上传下载进度条
相关数据库问题提问。
As function memo
Which securities company is good for opening a mobile account? Also, is it safe to open an account online?
力扣------替换空格
关注这场直播,了解能源行业双碳目标实现路径
安装setup对应的组件
High performance distributed execution framework ray
Wechat applet mall project