LWJGL 将一个 Vector3f 数组输入到片段着色器中
LWJGL imput a Vector3f Array into Fragment Shader
我目前正在开发 LWJGL 2 游戏。我正在使用 https://github.com/mattdesl/lwjgl-basics 中的库(例如 Texture 和 ShaderProgram class)。我知道,您可以在着色器中渲染有限数量的灯光,但我没有找到任何使照明更加动态和高效的解决方案。
我想将 Vector3f[] 灯作为统一的 vec3 灯 [] 传递到 .frag 文件中。我怎样才能做到这一点?还有其他动态改变着色时灯光数量的想法吗?这是片段着色器的代码:
//attributes from vertex shader
varying vec4 vColor;
varying vec2 vTexCoord;
uniform sampler2D u_texture;
uniform sampler2D u_normals;
//the array should be inserted as:
uniform vec3 Lights[];
uniform int numOfLights;
uniform vec2 Resolution;
uniform float Rotation;
//uniform vec3 LightPos;
uniform vec4 LightColor;
uniform vec4 AmbientColor;
uniform vec3 Falloff;
void main() {
vec4 sumColor = vec4(0.0,0.0,0.0,0.0);
for(int i = 0;i < numOfLights;i++){
vec3 LightPos = Lights[i];
vec4 DiffuseColor = texture2D(u_texture, vTexCoord);
vec3 NormalMap = texture2D(u_normals, vTexCoord).rgb;
vec3 LightDir = vec3(LightPos.x - gl_FragCoord.x/ Resolution.x,LightPos.y - gl_FragCoord.y / Resolution.y, LightPos.z);
float D = length(LightDir);
vec3 N = normalize(NormalMap * 2.0 - 1.0);
float LRot = atan(LightDir.y, LightDir.x);
vec3 L = normalize(vec3(cos(LRot + Rotation),sin(LRot + Rotation),LightDir.z + 0.5));
vec3 Diffuse = (LightColor.rgb * LightColor.a) * max(dot(N, L), 0.0);
vec3 Ambient = AmbientColor.rgb * AmbientColor.a;
float Attenuation = 1.0 / ( Falloff.x + (Falloff.y*D) + (Falloff.z*D*D) );
vec3 Intensity = 2 * Ambient + Diffuse * Attenuation;
vec3 FinalColor = DiffuseColor.rgb * Intensity;
//sumColor gets added as many times as there are lights
sumColor = sumColor + vColor * vec4(FinalColor, DiffuseColor.a);
}
gl_FragColor = sumColor;
}
目前,我使用 ShaderProgram class 中的 setUniformf(String name, Vector3f v) 方法将其他向量传递到 .frag 文件中。
例如,我想像这样传递它们:
public Vector3f[] lights;
//all the lights get add into the array
shader.setUniformf("Lights", lights);
如果我可以将灯光存储在 ListArray<> 中并将其传递到片段着色器中,那就更好了。
恐怕这是 OpenGL 中光照的问题。如果你想做所谓的前向着色,你的着色器中必须有有限数量的灯光。
有几种方法可以进行更高级的光照计算并且仍然具有良好的性能,其中之一是延迟着色。
您可以在此处找到有关此内容的精彩文章 http://learnopengl.com/#!Advanced-Lighting/Deferred-Shading
不幸的是,它使用 C++,但我希望你能找到更多关于如何使用 java 的信息。
我目前正在开发 LWJGL 2 游戏。我正在使用 https://github.com/mattdesl/lwjgl-basics 中的库(例如 Texture 和 ShaderProgram class)。我知道,您可以在着色器中渲染有限数量的灯光,但我没有找到任何使照明更加动态和高效的解决方案。 我想将 Vector3f[] 灯作为统一的 vec3 灯 [] 传递到 .frag 文件中。我怎样才能做到这一点?还有其他动态改变着色时灯光数量的想法吗?这是片段着色器的代码:
//attributes from vertex shader
varying vec4 vColor;
varying vec2 vTexCoord;
uniform sampler2D u_texture;
uniform sampler2D u_normals;
//the array should be inserted as:
uniform vec3 Lights[];
uniform int numOfLights;
uniform vec2 Resolution;
uniform float Rotation;
//uniform vec3 LightPos;
uniform vec4 LightColor;
uniform vec4 AmbientColor;
uniform vec3 Falloff;
void main() {
vec4 sumColor = vec4(0.0,0.0,0.0,0.0);
for(int i = 0;i < numOfLights;i++){
vec3 LightPos = Lights[i];
vec4 DiffuseColor = texture2D(u_texture, vTexCoord);
vec3 NormalMap = texture2D(u_normals, vTexCoord).rgb;
vec3 LightDir = vec3(LightPos.x - gl_FragCoord.x/ Resolution.x,LightPos.y - gl_FragCoord.y / Resolution.y, LightPos.z);
float D = length(LightDir);
vec3 N = normalize(NormalMap * 2.0 - 1.0);
float LRot = atan(LightDir.y, LightDir.x);
vec3 L = normalize(vec3(cos(LRot + Rotation),sin(LRot + Rotation),LightDir.z + 0.5));
vec3 Diffuse = (LightColor.rgb * LightColor.a) * max(dot(N, L), 0.0);
vec3 Ambient = AmbientColor.rgb * AmbientColor.a;
float Attenuation = 1.0 / ( Falloff.x + (Falloff.y*D) + (Falloff.z*D*D) );
vec3 Intensity = 2 * Ambient + Diffuse * Attenuation;
vec3 FinalColor = DiffuseColor.rgb * Intensity;
//sumColor gets added as many times as there are lights
sumColor = sumColor + vColor * vec4(FinalColor, DiffuseColor.a);
}
gl_FragColor = sumColor;
}
目前,我使用 ShaderProgram class 中的 setUniformf(String name, Vector3f v) 方法将其他向量传递到 .frag 文件中。 例如,我想像这样传递它们:
public Vector3f[] lights;
//all the lights get add into the array
shader.setUniformf("Lights", lights);
如果我可以将灯光存储在 ListArray<> 中并将其传递到片段着色器中,那就更好了。
恐怕这是 OpenGL 中光照的问题。如果你想做所谓的前向着色,你的着色器中必须有有限数量的灯光。 有几种方法可以进行更高级的光照计算并且仍然具有良好的性能,其中之一是延迟着色。
您可以在此处找到有关此内容的精彩文章 http://learnopengl.com/#!Advanced-Lighting/Deferred-Shading
不幸的是,它使用 C++,但我希望你能找到更多关于如何使用 java 的信息。