简体   繁体   English

GLSL点光源转换

[英]GLSL Point Light Transformation

I wrote a basic vertex shader in GLSL. 我在GLSL中编写了基本的顶点着色器。 Here is the source code: 这是源代码:

varying float intensity;
uniform vec4 lightPos;

void main() {
    intensity = 0.1;
    vec3 tfVert = (gl_ModelViewMatrix*gl_Vertex).xyz;
    vec3 tfNorm = normalize(gl_NormalMatrix*gl_Normal).xyz;
    vec3 tfLightPos = (gl_ModelViewMatrix*lightPos).xyz;

    vec3 lightDir = normalize(tfLightPos-tfVert).xyz;
    intensity += dot(lightDir, tfNorm);

    gl_Position = gl_ModelViewProjectionMatrix*gl_Vertex;
}

After that, the fragment shader lights the fragment with a brightness of the varying intensity float. 之后,片段着色器以可变强度浮动的亮度照亮片段。 Here's what goes wrong: I draw a few cubes, and load an OBJ model into the world, or something like that. 出问题了:我画了几个立方体,然后将OBJ模型加载到这个世界中,或者类似的东西。 Then I use glRotatef() and glTranslatef() and draw the same stuff again. 然后,我使用glRotatef()和glTranslatef()再次绘制相同的内容。 The light shows up twice, (sort of) at two different locations. 在两个不同的位置显示两次 (某种程度)。 Once at the translated position and once at the original position. 一次在平移位置,一次在原始位置。 The translated model is only affected by the light in the translated position, and the original model is only affected by the light in the original position. 平移模型仅受平移位置中的光线影响,而原始模型仅受原始位置中的光线影响。 Also, it is crucial that I do not use gl_LightSource0[] to fix this problem. 另外,至关重要的是,我不要使用gl_LightSource0 []来解决此问题。 Is there something I am doing wrong? 我做错什么了吗? Is there any way to fix this? 有没有什么办法解决这一问题? I am using glTranslatef() and glRotatef() for camera positioning and rotation, if that matters. 我正在使用glTranslatef()和glRotatef()进行相机的定位和旋转,如果这很重要的话。

Don't transform lightPos by gl_ModelViewMatrix 不要通过gl_ModelViewMatrix变换lightPos

Just use lightPos.xyz in place of tfLightPos -- in fact you dodn't need tfLightPos at all 只需使用lightPos.xyz代替tfLightPos-实际上您根本不需要tfLightPos

Sadly, this is impossible to fix without passing more information to the vertex shader. 可悲的是,如果不将更多信息传递给顶点着色器,则无法解决此问题。 Since glTranslatef and glRotatef are used for the camera movements as well as the model movements, there is no way for the shader to distinguish between the two without passing the transformations and rotations to GLSL in vec3s. 由于glTranslatef和glRotatef用于摄影机运动和模型运动,因此着色器无法在vec3中不将转换和旋转传递给GLSL的情况下区分两者。

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

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