Reputation: 151
So I've been working on a project involving a custom shader and with the help of people on here I've been able to implement a shader with lambert and phong glsl code with the help of Rabbid76.
Now I want to utilize the position of the light both in the glsl code and in libgdx.
Rabbid76 lent me this code for a phong shade with a struct Lightpoint. I gave it a run and I got nothing but a black screen. There is a uniform PointLight num_pointLights which I'm assuming is pointing to the struct. I'm unsure how to incorporate this as a uniform in libgdx code, or as a variable in libgdx.
Vertex:
attribute vec3 a_position;
attribute vec3 a_normal;
attribute vec2 a_texCoord0;
uniform mat4 u_worldTrans;
uniform mat4 u_projViewTrans;
varying vec2 v_texCoords;
varying vec3 v_vertPosWorld;
varying vec3 v_vertNVWorld;
void main()
{
vec4 vertPos = u_worldTrans * vec4(a_position, 1.0);
v_vertPosWorld = vertPos.xyz;
v_vertNVWorld = normalize(mat3(u_worldTrans) * a_normal);
v_texCoords = a_texCoord0;
gl_Position = u_projViewTrans * vertPos;
}
Fragment:
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 );
}
Its been suggested that I can initialize the PointLight class like: environment.add(pointLight = new PointLight().set( ..... ));, But I'm unsure how this would work because I would have to use an environment class variable instead of my shader.
I feel so close to getting this. But I'm not even sure if its possible using this method. If anyone is able to share some experience and knowledge about how to use the light position or the struct in a meaningful way, I'd really really appreciate. :)
Here is the link to my current project: here
Upvotes: 1
Views: 416
Reputation: 151
After a bit of tinkering and wondering what to do I decided to check out this. I somehow came across:
Vector3 LIGHT_POS = new Vector3(0, 0, 0);
..and then saw:
program.setUniformf("lightPosition", LIGHT_POS);
..which references whats in the shader. I was unsure if I could figure this out.
o_O
I incremented the x variable in the Vector3 LIGHT_POS, in the render function of the shader. It seems to work well. I guess I should try out the DirectionalLight when I get around to it.
code: here
Upvotes: 1