2016-11-22 54 views
1

我試圖在反應式本機應用程序中呈現opengl本機組件。目前我有一個空白的屏幕,我希望看到一個使用glClearColor的紅色矩形。代碼運行時沒有錯誤。 我是新來的目標c,所以有一些語法特性我可能會得到錯誤。但到目前爲止,這是我所擁有的。在反應本機中呈現開放式視圖

我定製 - 視圖 - manager.h

#import "RCTViewManager.h" 

@interface myCustomViewManager : RCTViewManager 

@end 

我定製 - 視圖 - manager.m

#import "myCustomViewManager.h" 
#import "myCustomView.h" 

#import <UIKit/UIKit.h> 

@implementation myCustomViewManager 

RCT_EXPORT_MODULE(); 

- (UIView *)view 
{ 
    myCustomView * v; 
    v = [[myCustomView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    return v; 
} 

@end 

我定製-view.h

#import <GLKit/GLKit.h> 
#import <QuartzCore/QuartzCore.h> 
#import <OpenGLES/ES2/gl.h> 
#import <OpenGLES/ES2/glext.h> 


@interface myCustomView : GLKView { 
    EAGLContext *context; 
} 

@end 

my-custom-view.m

#import "myCustomView.h" 
#import <UIKit/UIKit.h> 

@implementation myCustomView 

+ (Class)layerClass { 
    return [CAEAGLLayer class]; 
} 

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 

    if (self) { 
    CAEAGLLayer *EAGLLayer = (CAEAGLLayer *) super.layer; 
    EAGLLayer.opaque = YES; 

    context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2]; 

    if (!context || ![EAGLContext setCurrentContext:context]) { 
     NSLog(@"No context"); 
     exit(1); 
    } 

    GLuint framebuffer, renderbuffer; 
    glGenBuffers(1, &framebuffer); 
    glGenBuffers(1, &renderbuffer); 

    glBindFramebuffer(GL_FRAMEBUFFER, framebuffer); 
    glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer); 

    [context renderbufferStorage:GL_FRAMEBUFFER fromDrawable:EAGLLayer]; 

    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, renderbuffer); 

    glViewport(0, 0, 200, 200); 

    [self render]; 

    } 
    return self; 
} 

- (void)render 
{ 
    glClearColor(1.0, 0.0, 0.0, 1.0); 
    glClear(GL_COLOR_BUFFER_BIT); 

    [context presentRenderbuffer:GL_RENDERBUFFER]; 
} 

@end 

任何人都可以幫忙,並幫助我得到這個呈現的東西。

感謝

亞當

回答

1

代碼中的幾個電話都是錯誤的。你應該檢查一些工作示例。

反正這些都是需要改變線路:

glGenFramebuffers(1, &framebuffer); // change function 
glGenRenderbuffers(1, &renderbuffer); // change function 
[context renderbufferStorage:GL_RENDERBUFFER fromDrawable:EAGLLayer]; // Convert to GL_RENDERBUFFER 

但通常這不是一個GLKView程序。只需使用UIView作爲父類。

更安全的嘗試實現的東西如下:

- (void)initializeBuffers 
{ 
    CAEAGLLayer *layer = (CAEAGLLayer *)self.layer; 
    layer.opaque = YES; 
    layer.contentsScale = [UIScreen mainScreen].scale; 

    GLuint frameBuffer = 0; 
    glGenFramebuffers(1, &frameBuffer); 
    glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer); 
    self.frameBuffer = frameBuffer; 

    GLuint renderBuffer = 0; 
    glGenRenderbuffers(1, &renderBuffer); 
    glBindRenderbuffer(GL_RENDERBUFFER, renderBuffer); 
    self.renderBuffer = renderBuffer; 

    [_context renderbufferStorage:GL_RENDERBUFFER fromDrawable:layer]; 
    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, renderBuffer); 

    GLint bufferSize[2] = {0,0}; 
    glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, bufferSize); 
    glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, bufferSize+1); 
    self.bufferWidth = bufferSize[0]; 
    self.bufferHeight = bufferSize[1]; 

    if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) { 
     NSLog(@"Error creating framebuffer (error %d)", glCheckFramebufferStatus(GL_FRAMEBUFFER)); 
    } 

    glViewport(0, 0, self.bufferWidth, self.bufferHeight); 
} 
+0

乾杯是得到它的工作:) – adamdaly