1
\$\begingroup\$

So I have been shading (brighten/darken +adding color) my sprites by using glTexEnvf with GL_MODULATE and GL_ADD and a vertex color. I only had to rebind the batch every time I needed to change from modulate to add and vice versa. it was not perfect but worked well enough.

However libGDX 1.0 dropped support for OpenGL1.1 and the only way I see is to use shaders. I want to stay up to date and therefore want to update. I am new to shaders but I found some code that works to brighten a sprite. However as far as I understood I need to rebind the batch every time I want another value for my brightness => batch binding on every sprite I render. That is not very performant.

  • Is there a way to change values between the bindings? (I don't think it's possible because shaders get compiled afaik).
  • Or do you have another idea how to archive this effect with openGL2.0 (like I did before)?
  • Or is rebinding every sprite the most efficient way with OpenGL2.0?
\$\endgroup\$
3
\$\begingroup\$

Make a vertex attribute "vec4 Color", and multiply the color you get from the texture sampler with the Color vertex attribute.

vec4 color = texture2D(TextureSampler, TexCoord) * Color;

Supply white and the brightness will remain unchanged, supply black and the sprite will become black, supply something brighter than white the sprite will become brighter. (Brighter than white would be 2.0, 2.0, 2.0, 1.0)

As a added bonus you can also colorize the sprite this way, supply pure red and it will become (dark) red. And you can draw them half-transparent too by changing the 4. parameter, the alpha channel.

If you don't need that just multiply by a single float instead of a vec4.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ I am sorry but I am a bit overwhelmed by your answer. I have never used shaders before or learned OpenGL in depth. I get your idea it's basically what I did before with OpenGL1.0 but I don't understand how I can apply this to my code. Can you please provide more details on where to do this? My current solution is to rebind the batch with the shader every time I change the brightness of a sprite like this:"if (brightness != this.brightness){ end(); this.brightness = brightness; begin(); } }" Does your solution not need this? \$\endgroup\$ – Benedikt S. Vogler Apr 25 '14 at 13:08
  • \$\begingroup\$ Shaders are one of the most basic things, you should learn them from scratch. Vertex attributes are specified per vertex and interpolated between them, e.g. you will have a array with color values alongside the vertex position and texture coordinates. \$\endgroup\$ – API-Beast Apr 26 '14 at 7:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.