簡體   English   中英

GLSL移動點燈

[英]GLSL Moving Point Light

我一直在為我的LWJGL + Java應用程序編寫點光源着色器。 我是根據本教程編寫的。 with the camera, the light moves as well. 我的問題是當我用相機時,燈光也會移動。 另外,當我旋轉球體時,燈光也會隨之旋轉。

我相信問題出在Vertex Shader中 ,但我也將片段着色器放在以防萬一。

示例1(不移動)
例子1

示例2(向左移動並旋轉相機)
例子2

頂點着色器

#version 330

in vec4 in_Position;
in vec3 in_Normal;
in vec2 in_TextureCoord;

uniform mat4 projection;
uniform mat4 view;
uniform mat4 model;
uniform mat3 normal;

uniform vec4 light_Pos; //Set to 0, 3, 0, 1

out Data {
    vec3 normal;
    vec3 eye;
    vec3 lDir;
    vec2 st;
} Out;

void main(void) {
    vec4 vert = view * model * light_Pos;
    vec4 pos = model * view * in_Position;
    Out.normal = normalize(in_Normal);
    Out.lDir = vec3(vert - pos);
    Out.eye = vec3(-pos);
    Out.st = in_TextureCoord;
    gl_Position = projection * view * model * in_Position;
}

片段着色器

#version 330

uniform sampler2D texture_diffuse;

in Data {
    vec3 normal;
    vec3 eye;
    vec3 lDir;
    vec2 st;
} In;

out vec4 color;

void main(void) {
    vec4 diffuse = texture(texture_diffuse, In.st);
    vec4 spec = vec4(0.0);

    vec3 n = normalize(In.normal);
    vec3 l = normalize(In.lDir);
    vec3 e = normalize(In.eye);

    float i = max(dot(n,l), 0.0);
    if (i > 0.0) {
        vec3 h = normalize(l+e);
        float intSpec = max(dot(h,n), 0.0);
        spec = vec4(1) * pow(intSpec, 50);  //50 is the shininess
    }
    color = max(i * diffuse + spec, vec4(0.2));
}


我已經嘗試過此問題中提出的解決方案,但沒有解決我的問題。

乍一看,就好像您將光源的位置乘以視圖和模型矩陣:

vec4 vert = view * model * light_Pos;

這意味着,每當您走動/移動攝像機時,您都在更改會影響燈光位置的視圖矩陣,同樣,當您移動球體時,您也在更改也會影響燈光位置的模型矩陣。

換句話說,如果您希望光相對於世界是靜止的,則不要通過任何矩陣對其進行變換。

問題在於您的法線,Out.lDir和Out.eye不在同一坐標系中。 法線位於模型的坐標中,而其他坐標也位於眼部空間中。 嘗試以與light_Pos類似的方式將眼睛位置作為制服傳遞。

Light_Pos和Eye_Pos現在處於世界坐標系中。 現在算一下

vec4 pos = model * in_Position;
vec4 vert = light_Pos;

Out.eye = vec3(eye_Pos);

它應該工作。 執行空間操作時,請始終確保所有點/向量都在同一坐標系中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM