- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在研究法线贴图的实现,通过 ASSIMP 库计算切线 vector 。
法线贴图似乎非常适合模型矩阵接近单位矩阵的对象。只要我开始平移和缩放,我的照明似乎就关闭了。正如您在图片中看到的那样,法线贴图在容器立方体上完美运行,但照明在大地板上失败(镜面光的方向应该朝向玩家,而不是朝向容器)。
我觉得它与光的位置(当前从 x = -10 到 x = 10 随着时间的推移)有某种关系,只要我开始改变模型矩阵就没有正确地包含在计算中(通过翻译/缩放)。我发布了所有相关代码,希望你们能以某种方式看到我遗漏的东西,因为我已经盯着我的代码好几天了。
顶点着色器
#version 330
layout(location = 0) in vec3 position;
layout(location = 1) in vec3 normal;
layout(location = 2) in vec3 tangent;
layout(location = 3) in vec3 color;
layout(location = 4) in vec2 texCoord;
// fragment pass through
out vec3 Position;
out vec3 Normal;
out vec3 Tangent;
out vec3 Color;
out vec2 TexCoord;
out vec3 TangentSurface2Light;
out vec3 TangentSurface2View;
uniform vec3 lightPos;
// vertex transformation
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
void main()
{
mat3 normalMatrix = transpose(mat3(inverse(view * model)));
Position = vec3((view * model) * vec4(position, 1.0));
Normal = normalMatrix * normal;
Tangent = tangent;
Color = color;
TexCoord = texCoord;
gl_Position = projection * view * model * vec4(position, 1.0);
vec3 light = vec3(view * vec4(lightPos, 1.0));
vec3 n = normalize(normalMatrix * normal);
vec3 t = normalize(normalMatrix * tangent);
vec3 b = cross(n, t);
mat3 mat = mat3(t.x, b.x ,n.x, t.y, b.y ,n.y, t.z, b.z ,n.z);
vec3 vector = normalize(light - Position);
TangentSurface2Light = mat * vector;
vector = normalize(-Position);
TangentSurface2View = mat * vector;
}
片段着色器
#version 330
in vec3 Position;
in vec3 Normal;
in vec3 Tangent;
in vec3 Color;
in vec2 TexCoord;
in vec3 TangentSurface2Light;
in vec3 TangentSurface2View;
out vec4 outColor;
uniform vec3 lightPos;
uniform mat4 view;
uniform sampler2D texture0;
uniform sampler2D texture_normal; // normal
uniform float repeatFactor = 1;
void main()
{
vec4 texColor = texture(texture0, TexCoord * repeatFactor);
vec3 light = vec3(view * vec4(lightPos, 1.0));
float dist = length(light - Position);
float att = 1.0 / (1.0 + 0.01 * dist + 0.001 * dist * dist);
// Ambient
vec4 ambient = vec4(0.2);
// Diffuse
vec3 surface2light = normalize(TangentSurface2Light);
vec3 norm = normalize(texture(texture_normal, TexCoord * repeatFactor).xyz * 2.0 - 1.0);
float contribution = max(dot(norm, surface2light), 0.0);
vec4 diffuse = contribution * vec4(0.8);
// Specular
vec3 surf2view = normalize(TangentSurface2View);
vec3 reflection = reflect(-surface2light, norm); // reflection vector
float specContribution = pow(max(dot(surf2view, reflection), 0.0), 32);
vec4 specular = vec4(0.6) * specContribution;
outColor = (ambient + (diffuse * att)+ (specular * pow(att, 3))) * texColor;
}
OpenGL 绘图代码
void Render()
{
...
glm::mat4 view, projection; // Model will be done via MatrixStack
view = glm::lookAt(position, position + direction, up); // cam pos, look at (eye pos), up vec
projection = glm::perspective(45.0f, (float)width/(float)height, 0.1f, 1000.0f);
glUniformMatrix4fv(glGetUniformLocation(basicShader.shaderProgram, "view"), 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(glGetUniformLocation(basicShader.shaderProgram, "projection"), 1, GL_FALSE, glm::value_ptr(projection));
// Lighting
lightPos.x = 0.0 + sin(time / 125) * 10;
glUniform3f(glGetUniformLocation(basicShader.shaderProgram, "lightPos"), lightPos.x, lightPos.y, lightPos.z);
// Objects (use bump mapping on this cube)
bumpShader.Use();
glUniformMatrix4fv(glGetUniformLocation(bumpShader.shaderProgram, "view"), 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(glGetUniformLocation(bumpShader.shaderProgram, "projection"), 1, GL_FALSE, glm::value_ptr(projection));
glUniform3f(glGetUniformLocation(bumpShader.shaderProgram, "lightPos"), lightPos.x, lightPos.y, lightPos.z);
MatrixStack::LoadIdentity();
MatrixStack::Scale(2);
MatrixStack::ToShader(glGetUniformLocation(bumpShader.shaderProgram, "model"));
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, resources.GetTexture("container"));
glUniform1i(glGetUniformLocation(bumpShader.shaderProgram, "img"), 0);
glActiveTexture(GL_TEXTURE1); // Normal map
glBindTexture(GL_TEXTURE_2D, resources.GetTexture("container_normal"));
glUniform1i(glGetUniformLocation(bumpShader.shaderProgram, "normalMap"), 1);
glUniform1f(glGetUniformLocation(bumpShader.shaderProgram, "repeatFactor"), 1);
cubeNormal.Draw();
MatrixStack::LoadIdentity();
MatrixStack::Translate(glm::vec3(0.0f, -22.0f, 0.0f));
MatrixStack::Scale(glm::vec3(200.0f, 20.0f, 200.0f));
MatrixStack::ToShader(glGetUniformLocation(bumpShader.shaderProgram, "model"));
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, resources.GetTexture("floor"));
glActiveTexture(GL_TEXTURE1); // Normal map
glBindTexture(GL_TEXTURE_2D, resources.GetTexture("floor_normal"));
glUniform1f(glGetUniformLocation(bumpShader.shaderProgram, "repeatFactor"), 100);
cubeNormal.Draw();
MatrixStack::LoadIdentity();
glActiveTexture(GL_TEXTURE0);
...
}
编辑我现在使用启用了“aiProcess_CalcTangentSpace”标志的 ASSIMP 库加载我的对象,并相应地更改我的着色器以适应新的变化。因为 ASSIMP 现在自动计算正确的切线 vector ,我应该有有效的切线 vector ,我的问题应该得到解决(正如 Nicol Bolas 所指出的),但我仍然有同样的问题,镜面照明表现得很奇怪,漫射照明没有真正出现.我想还有其他东西不能正常工作。我没有将您的答案标记为正确答案 Nicol Bolas(目前)并相应地更新了我的代码,因为我仍然缺少一些东西。
这可能与翻译有关。一旦我向模型矩阵添加平移(y 方向上的 -22.0f),它就会对奇怪的光照使用react。只要地板(实际上是一个立方体)没有平移,照明看起来就很好。
最佳答案
calculating the tangent vectors in the vertex shader
那么这就是你的问题。这对于任意表面是不可能的。
切线和副切线不是相互垂直的任意 vector 。它们是指向纹理坐标方向的模型空间方向 vector 。切线指向 S 纹理坐标的方向,双切线指向 T 纹理坐标的方向(如果您愿意,也可以是纹理坐标的 U 和 V)。
这有效地计算了纹理相对于表面上每个顶点的方向。您需要这种方向,因为当您想要理解切线空间 vector 时,纹理映射到表面的方式很重要。
请记住:切线空间是垂直于表面的空间。但是您需要知道该表面是如何映射到对象的,以便知道“向上”在哪里,例如。取一个正方形表面。您可以映射纹理,使正方形的 +Y 部分沿纹理的 +T 方向定向。或者它可以沿着正方形的 +X。您甚至可以对其进行映射,使纹理变形或以任意角度旋转。
切线和副切线 vector 旨在校正此映射。它们指向模型空间中的 S 和 T 方向。因此,结合法线,它们形成一个变换矩阵,从切线空间变换到 3 个 vector 所在的任何空间(您通常将 NBT 变换到相机空间或在使用它们之前用于照明的任何空间)。
您不能仅通过采用法线并将其与某个任意 vector 相交来计算它们。这会产生垂直法线,但不是正确的法线。
为了正确计算切线/副切线,您需要访问多个顶点。您需要能够看到纹理坐标如何在网格表面上发生变化,这就是您计算相对于网格的 S 和 T 方向的方式。
顶点着色器不能访问多个顶点。几何着色器也不能(通常)访问足够的顶点来执行此操作。在 CPU 上离线计算切线/副切线。
关于c++ - 法线贴图和光照出错,无法正确显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18120651/
每当我尝试使用 AndroidStudio 生成一个新的 Android 项目时,它总是隐藏文件夹“drawables”。这以前从未发生在我身上。我环顾四周,发现它生成了这个名为“mipmap”的文件
因此,我试图使 native map 在EXPO应用程序中工作,文档页面显示“在EXPO应用程序中无需使用设置” https://docs.expo.io/versions/latest/sdk/ma
摘要 我正在尝试将位移贴图(高度贴图)应用于一个相当简单的对象(六角形平面),并且得到了一些意想不到的结果。我使用的是灰度,因此,我的印象是我的高度图应该只影响网格的 Z 值。然而,我创建的置换贴图在
我目前正在对three.js 进行一些试验,我想使用自发光贴图。我曾尝试将纹理加载到 phong Material 的自发光属性中,但不幸的是,它并没有像那样工作。这是我的代码: var params
我是地理编码方面的新手,所以我希望你能给我一些提示,告诉我如何获得以下内容: 我想用 D3 或传单制作一张 map ,上面有一些标记。如果单击标记,信息应弹出/显示在 map 旁边。有点像这个例子:h
我正在使用jvector maps客户网站上的 map ,他们知道希望我将其转换为 Concrete5,以便他们可以自行编辑。一切都很顺利,直到我尝试重新创建我使用 jVector map 的页面。当
这会导致疯狂的编译时间:~20 秒。请注意,它创建了 global-s9e3ed7fd2e.png 图像 5 次,而不是移动设备一次和桌面一次。 我只看到 @import "sprites/globa
我想创建一个 STL map 来查找一个项目是否足够接近 3 维空间中的另一个项目。到目前为止,我的“less-than-functor”工作得很好,粘贴到以下链接。 现在这个问题不完全是“最近邻”问
在初始化 map 时直接给出标记和多边形似乎可以工作,但是在调用 setState 方法时不会添加多边形和标记。 我不知道我似乎错过了什么, 以下是我迄今为止尝试过的。 有人可以帮助解决我所缺少的 m
我想弄清楚如何在盒子的正面和背面使用两种不同的纹理。每当我缩放我的盒子 (ExtrudeGeometry) 时,UV 贴图似乎都没有更新。因此,我为盒子的正面和背面定义了自己的 UV 贴图。 要定义我
我正在尝试正确映射 UV 纹理,但失败了... 我的应用程序中有下一个结果: 结果不是我在等待。我想要下一个描述的 View : 源代码在这里: http://pastebin.com/aDg981B
我正在尝试创建一个 Material 对话框以使用 AGM( Angular 谷歌地图)捕获用户位置。我的主页上有一个工作示例,但是当对话框打开时,它只会在 map 应该显示的位置显示一个空白区域。当
尝试将 UV 坐标映射到球体时遇到以下问题 这是我用来获取 UV 坐标的代码 glm::vec2 calcUV( glm::vec3 p) { p = glm::normalize(p);
我需要找到一种方法来解决在不使用设计工具的情况下为非凸多面体生成纹理贴图的问题。具体来说,我将模拟结果数据映射到 3D 表面上。也就是说,在代码中(最好是 C# 或 VB,因为我的目标是 WPF 3D
尝试添加 infoWindows 时,我收到 Unable to get gMarker frommarkerScope! (在 Angular map 中: https://github.com/a
这是一个非常简单的 Three.JS 草图,生成以下渲染: 正如您可能看到的,这个 THREE.BufferGeometry 是一个 8x8 矩阵,通过以这种方式设置 UV: for(var i =
我在使用 UV 贴图渲染 3D 对象时遇到问题。 首先,我的对象在 Wavefront 模型中。我使用一个解析器将整个文件拆分为顶点、法线、面和纹理坐标。解析 file.obj 后,我拥有了所有这些。
在 RStudio 中使用传单会在查看器中生成空白 map 。只能看到标记。在控制台中看不到任何警告或错误消息。当我使用 shiny 时,它在 RStudio 查看器中是一样的,但可以通过在 shin
在互联网上多次搜索后我找不到答案.. 情况如何?我是(比利时)大学计算机科学专业的学生,我必须使用图形库 LibGdx 用 Java 制作塔防。 问题是什么?在LibGdx中,有一个名为“Ti
如何将多个参数传递给通过 pool.map 调用的函数? 我的代码: import gevent from gevent.pool import Pool def process(param1, pa
我是一名优秀的程序员,十分优秀!