Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9110603
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T03:18:34+00:00 2026-06-17T03:18:34+00:00

I am trying to find a solution that will allow me to rotate point

  • 0

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);
    
    • Advantages:
      • Keeps shaders simple.
      • Simple code to compute matrices outside the shaders (using GLKit for example).
    • Disadvantages:
      • Massively increases the size of my point sprite data (from 16 to 80 bytes/point for a 4×4 matrix; to 52 bytes/point for a 3×3 matrix… I believe it’s possible to use a 3×3 rotation matrix?). This could potentially cause my app to crash 3-5 times sooner!
      • Pushes a lot more computation onto the CPU (hundreds/thousands of matrix calculations per frame).

  • 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.

    • Advantages:
      • Keeps point sprite data size small (from 16 to 20 bytes/point).
      • Pushes the heavy lifting matrix maths to the GPU.
    • Disadvantages:
      • Need to write custom GLSL function to create rotation matrix. Not a massive problem, but my matrix maths is rusty, so this could be error prone, especially if I’m trying to figure out the 3×3 matrix solution…
      • Given that this must happen on hundreds/thousands of vertices, is this going to be a serious drag on performance (despite being handled by the GPU)?

  • I could realistically cope with 1 byte for the angle attribute (255 different angles would be sufficient). Is there any way I could use some kind of lookup so that I don’t need to needlessly recalculate the same rotation matrices? Storing constants in the vertex shader was my first thought, but I don’t want to start putting branch statements in my shaders.

Any thoughts as to a good approach?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T03:18:35+00:00Added an answer on June 17, 2026 at 3:18 am

    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:

    • Keeps point sprite data size small.
    • Rotation calculations are performed by the GPU.

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently trying to find I solution that will not let a value
I am trying to find a solution that will resolve a recurring deadlock situation
I'm trying to formulate a regular expression that will allow me to find a
I was trying to find a solution but did not succeed even if it
I have been trying to find a solution to this one but could not
We're looking for a solution that will allow us to use HTTPS without encryption.
I'm trying to find a solution using variables or some other jQuery technique that
All, I am having problems trying to find a good solution that I can
I've researched this issue and can not find a solution. My problem is that
I have looked and looked, but I cannot find a method that will allow

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.