jfixby

Hi
I was checking this libGDX-Spine example: https://github.com/EsotericSoftware/spi ... pTest.java with dynamic lightning and I found that you use some shader program with one single light source:
String vert = "attribute vec4 a_position;\n" //
+ "attribute vec4 a_color;\n" //
+ "attribute vec2 a_texCoord0;\n" //
+ "uniform mat4 u_proj;\n" //
+ "uniform mat4 u_trans;\n" //
+ "uniform mat4 u_projTrans;\n" //
+ "varying vec4 v_color;\n" //
+ "varying vec2 v_texCoords;\n" //
+ "\n" //
+ "void main()\n" //
+ "{\n" //
+ " v_color = a_color;\n" //
+ " v_texCoords = a_texCoord0;\n" //
+ " gl_Position = u_projTrans * a_position;\n" //
+ "}\n" //
+ "";

String frag = "#ifdef GL_ES\n" //
+ "precision mediump float;\n" //
+ "#endif\n" //
+ "varying vec4 v_color;\n" //
+ "varying vec2 v_texCoords;\n" //
+ "uniform sampler2D u_texture;\n" //
+ "uniform sampler2D u_normals;\n" //
+ "uniform vec3 light;\n" //
+ "uniform vec3 ambientColor;\n" //
+ "uniform float ambientIntensity; \n" //
+ "uniform vec2 resolution;\n" //
+ "uniform vec3 lightColor;\n" //
+ "uniform bool useNormals;\n" //
+ "uniform bool useShadow;\n" //
+ "uniform vec3 attenuation;\n" //
+ "uniform float strength;\n" //
+ "uniform bool yInvert;\n" //
+ "\n" //
+ "void main() {\n" //
+ " // sample color & normals from our textures\n" //
+ " vec4 color = texture2D(u_texture, v_texCoords.st);\n" //
+ " vec3 nColor = texture2D(u_normals, v_texCoords.st).rgb;\n" //
+ "\n" //
+ " // some bump map programs will need the Y value flipped..\n" //
+ " nColor.g = yInvert ? 1.0 - nColor.g : nColor.g;\n" //
+ "\n" //
+ " // this is for debugging purposes, allowing us to lower the intensity of our bump map\n" //
+ " vec3 nBase = vec3(0.5, 0.5, 1.0);\n" //
+ " nColor = mix(nBase, nColor, strength);\n" //
+ "\n" //
+ " // normals need to be converted to [-1.0, 1.0] range and normalized\n" //
+ " vec3 normal = normalize(nColor * 2.0 - 1.0);\n" //
+ "\n" //
+ " // here we do a simple distance calculation\n" //
+ " vec3 deltaPos = vec3( (light.xy - gl_FragCoord.xy) / resolution.xy, light.z );\n" //
+ "\n" //
+ " vec3 lightDir = normalize(deltaPos);\n" //
+ " float lambert = useNormals ? clamp(dot(normal, lightDir), 0.0, 1.0) : 1.0;\n" //
+ " \n" //
+ " // now let's get a nice little falloff\n" //
+ " float d = sqrt(dot(deltaPos, deltaPos)); \n" //
+ " float att = useShadow ? 1.0 / ( attenuation.x + (attenuation.y*d) + (attenuation.z*d*d) ) : 1.0;\n" //
+ " \n" //
+ " vec3 result = (ambientColor * ambientIntensity) + (lightColor.rgb * lambert) * att;\n" //
+ " result *= color.rgb;\n" //
+ " \n" //
+ " gl_FragColor = v_color * vec4(result, color.a);\n" //
+ "}";
My question is: how can I modify it to allow multiple light sources?

///-----------------------------------------------------------------------
Here is my guess. Looks like this shader is some sort of C++ code, where I can change the uniform vec3 light to something like uniform vec3[] light. If this is correct assumption then how should I change the values assignment properly?

From this:
lightPosition.x = ... //some x
lightPosition.y = ... //some y
..
batch.begin();
program.setUniformf("ambientIntensity", ambientIntensity);
program.setUniformf("attenuation", attenuation);
program.setUniformf("light", lightPosition);
to what?
jfixby
Posts: 11

Nate

Shaders in libgdx are written using GLSL. I'm afraid I'm not a shader guru, you'd be better off posting on the libgdx forum, or even better asking in libgdx's IRC.
User avatar
Nate

Nate
Posts: 8086

jfixby

Nate wrote:Shaders in libgdx are written using GLSL. I'm afraid I'm not a shader guru, you'd be better off posting on the libgdx forum, or even better asking in libgdx's IRC.
Ok.

I just thought since that example code was saying "Copyright (c) 2013, Esoteric Software", so it would make sense to ask the authors about details ;)
jfixby
Posts: 11

Shiu

I suggest trying kalle_h on the #libgdx channel on irc.freenode.net :)
User avatar
Shiu

Shiu
Posts: 2221


Return to Editor