Vec--OpenGL的顶点基础

Task

Find the sum of three vectors in the program (blueChannel, redChannel, alphaChannel), and print the result using gl_FragColor variable.
求三个向量的和(blueChannel,redChannel,alphaChannel),并使用变量gl_FragColor输出结果

Theory

在 GLSL 中,vec 是一种用于定义向量的数据类型。向量用于表示以一维、二维、三维或四维的标量值集合(例如浮点数、整数等)。vec数据类型后面跟着一个数字,用于指定向量的维数。

GLSL 中常见的 vec 类型:

  • vec2:表示二维向量。
  • vec3:表示三维向量。
  • vec4:表示四维向量。

您可以在 GLSL 中使用向量来表示位置、方向、颜色、纹理坐标等。向量值可以像标量值一样应用相同的数学运算符。这些运算符对向量的每个元素执行分量运算。但是,要使这些运算符在向量上起作用,两个向量必须具有相同数量的元素。

在 GLSL 中使用 vec 的示例:

定义 vec 变量:

glsl 复制代码
vec3 position = vec3(1.0, 2.0, 3.0);

访问 vec 变量的组成部分:

glsl 复制代码
float x = position.x; // accesses the x component of the vec3 position
float y = position.y; // accesses the y component of the vec3 position
float z = position.z; // accesses the z component of the vec3 position

对 vec 变量执行操作:

glsl 复制代码
vec2 a = vec2(1.0, 2.0);
vec2 b = vec2(3.0, 4.0);
vec2 sum = a + b; // vector addition
vec2 difference = a - b; // vector subtraction
vec2 scaled = a * 2.0; // scalar multiplication

Answer

glsl 复制代码
void main() {
  vec4 blueChannel = vec4(0.0, 0.0, 0.75, 0.0);
  vec4 redChannel = vec4(0.5, 0.0, 0.0, 0.0);
  vec4 alphaChannel = vec4(0.0, 0.0, 0.0, 1.0);
  
  gl_FragColor = blueChannel + redChannel + alphaChannel;
}

练习

Vec

相关推荐
IMPYLH2 分钟前
HTML 的 <th> 元素
前端·html
风骏时光牛马3 分钟前
AI浪潮下程序员的职场破局与能力成长思考
前端
IMPYLH9 分钟前
HTML 的 <thead> 元素
前端·html
FungLeo9 小时前
成为全栈·React 管理后台篇·总复盘:从“接口能调通”到“后台值得使用”
前端·react.js·前端框架·项目复盘·成为全栈
Csvn10 小时前
并发模式:让渲染学会排队、插队和让路
前端
可乐鸡翅yeah_10 小时前
新手梳理:M3U8 线上问题,哪些是前端锅,哪些是后端锅
前端·ios·音视频·实时音视频·m3u8·音视频在线播放
Flynt10 小时前
Linear 用 1000 个 PR 换掉 styled-components,我写了 200 个按钮,把这笔账复现了一遍
前端·css·preact
JavaGuide11 小时前
NVIDIA 又开源了!这次给 AI Agent 加上权限管控
前端·后端
excel11 小时前
prisma 如何处理数据库竞态
前端·数据库·后端
莪_幻尘13 小时前
Skill 体检:30 个 Skill 全凭感觉?体检器先自曝了 8 个“假 0 分
前端·人工智能·llm