Corona SDK

Display an OpenGL image in Corona SDK.

  1. --Display "myImage.png"
  2. display.newImage("myImage.png")

Objective-C

Display an OpenGL image in Objective-C for Apple iPhone SDK.

  1. // Display "myImage.png"
  2.  
  3. // ----------------------------------------------------------------------------
  4. // OpenGLESTextureAppDelegate.m
  5. // ----------------------------------------------------------------------------
  6.  
  7. #import "OpenGLESTextureAppDelegate.h"
  8. #import "EAGLView.h"
  9. #import "OpenGLESTextureViewController.h"
  10.  
  11. @implementation OpenGLESTextureAppDelegate
  12.  
  13. @synthesize window=_window;
  14.  
  15. @synthesize viewController=_viewController;
  16.  
  17. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  18. {
  19. // Override point for customization after application launch.
  20. self.window.rootViewController = self.viewController;
  21. return YES;
  22. }
  23.  
  24. - (void)applicationDidBecomeActive:(UIApplication *)application
  25. {
  26. /*
  27. Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
  28. */
  29. [self.viewController drawFrame];
  30. }
  31.  
  32. - (void)dealloc
  33. {
  34. [_window release];
  35. [_viewController release];
  36. [super dealloc];
  37. }
  38.  
  39. @end
  40.  
  41.  
  42. // ----------------------------------------------------------------------------
  43. // EAGLView.m
  44. // ----------------------------------------------------------------------------
  45.  
  46. #import <QuartzCore/QuartzCore.h>
  47. #import "EAGLView.h"
  48.  
  49. @interface EAGLView (PrivateMethods)
  50. - (void)createFramebuffer;
  51. - (void)deleteFramebuffer;
  52. @end
  53.  
  54. @implementation EAGLView
  55.  
  56. @synthesize context;
  57.  
  58. // You must implement this method
  59. + (Class)layerClass
  60. {
  61. return [CAEAGLLayer class];
  62. }
  63.  
  64. //The EAGL view is stored in the nib file. When it's unarchived it's sent -initWithCoder:.
  65. - (id)initWithCoder:(NSCoder*)coder
  66. {
  67. self = [super initWithCoder:coder];
  68. if (self) {
  69. CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer;
  70.  
  71. eaglLayer.opaque = TRUE;
  72. eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
  73. [NSNumber numberWithBool:FALSE], kEAGLDrawablePropertyRetainedBacking,
  74. kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat,
  75. nil];
  76. }
  77.  
  78. return self;
  79. }
  80.  
  81. - (void)dealloc
  82. {
  83. [self deleteFramebuffer];
  84. [context release];
  85.  
  86. [super dealloc];
  87. }
  88.  
  89. - (void)setContext:(EAGLContext *)newContext
  90. {
  91. if (context != newContext) {
  92. [self deleteFramebuffer];
  93.  
  94. [context release];
  95. context = [newContext retain];
  96.  
  97. [EAGLContext setCurrentContext:nil];
  98. }
  99. }
  100.  
  101. - (void)createFramebuffer
  102. {
  103. if (context && !defaultFramebuffer) {
  104. [EAGLContext setCurrentContext:context];
  105.  
  106. // Create default framebuffer object.
  107. glGenFramebuffers(1, &defaultFramebuffer);
  108. glBindFramebuffer(GL_FRAMEBUFFER, defaultFramebuffer);
  109.  
  110. // Create color render buffer and allocate backing store.
  111. glGenRenderbuffers(1, &colorRenderbuffer);
  112. glBindRenderbuffer(GL_RENDERBUFFER, colorRenderbuffer);
  113. [context renderbufferStorage:GL_RENDERBUFFER fromDrawable:(CAEAGLLayer *)self.layer];
  114. glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &framebufferWidth);
  115. glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &framebufferHeight);
  116.  
  117. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, colorRenderbuffer);
  118.  
  119. if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
  120. NSLog(@"Failed to make complete framebuffer object %x", glCheckFramebufferStatus(GL_FRAMEBUFFER));
  121. }
  122. }
  123.  
  124. - (void)deleteFramebuffer
  125. {
  126. if (context) {
  127. [EAGLContext setCurrentContext:context];
  128.  
  129. if (defaultFramebuffer) {
  130. glDeleteFramebuffers(1, &defaultFramebuffer);
  131. defaultFramebuffer = 0;
  132. }
  133.  
  134. if (colorRenderbuffer) {
  135. glDeleteRenderbuffers(1, &colorRenderbuffer);
  136. colorRenderbuffer = 0;
  137. }
  138. }
  139. }
  140.  
  141. - (void)setFramebuffer
  142. {
  143. if (context) {
  144. [EAGLContext setCurrentContext:context];
  145.  
  146. if (!defaultFramebuffer)
  147. [self createFramebuffer];
  148.  
  149. glBindFramebuffer(GL_FRAMEBUFFER, defaultFramebuffer);
  150.  
  151. glViewport(0, 0, framebufferWidth, framebufferHeight);
  152. }
  153. }
  154.  
  155. - (BOOL)presentFramebuffer
  156. {
  157. BOOL success = FALSE;
  158.  
  159. if (context) {
  160. [EAGLContext setCurrentContext:context];
  161.  
  162. glBindRenderbuffer(GL_RENDERBUFFER, colorRenderbuffer);
  163.  
  164. success = [context presentRenderbuffer:GL_RENDERBUFFER];
  165. }
  166.  
  167. return success;
  168. }
  169.  
  170. - (void)layoutSubviews
  171. {
  172. // The framebuffer will be re-created at the beginning of the next setFramebuffer method call.
  173. [self deleteFramebuffer];
  174. }
  175.  
  176. @end
  177.  
  178.  
  179. // ----------------------------------------------------------------------------
  180. // OpenGLESTextureViewController.m
  181. // ----------------------------------------------------------------------------
  182.  
  183. #import <QuartzCore/QuartzCore.h>
  184. #import "OpenGLESTextureViewController.h"
  185. #import "EAGLView.h"
  186.  
  187. @interface OpenGLESTextureViewController ()
  188. @property (nonatomic, retain) EAGLContext *context;
  189. @property (nonatomic, assign) CADisplayLink *displayLink;
  190. - (void) loadTexture;
  191. @end
  192.  
  193. @implementation OpenGLESTextureViewController
  194.  
  195. @synthesize animating, context, displayLink;
  196.  
  197. - (void)awakeFromNib
  198. {
  199. EAGLContext *aContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1];
  200.  
  201. if (!aContext)
  202. NSLog(@"Failed to create ES context");
  203. else if (![EAGLContext setCurrentContext:aContext])
  204. NSLog(@"Failed to set ES context current");
  205.  
  206. self.context = aContext;
  207. [aContext release];
  208.  
  209. [(EAGLView *)self.view setContext:context];
  210. [(EAGLView *)self.view setFramebuffer];
  211.  
  212. [self loadTexture];
  213.  
  214. self.displayLink = nil;
  215. }
  216.  
  217. - (void) loadTexture
  218. {
  219. glEnable(GL_TEXTURE_2D);
  220. glEnable(GL_BLEND);
  221. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  222.  
  223. glGenTextures(1, &textureID);
  224. glBindTexture(GL_TEXTURE_2D, textureID);
  225. glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
  226. glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
  227.  
  228. NSString *path = [[NSBundle mainBundle] pathForResource:@"myImage" ofType:@"png"];
  229. NSData *texData = [[NSData alloc] initWithContentsOfFile:path];
  230. UIImage *image = [[UIImage alloc] initWithData:texData];
  231.  
  232. GLuint width = CGImageGetWidth(image.CGImage);
  233. GLuint height = CGImageGetHeight(image.CGImage);
  234. CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
  235. void *imageData = malloc( height * width * 4 );
  236. CGContextRef image_context = CGBitmapContextCreate( imageData, width, height, 8, 4 * width, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big );
  237. CGColorSpaceRelease( colorSpace );
  238. CGContextClearRect( image_context, CGRectMake( 0, 0, width, height ) );
  239. CGContextTranslateCTM( image_context, 0, height - height );
  240. CGContextDrawImage( image_context, CGRectMake( 0, 0, width, height ), image.CGImage );
  241.  
  242. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData);
  243.  
  244. CGContextRelease(image_context);
  245.  
  246. free(imageData);
  247. [image release];
  248. [texData release];
  249.  
  250. }
  251.  
  252. - (void)dealloc
  253. {
  254. glDeleteTextures(1, &textureID);
  255.  
  256. // Tear down context.
  257. if ([EAGLContext currentContext] == context)
  258. [EAGLContext setCurrentContext:nil];
  259.  
  260. [context release];
  261.  
  262. [super dealloc];
  263. }
  264.  
  265. - (void)viewDidUnload
  266. {
  267. [super viewDidUnload];
  268.  
  269. // Tear down context.
  270. if ([EAGLContext currentContext] == context)
  271. [EAGLContext setCurrentContext:nil];
  272. self.context = nil;
  273. }
  274.  
  275. - (void)drawFrame
  276. {
  277. [(EAGLView *)self.view setFramebuffer];
  278.  
  279. // Replace the implementation of this method to do your own custom drawing.
  280. static const GLfloat squareVertices[] = {
  281. -0.5f, -0.33f,
  282. 0.5f, -0.33f,
  283. -0.5f, 0.33f,
  284. 0.5f, 0.33f,
  285. };
  286.  
  287. static const GLfloat texCoords[] = {
  288. 0.0, 1.0,
  289. 1.0, 1.0,
  290. 0.0, 0.0,
  291. 1.0, 0.0
  292. };
  293.  
  294.  
  295. glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  296. glClear(GL_COLOR_BUFFER_BIT);
  297.  
  298. glMatrixMode(GL_PROJECTION);
  299. glLoadIdentity();
  300. glMatrixMode(GL_MODELVIEW);
  301. glLoadIdentity();
  302.  
  303. glVertexPointer(2, GL_FLOAT, 0, squareVertices);
  304. glEnableClientState(GL_VERTEX_ARRAY);
  305. glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
  306. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  307.  
  308. glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  309.  
  310. [(EAGLView *)self.view presentFramebuffer];
  311. }
  312.  
  313. @end

Corona SDK. Code Less. Play More. www.anscamobile.com | More examples