首页 > 解决方案 > 在 glsl 顶点着色器中使用额外的统一 mat4 转换灯光?

问题描述

我正在为我的场景使用场景图。它包含灯光和网格。网格具有局部变换,如果节点是网格节点的子节点,则其自身的局部变换与其父节点的变换组合在一起。

现在我正在尝试实现灯光。我将灯光的坐标(世界空间坐标)作为制服发送到我的着色器。

我必须转换这些光坐标吗?对我来说,必须使用特定于网格的变换来变换这些光坐标是没有意义的。

我是否需要向着色器发送另一个统一矩阵来以不同的方式变换灯光?

我的代码:

#version 330

const int numOfLights = 2;


// shader input
in vec2 vUV;                // vertex uv coordinate
in vec3 vNormal;            // untransformed vertex normal
in vec3 vPosition;          // untransformed vertex position

uniform vec3 pointLights[numOfLights];

// shader output
out vec4 normal;            // transformed vertex normal
out vec2 uv;    
out vec4 position;      
out vec3 lights[numOfLights];           
uniform mat4 transform; //transform unique for this mesh containing the camera transformation


// vertex shader
void main()
{ 
    position = transform * vec4(vPosition, 1.0);
    // transform vertex using supplied matrix
    gl_Position = position;

    // forward normal and uv coordinate; will be interpolated over triangle
    normal = transform * vec4( vNormal, 0.0f );
    uv = vUV;

    for (int i = 0; i < numOfLights; i++) {
        //lights[i] = (transform * vec4(pointLights[i], 1.0)).xyz; //**Transform**?
        lights[i] = pointLights[i];
    }

}

标签: openglglslopentkphong

解决方案


推荐阅读