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

边栏推荐
- ftplib+ tqdm 上传下载进度条
- Software tools_ Shortcut_ Operation summary
- RT thread Kernel Implementation (VI): time slice
- 手机开户一般哪个证券公司好?还有,在线开户安全么?
- Write a C program to judge whether the system is large end byte order or small end byte order
- Unable to read file for extraction: gdx64. dll
- InnoDB engine in MySQL
- MySQL中的InnoDB引擎
- ROS service communication programming
- SOC_SD_CLK
猜你喜欢
随机推荐
Loading class `com. mysql. jdbc. Driver‘. This is deprecated. The new driver class is `com. mysql. cj. jdb
0 basic job transfer software test, how to achieve a monthly salary of 9.5k+
oracle
Four ways to create multithreads
程序猿入门攻略(十一)——结构体
Cmake post makefile:32: * * * missing separator Stop.
C语言:练习题三
C language: exercise 3
Introduction to programming ape (11) -- structure
SOC_AHB_SD_IF
SOC_ AHB_ SD_ IF
The most complete sentence in history
Ls1028 manual
Combat simulation system data
1.4 - 定点数与浮点数
1.8 - multi level storage
RT thread Kernel Implementation (IV): multi priority
1.7 - CPU performance indicators
Control method of UAV formation
When to use redis









