Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (739)
Games in Android Showcase (224)
games submitted by our members
Games in WIP (820)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  LibGDX 3D Controlling the light position in custom shader  (Read 258 times)
0 Members and 1 Guest are viewing this topic.
Offline cybrmynd

Senior Newbie


Medals: 5



« Posted 2017-10-02 21:28:12 »

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;
}

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 );
}

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 );
}

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. Smiley

Heres the project itself on github: https://github.com/winnieTheWind/3DShaderLightingTest
Pages: [1]
  ignore  |  Print  
 
 

 
Ecumene (52 views)
2017-09-30 02:57:34

theagentd (76 views)
2017-09-26 18:23:31

cybrmynd (183 views)
2017-08-02 12:28:51

cybrmynd (182 views)
2017-08-02 12:19:43

cybrmynd (189 views)
2017-08-02 12:18:09

Sralse (197 views)
2017-07-25 17:13:48

Archive (747 views)
2017-04-27 17:45:51

buddyBro (881 views)
2017-04-05 03:38:00

CopyableCougar4 (1429 views)
2017-03-24 15:39:42

theagentd (1319 views)
2017-03-24 15:32:08
List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05

SF/X Libraries
by SkyAphid
2017-03-02 06:38:56

SF/X Libraries
by SkyAphid
2017-03-02 06:38:32

SF/X Libraries
by SkyAphid
2017-03-02 06:38:05

SF/X Libraries
by SkyAphid
2017-03-02 06:37:51
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!