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

相关推荐
FRing031 分钟前
JavaScript入门到精通二十四(正则表达式、元字符、修饰符)
开发语言·前端·javascript·正则表达式
葡萄城技术团队36 分钟前
在 SpreadJS 里新增右键菜单,把业务动作放到用户手边
前端
大明二代38 分钟前
从 RAG 到有记忆的 Agent:Mastra Memory 与 PostgreSQL 实战
前端·javascript·人工智能
deli00741 分钟前
代码截图一键出 PNG 实测:3 种窗口风格,长行超 95 字符会被裁
前端
大哥430943 分钟前
慢 ≠ 挂:AI 搜索降级判定的设计,与一次 reasoning 模型排查复盘
前端
UaoN1 小时前
作为全栈开发者,为什么看到 Shopify 回归原生,我依然选择 React Native
前端·后端
PBitW1 小时前
行内编辑,前端如何兜底?😭😭😭
前端·面试
小羊没烦恼!1 小时前
C#综合揭秘——Entity Framework 并发处理详解
java·开发语言·前端·javascript·数据库·mysql·c#
wing982 小时前
人生第一个私活:不到 2 小时,到手 350 元
前端·后端·程序员
张张123y2 小时前
后端第十二课:Agent 开发(让大模型从“会答“升级到“会做“)
前端·大模型·llm·agent·rag