0

我实现了用于绘制图像的 iOS 示例(GLSprite),但它没有显示任何内容。NSLogs 告诉我,一切都已初始化,所以我不知道,为什么什么都没有显示。

我想加载一个 png 文件并旋转它。

  • 上下文加载
  • 帧缓冲区创建
  • setupView 完成
  • drawView 完成

没有错误。除了这个黑屏之外,一切似乎都很好:)

有人知道为什么吗?

h文件

#import <Foundation/Foundation.h>
#import <OpenGLES/ES1/gl.h>
#import <OpenGLES/ES1/glext.h>

@interface CJTEAGLView : UIView
{
GLint backingWidth;
GLint backingHeight;

EAGLContext *context;

GLuint viewRenderbuffer;
GLuint viewFramebuffer;
GLuint viewDepthRenderbuffer;

GLuint playerTexture;

BOOL animating;
BOOL displayLinkSupported;

NSInteger animationFrameInterval;

id displayLink;
}

@property (nonatomic) BOOL animating;
@property (nonatomic) NSInteger animationFrameInterval;

- (void)startAnimation;
- (void)stopAnimation;
- (void)drawView;

@end

m文件

@interface CJTEAGLView()
- (void)createFramebuffer;
 - (void)destroyFramebuffer;
- (void)setupView;

@end

const GLfloat spriteVertices[] =
{
-0.5f, -0.5f,
0.5f, -0.5f,
-0.5f,  0.5f,
0.5f,  0.5f,
};

const GLshort spriteTexcoords[] =
{
0, 0,
1, 0,
0, 1,
1, 1,
};

@implementation CJTEAGLView

@synthesize animating;
@synthesize animationFrameInterval;

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

- (id)initWithCoder:(NSCoder*)coder
{
if(self = [super initWithCoder:coder])
{
    CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer;

    eaglLayer.opaque = YES;
    eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
                                    [NSNumber numberWithBool:FALSE], kEAGLDrawablePropertyRetainedBacking, kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat, nil];

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

    if(!context || ![EAGLContext setCurrentContext:context])
    {
        NSLog(@"Context failed");

        [self release];

        return nil;
    }

    animating = FALSE;
    displayLinkSupported = TRUE;
    animationFrameInterval = 1;
    displayLink = nil;

    [self createFramebuffer];
    [self setupView];
    [self drawView];
}

return self;
}

- (void)layoutSubviews
{
[EAGLContext setCurrentContext:context];

[self destroyFramebuffer];
[self createFramebuffer];
[self drawView];
}

- (void)createFramebuffer
{
glGenFramebuffersOES(1, &viewFramebuffer);
glGenRenderbuffersOES(1, &viewRenderbuffer);

glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
[context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:(id<EAGLDrawable>)self.layer];
glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, viewRenderbuffer);

glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth);
glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight);
}

- (void)destroyFramebuffer
{
glDeleteFramebuffers(1, &viewFramebuffer);
glDeleteRenderbuffers(1, &viewRenderbuffer);
glDeleteRenderbuffers(1, &viewDepthRenderbuffer);
}

- (NSInteger) animationFrameInterval
{
return animationFrameInterval;
}

- (void) setAnimationFrameInterval:(NSInteger)frameInterval
{
if (frameInterval >= 1)
{
    animationFrameInterval = frameInterval;

    if (animating)
    {
        [self stopAnimation];
        [self startAnimation];
    }
}
}

- (void) startAnimation
{
if (!animating)
{
    if (displayLinkSupported)
    {
        displayLink = [NSClassFromString(@"CADisplayLink") displayLinkWithTarget:self selector:@selector(drawView)];
        [displayLink setFrameInterval:animationFrameInterval];
        [displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    }

    animating = TRUE;
}
}

- (void)stopAnimation
{
if (animating)
{
    if (displayLinkSupported)
    {
        [displayLink invalidate];
        displayLink = nil;
    }

    animating = FALSE;
}
}

- (void)setupView
{
CGImageRef playerImage;
CGContextRef playerContext;
GLubyte *playerData;
size_t playerWidth;
size_t playerHeight;

glViewport(0, 0, backingWidth, backingHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrthof(-1.0f, 1.0f, -1.5f, 1.5f, -1.0f, 1.0f);
glMatrixMode(GL_MODELVIEW);
glClearColor(0, 0, 0, 1);

playerImage = [UIImage imageNamed:@"Cruiser.png"].CGImage;
playerWidth = CGImageGetWidth(playerImage);
playerHeight = CGImageGetHeight(playerImage);

if(playerImage)
{
    playerData = (GLubyte *) calloc(playerWidth * playerHeight * 4, sizeof(GLubyte));
    playerContext = CGBitmapContextCreate(playerData, playerWidth, playerHeight, 8, playerWidth * 4, CGImageGetColorSpace(playerImage), kCGImageAlphaPremultipliedLast);
    CGContextDrawImage(playerContext, CGRectMake(0, 0, (CGFloat)playerWidth, (CGFloat)playerHeight), playerImage);
    CGContextRelease(playerContext);

    glGenTextures(1, &playerTexture);
    glBindTexture(GL_TEXTURE_2D, playerTexture);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, playerWidth, playerHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, playerData);

    free(playerData);

    glEnable(GL_TEXTURE_2D);
    glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
    glEnable(GL_BLEND);
}
}

- (void)drawView
{
[EAGLContext setCurrentContext:context];

glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
glRotatef(3.0f, 0.0f, 0.0f, 1.0f);

glClear(GL_COLOR_BUFFER_BIT);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
[context presentRenderbuffer:GL_RENDERBUFFER_OES];
}

@end
4

0 回答 0