C++、OpenGL 裁剪

发布于 2024-10-08 03:15:35 字数 960 浏览 0 评论 0原文

我正在制作一个基于立方体的游戏(一切都是立方体),目前正在尝试通过不在视野之外绘制东西来优化它。

以下内容仅适用于 x 和 y 平面,稍后我会担心 z ......所以现在仅进行侧面裁剪。

我知道我自己在世界中的位置和旋转以及每个立方体的位置,因此想法是比较玩家和立方体相对于玩家的 z 和 x 旋转角度,并仅显示其中的立方体定义的范围。

代码时间:

// this is how I turn
float zrotrad = (float)zrot*DEG2RAD;
float view_limit = .4;

// distance between the cube and me
float dist_x = box_x-xpos;
float dist_y = box_y-ypos;
float dist_z = box_z-zpos;

// total distance (I'll use fast sqrt later)
float dist_tot = sqrt(dist_x*dist_x+dist_y*dist_y);

float angle = acos(dist_y/dist_tot);
// need to add 2 pies because acos returns a value [0,2PI]
float zcuberot = dist_x<0?2*PI-angle:angle;

if(zcuberot > zrotrad-view_limit && zcuberot < zrotrad+view_limit)
{
    drawcube(box_x, box_y, box_z);
}

正如您可能已经理解的那样,0 度附近存在问题,因为我的左视野限制变为负数,并且 zcuberot 添加了 2 个饼,范围变得混乱。 359 度的情况也是如此 - 实际上由于 view_limit = .4 而较少。

我已经为此搞砸了两天了,感觉自己问这个问题真是个傻瓜。

I'm making a cube based game (everything is a cube) and currently trying to optimize it by not drawing the stuff outside the field of view.

The following applies only to the x and y planes, I'll worry about the z later... so only side clipping for now.

I know my own position and rotation in the world and the position of every single cube there is, so the idea is to compare the z and x rotation angles of the player and the cube relative to the player and only display the cubes within with a defined range.

Code time:

// this is how I turn
float zrotrad = (float)zrot*DEG2RAD;
float view_limit = .4;

// distance between the cube and me
float dist_x = box_x-xpos;
float dist_y = box_y-ypos;
float dist_z = box_z-zpos;

// total distance (I'll use fast sqrt later)
float dist_tot = sqrt(dist_x*dist_x+dist_y*dist_y);

float angle = acos(dist_y/dist_tot);
// need to add 2 pies because acos returns a value [0,2PI]
float zcuberot = dist_x<0?2*PI-angle:angle;

if(zcuberot > zrotrad-view_limit && zcuberot < zrotrad+view_limit)
{
    drawcube(box_x, box_y, box_z);
}

As you've probably understood, there's a problem around 0 degrees, since my left field view limit becomes negative and the zcuberot gets 2 pies added to it the range gets messed up. Same story for the 359 degrees - actually less because of the view_limit = .4.

I've been messing around for 2 days with this and feel like such a dumbass for asking this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

向日葵 2024-10-15 03:15:35

实现八叉树会快得多(并且不易出错)。然后,您将能够通过丢弃八叉树中不与您的视锥体相交的部分来剔除所有不可见的立方体。您不必对每个立方体执行此测试,并且您将能够减少测试,将单个较大的立方体与 6 个平面进行比较。

与现有的相比,八叉树的速度会快几个数量级

您可以在 WikipediaFlipcode 等。

It would be much, much faster (and less error-prone) to implement an octree. You will then be able to cull all non-visible cubes by discarding the parts of the octree that do not intersect with your viewing frustum. You won't have to perform this test for every single cube, and you'll be able to reduce the test to comparing a single larger cube against 6 planes.

Compared to what you have, an octree will be a few orders of magnitude faster.

You can find a good deal of information about octrees on Wikipedia and Flipcode, among other places.

油饼 2024-10-15 03:15:35

你应该做的是夹住截锥体平面。
在这里,您将找到有关截锥体以及如何从视图矩阵中提取平面所需了解的几乎所有内容,甚至还有一些用于测试立方体是否位于视图体积内的代码:http://www.crownandcutlass.com/features/technicaldetails/frustum.html

What you should do is clip against the frustum planes.
Here you'll find pretty much everything you need to know about the frustum and how to extract the planes from the view matrix, and even some code to test if a cube is within the view volume: http://www.crownandcutlass.com/features/technicaldetails/frustum.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文