问题描述
我尝试在我的 opengl 应用程序中实现法线贴图,但我无法让它工作.
I tried to implement normal mapping in my opengl application but I can't get it to work.
这个是漫反射贴图(我添加了棕色)和这个是法线贴图.
This is the diffuse map (which I add a brown color to) and this is the normal map.
为了获得切线和双切线(在其他地方称为副法线?)向量,我对网格中的每个三角形运行此函数:
In order to get the tangent and bitangent (in other places called binormals?) vectors, I run this function for every triangle in my mesh:
之后,我调用 glBufferData 将顶点、法线、uvs、切线和双切线上传到 GPU.顶点着色器:
After that, I call glBufferData to upload the vertices, normals, uvs, tangents and bitangents to the GPU. The vertex shader:
片段着色器:
我得到的图像完全不正确.(灯位于相机上)
The image I get is completely incorrect. (light positioned on camera)
我在这里做错了什么?
推荐答案
我的赌注是在片段着色器中的颜色设置/混合...
My bet is on the color setting/mixing in fragment shader...
您正在多次设置输出颜色
如果我没记错的话,一些 gfx 驱动程序会出现大问题,例如行后的所有内容
If I remember correctly on some gfx drivers that do a big problems for example everything after the line
可以被驱动删除...
您正在添加 color
和 intensities
而不是 color*intensity
you are adding color
and intensities
instead of color*intensity
但我可以忽略一些东西.
but I could overlook someting.
首先尝试普通/凹凸阴影
忽略环境、反射、镜面反射...然后如果它有效,则将其余的一一添加.始终检查着色器的编译日志
Ignore ambient,reflect,specular... and then if it works add the rest one by one. Always check the shader's compilation logs
懒得进一步分析你的代码,所以我是这样做的:
左侧尺寸为固定功能渲染的太空船物体(类似于ZXS Elite的Viper).右侧相同(对象的旋转略有不同),带有 GLSL 着色器 和此法线/凹凸贴图
Left size is space ship object (similar to ZXS Elite's Viper) rendered with fixed function. Right side the same (a bit different rotation of object) with GLSL shader's in place and this normal/bump map
[顶点]
[片段]
这些源代码有点旧,并且针对特定应用混合了不同的东西
These source codes are bit old and mix of different things for specific application
所以只提取你需要的东西.如果您对变量名称感到困惑,请评论我...
So extract only what you need from it. If you are confused with the variable names then comment me...
tm_
代表变换矩阵l2g
代表局部坐标系到全局坐标系的变换dir
表示变换只是改变方向(偏移量为 0,0,0)g2s
代表 global to screen ...per
是透视变换...
tm_
stands for transform matrixl2g
stands for local coordinate system to global coordinate system transformdir
means that transformation changes just direction (offset is 0,0,0)g2s
stands for global to screen ...per
is perspective transform ...
GLSL 编译日志
您必须在编译着色器(不是应用程序!!!)后以编程方式获取其内容.我通过为每个着色器调用函数 glGetShaderInfoLog
来做到这一点,我使用的程序......
You have to obtain its content programaticaly after compilation of your shader's (not application!!!). I do it with calling the function glGetShaderInfoLog
for every shader,program I use ...
[注释]
某些驱动程序优化了未使用"的变量.正如您在图像中看到的那样,即使片段着色器在代码中具有 txr_texture1
也未找到,但此应用程序中未使用混合,因此驱动程序自行删除了它...
Some drivers optimize "unused" variables. As you can see at the image txr_texture1
is not found even if the fragment shader has it in code but the blending is not used in this App so driver deleted it on its own...
着色器日志可以显示很多信息(语法错误、警告...)
Shader logs can show you much (syntax errors, warnings...)
很少有 GLSL IDE 可以简化着色器,但我更喜欢我自己的,因为我可以直接在其中使用目标应用程序代码.我的看起来像这样:
there are few GLSL IDEs for making shader's easy but I prefer my own because I can use in it the target app code directly. Mine looks like this:
每个txt窗口都是一个着色器源(顶点,片段,...)右下角是剪贴板,左上角是上次编译后的着色器日志,左下角是预览.我设法像 Borland 风格的 IDE 一样编码它(还有键和语法高亮)我看到的其他 IDE 看起来很相似(粗的不同颜色:))无论如何如果你想玩着色器的下载这样的应用程序或者自己做它会有很大帮助...
each txt window is a shader source (vertex,fragment,...) the right bottom is clipboard, left top is shader's log after last compilation and left bottom is the preview. I managed to code it like Borland style IDE (with the keys also and syntax highlight) the other IDEs I saw look similar (different colors of coarse:)) anyway if you want to play with shader's download such App or do it your self it will help a lot...
TBN 创建也可能存在问题
您应该通过在每个顶点位置绘制彩色线来目视检查 TBN 向量(切线、副法线、法线)是否对应于对象表面.只是为了确定......像这样:
You should visually check if the TBN vectors (tangent,binormal,normal) correspond to object surface by drawing colored lines at each vertex position. Just to be sure... something like this:
这篇关于法线贴图严重错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!