Channels ▼
RSS

Tools

OpenGL and Mobile Devices: Round 2

Source Code Accompanies This Article. Download It Now.


Files and Resources

The last loose end is file loading. At the very least, you will want to load textures. To demonstrate the opposite of what we have just done, the GLTextureHelper class has a C++ style header that can be included in any C++ code. The implementation of the class is, however, in an Objective C .mm file. While the class itself is a C++ class, it makes use of Cocoa to load any Quartz-supported image file format and load it as an OpenGL texture. This class is implemented in the source code files GLTextureHelper.h and GLTextureHelper.mm. Using the class is simple. First declare an instance of the class:


GLTextureHelper helper(1024, 1024);
	

The only parameters to the constructor are the maximum expected size in width and height of any textures you plan to load. Here, I use the maximum size supported by the iPhone. Rather than allocate memory repeatedly for each texture loaded, this class reuses a single block of memory to avoid repeated malloc/free cycles.

The LoadTexture method then loads the specified texture file and calls glTexImage2D with the appropriate parameters. For example, to initialize a texture object and create mipmaps for the body.png texture:

	
// Load the body texture
glBindTexture(GL_TEXTURE_2D, textureObjects[BODY_TEXTURE]);
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
helper.LoadTexture("body.png");    
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
    GL_LINEAR_MIPMAP_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

Textures are placed in the \Resources folder. Right-click and select "Add Existing Files" and you can bring in your own textures.

Conclusion

It's exciting that independent developers can now get their hands on OpenGL ES devices and immediately start writing code. iPhones represent only one exciting opportunity to take advantage of the growing prevalence of mobile 3D devices.

The example code accompanying this article is a great place to start with iPhone (and iPod Touch) development. While not a complete development framework, it brings within reach GLUT-style technology demos and proof-of-concept prototypes.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 

Video