I am trying to find a solution that will allow me to rotate point sprites about the z-axis with a varying attribute (i.e. uniform will not do).
In my app I have many hundreds/thousands of point sprites being drawn per frame, which are then stored in VBOs (can quite feasibly end up being >1,000,000). As such, I am looking for the best compromise between memory usage and performance.
Vertex & fragment shaders current look like this:
// VERTEX SHADER
attribute vec4 a_position;
attribute vec4 a_color;
attribute float a_size;
uniform mat4 u_mvpMatrix;
varying vec4 v_color;
void main()
{
v_color = a_color;
gl_Position = u_mvpMatrix * a_position;
gl_PointSize = a_size;
}
// FRAGMENT SHADER
precision mediump float;
uniform sampler2D s_texture;
varying vec4 v_color;
void main()
{
vec4 textureColor = texture2D(s_texture, gl_PointCoord);
gl_FragColor = v_color * textureColor;
}
I can currently imagine the following possibilities:
Add a mat4 rotMatrix
attribute to my point sprite data. Pass this to the fragment shader and rotate each fragment:
vec2 texCoord = (rotMatrix * vec4(gl_PointCoord, 0, 1)).xy
gl_FragColor = v_color * texture2D(s_texture, texCoord);
GLKit
for example).Add a float angle
attribute to my point sprite data, then calculate the rotation matrix in the vertex shader. Pass the rotation matrix to the fragment shader as above.
Any thoughts as to a good approach?
The solution I went with in the end was the 2nd from the question: calculate the rotation matrix in the vertex shader. This has the following advantages:
The disadvantages I guessed at don't seem to apply. I have not noticed a performance hit, even running on a 1st gen iPad. The matrix calculation in GLSL is somewhat cumbersome, but works fine. For the benefit of anybody else trying to do the same, here is the relevant part of the vertex shader:
//...
attribute float a_angle;
varying mat4 v_rotationMatrix;
void main()
{
//...
float cos = cos(a_angle);
float sin = sin(a_angle);
mat4 transInMat = mat4(1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.5, 0.5, 0.0, 1.0);
mat4 rotMat = mat4(cos, -sin, 0.0, 0.0,
sin, cos, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0);
mat4 resultMat = transInMat * rotMat;
resultMat[3][0] = resultMat[3][0] + resultMat[0][0] * -0.5 + resultMat[1][0] * -0.5;
resultMat[3][1] = resultMat[3][1] + resultMat[0][1] * -0.5 + resultMat[1][1] * -0.5;
resultMat[3][2] = resultMat[3][2] + resultMat[0][2] * -0.5 + resultMat[1][2] * -0.5;
v_rotationMatrix = resultMat;
//...
}
Given that there is no noticeable performance hit this solution is ideal, as there is no need to create texture maps/lookups and consume additional memory, and it keeps the rest of the code clean and simple.
I can't say that there are no downsides to calculating a matrix for every vertex (reduced battery life, for example), and performance may be a problem in different scenarios, but it's good for what I need.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With