【发布时间】:2012-01-07 00:29:08
【问题描述】:
我目前有一个 UIView,它使用 OpenGL 在 MKMapView 上绘制雷达数据。由于雷达图像的细节层次,需要 OpenGL(CoreGraphics 不够快)。
我正在绘制的所有图像都保存在MKMapPoints 中。我选择它们而不是标准的CLLocationCoordinate2D,因为它们的长度不取决于纬度。绘制的基本方法是这样的:
- 将
GLView添加为MKMapView的子视图并设置GLView.frame = MKMapView.frame。 -
使用
GLOrthof,将GLView的投影设置为等于地图当前可见的MKMapRect。这是执行此操作的代码。CLLocationCoordinate2D coordinateTopLeft = [mapView convertPoint:CGPointMake(0, 0) toCoordinateFromView:mapView]; MKMapPoint pointTopLeft = MKMapPointForCoordinate(coordinateTopLeft); CLLocationCoordinate2D coordinateBottomRight = [mapView convertPoint:CGPointMake(mapView.frame.size.width, mapView.frame.size.height) toCoordinateFromView:mapView]; MKMapPoint pointBottomRight = MKMapPointForCoordinate(coordinateBottomRight); glLoadIdentity(); glOrthof(pointTopLeft.x, pointBottomRight.x, pointBottomRight.y, pointTopLeft.y, -1, 1); 使用
glViewport(0, 0, backingWidth, backingHeight)将视口设置为正确的大小,其中backingWidth和backingHeight是mapView的大小(以磅为单位)。- 使用
glDrawArrays绘制。不确定这是否重要,但GL_VERTEX_ARRAY和GL_TEXTURE_COORD_ARRAY在抽奖期间都启用了。
使用此方法,一切正常。绘图按预期进行。唯一的问题是因为它是mapView 的子视图(而不是覆盖),雷达图像被绘制在任何其他MKAnnotations 和MKOverlays 之上。我需要将此图层绘制在其他注释和叠加层下。
我试图做的是让glView 成为自定义MKOverlayView 的子视图,而不是mapView。我所做的是给MKOverlay 一个boundingMapRect 的MKMapRectWorld 并设置glView 的框架与我设置投影的方式相同(因为MKOverlayView 的框架由MKMapPoints 确定,而不是CGPoints)。同样,这里是代码。
CLLocationCoordinate2D coordinateTopLeft =
[mapView convertPoint:CGPointMake(0, 0)
toCoordinateFromView:mapView];
MKMapPoint pointTopLeft = MKMapPointForCoordinate(coordinateTopLeft);
CLLocationCoordinate2D coordinateBottomRight =
[mapView convertPoint:CGPointMake(mapView.frame.size.width,
mapView.frame.size.height)
toCoordinateFromView:mapView];
MKMapPoint pointBottomRight = MKMapPointForCoordinate(coordinateBottomRight);
glRadarView.frame = CGRectMake(pointTopLeft.x, pointTopLeft.y,
pointBottomRight.x - pointTopLeft.x,
pointBottomRight.y - pointTopLeft.y);
当我这样做时,glView 在屏幕上的位置正确(与它是mapView 的子视图时相同的位置),但绘图不再正常工作。当图像确实出现时,它的大小不正确,并且位置也不正确。我做了检查,backingWidth 和 backingHeight 仍然是视图的大小(应该是)。
知道为什么这不起作用吗?
【问题讨论】:
-
您确定需要实时渲染雷达吗?预渲染(例如,每一帧都包含一个 PNG)不是一种选择吗?
-
简答,不。我正在对数据做一些事情(我计划在更新中做更多事情),而预渲染的图像将无法正常工作。
标签: iphone opengl-es mkmapview