glLightfv GL_POSITION GL_LINEAR_ATTENUATION glsl OpenGL3或Ope
glLightfv GL_POSITION GL_LINEAR_ATTENUATION glsl OpenGL3 or OpenGL4 (positional light)
好吧,我又迷失在大量的教程中,这些教程似乎把旧的GL版本与GL3和4混淆了。大多数教程都使用废弃的代码,我正在寻找一个合适的OpenGL3,或者甚至更好的OpenGL4替代这个伪代码:
GLfloat LightRadius=0.5f; //or whatever value.
glLightf(NumLights, GL_LINEAR_ATTENUATION, LightRadius);
GLfloat light_position[] = { LightLoc.X,LightLoc.Y, LightLoc.Z, 1 }; //World space location
glLightfv(NumLights, GL_POSITION, light_position);
for phong lighting
vs可以像这样:
#version 330
layout (location = 0) in vec3 Position;
layout (location = 1) in vec3 PositionNormals;
uniform mat4 projMat;
uniform mat4 viewMat;
uniform mat4 modelMat;
out vec3 vposition;
out vec3 vnormal;
out mat4 vprojMat;
out mat4 vviewMat;
out mat4 vmodelMat;
void main(void)
{
vprojMat = projMat; // from what I understood I need those in the fs as well..?
vviewMat = viewMat;
vmodelMat = modelMat;
vposition = vec3(viewMat * modelMat * vec4 (Position, 1.0));
vnormal = vec3(viewMat * modelMat * vec4 (PositionNormals, 0.0));
gl_Position = projMat * vec4(vposition_eye, 1.0);
}
和fs:
in vec3 vposition;
in vec3 vnormal;
in mat4 vprojMat;
in mat4 vviewMat;
in mat4 vmodelMat;
struct LightInfo
{
vec3 LightLocation;
vec3 DiffuseLightColor;
vec3 AmbientLightColor;
vec3 SpecularLightColor;
float AmbientLightIntensity;
float SpecularLightIntensity;
float LightRadius;
};
uniform LightInfo gLight;
out vec4 FragColor;
void main (void)
{
//Diffuse Lighting
// and here I am lost. Was trying to do in eyespace, but the light seems to float more somewhere instead of having a fixed position.
vec3 light_position = ??? gLight.LightLocation; // probably normalized?
float dot_prod = ???
dot_prod = max (dot_prod, 0.0);
vec3 diffuse_intensity = gLight.DiffuseLightColor * dot_prod; // final diffuse intensity
FragColor=diffuse_intensity;
}
这个想法很简单,只是房间里的一盏灯照向各个方向(像太阳一样),根据任意半径有一个给定的衰减。我就是搞不懂它背后的数学原理。如果这是一个愚蠢的问题,请原谅我,但我读得越多,我就越困惑。我知道我需要计算漫射光的点,表面的朝向(法线)和从表面到光的方向,但是我不能把它们放在一起。
你应该有所有的顶点和光数据在同一个坐标系。如果你在世界坐标中发送光的位置,你也应该与世界空间中的法线做点积。在性能方面,最好将光线发送到眼睛/相机空间。要实现这一点,你应该像这样调用glLightfv:
// The 4th component of the light position should be 1, because it's a position, not a direction
GLfloat light_position[] = { LightLoc.X,LightLoc.Y, LightLoc.Z, 1 }; //World space location
// TODO: Change to eye space multiplying by the inverse of the modelView matrix
Matrix4 invModelView;
inverseOrtho(viewMatrix * modelMatrix, invModelView);
transformVector4(light_position, invModelView);
glLightfv(NumLights, GL_POSITION, light_position); // Send light in eye space
如果你在眼空间中有灯光,像素着色器比在世界空间中计算所有灯光更简单。
void main (void)
{
//Diffuse Lighting
vec3 light_position = gLight.LightLocation; // Light position in eye space
// calculate the light direction from the light to the vertex being iluminated
float dot_prod = dot((vposition - light_position).normalize(), vnormal);
dot_prod = max (dot_prod, 0.0);
vec3 diffuse_intensity = gLight.DiffuseLightColor * dot_prod; // final diffuse intensity
FragColor=diffuse_intensity;
}
相关文章:
- Visual Studio 19-17 库兼容性根据 GL 标志
- glDrawElements GL 错误类型 = 0x824c,严重性 = 0x9146,消息 = GL_INVALID
- 将"Compass"与相机旋转隔离开GL
- 是否有一个版本的glCopyImageSubData在Openg GL中使用混合?
- 计算着色器Open GL ES的多个输入
- Open GL ES 3.1 的计算着色器的最小工作示例
- 致命错误 C1083:无法打开包含文件:"GL/glew.h":没有此类文件或目录
- C++ 致命错误:glad/gl.h:没有这样的文件或目录
- 打开 gl 精灵类不起作用
- Qt 找不到 GL 库
- GCC 相当于 MSVC 中的 /GS、/GL、/Gy、/Oi、/MD
- opengl3 2 个点向量的颜色相同
- 失败的 glewInit:缺少 GL 版本
- 何时在OS X上使用OpenGL vs GL
- 识别安卓中意外的 GL 上下文丢失
- Android Native:CMake 链接错误:未定义对 GL 函数的引用 - 即使包含并链接了 EGL 和 GLESv3
- Glew - 与 GLFW 配对时 gl- 函数符号上出现链接错误
- 将DepthBuffer和TextureBuffer发送到同一片段OpenGL3
- 打开gl es 2.0为什么我的VBO不起作用
- iOS GL ES exc_bad_access on glDrawElements