Hello there,
I’m currently working on a 3D project in LibGDX using an AssetManager, a simple setup for rendering and a custom shader using a vertex and fragment glsl file. I have some lambert and phong shading and it looks fine, but I’m unsure how to control the position of the lighting.
lambert vertex:
[quote]attribute vec3 a_position;
attribute vec3 a_normal;
attribute vec2 a_texCoord0;
uniform mat4 u_worldTrans;
uniform mat4 u_projViewTrans;
uniform vec3 lightPosition;
varying vec2 v_texCoords;
varying float v_lightIntensity;
void main()
{
vec4 vertPos = u_worldTrans * vec4(a_position, 1.0);
vec3 normal = normalize(mat3(u_worldTrans) * a_normal);
vec3 toLightVector = normalize(lightPosition - vertPos.xyz);
v_lightIntensity = max( 0.3, dot(normal, toLightVector));
v_texCoords = a_texCoord0;
gl_Position = u_projViewTrans * vertPos;
}
[/quote]
lambert fragment:
[quote]varying vec2 v_texCoords;
varying vec3 v_vertPosWorld;
varying vec3 v_vertNVWorld;
uniform vec3 lightPosition;
uniform sampler2D u_texture;
void main()
{
vec3 toLightVector = normalize(lightPosition - v_vertPosWorld.xyz);
float lightIntensity = max( 0.0, dot(v_vertNVWorld, toLightVector));
vec4 texCol = texture( u_texture, v_texCoords.st );
gl_FragColor = vec4( texCol.rgb * lightIntensity, 1.0 );
}
[/quote]
I’ve been lurking on stackOverflow with the question and got a response. While the shaders work, I’m unsure about how I link the uniform vec3 lightPosition in the shader with a usable variable in java libGDX in order to change the position of the lighting. Another example I was given was using a struct Pointlight like this for example:
[quote]varying vec2 v_texCoords;
varying vec3 v_vertPosWorld;
varying vec3 v_vertNVWorld;
uniform sampler2D u_texture;
struct PointLight
{
vec3 color;
vec3 position;
float intensity;
};
uniform PointLight u_pointLights[1];
void main()
{
vec3 toLightVector = normalize(u_pointLights[0].position - v_vertPosWorld.xyz);
float lightIntensity = max( 1.0, dot(v_vertNVWorld, toLightVector));
vec4 texCol = texture( u_texture, v_texCoords.st );
vec3 finalCol = texCol.rgb * lightIntensity * u_pointLights[0].color;
gl_FragColor = vec4( finalCol.rgb * lightIntensity, 10.0 );
}
[/quote]
Now I noticed that the struct has the same variables as a PointLight class in libGDX, so they must be related, but I can’t seem to figure out how to make the struct and uniform usable in libGDX.
Anyone who can point me to the right direction or lend me a hand, I’d really really greatly appreciate your help. Thank you very much.
Heres the project itself on github: https://github.com/winnieTheWind/3DShaderLightingTest