代码之家  ›  专栏  ›  技术社区  ›  nomann

我的“drawview”函数只被调用一次?

  •  0
  • nomann  · 技术社区  · 15 年前

    我不熟悉OpenGLES,在我的项目中有一个简单的问题。我已经在屏幕上成功绘制了一个正方形,但问题是我的eaglview中的“drawview”函数是 只调用过一次,但是我编写了代码再次调用n,就像在Xcode的OpenGLES模板中一样。

    有人知道错误在哪里吗?

    以下是我的eaglview.m文件的代码:

    #import <QuartzCore/QuartzCore.h>
    #import <OpenGLES/EAGLDrawable.h>
    
    #import "EAGLView.h"
    
    #define USE_DEPTH_BUFFER 0
    
    // A class extension to declare private methods
    @interface EAGLView ()
    @property (nonatomic, retain) EAGLContext *context;
    - (BOOL) createFramebuffer;
    - (void) destroyFramebuffer;
    @end
    
    
    @implementation EAGLView
    
    @synthesize context;
    @synthesize animating;
    @dynamic animationFrameInterval;
    
    
    // You must implement this method
    + (Class)layerClass {
        return [CAEAGLLayer class];
    }
    
    //The GL view is stored in the nib file. When it's unarchived it's sent -initWithCoder:
    - (id)initWithCoder:(NSCoder*)coder {
    
        if ((self = [super initWithCoder:coder])) {
            // Get the layer
            CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer;
    
            eaglLayer.opaque = YES;
            eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
                                            [NSNumber numberWithBool:NO], kEAGLDrawablePropertyRetainedBacking, kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat, nil];
    
            context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1];
    
            if (!context || ![EAGLContext setCurrentContext:context]) {
                [self release];
                return nil;
            }
    
            animating = FALSE;
            displayLinkSupported = FALSE;
            animationFrameInterval = 1;
            displayLink = nil;
            animationTimer = nil;
    
            // A system version of 3.1 or greater is required to use CADisplayLink. The NSTimer
            // class is used as fallback when it isn't available.
            NSString *reqSysVer = @"3.1";
            NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
            if ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending)
                displayLinkSupported = TRUE;
        }
        return self;
    }
    
    - (void)drawView {
        NSLog(@"In EAGLView's drawView func");
    
        static const GLfloat squareVertices[] = {
            -0.5f,  -0.33f,
            0.5f,  -0.33f,
            -0.5f,   0.33f,
            0.5f,   0.33f,
        };
    
        static const GLubyte squareColors[] = {
            255, 0, 0, 255,
            0, 255, 0, 255,
            0, 0, 255, 255,
            0, 0, 0, 0,
        };
    
        [EAGLContext setCurrentContext:context];
    
        glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
        glViewport(0, 0, backingWidth, backingHeight);
    
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
    
        glRotatef(10.0f, 0.0f, 0.0f, 1.0f);
    
        glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);
    
        glVertexPointer(2, GL_FLOAT, 0, squareVertices);
        glEnableClientState(GL_VERTEX_ARRAY);
        glColorPointer(4, GL_UNSIGNED_BYTE, 0, squareColors);
        glEnableClientState(GL_COLOR_ARRAY);
    
        glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
    
        glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
        [context presentRenderbuffer:GL_RENDERBUFFER_OES];
    }
    
    - (void)layoutSubviews {
        [EAGLContext setCurrentContext:context];
        [self destroyFramebuffer];
        [self createFramebuffer];
        [self drawView];
    }
    
    - (BOOL)createFramebuffer {
        glGenFramebuffersOES(1, &viewFramebuffer);
        glGenRenderbuffersOES(1, &viewRenderbuffer);
    
        glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
        glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
        [context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:(CAEAGLLayer*)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);
    
        if (USE_DEPTH_BUFFER) {
            glGenRenderbuffersOES(1, &depthRenderbuffer);
            glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthRenderbuffer);
            glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_DEPTH_COMPONENT16_OES, backingWidth, backingHeight);
            glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthRenderbuffer);
        }
    
        if(glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) != GL_FRAMEBUFFER_COMPLETE_OES) {
            NSLog(@"failed to make complete framebuffer object %x", glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES));
            return NO;
        }
    
        return YES;
    }
    
    - (void)destroyFramebuffer {
        glDeleteFramebuffersOES(1, &viewFramebuffer);
        viewFramebuffer = 0;
        glDeleteRenderbuffersOES(1, &viewRenderbuffer);
        viewRenderbuffer = 0;
    
        if(depthRenderbuffer) {
            glDeleteRenderbuffersOES(1, &depthRenderbuffer);
            depthRenderbuffer = 0;
        }
    }
    
    - (NSInteger)animationFrameInterval
    {
        return animationFrameInterval;
    }
    
    - (void)setAnimationFrameInterval:(NSInteger)frameInterval
    {
        // Frame interval defines how many display frames must pass between each time the
        // display link fires. The display link will only fire 30 times a second when the
        // frame internal is two on a display that refreshes 60 times a second. The default
        // frame interval setting of one will fire 60 times a second when the display refreshes
        // at 60 times a second. A frame interval setting of less than one results in undefined
        // behavior.
        if (frameInterval >= 1)
        {
            animationFrameInterval = frameInterval;
    
            if (animating)
            {
                [self stopAnimation];
                [self startAnimation];
            }
        }
    }
    
    - (void)startAnimation
    {
        if (!animating)
        {
            if (displayLinkSupported)
            {
                // CADisplayLink is API new to iPhone SDK 3.1. Compiling against earlier versions will result in a warning, but can be dismissed
                // if the system version runtime check for CADisplayLink exists in -initWithCoder:. The runtime check ensures this code will
                // not be called in system versions earlier than 3.1.
    
                displayLink = [NSClassFromString(@"CADisplayLink") displayLinkWithTarget:self selector:@selector(drawView)];
                [displayLink setFrameInterval:animationFrameInterval];
                [displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
            }
            else
                animationTimer = [NSTimer scheduledTimerWithTimeInterval:(NSTimeInterval)((1.0 / 60.0) * animationFrameInterval) target:self selector:@selector(drawView) userInfo:nil repeats:TRUE];
    
            animating = TRUE;
        }
    }
    
    - (void)stopAnimation
    {
        if (animating)
        {
            if (displayLinkSupported)
            {
                [displayLink invalidate];
                displayLink = nil;
            }
            else
            {
                [animationTimer invalidate];
                animationTimer = nil;
            }
    
            animating = FALSE;
        }
    }
    
    - (void)dealloc {
        if ([EAGLContext currentContext] == context) {
            [EAGLContext setCurrentContext:nil];
        }
    
        [context release];
        [super dealloc];
    }
    
    @end
    
    1 回复  |  直到 15 年前
        1
  •  0
  •   No one in particular    15 年前

    检查您的方法是否:

    - (void)startAnimation
    

    正在被呼叫。