iPhone 3D Programming : Textures and Image Capture - Creating Textures with the Camera
Published 8 min readBy Programming4us Editorial Team
Let’s create an app called CameraTexture that allows
the user to snap a photo and wrap it around an ellipsoid (a squashed
sphere). The embarrassingly simple user interface consists of a single
button for taking a new photo, as shown in Figure 1. We’ll also add some animation by
periodically spinning the ellipsoid along the x-axis.
Figure 1. CameraTexture sample
Unlike much of the sample code in this book,
the interesting parts here will actually be in Objective-C rather than
C++. The application logic is simple enough that we can dispense with the
IApplicationEngine interface.
Using ModelViewer as the baseline, start by
removing all the ApplicationEngine-related code as
follows:
Remove
IApplicationEngine and
CreateApplicationEngine from
Interfaces.hpp.
Remove the
ApplicationEngine.ParametricViewer.cpp file
from the Xcode project, and send it to the trash.
Remove the
m_applicationEngine field from
GLView.h.
Remove the call to
CreateApplicationEngine from
GLView.mm.
Replace the call to
m_applicationEngine->Initialize with
m_renderingEngine->Initialize().
Remove touchesBegan,
touchesEnded, and touchesMoved
from GLView.mm.
The code won’t build until we fill it out a bit more.
Replace the IRenderingEngine interface in
Interfaces.hpp with Example 1, and move the
TextureFormat and TextureDescription type definitions to the
top of the file.
Example 1. CameraTexture’s IRenderingEngine interface
We’ll go over the implementation of these
methods later. Let’s jump back to the Objective-C since that’s where the
interesting stuff is. For starters, we need to modify the
GLView class declaration by adopting a couple new
protocols and adding a few data fields; see Example 2. New code is shown in bold.
Example 2. CameraTexture’s GLView.h
Next, open GLView.mm, and
rewrite the drawView method as in Example 3. The code that computes the time step
is the same as previous examples; perhaps more interesting are the
mathematical shenanigans used to oscillate between two types of useless
and silly animation: “spinning” and “pulsing.”
Example 3. CameraTexture’s drawView method
While we’re still in
GLView.mm, let’s go ahead and write the touch
handler. Because of the embarrassingly simple UI, we need to handle only a
single touch event: touchesEnded,
as shown in Example 4. Note that the
first thing it does is check whether the touch location lies within the
bounds of the button’s rectangle; if not, it returns early.
Example 4. CameraTexture’s touchesEnded method
Warning:
When developing with UIKit, the usual
convention is that the view controller owns the view, but in this case,
the view owns the view controller. This is acceptable in our situation,
since our application is mostly rendered with OpenGL, and we want to
achieve the desired functionality in the simplest possible way. I’m
hoping that Apple will release a lower-level camera API in future
versions of the SDK, so that we don’t need to bother with view
controllers.
Perhaps the most interesting piece in Example 4 is the code that checks whether the
camera is supported; if so, it sets the camera as the picker’s source
type:
UIImagePickerControllerSourceType source = UIImagePickerControllerSourceTypeCamera; if ([UIImagePickerController isSourceTypeAvailable:source]) imagePicker.sourceType = source;
I recommend following this pattern even if you
know a priori that your application will run only on devices with cameras.
The fallback path provides a convenient testing platform on the iPhone
Simulator; by default, the image picker simply opens a file picker with
image thumbnails.
Next we’ll add a couple new methods to
GLView.mm for implementing the
UIImagePickerControllerDelegate protocol, as shown in
Example 5. Depending on the megapixel
resolution of your camera, the captured image can be quite large, much
larger than what we need for an OpenGL texture. So, the first thing we do
is scale the image down to 256×256. Since this destroys the aspect ratio,
we’ll store the original image’s dimensions in the
TextureDescription structure just in case. A more
detailed explanation of the code follows the listing.
Example 5. imagePickerControllerDidCancel and
didFinishPickingMediaWithInfo
1. CameraTexture: Rendering Engine Implementation
Crack your OpenGL ES knuckles; it’s time to
implement the rendering engine using ES 1.1. Go ahead and remove the
contents of RenderingEngine.ES1.cpp, and add the new
class declaration and Initialize method, shown in
Example 6.
Example 6. RenderingEngine class declaration and initialization
There are no new concepts in Example 5-31; at a high level, the
Initialize method performs the following
tasks:
Creates two vertex buffers using the
parametric surface helper: a quad for the button and a sphere for
the ellipsoid.
Creates three textures: the initial
ellipsoid texture, the “Please Wait” text, and the “Take Picture”
button text.
Performs some standard initialization
work, such as creating the FBO and setting up the transformation
matrices.
Next, let’s implement the two public methods,
Render and LoadCameraTexture, as
shown in Example 7.
Example 7. Render and LoadCameraTexture
That was simple! Next we’ll implement the
four private methods (Example 8).
Example 8. CreateTexture, CreateDrawable, RenderDrawable,
UploadImage
Much of Example 5-33 is fairly
straightforward. The UploadImage method is used both
for camera data (where the raw data is passed in) and for image files
(where the raw data is obtained from the resource manager).
We won’t bother with an ES 2.0 backend in
this case, so you’ll want to turn on the ForceES1
flag in GLView.mm, comment out the call to
ES2::CreateRenderingEngine, and remove
RenderingEngine.ES2.cpp from the project.
This completes the CameraTexture sample,
another fun but useless iPhone program!