当前位置:网站首页>【我的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

边栏推荐
- Picture.....
- 1.2(补充)
- Porting RT thread to s5p4418 (V): thread communication
- Idea add database
- Unable to access the Internet at win10 /11 hotspot
- 力扣------替换空格
- RT thread Kernel Implementation (I): threads and scheduling
- Never forget the original intention, and be lazy if you can: C # operate word files
- IO streams (common streams)
- tomorrow! "Mobile cloud Cup" competition air publicity will start!
猜你喜欢
随机推荐
ROS multi machine
Basic questions (I)
原理:WebMvcConfigurer 与 WebMvcConfigurationSupport避坑指南
Numpy中的四个小技巧
程序猿入门攻略(十一)——结构体
原来你是这样的数组,终于学会了
Improve simulation speed during ROS and Px4 joint simulation
安装setup对应的组件
Fastapi learning Day1
1.7 - CPU performance indicators
It turns out that you are such an array. You have finally learned
1.8 - 多级存储
C语言:练习题三
Cmake post makefile:32: * * * missing separator Stop.
RT thread application
Record one time of Tencent Test Development Engineer's automation interface test practice experience
Ffmplay is not generated during the compilation and installation of ffmpeg source code
Centos8 install redis
Several C language implementations
ROS system problem: rosdep init









