在Apple pARk示例代码中使用数学 [英] Use of maths in the Apple pARk sample code

查看:78
本文介绍了在Apple pARk示例代码中使用数学的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我研究了pARK示例项目(http://developer.apple.com/library/IOS/#samplecode/pARk/Introduction/Intro.html#//apple_ref/doc/uid/DTS40011083)所以我可以在我正在开发的应用程序中应用它的一些基础知识。我理解几乎所有的东西,除了:

I'm studied the pARK example project (http://developer.apple.com/library/IOS/#samplecode/pARk/Introduction/Intro.html#//apple_ref/doc/uid/DTS40011083) so I can apply some of its fundamentals in an app i'm working on. I understand nearly everything, except:


  • 如果必须出现兴趣点,它必须计算的方式。它获得态度,将其与投影矩阵相乘(以获得GL坐标中的旋转?),然后将该矩阵与感兴趣点的坐标相乘,最后,查看该矢量的最后一个坐标以找出如果必须显示兴趣点。这是什么数学基础?

非常感谢!!

推荐答案

我假设你指的是以下方法:

I assume you are referring to the following method:

- (void)drawRect:(CGRect)rect
{
     if (placesOfInterestCoordinates == nil) {
         return;
     }

    mat4f_t projectionCameraTransform;
    multiplyMatrixAndMatrix(projectionCameraTransform, projectionTransform, cameraTransform);

    int i = 0;
    for (PlaceOfInterest *poi in [placesOfInterest objectEnumerator]) {
        vec4f_t v;
        multiplyMatrixAndVector(v, projectionCameraTransform, placesOfInterestCoordinates[i]);

        float x = (v[0] / v[3] + 1.0f) * 0.5f;
        float y = (v[1] / v[3] + 1.0f) * 0.5f;
        if (v[2] < 0.0f) {
            poi.view.center = CGPointMake(x*self.bounds.size.width, self.bounds.size.height-y*self.bounds.size.height);
            poi.view.hidden = NO;
        } else {
            poi.view.hidden = YES;
        }
        i++;
    }
}

这是在地方执行类似顶点转换的OpenGL感兴趣的是检查它们是否在可见的视锥体中。截锥体在以下行中创建:

This is performing an OpenGL like vertex transformation on the places of interest to check if they are in a viewable frustum. The frustum is created in the following line:

createProjectionMatrix(projectionTransform, 60.0f*DEGREES_TO_RADIANS, self.bounds.size.width*1.0f / self.bounds.size.height, 0.25f, 1000.0f);

这设置了一个60度视野,近似剪裁平面0.25和a的平截头体远剪裁平面1000.任何远远超过1000个单位的兴趣点都将不可见。

This sets up a frustum with a 60 degree field of view, a near clipping plane of 0.25 and a far clipping plane of 1000. Any point of interest that is further away than 1000 units will then not be visible.

因此,要逐步执行代码,首先是设置平截头体的投影矩阵,以及相机视图矩阵,它只是旋转对象,因此它是正确的方式相对于相机,相乘,相乘。然后,对于每个感兴趣的地方,其位置乘以viewProjection矩阵。这会将感兴趣的地方的位置投影到视锥体,应用旋转和透视。

So, to step through the code, first the projection matrix that sets up the frustum, and the camera view matrix, which simply rotates the object so it is the right way up relative to the camera, are multiplied together. Then, for each place of interest, its location is multiplied by the viewProjection matrix. This will project the location of the place of interest into the view frustum, applying rotation and perspective.

接下来的两行然后将地点的变换位置转换为已知的数字作为标准化设备坐标。需要将4分量矢量折叠到3维空间,这是通过将矢量除以其w分量v [3]将其投影到平面w == 1上来实现的。然后可以通过检查其坐标是否位于具有原点[0,0,0]的边长2的立方体中来确定该点是否位于投影平截头体内。在这种情况下,x和y坐标偏离范围[-1 1]到[0 1]以匹配 UIKit 坐标系,加1除以2.

The next two lines then convert the transformed location of the place into whats known as normalized device coordinates. The 4 component vector needs to be collapsed to 3 dimensional space, this is achieved by projecting it onto the plane w == 1, by dividing the vector by its w component, v[3]. It is then possible to determine if the point lies within the projection frustum by checking if its coordinates lie in the cube with side length 2 with origin [0, 0, 0]. In this case, the x and y coordinates are being biased from the range [-1 1] to [0 1] to match up with the UIKit coordinate system, by adding 1 and dividing by 2.

接下来,检查v [2]组件z是否大于0.这实际上是不正确的,因为它没有有偏见,应该检查它是否大于-1。这将检测感兴趣的地方是否位于投影平截头体的前半部分,如果是,那么该物体被视为可见并显示。

Next, the v[2] component, z, is checked to see if it is greater than 0. This is actually incorrect as it has not been biased, it should be checked to see if it is greater than -1. This will detect if the place of interest is in the first half of the projection frustum, if it is then the object is deemed visible and displayed.

如果您不熟悉顶点投影和坐标系统,这是一个相当陡峭的学习曲线的巨大主题。然而,网上有很多材料可供选择,以下是一些可以帮助您入门的链接:

If you are unfamiliar with vertex projection and coordinate systems, this is a huge topic with a fairly steep learning curve. There is however a lot of material online covering it, here are a couple of links to get you started:

http://www.falloutsoftware.com/tutorials/gl/gl0.htm

http://www.opengl.org/wiki/Vertex_Transformation

好运气//

这篇关于在Apple pARk示例代码中使用数学的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