<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>iphone.mitchallen.com &#187; texture</title>
	<atom:link href="http://mitchallen.com/iphone/archives/tag/texture/feed" rel="self" type="application/rss+xml" />
	<link>http://mitchallen.com/iphone</link>
	<description>random notes on iPhone development</description>
	<lastBuildDate>Tue, 10 Aug 2010 20:36:30 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Refactored Texture Class</title>
		<link>http://mitchallen.com/iphone/archives/91</link>
		<comments>http://mitchallen.com/iphone/archives/91#comments</comments>
		<pubDate>Tue, 05 May 2009 12:43:52 +0000</pubDate>
		<dc:creator>Mitch  Allen</dc:creator>
				<category><![CDATA[OpenGL]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[texture]]></category>

		<guid isPermaLink="false">http://mitchallen.com/iphone/?p=91</guid>
		<description><![CDATA[From my previous example, I&#8217;ve refactored the texture class. I&#8217;ve moved some of the common code into a new method. /* * mcaTexture.h * * Created by Mitchell Allen on 4/17/09. * Copyright 2009 __MyCompanyName__. All rights reserved. * */ &#160; #ifndef __MCA_TEXTURE__ #define __MCA_TEXTURE__ &#160; #ifdef TARGET_OS_IPHONE #import &#60;OpenGLES/ES1/gl.h&#62; #import &#60;OpenGLES/ES1/glext.h&#62; #endif &#160; class [...]]]></description>
			<content:encoded><![CDATA[<p>From my <a href="http://mitchallen.com/iphone/archives/75">previous example</a>, I&#8217;ve refactored the texture class.  I&#8217;ve moved some of the common code into a new method.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">/*
 *  mcaTexture.h
 *
 *  Created by Mitchell Allen on 4/17/09.
 *  Copyright 2009 __MyCompanyName__. All rights reserved.
 *
 */</span>
&nbsp;
<span style="color: #6e371a;">#ifndef  __MCA_TEXTURE__</span>
<span style="color: #6e371a;">#define  __MCA_TEXTURE__</span>
&nbsp;
<span style="color: #6e371a;">#ifdef TARGET_OS_IPHONE</span>
<span style="color: #6e371a;">#import &lt;OpenGLES/ES1/gl.h&gt;</span>
<span style="color: #6e371a;">#import &lt;OpenGLES/ES1/glext.h&gt;</span>
<span style="color: #6e371a;">#endif</span>
&nbsp;
class mcaTexture <span style="color: #002200;">&#123;</span>
&nbsp;
protected<span style="color: #002200;">:</span>
&nbsp;
	GLuint m_texture;	<span style="color: #11740a; font-style: italic;">// OpenGL name for the  texture</span>
&nbsp;
	<span style="color: #a61390;">void</span> initTexture<span style="color: #002200;">&#40;</span> <span style="color: #a61390;">size_t</span> w, <span style="color: #a61390;">size_t</span> h, <span style="color: #a61390;">int</span> iType, GLvoid <span style="color: #002200;">*</span>pImage <span style="color: #002200;">&#41;</span>;
&nbsp;
public<span style="color: #002200;">:</span>
&nbsp;
	GLuint getTexture<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #a61390;">void</span> initWithChecks<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #a61390;">void</span> initFromImage<span style="color: #002200;">&#40;</span> <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>location<span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #002200;">&#125;</span>;
&nbsp;
<span style="color: #6e371a;">#endif</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">/*
 *  mcaTexture.mm
 *  mcaGL1
 *
 *  Created by Mitchell Allen on 4/24/09.
 *  Copyright 2009 __MyCompanyName__. All rights reserved.
 *
 */</span>
&nbsp;
<span style="color: #6e371a;">#include &quot;mcaTexture.h&quot;</span>
&nbsp;
<span style="color: #6e371a;">#define checkImageWidth	64</span>
<span style="color: #6e371a;">#define checkImageHeight 64</span>
<span style="color: #a61390;">static</span> GLubyte checkImage<span style="color: #002200;">&#91;</span> checkImageHeight<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#91;</span>checkImageWidth <span style="color: #002200;">&#93;</span><span style="color: #002200;">&#91;</span> <span style="color: #2400d9;">4</span> <span style="color: #002200;">&#93;</span>;
&nbsp;
GLuint mcaTexture<span style="color: #002200;">::</span>getTexture<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> m_texture;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">void</span> mcaTexture<span style="color: #002200;">::</span>initTexture<span style="color: #002200;">&#40;</span> <span style="color: #a61390;">size_t</span> w, <span style="color: #a61390;">size_t</span> h, <span style="color: #a61390;">int</span> iType, GLvoid <span style="color: #002200;">*</span>pImage <span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
	NSLog<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;START: mcaTexture::initTexture( '...' )&quot;</span> <span style="color: #002200;">&#41;</span>;
&nbsp;
	GLint					saveName;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Now use OpenGL ES to generate a name for the texture.</span>
	<span style="color: #11740a; font-style: italic;">// Pass by reference so that our texture variable gets set.</span>
&nbsp;
	glGenTextures<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">1</span>, <span style="color: #002200;">&amp;</span>m_texture<span style="color: #002200;">&#41;</span>;
&nbsp;
	glGetIntegerv<span style="color: #002200;">&#40;</span>GL_TEXTURE_BINDING_2D, <span style="color: #002200;">&amp;</span>saveName<span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Bind the texture name. </span>
	glBindTexture<span style="color: #002200;">&#40;</span>GL_TEXTURE_2D, m_texture<span style="color: #002200;">&#41;</span>;
&nbsp;
	glTexParameteri<span style="color: #002200;">&#40;</span>GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR<span style="color: #002200;">&#41;</span>;
	glTexParameteri<span style="color: #002200;">&#40;</span>GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR<span style="color: #002200;">&#41;</span>;
	glTexParameteri<span style="color: #002200;">&#40;</span>GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE<span style="color: #002200;">&#41;</span>;
	glTexParameteri<span style="color: #002200;">&#40;</span>GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE<span style="color: #002200;">&#41;</span>;
&nbsp;
	glTexEnvi<span style="color: #002200;">&#40;</span> GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE <span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Specify a 2D texture image, providing a pointer to the image data in memory</span>
&nbsp;
	<span style="color: #a61390;">switch</span><span style="color: #002200;">&#40;</span> iType <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
		<span style="color: #a61390;">case</span> <span style="color: #2400d9;">1</span><span style="color: #002200;">:</span>
			glTexImage2D<span style="color: #002200;">&#40;</span>GL_TEXTURE_2D, <span style="color: #2400d9;">0</span>, GL_RGBA, w, h, <span style="color: #2400d9;">0</span>, GL_RGBA, GL_UNSIGNED_BYTE, pImage<span style="color: #002200;">&#41;</span>;
			<span style="color: #a61390;">break</span>;
		<span style="color: #a61390;">case</span> <span style="color: #2400d9;">2</span><span style="color: #002200;">:</span>
			glTexImage2D<span style="color: #002200;">&#40;</span>GL_TEXTURE_2D, <span style="color: #2400d9;">0</span>, GL_RGB, w, h, <span style="color: #2400d9;">0</span>, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, pImage<span style="color: #002200;">&#41;</span>;
			<span style="color: #a61390;">break</span>;
		<span style="color: #a61390;">case</span> <span style="color: #2400d9;">3</span><span style="color: #002200;">:</span>
			glTexImage2D<span style="color: #002200;">&#40;</span>GL_TEXTURE_2D, <span style="color: #2400d9;">0</span>, GL_ALPHA, w, h, <span style="color: #2400d9;">0</span>, GL_ALPHA, GL_UNSIGNED_BYTE, pImage<span style="color: #002200;">&#41;</span>;
			<span style="color: #a61390;">break</span>;
		<span style="color: #a61390;">default</span><span style="color: #002200;">:</span>
			<span style="color: #a61390;">break</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	glBindTexture<span style="color: #002200;">&#40;</span>GL_TEXTURE_2D, saveName<span style="color: #002200;">&#41;</span>;
&nbsp;
	NSLog<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;END: mcaTexture::initTexture()&quot;</span> <span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">void</span> mcaTexture<span style="color: #002200;">::</span>initWithChecks<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span> 
<span style="color: #002200;">&#123;</span>
	NSLog<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;START: mcaTexture::initWithChecks( '...' )&quot;</span> <span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #a61390;">int</span> i, j, c;
&nbsp;
	<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span> i <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; i &lt; checkImageHeight; i<span style="color: #002200;">++</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
		<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span> j <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; j &lt; checkImageWidth; j<span style="color: #002200;">++</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
			c <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>i<span style="color: #002200;">&amp;</span>0x8<span style="color: #002200;">&#41;</span><span style="color: #002200;">==</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">^</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>j<span style="color: #002200;">&amp;</span>0x8<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">==</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">*</span><span style="color: #2400d9;">255</span>;
			checkImage<span style="color: #002200;">&#91;</span>i<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#91;</span>j<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#91;</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>GLubyte<span style="color: #002200;">&#41;</span> c;
			checkImage<span style="color: #002200;">&#91;</span>i<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#91;</span>j<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#91;</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>GLubyte<span style="color: #002200;">&#41;</span> c;
			checkImage<span style="color: #002200;">&#91;</span>i<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#91;</span>j<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#91;</span><span style="color: #2400d9;">2</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>GLubyte<span style="color: #002200;">&#41;</span> c;
			checkImage<span style="color: #002200;">&#91;</span>i<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#91;</span>j<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#91;</span><span style="color: #2400d9;">3</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>GLubyte<span style="color: #002200;">&#41;</span> <span style="color: #2400d9;">255</span>;  <span style="color: #11740a; font-style: italic;">// For ghost effect ( 255 / 2 )</span>
		<span style="color: #002200;">&#125;</span>
	<span style="color: #002200;">&#125;</span>
&nbsp;
	initTexture<span style="color: #002200;">&#40;</span> checkImageWidth, checkImageHeight, <span style="color: #2400d9;">1</span>, checkImage <span style="color: #002200;">&#41;</span>;
&nbsp;
	NSLog<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;END: mcaTexture::initWithChecks( '...' )&quot;</span> <span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #a61390;">void</span> mcaTexture<span style="color: #002200;">::</span>initFromImage<span style="color: #002200;">&#40;</span> <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>location<span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
	NSLog<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;START: mcaTexture::initFromImage( '...' )&quot;</span> <span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Creates a Core Graphics image from an image file using our location.</span>
	CGImageRef spriteImage <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIImage imageNamed<span style="color: #002200;">:</span>location<span style="color: #002200;">&#93;</span>.CGImage;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Get the width and height of the image.</span>
	<span style="color: #a61390;">size_t</span> w <span style="color: #002200;">=</span> CGImageGetWidth<span style="color: #002200;">&#40;</span>spriteImage<span style="color: #002200;">&#41;</span>;
	<span style="color: #a61390;">size_t</span> h <span style="color: #002200;">=</span> CGImageGetHeight<span style="color: #002200;">&#40;</span>spriteImage<span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">//TODO - resize the width and the height to the nearest power of 2.</span>
&nbsp;
	<span style="color: #11740a; font-style: italic;">//Only create a sprite if we were able to properly load the CG image.</span>
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>spriteImage<span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		NSLog<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;... image loaded successfully ...&quot;</span> <span style="color: #002200;">&#41;</span>;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// Allocated memory needed for the bitmap context</span>
		GLubyte <span style="color: #002200;">*</span>spriteData <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>GLubyte <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> <span style="color: #a61390;">malloc</span><span style="color: #002200;">&#40;</span>w <span style="color: #002200;">*</span> h <span style="color: #002200;">*</span> <span style="color: #2400d9;">4</span><span style="color: #002200;">&#41;</span>;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// Use the bitmap creation function provided by the Core Graphics framework. </span>
		CGContextRef spriteContext <span style="color: #002200;">=</span> CGBitmapContextCreate<span style="color: #002200;">&#40;</span>spriteData, w, h, <span style="color: #2400d9;">8</span>, w <span style="color: #002200;">*</span> <span style="color: #2400d9;">4</span>, CGImageGetColorSpace<span style="color: #002200;">&#40;</span>spriteImage<span style="color: #002200;">&#41;</span>, kCGImageAlphaPremultipliedLast<span style="color: #002200;">&#41;</span>;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// After we create the context, we can draw the sprite image to the context.</span>
		CGContextDrawImage<span style="color: #002200;">&#40;</span>spriteContext, CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0.0</span>, <span style="color: #2400d9;">0.0</span>, <span style="color: #002200;">&#40;</span>CGFloat<span style="color: #002200;">&#41;</span>w, <span style="color: #002200;">&#40;</span>CGFloat<span style="color: #002200;">&#41;</span>h<span style="color: #002200;">&#41;</span>, spriteImage<span style="color: #002200;">&#41;</span>;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// We don't need the context at this point, so we need to release it to avoid memory leaks.</span>
		CGContextRelease<span style="color: #002200;">&#40;</span>spriteContext<span style="color: #002200;">&#41;</span>;
&nbsp;
		initTexture<span style="color: #002200;">&#40;</span> w, h, <span style="color: #2400d9;">1</span>, spriteData <span style="color: #002200;">&#41;</span>;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// Release the image data, which is now unused.</span>
		<span style="color: #a61390;">free</span><span style="color: #002200;">&#40;</span>spriteData<span style="color: #002200;">&#41;</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	GLenum	errGL <span style="color: #002200;">=</span> glGetError<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
	NSLog<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;... glGetError = %1 ...&quot;</span>, errGL <span style="color: #002200;">&#41;</span>;
&nbsp;
	NSLog<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;END: mcaTexture::initFromImage( '...' )&quot;</span> <span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://mitchallen.com/iphone/archives/91/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Experimental Texture Class</title>
		<link>http://mitchallen.com/iphone/archives/75</link>
		<comments>http://mitchallen.com/iphone/archives/75#comments</comments>
		<pubDate>Mon, 04 May 2009 12:58:57 +0000</pubDate>
		<dc:creator>Mitch  Allen</dc:creator>
				<category><![CDATA[OpenGL]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[texture]]></category>

		<guid isPermaLink="false">http://mitchallen.com/iphone/?p=75</guid>
		<description><![CDATA[The code below is my experiment with creating a C++ class for generating and/or loading OpenGL textures for the iPhone (it&#8217;s also an experiment with posting mixed C++ and Objective-C code). Not all graphics will load. It will load the ship.png file from the Apple CrashLanding example. No one codes in a vacuum. A lot [...]]]></description>
			<content:encoded><![CDATA[<p>The code below is my experiment with creating a C++ class for generating and/or loading OpenGL textures for the iPhone (it&#8217;s also an experiment with posting mixed C++ and Objective-C code). Not all graphics will load. It will load the ship.png file from the Apple CrashLanding example.</p>
<p>No one codes in a vacuum. A lot of the code here is from bits I pulled together from Apple sample code as well as the following:</p>
<p><a href="http://www.amazon.com/gp/product/0321498828?ie=UTF8&amp;tag=webmaster.mitchallen-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=0321498828">OpenGL(R) SuperBible: Comprehensive Tutorial and Reference (4th Edition)</a><img style="border:none !important; margin:0px !important;" src="http://www.assoc-amazon.com/e/ir?t=webmaster.mitchallen-20&amp;l=as2&amp;o=1&amp;a=0321498828" border="0" alt="" width="1" height="1" /></p>
<p><a href="http://www.amazon.com/gp/product/0321481003?ie=UTF8&amp;tag=webmaster.mitchallen-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=0321481003">OpenGL(R) Programming Guide: The Official Guide to Learning OpenGL(R), Version 2.1 (6th Edition)</a><img style="border:none !important; margin:0px !important;" src="http://www.assoc-amazon.com/e/ir?t=webmaster.mitchallen-20&amp;l=as2&amp;o=1&amp;a=0321481003" border="0" alt="" width="1" height="1" /> &#8211; the source of the checkerboard texture code.</p>
<p><a href="http://discussions.apple.com/thread.jspa?messageID=8858657&amp;tstart=0">http://discussions.apple.com/thread.jspa?messageID=8858657&amp;tstart=0</a></p>
<p><a href="http://www.idevgames.com/forum/showthread.php?t=16578">http://www.idevgames.com/forum/showthread.php?t=16578</a></p>
<p><a href="http://andreicostin.com">http://andreicostin.com</a> &#8211; check out his <a href="http://www.youtube.com/watch?v=3KrkU70J4rE">video demo</a>.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">/*
 *  mcaTexture.h
 *
 *  Created by Mitchell Allen on 4/17/09.
 *  Copyright 2009 __MyCompanyName__. All rights reserved.
 *
 */</span>
&nbsp;
<span style="color: #6e371a;">#ifndef  __MCA_TEXTURE__</span>
<span style="color: #6e371a;">#define  __MCA_TEXTURE__</span>
&nbsp;
<span style="color: #6e371a;">#ifdef TARGET_OS_IPHONE</span>
<span style="color: #6e371a;">#import </span>
<span style="color: #6e371a;">#import </span>
<span style="color: #6e371a;">#endif</span>
&nbsp;
class mcaTexture <span style="color: #002200;">&#123;</span>
&nbsp;
protected<span style="color: #002200;">:</span>
&nbsp;
	GLuint m_texture;	<span style="color: #11740a; font-style: italic;">// OpenGL name for the  texture</span>
&nbsp;
public<span style="color: #002200;">:</span>
&nbsp;
	GLuint getTexture<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #a61390;">void</span> initWithChecks<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #a61390;">void</span> initFromImage<span style="color: #002200;">&#40;</span> <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>location<span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #002200;">&#125;</span>;
&nbsp;
<span style="color: #6e371a;">#endif</span></pre></div></div>

<p>UPDATE: Since the two main methods below have a block of common code &#8211; I refactored it into another method. You can find the updated listing in my next post: <a href="http://mitchallen.com/iphone/archives/91">Refactored Texture Class</a>.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">/*
 *  mcaTexture.mm
 *  mcaGL1
 *
 *  Created by Mitchell Allen on 4/24/09.
 *  Copyright 2009 __MyCompanyName__. All rights reserved.
 *
 */</span>
&nbsp;
<span style="color: #6e371a;">#include &quot;mcaTexture.h&quot;</span>
&nbsp;
<span style="color: #6e371a;">#define checkImageWidth	64</span>
<span style="color: #6e371a;">#define checkImageHeight 64</span>
<span style="color: #a61390;">static</span> GLubyte checkImage<span style="color: #002200;">&#91;</span> checkImageHeight<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#91;</span>checkImageWidth <span style="color: #002200;">&#93;</span><span style="color: #002200;">&#91;</span> <span style="color: #2400d9;">4</span> <span style="color: #002200;">&#93;</span>;
&nbsp;
GLuint mcaTexture<span style="color: #002200;">::</span>getTexture<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> m_texture;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">void</span> mcaTexture<span style="color: #002200;">::</span>initWithChecks<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
	NSLog<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;START: mcaTexture::initWithChecks( '...' )&quot;</span> <span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #a61390;">int</span> i, j, c;
&nbsp;
	<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span> i <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; i <span style="color: #002200;">&amp;</span>lt; checkImageHeight; i<span style="color: #002200;">++</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
		<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span> j <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; j <span style="color: #002200;">&amp;</span>lt; checkImageWidth; j<span style="color: #002200;">++</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
			c <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>i<span style="color: #002200;">&amp;</span>amp;0x8<span style="color: #002200;">&#41;</span><span style="color: #002200;">==</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">^</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>j<span style="color: #002200;">&amp;</span>amp;0x8<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">==</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">*</span><span style="color: #2400d9;">255</span>;
			checkImage<span style="color: #002200;">&#91;</span>i<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#91;</span>j<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#91;</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>GLubyte<span style="color: #002200;">&#41;</span> c;
			checkImage<span style="color: #002200;">&#91;</span>i<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#91;</span>j<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#91;</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>GLubyte<span style="color: #002200;">&#41;</span> c;
			checkImage<span style="color: #002200;">&#91;</span>i<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#91;</span>j<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#91;</span><span style="color: #2400d9;">2</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>GLubyte<span style="color: #002200;">&#41;</span> c;
			checkImage<span style="color: #002200;">&#91;</span>i<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#91;</span>j<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#91;</span><span style="color: #2400d9;">3</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>GLubyte<span style="color: #002200;">&#41;</span> <span style="color: #2400d9;">255</span>;  <span style="color: #11740a; font-style: italic;">// For ghost effect ( 255 / 2 )</span>
		<span style="color: #002200;">&#125;</span>
	<span style="color: #002200;">&#125;</span>
&nbsp;
	GLint					saveName;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Now use OpenGL ES to generate a name for the texture.</span>
	<span style="color: #11740a; font-style: italic;">// Pass by reference so that our texture variable gets set.</span>
	glGenTextures<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">1</span>, <span style="color: #002200;">&amp;</span>amp;m_texture<span style="color: #002200;">&#41;</span>;
&nbsp;
	glGetIntegerv<span style="color: #002200;">&#40;</span>GL_TEXTURE_BINDING_2D, <span style="color: #002200;">&amp;</span>amp;saveName<span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Bind the texture name.</span>
	glBindTexture<span style="color: #002200;">&#40;</span>GL_TEXTURE_2D, m_texture<span style="color: #002200;">&#41;</span>;
&nbsp;
	glTexParameteri<span style="color: #002200;">&#40;</span>GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR<span style="color: #002200;">&#41;</span>;
	glTexParameteri<span style="color: #002200;">&#40;</span>GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR<span style="color: #002200;">&#41;</span>;
	glTexParameteri<span style="color: #002200;">&#40;</span>GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE<span style="color: #002200;">&#41;</span>;
	glTexParameteri<span style="color: #002200;">&#40;</span>GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE<span style="color: #002200;">&#41;</span>;
&nbsp;
	glTexEnvi<span style="color: #002200;">&#40;</span> GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE <span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Specify a 2D texture image, providing a pointer to the image data in memory</span>
	glTexImage2D<span style="color: #002200;">&#40;</span>GL_TEXTURE_2D, <span style="color: #2400d9;">0</span>, GL_RGBA, checkImageWidth, checkImageHeight, <span style="color: #2400d9;">0</span>, GL_RGBA, GL_UNSIGNED_BYTE, checkImage<span style="color: #002200;">&#41;</span>;
	<span style="color: #11740a; font-style: italic;">// glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, spriteData);</span>
	<span style="color: #11740a; font-style: italic;">// glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, w, h, 0, GL_ALPHA, GL_UNSIGNED_BYTE, spriteData);</span>
&nbsp;
	glBindTexture<span style="color: #002200;">&#40;</span>GL_TEXTURE_2D, saveName<span style="color: #002200;">&#41;</span>;
&nbsp;
	NSLog<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;END: mcaTexture::initWithChecks( '...' )&quot;</span> <span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">void</span> mcaTexture<span style="color: #002200;">::</span>initFromImage<span style="color: #002200;">&#40;</span> <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>location<span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
	NSLog<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;START: mcaTexture::initFromImage( '...' )&quot;</span> <span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Creates a Core Graphics image from an image file using our location.</span>
	CGImageRef spriteImage <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIImage imageNamed<span style="color: #002200;">:</span>location<span style="color: #002200;">&#93;</span>.CGImage;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Get the width and height of the image.</span>
	<span style="color: #a61390;">size_t</span> w <span style="color: #002200;">=</span> CGImageGetWidth<span style="color: #002200;">&#40;</span>spriteImage<span style="color: #002200;">&#41;</span>;
	<span style="color: #a61390;">size_t</span> h <span style="color: #002200;">=</span> CGImageGetHeight<span style="color: #002200;">&#40;</span>spriteImage<span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">//TODO - resize the width and the height to the nearest power of 2.</span>
&nbsp;
	<span style="color: #11740a; font-style: italic;">//Only create a sprite if we were able to properly load the CG image.</span>
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>spriteImage<span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		NSLog<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;... image loaded successfully ...&quot;</span> <span style="color: #002200;">&#41;</span>;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// Allocated memory needed for the bitmap context</span>
		GLubyte <span style="color: #002200;">*</span>spriteData <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>GLubyte <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> <span style="color: #a61390;">malloc</span><span style="color: #002200;">&#40;</span>w <span style="color: #002200;">*</span> h <span style="color: #002200;">*</span> <span style="color: #2400d9;">4</span><span style="color: #002200;">&#41;</span>;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// Use the bitmap creation function provided by the Core Graphics framework.</span>
		CGContextRef spriteContext <span style="color: #002200;">=</span> CGBitmapContextCreate<span style="color: #002200;">&#40;</span>spriteData, w, h, <span style="color: #2400d9;">8</span>, w <span style="color: #002200;">*</span> <span style="color: #2400d9;">4</span>, CGImageGetColorSpace<span style="color: #002200;">&#40;</span>spriteImage<span style="color: #002200;">&#41;</span>, kCGImageAlphaPremultipliedLast<span style="color: #002200;">&#41;</span>;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// After we create the context, we can draw the sprite image to the context.</span>
		CGContextDrawImage<span style="color: #002200;">&#40;</span>spriteContext, CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0.0</span>, <span style="color: #2400d9;">0.0</span>, <span style="color: #002200;">&#40;</span>CGFloat<span style="color: #002200;">&#41;</span>w, <span style="color: #002200;">&#40;</span>CGFloat<span style="color: #002200;">&#41;</span>h<span style="color: #002200;">&#41;</span>, spriteImage<span style="color: #002200;">&#41;</span>;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// We don't need the context at this point, so we need to release it to avoid memory leaks.</span>
		CGContextRelease<span style="color: #002200;">&#40;</span>spriteContext<span style="color: #002200;">&#41;</span>;
&nbsp;
		GLint					saveName;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// Now use OpenGL ES to generate a name for the texture.</span>
		<span style="color: #11740a; font-style: italic;">// Pass by reference so that our texture variable gets set.</span>
		glGenTextures<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">1</span>, <span style="color: #002200;">&amp;</span>amp;m_texture<span style="color: #002200;">&#41;</span>;
&nbsp;
		glGetIntegerv<span style="color: #002200;">&#40;</span>GL_TEXTURE_BINDING_2D, <span style="color: #002200;">&amp;</span>amp;saveName<span style="color: #002200;">&#41;</span>;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// Bind the texture name.</span>
		glBindTexture<span style="color: #002200;">&#40;</span>GL_TEXTURE_2D, m_texture<span style="color: #002200;">&#41;</span>;
&nbsp;
		glTexParameteri<span style="color: #002200;">&#40;</span>GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR<span style="color: #002200;">&#41;</span>;
		glTexParameteri<span style="color: #002200;">&#40;</span>GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR<span style="color: #002200;">&#41;</span>;
		glTexParameteri<span style="color: #002200;">&#40;</span>GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE<span style="color: #002200;">&#41;</span>;
		glTexParameteri<span style="color: #002200;">&#40;</span>GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE<span style="color: #002200;">&#41;</span>;
&nbsp;
		glTexEnvi<span style="color: #002200;">&#40;</span> GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE <span style="color: #002200;">&#41;</span>;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// Specify a 2D texture image, providing a pointer to the image data in memory</span>
		glTexImage2D<span style="color: #002200;">&#40;</span>GL_TEXTURE_2D, <span style="color: #2400d9;">0</span>, GL_RGBA, w, h, <span style="color: #2400d9;">0</span>, GL_RGBA, GL_UNSIGNED_BYTE, spriteData<span style="color: #002200;">&#41;</span>;
		<span style="color: #11740a; font-style: italic;">// glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, spriteData);</span>
		<span style="color: #11740a; font-style: italic;">// glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, w, h, 0, GL_ALPHA, GL_UNSIGNED_BYTE, spriteData);</span>
&nbsp;
		glBindTexture<span style="color: #002200;">&#40;</span>GL_TEXTURE_2D, saveName<span style="color: #002200;">&#41;</span>;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// glEnable(GL_LIGHTING);</span>
&nbsp;
		<span style="color: #11740a; font-style: italic;">// Release the image data, which is now unused.</span>
		<span style="color: #a61390;">free</span><span style="color: #002200;">&#40;</span>spriteData<span style="color: #002200;">&#41;</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	GLenum	errGL <span style="color: #002200;">=</span> glGetError<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
	NSLog<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;... glGetError = %1 ...&quot;</span>, errGL <span style="color: #002200;">&#41;</span>;
&nbsp;
	NSLog<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;END: mcaTexture::initFromImage( '...' )&quot;</span> <span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p><strong>Sample Usage</strong></p>
<p>The sample code below is incomplete for brevity. It&#8217;s just meant to give you a general idea of how to use the texture class.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">/*
 *  mcaPlane.mm
 *
 *  Created by Mitchell Allen on 4/17/09.
 *  Copyright 2009 __MyCompanyName__. All rights reserved.
 *
 */</span>
&nbsp;
<span style="color: #6e371a;">#include &quot;mcaPlane.h&quot;</span>
&nbsp;
<span style="color: #a61390;">void</span> mcaPlane<span style="color: #002200;">::</span>init<span style="color: #002200;">&#40;</span> GLfloat xpos, GLfloat ypos, GLfloat fSize <span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
	<span style="color: #11740a; font-style: italic;">// ...</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">void</span> mcaPlane<span style="color: #002200;">::</span>initWithTexture<span style="color: #002200;">&#40;</span> GLfloat xpos, GLfloat ypos, GLfloat fSize, GLuint gTexture <span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
	init<span style="color: #002200;">&#40;</span> xpos, ypos, fSize <span style="color: #002200;">&#41;</span>;
&nbsp;
	m_glTexture <span style="color: #002200;">=</span> gTexture;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">void</span> mcaPlane<span style="color: #002200;">::</span>paint<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">!</span> isVisible<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span> <span style="color: #a61390;">return</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">//Push the matrix so we can keep it as it was previously.</span>
	glPushMatrix<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
	glMatrixMode<span style="color: #002200;">&#40;</span>GL_MODELVIEW<span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">//Enable 2D textures.</span>
	glEnable<span style="color: #002200;">&#40;</span>GL_TEXTURE_2D<span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">//Bind this texture.</span>
	glBindTexture<span style="color: #002200;">&#40;</span>GL_TEXTURE_2D, m_glTexture <span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Set the texture parameters to use a minifying filter and a linear filer.</span>
	glTexParameteri<span style="color: #002200;">&#40;</span>GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR<span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">//Store the coordinates/dimensions from the rectangle.</span>
	CGFloat w <span style="color: #002200;">=</span> m_planeSize;
	CGFloat h <span style="color: #002200;">=</span> m_planeSize;
&nbsp;
	<span style="color: #11740a; font-style: italic;">//Translate the OpenGL context to the center for rotation.</span>
	glTranslatef<span style="color: #002200;">&#40;</span> m_xpos <span style="color: #002200;">+</span> w<span style="color: #002200;">/</span><span style="color: #2400d9;">2</span>, m_ypos <span style="color: #002200;">+</span>h<span style="color: #002200;">/</span><span style="color: #2400d9;">2</span>, 0.0f<span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">//Apply the rotation over the axis.</span>
	glRotatef<span style="color: #002200;">&#40;</span>m_spriteRotation, 1.0f, 0.0f, 0.0f<span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">//Translate back to the top left corner  for drawing.</span>
	glTranslatef<span style="color: #002200;">&#40;</span><span style="color: #002200;">-</span>w<span style="color: #002200;">/</span><span style="color: #2400d9;">2</span>, <span style="color: #002200;">-</span>h<span style="color: #002200;">/</span><span style="color: #2400d9;">2</span>, 0.0f<span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">//Draw to match </span>
&nbsp;
	<span style="color: #a61390;">const</span> GLfloat planeVertices<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">=</span> <span style="color: #002200;">&#123;</span>
&nbsp;
            <span style="color: #11740a; font-style: italic;">// Define the plane face</span>
&nbsp;
            <span style="color: #002200;">-</span><span style="color: #2400d9;">1.0</span>,   <span style="color: #2400d9;">1.0</span>, <span style="color: #2400d9;">0.0</span>,            <span style="color: #11740a; font-style: italic;">// top left</span>
            <span style="color: #002200;">-</span><span style="color: #2400d9;">1.0</span>, <span style="color: #002200;">-</span><span style="color: #2400d9;">1.0</span>, <span style="color: #2400d9;">0.0</span>,            <span style="color: #11740a; font-style: italic;">// bottom left</span>
              <span style="color: #2400d9;">1.0</span>, <span style="color: #002200;">-</span><span style="color: #2400d9;">1.0</span>, <span style="color: #2400d9;">0.0</span>,            <span style="color: #11740a; font-style: italic;">// bottom right</span>
             <span style="color: #2400d9;">1.0</span>,    <span style="color: #2400d9;">1.0</span>, <span style="color: #2400d9;">0.0</span>,            <span style="color: #11740a; font-style: italic;">// top right</span>
       <span style="color: #002200;">&#125;</span>;    
&nbsp;
	<span style="color: #a61390;">const</span> GLshort squareTextureCoords<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">=</span> <span style="color: #002200;">&#123;</span>
&nbsp;
           <span style="color: #11740a; font-style: italic;">// Plane</span>
&nbsp;
           <span style="color: #2400d9;">0</span>, <span style="color: #2400d9;">1</span>,       <span style="color: #11740a; font-style: italic;">// top left</span>
           <span style="color: #2400d9;">0</span>, <span style="color: #2400d9;">0</span>,       <span style="color: #11740a; font-style: italic;">// bottom left</span>
           <span style="color: #2400d9;">1</span>, <span style="color: #2400d9;">0</span>,       <span style="color: #11740a; font-style: italic;">// bottom right</span>
           <span style="color: #2400d9;">1</span>, <span style="color: #2400d9;">1</span>,       <span style="color: #11740a; font-style: italic;">// top right</span>
        <span style="color: #002200;">&#125;</span>;
&nbsp;
	glScalef<span style="color: #002200;">&#40;</span> m_planeSize, m_planeSize, m_planeSize <span style="color: #002200;">&#41;</span>;
&nbsp;
	glRotatef<span style="color: #002200;">&#40;</span> m_xRot, 1.0f, 0.0f, 0.0f <span style="color: #002200;">&#41;</span>;
	glRotatef<span style="color: #002200;">&#40;</span> m_yRot, 0.0f, 1.0f, 0.0f <span style="color: #002200;">&#41;</span>;
	glRotatef<span style="color: #002200;">&#40;</span> m_zRot, 0.0f, 0.0f, 1.0f <span style="color: #002200;">&#41;</span>;
&nbsp;
	glEnableClientState<span style="color: #002200;">&#40;</span>GL_VERTEX_ARRAY<span style="color: #002200;">&#41;</span>;
	glEnableClientState<span style="color: #002200;">&#40;</span>GL_TEXTURE_COORD_ARRAY<span style="color: #002200;">&#41;</span>;
&nbsp;
	glBindTexture<span style="color: #002200;">&#40;</span>GL_TEXTURE_2D, m_glTexture <span style="color: #002200;">&#41;</span>;
&nbsp;
	glVertexPointer<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">3</span>, GL_FLOAT, <span style="color: #2400d9;">0</span>, planeVertices <span style="color: #002200;">&#41;</span>;
	glTexCoordPointer<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">2</span>, GL_SHORT, <span style="color: #2400d9;">0</span>, squareTextureCoords <span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Draw the plane in white</span>
	glColor4f<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">1.0</span>, <span style="color: #2400d9;">1.0</span>, <span style="color: #2400d9;">1.0</span>, <span style="color: #2400d9;">1.0</span><span style="color: #002200;">&#41;</span>;
        glDrawArrays<span style="color: #002200;">&#40;</span>GL_TRIANGLE_FAN, <span style="color: #2400d9;">0</span>, <span style="color: #2400d9;">4</span><span style="color: #002200;">&#41;</span>;
&nbsp;
	glDisableClientState<span style="color: #002200;">&#40;</span>GL_VERTEX_ARRAY<span style="color: #002200;">&#41;</span>;
	glDisableClientState<span style="color: #002200;">&#40;</span>GL_TEXTURE_COORD_ARRAY<span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">//Allow transparency and blending.</span>
	glEnable<span style="color: #002200;">&#40;</span>GL_BLEND<span style="color: #002200;">&#41;</span>;
	glBlendFunc<span style="color: #002200;">&#40;</span>GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA<span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">//Restore the model view matrix to prevent contamination.</span>
	glPopMatrix<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">	mcaTexture txChecks, txShip;
&nbsp;
	NSLog<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;... initializing textures ...&quot;</span> <span style="color: #002200;">&#41;</span>;
&nbsp;
	txChecks.initWithChecks<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
	txShip.initFromImage<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Ship.png&quot;</span> <span style="color: #002200;">&#41;</span>;
&nbsp;
	NSLog<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;... initializing objects ...&quot;</span> <span style="color: #002200;">&#41;</span>;
&nbsp;
	testPlane<span style="color: #002200;">&#91;</span> <span style="color: #2400d9;">0</span> <span style="color: #002200;">&#93;</span>.initWithTexture<span style="color: #002200;">&#40;</span>  0.0f, 0.0f, 100.f, txChecks.getTexture<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span>;
	testPlane<span style="color: #002200;">&#91;</span> <span style="color: #2400d9;">1</span> <span style="color: #002200;">&#93;</span>.initWithTexture<span style="color: #002200;">&#40;</span> 50.0f, 0.0f, 100.f, txChecks.getTexture<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span>;
	testPlane<span style="color: #002200;">&#91;</span> <span style="color: #2400d9;">2</span> <span style="color: #002200;">&#93;</span>.initWithTexture<span style="color: #002200;">&#40;</span>  0.0f, 0.0f, 125.f, txShip.getTexture<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span>;</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://mitchallen.com/iphone/archives/75/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
