|
EAGLContext* _context; |
|
GLKView* _glkView; |
|
|
|
- (UIImage*) renderImageFromSCNNode:(SCNNode*) node { |
|
|
|
int width = 640; |
|
int height = 480; |
|
|
|
glClear(GL_COLOR_BUFFER_BIT); |
|
|
|
SCNScene *scnScene = [SCNScene scene]; |
|
|
|
glViewport(0, 0, width, height); |
|
[scnScene.rootNode addChildNode: _node]; |
|
SCNRenderer *scnRender = [SCNRenderer rendererWithContext:_context options:nil]; |
|
scnRender.scene = scnScene; |
|
SCNNode *cameraNode = [SCNNode node]; |
|
SCNCamera *mycamera = [SCNCamera camera]; |
|
cameraNode.camera = mycamera; |
|
cameraNode.position = SCNVector3Make(0, 0, 0); |
|
|
|
GLKMatrix4 mat = GLKMatrix4MakeOrtho(-1, 1, |
|
-5, 5, |
|
0.01, 100); // z range arbitrary |
|
[cameraNode.camera setProjectionTransform:SCNMatrix4FromGLKMatrix4(mat)]; |
|
scnRender.pointOfView = cameraNode; |
|
scnRender.delegate = self; |
|
[scnRender render]; |
|
|
|
GLint backingWidth, backingHeight; |
|
|
|
glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &backingWidth); |
|
glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &backingHeight); |
|
|
|
|
|
NSInteger x = 0, y = 0; |
|
NSInteger dataLength = width * height * 4; |
|
GLubyte *data = (GLubyte*)malloc(dataLength * sizeof(GLubyte)); |
|
|
|
glPixelStorei(GL_PACK_ALIGNMENT, 4); |
|
glReadPixels(x, y, width, height, GL_RGBA, GL_UNSIGNED_BYTE, data); |
|
|
|
CGDataProviderRef ref = CGDataProviderCreateWithData(NULL, data, dataLength, NULL); |
|
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB(); |
|
CGImageRef iref = CGImageCreate(width, height, 8, 32, width * 4, colorspace, kCGBitmapByteOrder32Big | kCGImageAlphaPremultipliedLast, |
|
ref, NULL, true, kCGRenderingIntentDefault); |
|
|
|
UIGraphicsBeginImageContext(CGSizeMake(width, height)); |
|
CGContextRef cgcontext = UIGraphicsGetCurrentContext(); |
|
CGContextSetBlendMode(cgcontext, kCGBlendModeCopy); |
|
CGContextDrawImage(cgcontext, CGRectMake(0.0, 0.0, width, height), iref); |
|
UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); |
|
UIGraphicsEndImageContext(); |
|
|
|
free(data); |
|
CFRelease(ref); |
|
CFRelease(colorspace); |
|
CGImageRelease(iref); |
|
|
|
[EAGLContext setCurrentContext:nil]; |
|
|
|
return image; |
|
} |
|
|
|
- (void) initEAGLContext { |
|
|
|
_context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2]; |
|
int width = 640; |
|
int height = 480; |
|
|
|
// [EAGLContext setCurrentContext: _context]; |
|
// |
|
// glGenFramebuffers(1, &framebuffer); |
|
// glBindFramebuffer(GL_FRAMEBUFFER, framebuffer); |
|
// |
|
// glGenRenderbuffers(1, &colorRenderbuffer); |
|
// glBindRenderbuffer(GL_RENDERBUFFER, colorRenderbuffer); |
|
// glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, width, height); |
|
// glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, colorRenderbuffer); |
|
// |
|
// glGenRenderbuffers(1, &depthRenderbuffer); |
|
// glBindRenderbuffer(GL_RENDERBUFFER, depthRenderbuffer); |
|
// glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, width, height); |
|
// glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthRenderbuffer); |
|
// |
|
// GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER) ; |
|
// if(status != GL_FRAMEBUFFER_COMPLETE) { |
|
// NSLog(@"failed to make complete framebuffer object %x", status); |
|
// } |
|
|
|
// These three lines are needed to make it work |
|
_glkView = [[GLKView alloc] initWithFrame:CGRectMake(0, 0, 640, 480)]; |
|
_glkView.context = _context; |
|
[_glkView display]; |
|
|
|
} |