26 #import "GlassOffscreen.h" 27 28 #import "GlassFrameBufferObject.h" 29 //#import "GlassPBuffer.h" 30 31 //#define VERBOSE 32 #ifndef VERBOSE 33 #define LOG(MSG, ...) 34 #else 35 #define LOG(MSG, ...) GLASS_LOG(MSG, ## __VA_ARGS__); 36 #endif 37 38 @interface GlassOffscreen () 39 - (void)setContext; 40 - (void)unsetContext; 41 @end 42 43 @implementation GlassOffscreen 44 45 - (id)initWithContext:(CGLContextObj)ctx 46 { 47 self = [super init]; 48 if (self != nil) 49 { 50 self->_ctx = CGLRetainContext(ctx); 51 52 self->_backgroundR = 1.0f; 53 self->_backgroundG = 1.0f; 54 self->_backgroundB = 1.0f; 55 self->_backgroundA = 1.0f; 56 57 [self setContext]; 58 { 59 self->_offscreen = [[GlassFrameBufferObject alloc] init]; 60 if (self->_offscreen == nil) 61 { 62 // TODO: implement PBuffer if needed 63 //self->_offscreen = [[GlassPBuffer alloc] init]; 64 } 65 } 66 [self unsetContext]; 67 } 68 return self; 69 } 70 71 - (CGLContextObj)getContext; 72 { 73 return self->_ctx; 74 } 75 76 - (void)dealloc 77 { 78 [self setContext]; 79 { 80 [(NSObject*)self->_offscreen release]; 81 self->_offscreen = NULL; 82 } 83 [self unsetContext]; 84 122 self->_layer = new_layer; 123 } 124 125 - (void)setContext 126 { 127 self->_ctxToRestore = CGLGetCurrentContext(); 128 CGLLockContext(self->_ctx); 129 CGLSetCurrentContext(self->_ctx); 130 } 131 132 - (void)unsetContext 133 { 134 CGLSetCurrentContext(self->_ctxToRestore); 135 CGLUnlockContext(self->_ctx); 136 } 137 138 - (void)bindForWidth:(GLuint)width andHeight:(GLuint)height 139 { 140 [self setContext]; 141 [self->_offscreen bindForWidth:width andHeight:height]; 142 [self unsetContext]; 143 } 144 145 - (void)blit 146 { 147 [self blitForWidth:[self->_offscreen width] andHeight:[self->_offscreen height]]; 148 } 149 150 - (GLuint)texture 151 { 152 return [self->_offscreen texture]; 153 } 154 155 - (void)blitForWidth:(GLuint)width andHeight:(GLuint)height 156 { 157 { 158 #if 1 159 glClearColor(self->_backgroundR, self->_backgroundG, self->_backgroundB, self->_backgroundA); 160 glClear(GL_COLOR_BUFFER_BIT); 161 #else | 26 #import "GlassOffscreen.h" 27 28 #import "GlassFrameBufferObject.h" 29 //#import "GlassPBuffer.h" 30 31 //#define VERBOSE 32 #ifndef VERBOSE 33 #define LOG(MSG, ...) 34 #else 35 #define LOG(MSG, ...) GLASS_LOG(MSG, ## __VA_ARGS__); 36 #endif 37 38 @interface GlassOffscreen () 39 - (void)setContext; 40 - (void)unsetContext; 41 @end 42 43 @implementation GlassOffscreen 44 45 - (id)initWithContext:(CGLContextObj)ctx 46 andIsSwPipe:(BOOL)isSwPipe; 47 { 48 self = [super init]; 49 if (self != nil) 50 { 51 self->_ctx = CGLRetainContext(ctx); 52 53 self->_backgroundR = 1.0f; 54 self->_backgroundG = 1.0f; 55 self->_backgroundB = 1.0f; 56 self->_backgroundA = 1.0f; 57 58 [self setContext]; 59 { 60 self->_offscreen = [[GlassFrameBufferObject alloc] init]; 61 if (self->_offscreen == nil) 62 { 63 // TODO: implement PBuffer if needed 64 //self->_offscreen = [[GlassPBuffer alloc] init]; 65 } 66 [(GlassFrameBufferObject*)self->_offscreen setIsSwPipe:(BOOL)isSwPipe]; 67 } 68 [self unsetContext]; 69 } 70 return self; 71 } 72 73 - (CGLContextObj)getContext; 74 { 75 return self->_ctx; 76 } 77 78 - (void)dealloc 79 { 80 [self setContext]; 81 { 82 [(NSObject*)self->_offscreen release]; 83 self->_offscreen = NULL; 84 } 85 [self unsetContext]; 86 124 self->_layer = new_layer; 125 } 126 127 - (void)setContext 128 { 129 self->_ctxToRestore = CGLGetCurrentContext(); 130 CGLLockContext(self->_ctx); 131 CGLSetCurrentContext(self->_ctx); 132 } 133 134 - (void)unsetContext 135 { 136 CGLSetCurrentContext(self->_ctxToRestore); 137 CGLUnlockContext(self->_ctx); 138 } 139 140 - (void)bindForWidth:(GLuint)width andHeight:(GLuint)height 141 { 142 [self setContext]; 143 [self->_offscreen bindForWidth:width andHeight:height]; 144 } 145 146 - (void)unbind 147 { 148 assert(CGLGetCurrentContext() == self->_ctx); 149 [self->_offscreen unbind]; 150 [self unsetContext]; 151 } 152 153 - (void)blit 154 { 155 [self blitForWidth:[self->_offscreen width] andHeight:[self->_offscreen height]]; 156 } 157 158 - (GLuint)texture 159 { 160 return [self->_offscreen texture]; 161 } 162 163 - (void)blitForWidth:(GLuint)width andHeight:(GLuint)height 164 { 165 { 166 #if 1 167 glClearColor(self->_backgroundR, self->_backgroundG, self->_backgroundB, self->_backgroundA); 168 glClear(GL_COLOR_BUFFER_BIT); 169 #else |