为什么 golang gomobile 基本示例为 vec4 属性设置了 3 个浮点数的大小?

huangapple go评论85阅读模式
英文:

Why does golang gomobile basic example sets 3-float size for a vec4 attribute?

问题

Golang gomobile基本示例[1]使用VertexAttribPointer来设置每个顶点的3个FLOATS。

然而,顶点着色器属性类型是vec4。它不应该是vec3吗?

为什么?

在渲染循环中:

glctx.VertexAttribPointer(position, coordsPerVertex, gl.FLOAT, false, 0, 0)

三角形数据:

var triangleData = f32.Bytes(binary.LittleEndian,
	0.0, 0.4, 0.0, // 左上角
	0.0, 0.0, 0.0, // 左下角
	0.4, 0.0, 0.0, // 右下角
)

常量声明:

const (
	coordsPerVertex = 3
	vertexCount     = 3
)

在顶点着色器中:

attribute vec4 position;

[1] gomobile基本示例:https://github.com/golang/mobile/blob/master/example/basic/main.go

英文:

Golang gomobile basic example [1] uses VertexAttribPointer to set 3 x FLOATS per vertex.

However the vertex shader attribute type is vec4. Shouldn't it be vec3?

Why?

Within render loop:

glctx.VertexAttribPointer(position, coordsPerVertex, gl.FLOAT, false, 0, 0)

Triangle data:

var triangleData = f32.Bytes(binary.LittleEndian,
	0.0, 0.4, 0.0, // top left
	0.0, 0.0, 0.0, // bottom left
	0.4, 0.0, 0.0, // bottom right
)

Constant declaration:

const (
	coordsPerVertex = 3
	vertexCount     = 3
)

In vertex shader:

attribute vec4 position;

[1] gomobile basic example: https://github.com/golang/mobile/blob/master/example/basic/main.go

答案1

得分: 2

顶点属性在概念上始终是4个分量的向量。在着色器中使用的分量数量和为属性指针设置的分量数量并没有要求必须匹配。如果你的数组具有比着色器消耗的分量更多的分量,额外的分量将被忽略。如果你的数组提供的分量较少,属性将填充为形式为(0,0,0,1)的向量(这对于齐次位置向量和RGBA颜色也是有意义的)。

通常情况下,你希望每个输入位置的w=1,没有必要将其存储在数组中。但是,在应用变换矩阵时(甚至在直接将值作为gl_Position转发时),通常需要完整的4D形式。因此,你的着色器可以概念上执行以下操作:

in vec3 pos;
gl_Position=vec4(pos,1);

但这等效于只写:

in vec4 pos;
gl_Position=pos;
英文:

Vertex attributes are conceptually always 4 component vectors. There is no requirement that the number of components you use in the shader and the one you set up for the attribute pointer have to match. If your array has more components than your shader consumes, the additional components are just ignored. If your array supplies less components, the attribute is filled to a vector of the form (0,0,0,1) (which makes sense for homogeneous position vectors as well as RGBA colors).

In the usual case, you want w=1 for every input position anyway, there is no need to store that in an array. But you usually need the full 4D form when applying the transformation matrices (or even when directly forwarding the value as gl_Position). So your shader could conceptually do

in vec3 pos;
gl_Position=vec4(pos,1);

but that would be equivalent of just writing

in vec4 pos;
gl_Position=pos;

huangapple
  • 本文由 发表于 2017年6月28日 06:20:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/44790847.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定