0

I am trying to draw twinkling stars in the background of my live-wallpaper app to give it some "spice". I have thought of 2 ways to do this. 1st, make an animated gif in Photoshop convert it to a movie in Android studio and set it as the background. Or 2nd, draw twinkling stars using OpenGL ES. I would much prefer the second way, but how would I go about do this? I was thinking maybe drawing a circle then scaling it to be really small and duplicate it (a hundred times) and translate it all across the screen but that doesn't seem too efficient. Can anyone tell me of any ways to do this?

Peter O.
  • 32,158
  • 14
  • 82
  • 96
Dashboarrd
  • 169
  • 1
  • 2
  • 11

1 Answers1

1

You can see this How to draw a star in iOS OpenGL ES 2.0.

Vertex Shader

In the vertex shader, you transform the vertex as you normally would, and also set the built-in gl_PointSize variable:

 uniform float PointSize; attribute vec4 Position;

 void main() {
    gl_Position = ...;  // Transform Position attribute;
    gl_PointSize = PointSize; } 

For the example, I used a uniform for the point size, which means that all stars will have the same size.

Depending on the desired effect, you could also calculate the size based on the distance, or use an additional vertex attribute to specify a different size for each star.

Fragment Shader

In the fragment shader, you can now access the built-in gl_PointCoord variable to get the relative coordinates of the fragment within the point sprite. If your point sprite is a simple texture image, you can use it directly as the texture coordinates.

uniform sampler2D SpriteTex;

 void main() {
     gl_FragColor = texture2D(SpriteTex, gl_PointCoord); }
Community
  • 1
  • 1
UtopiaIsGood
  • 169
  • 3
  • 15