热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

反射矩阵不能正常工作-reflectionmatrixdoesnotworkproperly

Itriedthistutorialandthisishowicreateandusethereflectionmatrix(mywaterplaneisz

I tried this tutorial and this is how i create and use the reflection matrix (my water plane is z = 0):

我尝试了本教程,这就是我如何创建和使用反射矩阵(我的水平面是z = 0):

glm::mat4 mReflection = glm::mat4
    (
    1.0, 0.0, 0.0, 0.0,
    0.0, 1.0, 0.0, 0.0,
    0.0, 0.0, -1.0, 0.0,
    0.0, 0.0, 0.0, 1.0
    );

camera.setViewMatrix(mReflection * camera.View());
glCullFace(GL_FRONT);

shader:

材质:

vec4 mvpvertex = MVP_reflection * vec4(vertex, 1.0);
vec2 projCoord = mvpvertex.xy / mvpvertex.w;
projCoord = vec2(0.5, 0.5) + 0.5 * projCoord;
projCoord = clamp(projCoord, 0.0, 1.0);

But the whole scene seems to be flipped arround. The things i should see the reflection of in front of me are behind me and vice versa. I tried some more matrix transformations but none if my tries worked. What am I doing wrong and how can I fix it?

但整个场面似乎都颠倒过来了。我应该看到的在我面前的倒影在我背后,反之亦然。我尝试了更多的矩阵变换,但是如果我的尝试成功了,没有一个。我做错了什么,我该如何改正?

1 个解决方案

#1


1  

Since you're multiplying the reflection matrix from the left, this means that it is applied after the view matrix. So a reflection in the z-direction, which is what your matrix defines, will flip front and back.

因为你把反射矩阵从左边相乘,这意味着它在视图矩阵之后被应用。所以z方向的反射,也就是矩阵定义的,会前后翻转。

I think you have two options to fix this:

我认为你有两个选择来解决这个问题:

  1. Apply the reflection before the view transformation. Sine the water plane is at z = 0 in your world space, reflecting in z-direction would then be correct. To do this, you could either make the reflection part of your model transformation (if you have one in code not shown), or swap the order of the multiplication:

    在视图转换之前应用反射。在你的世界空间里,水平面在z = 0处,以z方向反射将是正确的。为此,您可以将反射部分作为模型转换的一部分(如果代码中没有显示反射部分),或者交换乘法的顺序:

    camera.setViewMatrix(camera.View(), mReflection);
    
  2. Since you're talking about a water reflection, I figure that the reflection swaps up-down, which (unless you are using a very unusual projection matrix) is the y-direction after the model and view transformations have been applied. So if you want to apply the reflection after the view transformation, you need a y-reflection:

    因为你说的是水的反射,我认为反射是向上向下的,这(除非你使用一个非常不寻常的投影矩阵)是模型和视图转换应用后的y方向。如果你想在视图转换后应用反射,你需要一个y反射:

    1.0,  0.0, 0.0, 0.0,
    0.0, -1.0, 0.0, 0.0,
    0.0,  0.0, 1.0, 0.0,
    0.0,  0.0, 0.0, 1.0
    

I haven't studied the tutorial you're following in detail, so I'm not sure which of the two options will work better in the context. From a superficial look, it seems like they actually end up flipping twice, both before and after the view transformation. You will have to figure out the details, but I hope this will resolve your main obstacle.

我还没有详细地学习过你正在学习的教程,所以我不确定这两个选项中哪一个在上下文中会更好。从表面上看,它们看起来实际上翻了两次,在视图转换前后都是如此。你必须弄清楚细节,但我希望这能解决你的主要障碍。

The above assumes that you're using a matrix library that operates in the most standard way, where matrices are constructed for vectors being multiplied from the right. The order of multiplication would revert if you operate with row major matrices, and then transpose the matrices when feeding them into the shader.

上面假设你使用的矩阵库以最标准的方式运行,其中矩阵是为从右边相乘的向量构造的。如果你使用行主矩阵,那么乘法的顺序将会恢复,然后在将矩阵输入着色器时,将它们转置。


推荐阅读
author-avatar
化合价steuart_968
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有