I have a texture position (2d vector), which I multiply by a 4x4 matrix in the shader. Currently I'm passing the vector as a vec2 attribute and then creating a vec4 out of it:
Java :
private static final float[] TEXTURE_COORDINATES = {
0, 1, // bottom left
1, 1, // bottom right
0, 0, // top left
1, 0, // top right
};
Vertex-shader :
attribute vec2 texturePosition;
uniform mat4 textureMatrix;
// more variables
void main() {
vec4 tp = vec4(texturePosition.x, texturePosition.y, 1, 1);
tp = textureMatrix * tp;
// more code
}
Would it be better (in which way?) to directly pass a vec4 attribute and store the two 1s on the java-side?
Java :
private static final float[] TEXTURE_COORDINATES = {
0, 1, 1, 1, // bottom left
1, 1, 1, 1, // bottom right
0, 0, 1, 1, // top left
1, 0, 1, 1, // top right
};
Vertex-shader :
attribute vec4 texturePosition;
uniform mat4 textureMatrix;
// more variables
void main() {
vec4 tp = textureMatrix * tp;
// more code
}
Would it be better to directly pass a vec4 attribute and store the two 1s on the java-side
No, never send data per vertex if you can just pass it in as constants, it's just a complete waste of bandwidth.
That said if you know ahead of time it's a vec2 input, why on earth are you bothering to use a 4x4 matrix? If all you need it a texture coordinate transform then surely a 2x2 matrix or a 3x3 (for affine) is all you need?
https://en.wikibooks.org/wiki/GLSL_Programming/Vector_and_Matrix_Operations as said there:
"Casting a higher-dimensional vector to a lower-dimensional vector is also achieved with these constructors:"
vec4 a = vec4(-1.0, 2.5, 4.0, 1.0);
vec3 b = vec3(a); // = vec3(-1.0, 2.5, 4.0)
vec2 c = vec2(b); // = vec2(-1.0, 2.5)
"Casting a lower-dimensional vector to a higher-dimensional vector is achieved by supplying these constructors with the correct number of components:"
vec2 a = vec2(0.1, 0.2);
vec3 b = vec3(0.0, a); // = vec3(0.0, 0.1, 0.2)
vec4 c = vec4(b, 1.0); // = vec4(0.0, 0.1, 0.2, 1.0)
or something like this
vec2 position;
gl_Position = gl_ModelViewProjectionMatrix * vec4(position.x, position.y, 0.0, 1.0);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.