Skybox OpenGL ES iPhone 和 iPad [英] Skybox OpenGL ES iPhone and iPad

查看:24
本文介绍了Skybox OpenGL ES iPhone 和 iPad的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为 iOS 创建一个虚拟游览工具.这是一个考古应用程序:用户可以在历史建筑内或参观考古发掘时打开它.不需要末日般的主观观点:只是一个天空盒.该应用程序将包含一个兴趣点 (POI) 列表.每个 POI 都会有自己的天空盒.

I need to create a virtual tour tool for iOS. It's an archaeological application: the user could open it when he's inside an historic building or when he's visiting an archaeological dig. No need of doom-like subjective point of view: just a skybox. The application will have a list of points of interest (POIs). Every POI will have its own skybox.

我认为我可以使用 OpenGL-ES 创建一种可以通过触摸驱动/旋转的带纹理的天空盒.纹理是高分辨率 PNG 照片.

I thought that I could use using OpenGL-ES to create a sort of textured skyboxes that could be driven/rotated by touches. Textures are hi-resolution PNG photos.

这是一个受资助的项目,我有 4 个月的时间.

It's a funded project and I have 4 months.

我必须去哪里学习如何开发它?我必须买书吗?哪一个?

Where do I have to go to learn how to develop it? Do I have to purchase a book? Which one?

我的 Objectve-C 和 Cocoa-touch 技能适中,因为我只为 iPad 构建了一个应用程序.我对 OpenGL-ES 的了解为零.

I have just moderate Objectve-C and Cocoa-touch skills, since I've built just one application for the iPad. I have zero knowledge of OpenGL-ES.

推荐答案

因为我非常了解 OpenGL ES,所以我尝试了一个演示项目,按照您的描述做了很多事情.其具体目的是在 OpenGL ES 下以最简单的方式完成所有工作,只要性能足够好.

Since I know OpenGL ES quite well, I had a go at a demo project, doing much of what you describe. The specific intention was to do everything in the simplest way available under OpenGL ES as long as the performance was good enough.

从 Apple 提供的 OpenGL 模板开始,我编写了一个新的类,其中包含一个 122 行长的大量注释实现文件,它将 PNG 图像加载为纹理.我修改了示例视图控制器以根据需要绘制天空盒,并使用普通 iPhone 惯性滚动的版本来响应触摸,这意味着编写不到 200 行(也有注释)的代码.

Starting from the OpenGL template that Apple supply, I have written one new class with a heavily commented implementation file 122 lines long that loads PNG images as textures. I've modified the sample view controller to draw a skybox as required and to respond to touches with a version of the normal iPhone inertial scrolling, which has meant writing less than 200 lines of (also commented) code.

要实现这一点,我需要知道:

To achieve this I needed to know:

  • CoreGraphics 用于从 PNG 中获取像素数据
  • 如何设置 PROJECTION 堆栈以获得具有正确纵横比的透视投影
  • 如何操作 MODELVIEW 堆栈以确保根据成员变量进行场景的双轴旋转(第一人称射击游戏或 Google StreetView 风格),并确保我定义的立方体几何图形不会明显与近裁剪平面相交
  • 如何为 OpenGL 指定顶点位置和纹理坐标
  • 如何指定 OpenGL 应在顶点之间构造的三角形
  • 如何相应地设置 OpenGL 纹理参数以仅提供纹理的一级细节
  • 如何跟踪触摸以操纵指示旋转的成员变量,包括提供惯性旋转的一点点机制

当然,遵守正常的视图控制器生命周期指令.纹理在 viewDidLoad 上加载并在 viewDidUnload 上释放,例如,以确保此视图控制器能够很好地处理潜在的内存警告.

Of course, the normal view controller lifecycle instructions are obeyed. Textures are loaded on viewDidLoad and released on viewDidUnload, for example, to ensure that this view controller plays nicely with potential memory warnings.

主要观察结果是,除了了解 Objective-C 的信号机制之外,大部分都是 C 的东西.您主要使用 C 数组和引用来进行 C 函数调用,包括 OpenGL 和 CoreGraphics.因此,自己编写代码的先决条件是对 C 感到满意,而不仅仅是 Objective-C.

The main observations are that, beyond knowing the Objective-C signalling mechanisms, most of this is C stuff. You're primarily using C arrays and references to make C function calls, both for OpenGL and CoreGraphics. So a prerequisite for coding this yourself is being happy in C, not just Objective-C.

CoreGraphics 的东西有点乏味,但它只是阅读文档以了解每种类型的事物与下一种事物的关系——这些都不是真正令人困惑的.只要进入你的脑海,你需要一个 PNG 数据的数据提供者,你可以从该数据提供者创建一个图像,然后使用你自己分配的内存创建一个位图上下文,将图像绘制到上下文中,然后释放所有内容除了您分配给自​​己的内存以保留结果.该结果可以直接上传到 OpenGL.它是相对较短的样板文件,但 OpenGL 没有 PNG 的概念,CoreGraphics 也没有将东西推入 OpenGL 的便捷方法.

The CoreGraphics stuff is a bit tedious but it's all just reading the docs to figure out how each type of thing relates to the next — none of it is really confusing. Just get into your head that you need a data provider for the PNG data, you can create an image from that data provider and then create a bitmap context with memory that you've allocated yourself, draw the image into the context and then release everything except the memory you allocated yourself to be left with the result. That result can be directly uploaded to OpenGL. It's relatively short boilerplate stuff, but OpenGL has no concept of PNGs and CoreGraphics has no convenient methods of pushing things into OpenGL.

我假设纹理在磁盘上的大小合适.出于实际目的,这意味着假设它们沿每个边缘的大小是 2 的幂.我的是 512x512.

