简体   繁体   English

LWJGL GLSL着色器定向光由内向外出现

[英]LWJGL GLSL shader directional light appears inside out

When I render a vbo cube using a directional light shader, the light appears "inside out". 当我使用定向光着色器渲染vbo立方体时,灯光显示为“由内而外”。 (Sorry if I can't explain it better). (对不起,如果我无法更好地解释它)。

外

内

Here is my vertex shader code (My fragment shader just applies the color, all the work is done in the vertex shader). 这是我的顶点着色器代码(我的片段着色器仅应用颜色,所有工作都在顶点着色器中完成)。

#version 150 core
in vec4 in_Position;
in vec4 in_Color;
in vec2 in_TextureCoord;
in vec3 in_Normal;

uniform sampler2D texture;

uniform mat4 projection;
uniform mat4 view;
uniform mat4 model;
uniform mat3 normalMatrix;
uniform vec3 lDir;

out vec4 color;

void main(void) {
    mat3 nMat = normalMatrix;
    vec3 normal = normalize(in_Normal * nMat);
    vec3 dir = normalize(lDir);
    float nl = max(dot(dir, normal), 0.25);
    //color = NdotL * vec4(0,1,0,1);
    color = vec4(0,1,0,1) * nl;
    gl_Position = projection * view * model * in_Position;
}

The lDir uniform variable stores a float[] array of {0,1,1}. lDir统一变量存储{0,1,1}的float []数组。 The uniform variable normalMatrix is calculated like this. 统一变量normalMatrix这样计算。

    FloatBuffer preBuffer = BufferUtils.createFloatBuffer(16);
    modelMatrix.store(preBuffer);
    preBuffer.flip();
    Matrix4f preMat = new Matrix4f();
    preMat.load(preBuffer);

    Matrix3f preModel3f;
    Matrix3f normal3f = new Matrix3f();
    preModel3f = Tk.to3f(preMat);
    preModel3f.transpose(normal3f);
    normal3f.invert();

    normal3f.store(nmBuffer);
    nmBuffer.flip();
    glUniformMatrix3(glGetUniformLocation(ss.pId, "normalMatrix"), false, nmBuffer);

the modelMatrix variable is self explanitory, minus the fact that it is a buffer, not a matrix. modelMatrix变量是自解释的,减去它是缓冲区而不是矩阵的事实。 The normal3f variable stores the transposed inverse of the model matrix (which I am pretty sure is the correct way to calculate the normal Matrix). normal3f变量存储模型矩阵的转置逆(我很确定这是计算正常矩阵的正确方法)。 I have to put the modelMatrix buffer into a buffer called preBuffer, if I don't I get errors for some reason. 如果由于某种原因我没有收到错误,我必须将modelMatrix缓冲区放入一个名为preBuffer的缓冲区中。

Leave a comment if you need anything else to help solve this. 如果您需要其他帮助解决此问题,请发表评论。

I figured out the problem, when I rendered the text to the screen (the text that describes the problem with the cube), I forgot to set the glBlendFunc() back to this setting, 我发现了问题,当我将文本呈现到屏幕上(描述多维数据集问题的文本)时,我忘记将glBlendFunc()设置回此设置,

glBlendFunc(GL_ONE, GL_ZERO);

This was causing the transparency issue and also fixed some other problems. 这导致了透明度问题,还解决了其他一些问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM