bzr branch
/browse/iphone/common
| Line | Revision | Contents |
| 1 | 12 | // |
| 2 | // GLESView.m |
|
| 3 | // MarineCompass |
|
| 4 | // |
|
| 5 | // Created by döme on 03.08.2009. |
|
| 6 | // |
|
| 7 | ||
| 8 | 54 | /* |
| 9 | * Copyright (c) 2009 Doemoetoer Gulyas. |
|
| 10 | * All rights reserved. |
|
| 11 | * |
|
| 12 | * Redistribution and use in source and binary forms, with or without |
|
| 13 | * modification, are permitted provided that the following conditions |
|
| 14 | * are met: |
|
| 15 | * 1. Redistributions of source code must retain the above copyright |
|
| 16 | * notice, this list of conditions and the following disclaimer. |
|
| 17 | * 2. Redistributions in binary form must reproduce the above copyright |
|
| 18 | * notice, this list of conditions and the following disclaimer in the |
|
| 19 | * documentation and/or other materials provided with the distribution. |
|
| 20 | * 3. The name of the author may not be used to endorse or promote products |
|
| 21 | * derived from this software without specific prior written permission. |
|
| 22 | * |
|
| 23 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
|
| 24 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
|
| 25 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
|
| 26 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
|
| 27 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
|
| 28 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
| 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
| 30 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
| 31 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
|
| 32 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
| 33 | */ |
|
| 34 | ||
| 35 | ||
| 36 | 12 | #import "GLESView.h" |
| 37 | ||
| 38 | #import <QuartzCore/QuartzCore.h> |
|
| 39 | #import <OpenGLES/EAGLDrawable.h> |
|
| 40 | #import <CoreGraphics/CoreGraphics.h> |
|
| 41 | ||
| 42 | 71 | #import <unistd.h> |
| 43 | ||
| 44 | 12 | #import "geometry.h" |
| 45 | #import "VertexArray.h" |
|
| 46 | 20 | #import "ESShader.h" |
| 47 | 78 | #import "PVRTexture.h" |
| 48 | 20 | |
| 49 | ||
| 50 | 81 | |
| 51 | void* loadGrayscaleImageAtPath(NSString* path, int* wp, int* hp) |
|
| 52 | { |
|
| 53 | void* data = NULL; |
|
| 54 | *wp = *hp = 0; |
|
| 55 | UIImage* outlinesImage = [[UIImage alloc] initWithContentsOfFile: path]; |
|
| 56 | if (outlinesImage) |
|
| 57 | { |
|
| 58 | CGImageRef img = [outlinesImage CGImage]; |
|
| 59 | int w = CGImageGetWidth(img), h = CGImageGetHeight(img); |
|
| 60 | *wp = w; *hp = h; |
|
| 61 | 91 | int rowbytes = ((w+3)/4)*4; |
| 62 | data = calloc(1, rowbytes*h); |
|
| 63 | 81 | |
| 64 | ||
| 65 | CGColorSpaceRef outlineColorSpace = CGColorSpaceCreateDeviceGray(); |
|
| 66 | 91 | CGContextRef outlineContext = CGBitmapContextCreate(data, w, h, 8, rowbytes, outlineColorSpace, kCGImageAlphaNone); |
| 67 | 81 | CGColorSpaceRelease(outlineColorSpace); |
| 68 | CGContextSetBlendMode(outlineContext, kCGBlendModeCopy); |
|
| 69 | ||
| 70 | CGContextDrawImage(outlineContext, CGRectMake(0,0,w,h), img); |
|
| 71 | |
|
| 72 | CGContextRelease(outlineContext); |
|
| 73 | |
|
| 74 | } |
|
| 75 | return data; |
|
| 76 | } |
|
| 77 | ||
| 78 | ||
| 79 | ||
| 80 | 20 | @interface ESShader (GLESView) |
| 81 | + (void) setShaderAPI: (EAGLRenderingAPI) api; |
|
| 82 | @end |
|
| 83 | ||
| 84 | 71 | @interface GLESView (Private) |
| 85 | - (void) destroyFramebuffer; |
|
| 86 | - (BOOL) createFramebuffer; |
|
| 87 | @end |
|
| 88 | ||
| 89 | 72 | static EAGLContext* _sharedContext = nil; |
| 90 | 12 | |
| 91 | @implementation GLESView |
|
| 92 | ||
| 93 | 13 | + (Class)layerClass |
| 94 | { |
|
| 95 | 12 | return [CAEAGLLayer class]; |
| 96 | } |
|
| 97 | ||
| 98 | 17 | - (void) lineGradientTexture: (GLuint*) texname red: (float) r green: (float) g blue: (float) b |
| 99 | { |
|
| 100 | uint8_t* spriteData = calloc(1, 256 * 4); |
|
| 101 | 29 | for (int i = 0; i < 256; ++i) |
| 102 | 17 | { |
| 103 | spriteData[4*i+0] = r*(float)i; |
|
| 104 | spriteData[4*i+1] = g*(float)i; |
|
| 105 | spriteData[4*i+2] = b*(float)i; |
|
| 106 | spriteData[4*i+3] = i; |
|
| 107 | } |
|
| 108 | ||
| 109 | 85 | if (*texname == 0) |
| 110 | glGenTextures(1, texname); |
|
| 111 | 17 | glBindTexture(GL_TEXTURE_2D, *texname); |
| 112 | ||
| 113 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, spriteData); |
|
| 114 | ||
| 115 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
|
| 116 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
|
| 117 | ||
| 118 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
|
| 119 | ||
| 120 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
|
| 121 | ||
| 122 | 73 | glBindTexture(GL_TEXTURE_2D, 0); |
| 123 | ||
| 124 | 17 | free(spriteData); |
| 125 | } |
|
| 126 | ||
| 127 | 29 | - (void) doubleGradientTexture: (GLuint*) texname red: (float) r green: (float) g blue: (float) b |
| 128 | { |
|
| 129 | uint8_t* spriteData = calloc(1, 512 * 4); |
|
| 130 | for (int i = 0; i < 256; ++i) |
|
| 131 | { |
|
| 132 | spriteData[4*i+0] = r*(float)i; |
|
| 133 | spriteData[4*i+1] = g*(float)i; |
|
| 134 | spriteData[4*i+2] = b*(float)i; |
|
| 135 | spriteData[4*i+3] = i; |
|
| 136 | } |
|
| 137 | |
|
| 138 | for (int i = 256; i < 512; ++i) |
|
| 139 | { |
|
| 140 | spriteData[4*i+0] = r*(float)(511-i); |
|
| 141 | spriteData[4*i+1] = g*(float)(511-i); |
|
| 142 | spriteData[4*i+2] = b*(float)(511-i); |
|
| 143 | spriteData[4*i+3] = (511-i); |
|
| 144 | } |
|
| 145 | ||
| 146 | ||
| 147 | 85 | if (*texname == 0) |
| 148 | glGenTextures(1, texname); |
|
| 149 | 29 | glBindTexture(GL_TEXTURE_2D, *texname); |
| 150 | ||
| 151 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 512, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, spriteData); |
|
| 152 | ||
| 153 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
|
| 154 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
|
| 155 | ||
| 156 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
|
| 157 | ||
| 158 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
|
| 159 | ||
| 160 | 73 | glBindTexture(GL_TEXTURE_2D, 0); |
| 161 | ||
| 162 | 29 | free(spriteData); |
| 163 | } |
|
| 164 | ||
| 165 | ||
| 166 | 80 | - (void) textureFromCgImage: (CGImageRef) img nonUniformScale: (BOOL) nonUniformScale texName: (GLuint*) texname width: (size_t) width height: (size_t) height repeat: (BOOL) doRepeat mipmap: (BOOL) doMipmap filter: (BOOL) doFilter |
| 167 | 13 | { |
| 168 | 80 | void* spriteData = calloc(width * height, 4); |
| 169 | 23 | |
| 170 | assert(img); |
|
| 171 | ||
| 172 | CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); |
|
| 173 | CGContextRef spriteContext = CGBitmapContextCreate(spriteData, width, height, 8, width * 4, colorSpace, kCGImageAlphaPremultipliedLast); |
|
| 174 | CGColorSpaceRelease(colorSpace); |
|
| 175 | ||
| 176 | 80 | if (nonUniformScale) |
| 177 | CGContextDrawImage(spriteContext, CGRectMake(0.0, 0.0, (CGFloat)width, (CGFloat)height), img); |
|
| 178 | else |
|
| 179 | { |
|
| 180 | CGSize s = CGSizeFitToSize(CGSizeMake(CGImageGetWidth(img), CGImageGetHeight(img)), CGSizeMake(width,height)); |
|
| 181 | |
|
| 182 | CGContextDrawImage(spriteContext, CGRectMake(0.5f*(width-s.width), 0.5f*(height-s.height), s.width, s.height), img); |
|
| 183 | } |
|
| 184 | |
|
| 185 | 13 | CGContextRelease(spriteContext); |
| 186 | |
|
| 187 | 85 | if (*texname == 0) |
| 188 | glGenTextures(1, texname); |
|
| 189 | 13 | glBindTexture(GL_TEXTURE_2D, *texname); |
| 190 | ||
| 191 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, spriteData); |
|
| 192 | ||
| 193 | free(spriteData); |
|
| 194 | ||
| 195 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, doRepeat ? GL_REPEAT : GL_CLAMP_TO_EDGE); |
|
| 196 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, doRepeat ? GL_REPEAT : GL_CLAMP_TO_EDGE); |
|
| 197 | ||
| 198 | if (doMipmap) |
|
| 199 | glGenerateMipmap(GL_TEXTURE_2D); |
|
| 200 | 17 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, doFilter ? (doMipmap ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR) : GL_NEAREST); |
| 201 | 13 | |
| 202 | 17 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, doFilter ? GL_LINEAR : GL_NEAREST); |
| 203 | 73 | |
| 204 | glBindTexture(GL_TEXTURE_2D, 0); |
|
| 205 | 13 | } |
| 206 | ||
| 207 | 80 | - (void) loadTextureFromPvrAtPath: (NSString*) path texName: (GLuint*) texname |
| 208 | { |
|
| 209 | 85 | assert(GL_NO_ERROR == glGetError()); |
| 210 | ||
| 211 | if (*texname == 0) |
|
| 212 | glGenTextures(1, texname); |
|
| 213 | ||
| 214 | 80 | assert([PVRTexture loadPvrTextureWithContentsOfFile: path intoTexture: *texname]); |
| 215 | ||
| 216 | } |
|
| 217 | ||
| 218 | 81 | - (void) textureFromGrayscaleData: (void*) data texName: (GLuint*) texname width: (size_t) width height: (size_t) height repeat: (BOOL) doRepeat mipmap: (BOOL) doMipmap filter: (BOOL) doFilter |
| 219 | { |
|
| 220 | ||
| 221 | 85 | if (*texname == 0) |
| 222 | glGenTextures(1, texname); |
|
| 223 | 81 | glBindTexture(GL_TEXTURE_2D, *texname); |
| 224 | 91 | |
| 225 | // int rowbytes = ((width+3)/4)*4; |
|
| 226 | 81 | |
| 227 | 91 | glPixelStorei(GL_UNPACK_ALIGNMENT, 4); |
| 228 | 81 | glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, width, height, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, data); |
| 229 | ||
| 230 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, doRepeat ? GL_REPEAT : GL_CLAMP_TO_EDGE); |
|
| 231 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, doRepeat ? GL_REPEAT : GL_CLAMP_TO_EDGE); |
|
| 232 | ||
| 233 | if (doMipmap) |
|
| 234 | glGenerateMipmap(GL_TEXTURE_2D); |
|
| 235 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, doFilter ? (doMipmap ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR) : GL_NEAREST); |
|
| 236 | ||
| 237 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, doFilter ? GL_LINEAR : GL_NEAREST); |
|
| 238 | |
|
| 239 | glBindTexture(GL_TEXTURE_2D, 0); |
|
| 240 | } |
|
| 241 | ||
| 242 | - (void) loadGrayscaleTextureFromBitmapAtPath: (NSString*) path texName: (GLuint*) texname repeat: (BOOL) doRepeat mipmap: (BOOL) doMipmap filter: (BOOL) doFilter |
|
| 243 | { |
|
| 244 | int h=0, w=0; |
|
| 245 | void* data = loadGrayscaleImageAtPath(path, &w, &h); |
|
| 246 | |
|
| 247 | [self textureFromGrayscaleData: data texName: texname width: w height: h repeat: doRepeat mipmap: doMipmap filter: doFilter]; |
|
| 248 | |
|
| 249 | free(data); |
|
| 250 | |
|
| 251 | } |
|
| 252 | ||
| 253 | ||
| 254 | 23 | - (void) loadTextureFromBitmapNamed: (NSString*) name texName: (GLuint*) texname repeat: (BOOL) doRepeat mipmap: (BOOL) doMipmap filter: (BOOL) doFilter |
| 255 | 12 | { |
| 256 | ||
| 257 | 78 | if ([[name pathExtension] isEqual: @"pvr"]) |
| 258 | { |
|
| 259 | 89 | NSString* path = ([name isAbsolutePath] ? name : [[NSBundle mainBundle] pathForResource: [name stringByDeletingPathExtension] ofType: [name pathExtension]]); |
| 260 | 80 | [self loadTextureFromPvrAtPath: path texName: texname]; |
| 261 | 78 | return; |
| 262 | } |
|
| 263 | ||
| 264 | 12 | // Creates a Core Graphics image from an image file |
| 265 | 13 | CGImageRef spriteImage = [UIImage imageNamed: name].CGImage; |
| 266 | 12 | // Get the width and height of the image |
| 267 | 13 | size_t width = CGImageGetWidth(spriteImage); |
| 268 | size_t height = CGImageGetHeight(spriteImage); |
|
| 269 | ||
| 270 | ||
| 271 | 80 | [self textureFromCgImage: spriteImage nonUniformScale: YES texName: texname width: width height: height repeat: doRepeat mipmap: doMipmap filter: doFilter]; |
| 272 | 13 | |
| 273 | 12 | } |
| 274 | ||
| 275 | 23 | static CGImageRef CreateCGImageFromPDFPage(CGPDFDocumentRef document, size_t pageNumber, int width, int height) |
| 276 | { |
|
| 277 | ||
| 278 | CGPDFPageRef page = CGPDFDocumentGetPage(document, pageNumber); |
|
| 279 | if(!page) |
|
| 280 | return NULL; |
|
| 281 | ||
| 282 | // CGRect pageRect = CGPDFPageGetBoxRect(page, kCGPDFMediaBox); |
|
| 283 | ||
| 284 | CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); |
|
| 285 | // void* data = malloc(4*width*height); |
|
| 286 | CGContextRef bitmapContext = CGBitmapContextCreate(NULL, width, height, 8, width * 4, colorSpace, kCGImageAlphaPremultipliedLast); |
|
| 287 | CGColorSpaceRelease(colorSpace); |
|
| 288 | ||
| 289 | if(!bitmapContext) |
|
| 290 | { |
|
| 291 | //free(data); |
|
| 292 | return NULL; |
|
| 293 | } |
|
| 294 | ||
| 295 | CGAffineTransform M = CGPDFPageGetDrawingTransform(page, kCGPDFMediaBox, CGRectMake(0.0f, 0.0f, width, height), 0, NO); |
|
| 296 | |
|
| 297 | CGContextConcatCTM(bitmapContext, M); |
|
| 298 | CGContextDrawPDFPage(bitmapContext, page); |
|
| 299 | CGImageRef pdfImage = CGBitmapContextCreateImage(bitmapContext); |
|
| 300 | CFRelease(bitmapContext); |
|
| 301 | //free(data); |
|
| 302 | return pdfImage; |
|
| 303 | ||
| 304 | } |
|
| 305 | ||
| 306 | static CGImageRef CreateImageFromPDFNamed(NSString* name, int width, int height) |
|
| 307 | { |
|
| 308 | 94 | CFURLRef url = CFURLCreateWithFileSystemPath(NULL, (__bridge CFStringRef) [[NSBundle mainBundle] pathForResource: name ofType: nil], 0, NO); |
| 309 | 23 | CGPDFDocumentRef pdfDoc = CGPDFDocumentCreateWithURL(url); |
| 310 | CFRelease(url); |
|
| 311 | assert(pdfDoc); |
|
| 312 | |
|
| 313 | CGImageRef img = CreateCGImageFromPDFPage(pdfDoc, 1, width, height); |
|
| 314 | |
|
| 315 | CGPDFDocumentRelease(pdfDoc); |
|
| 316 | |
|
| 317 | return img; |
|
| 318 | } |
|
| 319 | ||
| 320 | ||
| 321 | 72 | + (EAGLSharegroup*) glSharegroup |
| 322 | { |
|
| 323 | assert([NSThread isMainThread]); |
|
| 324 | ||
| 325 | if (!_sharedContext) |
|
| 326 | { |
|
| 327 | _sharedContext = [[EAGLContext alloc] initWithAPI: kEAGLRenderingAPIOpenGLES2]; |
|
| 328 | if (!_sharedContext) |
|
| 329 | _sharedContext = [[EAGLContext alloc] initWithAPI: kEAGLRenderingAPIOpenGLES1]; |
|
| 330 | } |
|
| 331 | return [_sharedContext sharegroup]; |
|
| 332 | } |
|
| 333 | ||
| 334 | 23 | - (void) loadTextureFromPDFNamed: (NSString*) name texName: (GLuint*) texname width: (size_t) width height: (size_t) height repeat: (BOOL) doRepeat mipmap: (BOOL) doMipmap filter: (BOOL) doFilter |
| 335 | { |
|
| 336 | ||
| 337 | CGImageRef img = CreateImageFromPDFNamed(name, width, height); |
|
| 338 | 80 | [self textureFromCgImage: img nonUniformScale: YES texName: texname width: width height: height repeat: doRepeat mipmap: doMipmap filter: doFilter]; |
| 339 | 23 | CGImageRelease(img); |
| 340 | } |
|
| 341 | ||
| 342 | 12 | |
| 343 | //The GL view is stored in the nib file. When it's unarchived it's sent -initWithCoder: |
|
| 344 | - (id)initWithCoder:(NSCoder*)coder { |
|
| 345 | |
|
| 346 | if (!(self = [super initWithCoder:coder])) |
|
| 347 | return nil; |
|
| 348 | ||
| 349 | CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer; |
|
| 350 | |
|
| 351 | eaglLayer.opaque = YES; |
|
| 352 | eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithBool:NO], kEAGLDrawablePropertyRetainedBacking, kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat, nil]; |
|
| 353 | |
|
| 354 | 72 | context = [[EAGLContext alloc] initWithAPI: kEAGLRenderingAPIOpenGLES2 sharegroup: [GLESView glSharegroup]]; |
| 355 | 21 | if (!context) |
| 356 | 72 | context = [[EAGLContext alloc] initWithAPI: kEAGLRenderingAPIOpenGLES1 sharegroup: [GLESView glSharegroup]]; |
| 357 | 12 | |
| 358 | 20 | if (!context || ![EAGLContext setCurrentContext: context]) |
| 359 | { |
|
| 360 | 12 | return nil; |
| 361 | } |
|
| 362 | 70 | |
| 363 | osVersion = [[[UIDevice currentDevice] systemVersion] floatValue]; |
|
| 364 | 71 | recreateFramebuffer = YES; |
| 365 | 70 | |
| 366 | 21 | [self setupGraphicsForApi: [context API]]; |
| 367 | 12 | |
| 368 | 85 | assert(GL_NO_ERROR == glGetError()); |
| 369 | ||
| 370 | 12 | return self; |
| 371 | } |
|
| 372 | ||
| 373 | 51 | - (id)initWithFrame:(CGRect)frame { |
| 374 | |
|
| 375 | if (!(self = [super initWithFrame:frame])) |
|
| 376 | return nil; |
|
| 377 | ||
| 378 | CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer; |
|
| 379 | |
|
| 380 | eaglLayer.opaque = YES; |
|
| 381 | eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithBool:NO], kEAGLDrawablePropertyRetainedBacking, kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat, nil]; |
|
| 382 | |
|
| 383 | 82 | context = [[EAGLContext alloc] initWithAPI: kEAGLRenderingAPIOpenGLES2 sharegroup: [GLESView glSharegroup]]; |
| 384 | 51 | if (!context) |
| 385 | 82 | context = [[EAGLContext alloc] initWithAPI: kEAGLRenderingAPIOpenGLES1 sharegroup: [GLESView glSharegroup]]; |
| 386 | 51 | |
| 387 | if (!context || ![EAGLContext setCurrentContext: context]) |
|
| 388 | { |
|
| 389 | return nil; |
|
| 390 | } |
|
| 391 | 70 | |
| 392 | osVersion = [[[UIDevice currentDevice] systemVersion] floatValue]; |
|
| 393 | 71 | recreateFramebuffer = YES; |
| 394 | 51 | |
| 395 | 70 | |
| 396 | 51 | |
| 397 | [self setupGraphicsForApi: [context API]]; |
|
| 398 | ||
| 399 | 85 | assert(GL_NO_ERROR == glGetError()); |
| 400 | ||
| 401 | 51 | return self; |
| 402 | } |
|
| 403 | ||
| 404 | ||
| 405 | ||
| 406 | 12 | - (void) setupGraphicsForApi: (EAGLRenderingAPI) glApi |
| 407 | { |
|
| 408 | 70 | |
| 409 | 12 | glClearColor(0.0,0.0,1.0,1.0); |
| 410 | } |
|
| 411 | ||
| 412 | ||
| 413 | - (void)dealloc |
|
| 414 | { |
|
| 415 | [self stopAnimation]; |
|
| 416 | 70 | |
| 417 | 73 | // [VertexArray releaseSharedObjects]; |
| 418 | 12 | |
| 419 | if ([EAGLContext currentContext] == context) |
|
| 420 | 73 | [EAGLContext setCurrentContext: nil]; |
| 421 | 12 | |
| 422 | } |
|
| 423 | ||
| 424 | ||
| 425 | - (void) setupViewDrawing |
|
| 426 | { |
|
| 427 | [EAGLContext setCurrentContext:context]; |
|
| 428 | 71 | |
| 429 | if (recreateFramebuffer) |
|
| 430 | { |
|
| 431 | [self destroyFramebuffer]; |
|
| 432 | [self createFramebuffer]; |
|
| 433 | recreateFramebuffer = NO; |
|
| 434 | } |
|
| 435 | 12 | |
| 436 | glBindFramebuffer(GL_FRAMEBUFFER, viewFramebuffer); |
|
| 437 | glViewport(0, 0, backingWidth, backingHeight); |
|
| 438 | ||
| 439 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
|
| 440 | } |
|
| 441 | ||
| 442 | - (void) finishViewDrawing |
|
| 443 | { |
|
| 444 | 70 | |
| 445 | if (osVersion >= 4.0) |
|
| 446 | { |
|
| 447 | GLenum attachments[] = {GL_DEPTH_ATTACHMENT}; |
|
| 448 | glDiscardFramebufferEXT(GL_READ_FRAMEBUFFER_APPLE, 1, attachments); |
|
| 449 | } |
|
| 450 | ||
| 451 | 12 | glBindRenderbuffer(GL_RENDERBUFFER, viewRenderbuffer); |
| 452 | [context presentRenderbuffer: GL_RENDERBUFFER]; |
|
| 453 | 36 | |
| 454 | GLenum err = glGetError(); |
|
| 455 | if (err) |
|
| 456 | 86 | NSLog(@"OpenGL error 0x%x occured", err); |
| 457 | 12 | } |
| 458 | ||
| 459 | 88 | - (void) drawView: (double) time |
| 460 | 12 | { |
| 461 | [self setupViewDrawing]; |
|
| 462 | |
|
| 463 | [self finishViewDrawing]; |
|
| 464 | } |
|
| 465 | ||
| 466 | ||
| 467 | - (void)drawRect:(CGRect)rect |
|
| 468 | { |
|
| 469 | [super drawRect: rect]; |
|
| 470 | |
|
| 471 | 73 | // [self drawView]; |
| 472 | 12 | |
| 473 | // [[UIColor clearColor] set]; |
|
| 474 | // UIRectFill(rect); |
|
| 475 | } |
|
| 476 | ||
| 477 | 88 | - (void)animationCallback: (double) time |
| 478 | 12 | { |
| 479 | 70 | } |
| 480 | ||
| 481 | ||
| 482 | - (void) displayLinkCallback: (CADisplayLink*) sender |
|
| 483 | { |
|
| 484 | 88 | [self animationCallback: [sender timestamp]]; |
| 485 | 70 | } |
| 486 | 12 | |
| 487 | 71 | - (void) displayThreadMain: (id) obj |
| 488 | { |
|
| 489 | 94 | @autoreleasepool { |
| 490 | |
|
| 491 | NSRunLoop* runLoop = [NSRunLoop currentRunLoop]; |
|
| 492 | |
|
| 493 | @synchronized(self) |
|
| 494 | { |
|
| 495 | displayLink = [CADisplayLink displayLinkWithTarget: self selector: @selector(displayLinkCallback:)]; |
|
| 496 | [displayLink addToRunLoop: [NSRunLoop currentRunLoop] forMode: NSRunLoopCommonModes]; |
|
| 497 | } |
|
| 498 | |
|
| 499 | while (displayThreadShouldRun) |
|
| 500 | { |
|
| 501 | @autoreleasepool { |
|
| 502 | [runLoop runUntilDate: [NSDate dateWithTimeIntervalSinceNow: 1.0]]; |
|
| 503 | } |
|
| 504 | } |
|
| 505 | |
|
| 506 | @synchronized(self) |
|
| 507 | { |
|
| 508 | [displayLink invalidate]; |
|
| 509 | displayLink = nil; |
|
| 510 | } |
|
| 511 | |
|
| 512 | [EAGLContext setCurrentContext: nil]; |
|
| 513 | |
|
| 514 | } |
|
| 515 | 73 | // [NSThread exit]; |
| 516 | 71 | } |
| 517 | ||
| 518 | - (void)startAnimation: (BOOL) threaded |
|
| 519 | { |
|
| 520 | @synchronized(self) |
|
| 521 | { |
|
| 522 | [self stopAnimation]; |
|
| 523 | ||
| 524 | if (threaded) |
|
| 525 | { |
|
| 526 | 73 | displayThreadShouldRun = YES; |
| 527 | 71 | displayThread = [[NSThread alloc] initWithTarget: self selector: @selector(displayThreadMain:) object: nil]; |
| 528 | |
|
| 529 | [displayThread start]; |
|
| 530 | } |
|
| 531 | else |
|
| 532 | { |
|
| 533 | [displayLink invalidate]; |
|
| 534 | displayLink = [CADisplayLink displayLinkWithTarget: self selector: @selector(displayLinkCallback:)]; |
|
| 535 | [displayLink addToRunLoop: [NSRunLoop currentRunLoop] forMode: NSRunLoopCommonModes]; |
|
| 536 | } |
|
| 537 | } |
|
| 538 | 12 | } |
| 539 | ||
| 540 | ||
| 541 | - (void)stopAnimation |
|
| 542 | { |
|
| 543 | 71 | @synchronized(self) |
| 544 | { |
|
| 545 | [displayLink invalidate]; |
|
| 546 | displayLink = nil; |
|
| 547 | 73 | } |
| 548 | if (displayThread) |
|
| 549 | { |
|
| 550 | displayThreadShouldRun = NO; |
|
| 551 | while ([displayThread isExecuting]) |
|
| 552 | usleep(1); |
|
| 553 | @synchronized(self) |
|
| 554 | 71 | { |
| 555 | displayThread = nil; |
|
| 556 | } |
|
| 557 | } |
|
| 558 | 12 | } |
| 559 | ||
| 560 | ||
| 561 | ||
| 562 | - (BOOL)createFramebuffer { |
|
| 563 | |
|
| 564 | glGenFramebuffers(1, &viewFramebuffer); |
|
| 565 | glGenRenderbuffers(1, &viewRenderbuffer); |
|
| 566 | |
|
| 567 | glBindFramebuffer(GL_FRAMEBUFFER, viewFramebuffer); |
|
| 568 | glBindRenderbuffer(GL_RENDERBUFFER, viewRenderbuffer); |
|
| 569 | [context renderbufferStorage:GL_RENDERBUFFER fromDrawable:(CAEAGLLayer*)self.layer]; |
|
| 570 | glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, viewRenderbuffer); |
|
| 571 | |
|
| 572 | glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &backingWidth); |
|
| 573 | glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &backingHeight); |
|
| 574 | |
|
| 575 | 72 | if (USE_DEPTH_BUFFER) |
| 576 | { |
|
| 577 | 12 | glGenRenderbuffers(1, &depthRenderbuffer); |
| 578 | glBindRenderbuffer(GL_RENDERBUFFER, depthRenderbuffer); |
|
| 579 | glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, backingWidth, backingHeight); |
|
| 580 | glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthRenderbuffer); |
|
| 581 | } |
|
| 582 | |
|
| 583 | 38 | if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) |
| 584 | { |
|
| 585 | 12 | NSLog(@"failed to make complete framebuffer object %x", glCheckFramebufferStatus(GL_FRAMEBUFFER)); |
| 586 | 38 | NSLog(@"%f %f %f %f", self.frame.origin.x, self.frame.origin.y, self.frame.size.width, self.frame.size.height); |
| 587 | 12 | return NO; |
| 588 | } |
|
| 589 | 73 | |
| 590 | glBindRenderbuffer(GL_RENDERBUFFER, 0); |
|
| 591 | glBindFramebuffer(GL_FRAMEBUFFER, 0); |
|
| 592 | ||
| 593 | 12 | return YES; |
| 594 | } |
|
| 595 | ||
| 596 | ||
| 597 | 72 | - (void) destroyFramebuffer |
| 598 | { |
|
| 599 | 12 | |
| 600 | glDeleteFramebuffers(1, &viewFramebuffer); |
|
| 601 | viewFramebuffer = 0; |
|
| 602 | glDeleteRenderbuffers(1, &viewRenderbuffer); |
|
| 603 | viewRenderbuffer = 0; |
|
| 604 | |
|
| 605 | if(depthRenderbuffer) |
|
| 606 | { |
|
| 607 | glDeleteRenderbuffers(1, &depthRenderbuffer); |
|
| 608 | depthRenderbuffer = 0; |
|
| 609 | } |
|
| 610 | } |
|
| 611 | ||
| 612 | 71 | - (void)layoutSubviews |
| 613 | { |
|
| 614 | @synchronized(self) |
|
| 615 | { |
|
| 616 | recreateFramebuffer = YES; |
|
| 617 | /* |
|
| 618 | 12 | [EAGLContext setCurrentContext:context]; |
| 619 | [self destroyFramebuffer]; |
|
| 620 | [self createFramebuffer]; |
|
| 621 | [self drawView]; |
|
| 622 | 71 | */ |
| 623 | } |
|
| 624 | 12 | } |
| 625 | ||
| 626 | @synthesize animationInterval; |
|
| 627 | ||
| 628 | @end |
Loggerhead 1.17 is a web-based interface for Bazaar branches