如何根据场景max和最小坐标设置glm::ortho的边界?

时间:2022-09-28 14:20:24

I have a triangle and have 3 vertices anywhere in space.

我有一个三角形有3个顶点。

I attempted to get the max and min coordinates for it.

我试图得到它的最大和最小坐标。

void findBoundingBox(glm::vec3 & minBB, glm::vec3 & maxBB)
{
        minBB.x = std::min(minBB.x, mCoordinate.x);  
        minBB.y = std::min(minBB.y, mCoordinate.y);
        minBB.z = std::min(minBB.z, mCoordinate.z);
        maxBB.x = std::max(maxBB.x, mCoordinate.x);
        maxBB.y = std::max(maxBB.y, mCoordinate.y);
        maxBB.z = std::max(maxBB.z, mCoordinate.z);
    }
}

Now I tried to set :

现在我试着设置:

glm::vec3 InverseViewDirection(50.0f, 200, 200);  //Inverse View Direction
glm::vec3 LookAtPosition(0.0,0,0); // I can make it anywhere with barycentric coord, but this is the simple case
glm::vec3 setupVector(0.0, 1, 0);

I tried to set the orthographic view to wrap the triangle by:

我试图将正投影视图设置为:

myCamera.setProjectionMatrix(min.x, max.x, max.y,min.y, 0.0001f, 10000.0f);

But its not neatly bounding the triangle in my view.

但在我看来,它并没有完全包围三角形。

I've been stumped on this for a day, any pointers?

我被难住一天了,有什么建议吗?

Bad: output : (I want the view to neatly bound the triangle)

坏:输出:(我希望视图将三角形巧妙地绑定在一起)

如何根据场景max和最小坐标设置glm::ortho的边界?

Edit:

编辑:

Based on a comment ( I have tried to update the bounds with the view matrix (model is identity, so ignoring that for now) still no luck :(

基于一个注释(我尝试用视图矩阵更新边界(模型是恒等,所以暂时忽略它)仍然没有运气:(

glm::vec4 minSS = ((myCamera.getViewMatrix()) * glm::vec4(minWS, 0.0));
glm::vec4 maxSS = ((myCamera.getViewMatrix()) * glm::vec4(maxWS, 0.0));

myCamera.setProjectionMatrix(minSS.x, maxSS.x, maxSS.y, minSS.y, -200.0001f, 14900.0f);

1 个解决方案

#1


1  

You will need to apply all transformations that come before the perspective transformation to your input points when you calculate the bounding box.

当您计算边界框时,您需要将透视图转换之前的所有转换应用到您的输入点。

In your code fragments, it looks like you're applying a viewing transform with an arbitrary viewpoint (50, 200, 200) as part of your rendering. You need to apply this same transformation to your input points before you feed them into your findBoundingBox() function.

在您的代码片段中,看起来您正在应用一个带有任意视角的查看转换(50,200,200)作为呈现的一部分。在将它们输入到findBoundingBox()函数之前,您需要对输入点应用相同的转换。

In more mathematical terms, you typically have something like this in your vertex shader, with InputPosition being the original vertex coordinates:

用更数学的术语来说,你的顶点着色器中通常有这样的东西,InputPosition是原始的顶点坐标:

gl_Position = ProjectionMatrix * ViewMatrix * ModelMatrix * InputPosition;

To determine a projection matrix that will map all your points to a given range, you need to look at all points that the projection matrix is applied to. With the notation above, those points are ViewMatrix * ModelMatrix * InputPosition. So when you calculate the bounding box, the model and view matrices (or the modelview matrix if you combine them) needs to be applied to the input points.

要确定将所有点映射到给定范围的投影矩阵,需要查看投影矩阵应用的所有点。在上面的符号中,这些点是ViewMatrix * ModelMatrix * InputPosition。所以当你计算边界框时,模型和视图矩阵(如果你把它们组合起来的话)需要应用到输入点上。

#1


1  

You will need to apply all transformations that come before the perspective transformation to your input points when you calculate the bounding box.

当您计算边界框时,您需要将透视图转换之前的所有转换应用到您的输入点。

In your code fragments, it looks like you're applying a viewing transform with an arbitrary viewpoint (50, 200, 200) as part of your rendering. You need to apply this same transformation to your input points before you feed them into your findBoundingBox() function.

在您的代码片段中,看起来您正在应用一个带有任意视角的查看转换(50,200,200)作为呈现的一部分。在将它们输入到findBoundingBox()函数之前,您需要对输入点应用相同的转换。

In more mathematical terms, you typically have something like this in your vertex shader, with InputPosition being the original vertex coordinates:

用更数学的术语来说,你的顶点着色器中通常有这样的东西,InputPosition是原始的顶点坐标:

gl_Position = ProjectionMatrix * ViewMatrix * ModelMatrix * InputPosition;

To determine a projection matrix that will map all your points to a given range, you need to look at all points that the projection matrix is applied to. With the notation above, those points are ViewMatrix * ModelMatrix * InputPosition. So when you calculate the bounding box, the model and view matrices (or the modelview matrix if you combine them) needs to be applied to the input points.

要确定将所有点映射到给定范围的投影矩阵,需要查看投影矩阵应用的所有点。在上面的符号中,这些点是ViewMatrix * ModelMatrix * InputPosition。所以当你计算边界框时,模型和视图矩阵(如果你把它们组合起来的话)需要应用到输入点上。