I've assumed that textures are a suitable size on disk. For practical purposes, that means assuming they're a power-of-two in size along each edge. Mine are 512x512.

OpenGL 纹理管理的东西很简单;它只是阅读手册来了解纹理名称、名称分配、纹理参数和上传图像数据.更多的是关于了解正确功能而不是管理直观飞跃的常规内容.

The OpenGL texture management stuff is easy enough; it's just reading the manual to learn about texture names, name allocation, texture parameters and uploading image data. More routine stuff that is more about knowing the right functions than managing an intuitive leap.

为了向 OpenGL 提供几何图形,我刚刚完整地写出了数组.我想你需要一点空间思维才能做到这一点,但是在纸上画出一个 3d 立方体并对角进行编号会很有帮助.有三个相关的数组:

For supplying the geometry to OpenGL I've just written out the arrays in full. I guess you need a bit of a spatial mind to do it, but sketching out a 3d cube on paper and numbering the corners would be a big help. There are three relevant arrays:

  • 顶点位置
  • 每个顶点位置的纹理坐标
  • 引用定义几何的顶点位置的索引列表

在我的代码中,我使用了 24 个顶点,将立方体的每个面视为逻辑上离散的事物(因此,六个面,每个面有四个顶点).为简单起见,我仅使用三角形定义了几何图形.开始时向 OpenGL 提供这些东西实际上很烦人.出错通常意味着您的程序在 OpenGL 驱动程序的深处崩溃,而没有提示您做错了什么.最好一次建立一点.

In my code I've used 24 vertices, treating each face of the cube as a logically discrete thing (so, six faces, each with four vertices). I've defined the geometry using triangles only, for simplicity. Supplying this stuff to OpenGL is actually quite annoying when you're starting; making an error generally means your program crashes deep inside the OpenGL driver without giving you a hint as to what you did wrong. It's probably best to build up a bit at a time.

就能够承载 OpenGL 内容的 UIView 而言,我或多或少地使用了 Apple 在 OpenGL 模板中直接提供的普通内容.我所做的一项更改是明确禁止任何尝试使用 OpenGL ES 2.x.1.x 对这项任务来说绰绰有余,因此我们首先通过不提供两个替代渲染路径来获得简单性,其次因为 ES 2.x 路径会复杂得多.ES 2.x 是具有像素和顶点着色器的完全可编程管线,但在 ES 领域中,固定管线被完全移除.因此,如果您想要一个,那么您必须为普通矩阵堆栈提供自己的替代品,您必须编写顶点和片段着色器来执行带有纹理的三角形"等.

In terms of a UIView capable of hosting OpenGL content, I've more or less used the vanilla stuff Apple directly supply in the OpenGL template. The one change I made was explicitly to disable any attempted use of OpenGL ES 2.x. 1.x is more than sufficient for this task, so we gain simplicity firstly by not providing two alternative rendering paths and secondly because the ES 2.x path would be a lot more complicated. ES 2.x is the fully programmable pipeline with pixel and vertex shaders, but in ES land the fixed pipeline is completely removed. So if you want one then you have to supply your own substitutes for the normal matrix stacks, you have to write vertex and fragment shaders to do 'a triangle with a texture', etc.

触摸跟踪并不是特别复杂,或多或少只需要我了解视锥体是如何工作的以及如何在 Cocoa Touch 中传递触摸.完成其他所有操作后,这一点应该很容易.

The touch tracking isn't particularly complicated, more or less just requiring me to understand how the view frustum works and how touches are delivered in Cocoa Touch. Once you've done everything else, this bit should be quite easy.

值得注意的是,我必须实现的数学运算非常简单.真的只是触摸跟踪.例如,假设您想要一个 Google Maps 类型的视图意味着我可以完全依赖 OpenGL 的内置旋转功能.我从来没有明确地处理矩阵.

Notably, the maths I had to implement was extremely simple. Just the touch tracking, really. Assuming you wanted a Google Maps-type view meant that I could rely entirely on OpenGL's built-in ability to rotate things, for example. At no point do I explicitly handle a matrix.

所以,你需要多长时间来编写代码取决于你自己对 C 和 CoreGraphics 的信心,以及你有时在黑暗中编码的快乐程度.因为我知道自己在做什么,所以整个过程花了两三个小时.

So, how long it would take you to write depends on your own confidence with C and with CoreGraphics, and how happy you are sometimes coding in the dark. Because I know what I'm doing, the whole thing took two or three hours.

我会设法找到上传项目的地方,以便您查看.我认为翻阅它并看看它看起来多么陌生会很有帮助.这可能会让您很好地了解您是否可以在项目的时间范围内实施满足您所有需求的东西.

I'll try to find somewhere to upload the project so that you can have a look at it. I think it'd be helpful to leaf through it and see how alien it looks. That'll probably give you a good idea about whether you could implement something that meets all of your needs within the time frame of your project.

我将视图控制器保留为只有一个视图,即 OpenGL 视图.然而,普通的 iPhone 合成规则适用,在您的项目中,您可以轻松地将普通控件放在上面.您可以在 mediafire 获取我的小实现.StackOverflow 帖子长度限制使我无法在此处放置大段代码,但如果您有任何具体问题,请随时询问.

I've left the view controller as having exactly one view, which is the OpenGL view. However, the normal iPhone compositing rules apply and in your project you can easily put normal controls on top. You can grab my little implementation at mediafire. StackOverflow post length limits prevent me from putting big snippets of code here, but please feel free to ask if you have any specific questions.

这篇关于Skybox OpenGL ES iPhone 和 iPad的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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