简体   繁体   English

GLSL灯/高亮移动相机

[英]GLSL light/highlight moving with camera

VC++ 2010, OpenGL, GLSL, SDL VC ++ 2010,OpenGL,GLSL,SDL

I am moving over to shaders, and have run into a problem that originally occured while working with the ogl pipeline. 我现在转到着色器,并遇到了在使用ogl管道时最初发生的问题。 That is, the position of the light seems to point in whatever direction my camera faces. 也就是说,光的位置似乎指向我的相机面向的任何方向。 In the ogl pipeline it was just the specular highlight, which was fixable with: 在ogl管道中,它只是镜面高光,可修复:

glLightModelf(GL_LIGHT_MODEL_LOCAL_VIEWER, 1.0f);

Here are the two shaders: 这是两个着色器:

Vertex 顶点

varying vec3 lightDir,normal;

void main()
{
    normal = normalize(gl_NormalMatrix * gl_Normal);
    lightDir = normalize(vec3(gl_LightSource[0].position));

    gl_TexCoord[0] = gl_MultiTexCoord0;
    gl_Position = ftransform();
}

Fragment 分段

varying vec3 lightDir,normal;
uniform sampler2D tex;

void main()
{
    vec3 ct,cf;
    vec4 texel;
    float intensity,at,af;

    intensity = max(dot(lightDir,normalize(normal)),0.0);

    cf = intensity * (gl_FrontMaterial.diffuse).rgb + 
                        gl_FrontMaterial.ambient.rgb;
    af = gl_FrontMaterial.diffuse.a;

    texel = texture2D(tex,gl_TexCoord[0].st);
    ct = texel.rgb;
    at = texel.a;

    gl_FragColor = vec4(ct * cf, at * af);  
}

Any help would be much appreciated! 任何帮助将非常感激!

The question is: What coordinate system (reference frame) do you want the lights to be in? 问题是:你希望灯光在哪个坐标系(参考系)? Probably "the world". 可能是“世界”。

OpenGL's fixed-function pipeline, however, has no notion of world coordinates, because it uses a modelview matrix, which transforms directly from eye (camera) coordinates to model coordinates. 然而,OpenGL的固定功能管道没有世界坐标的概念,因为它使用了模型视图矩阵,它直接从眼睛(相机)坐标转换为模型坐标。 In order to have “fixed” lights, you could do one of these: 为了拥有“固定”灯,您可以执行以下操作之一:

  • The classic OpenGL approach is to, every frame, set up the modelview matrix to be the view transform only (that is, be the coordinate system you want to specify your light positions in) and then use glLight to set the position (which is specified to apply the modelview matrix to the input). 经典的OpenGL方法是,每一帧,将模型视图矩阵设置为仅视图变换(即,您要指定光位置的坐标系),然后使用glLight设置位置(指定的位置)将模型视图矩阵应用于输入)。

  • Since you are using shaders, you could also have separate model and view matrices and have your shader apply both (rather than using ftransform) to vertices, but only the view matrix to lights. 由于您使用着色器,您还可以使用单独的模型和视图矩阵,并使着色器将两者(而不是使用ftransform)应用于顶点,但仅将视图矩阵应用于灯光。 However, this means more per-vertex matrix operations and is probably not an especially good idea unless you are looking for clarity rather than performance. 然而,这意味着更多的每顶点矩阵运算,并且可能不是一个特别好的主意,除非您正在寻找清晰度而不是性能。

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

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