我试图在react本机应用中呈现opengl es本机组件。目前我有一个空白屏幕,我希望看到一个使用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
MY-定制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-定制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
任何人都可以帮忙,帮助我做这件事。
由于
亚当
答案 0 :(得分: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);
}