OpenGL Loop mit DisplayLink

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • Mit folgendem Code bekomme ich ein schwarzes Bild:

    Quellcode

    1. - (void)prepareOpenGL
    2. {
    3. GLint swapInt = 1;
    4. [[self openGLContext] setValues:&swapInt forParameter:NSOpenGLCPSwapInterval];
    5. CVDisplayLinkCreateWithActiveCGDisplays(&displayLink);
    6. CVDisplayLinkSetOutputCallback(displayLink, &MyDisplayLinkCallback, (__bridge void *) self);
    7. CGLContextObj cglContext = [[self openGLContext] CGLContextObj];
    8. CGLPixelFormatObj cglPixelFormat = [[self pixelFormat] CGLPixelFormatObj];
    9. CVDisplayLinkSetCurrentCGDisplayFromOpenGLContext(displayLink, cglContext, cglPixelFormat);
    10. CVDisplayLinkStart(displayLink);
    11. }
    12. - (void)renderScene
    13. {
    14. [[self openGLContext] makeCurrentContext];
    15. CGLLockContext((CGLContextObj)[[self openGLContext] CGLContextObj]);
    16. // Render - Start
    17. glClear(GL_COLOR_BUFFER_BIT);
    18. glColor3f(1.0, 0.0, 0.0);
    19. glLoadIdentity();
    20. glBegin(GL_TRIANGLES);
    21. {
    22. glVertex3f(-3.0, -3.0, -3.0);
    23. glVertex3f( 0.0, 3.0, -3.0);
    24. glVertex3f( 3.0, -3.0, -3.0);
    25. }
    26. glEnd();
    27. glFlush();
    28. // Render - Ende
    29. CGLFlushDrawable((CGLContextObj)[[self openGLContext] CGLContextObj]);
    30. CGLUnlockContext((CGLContextObj)[[self openGLContext] CGLContextObj]);
    31. }
    32. static CVReturn MyDisplayLinkCallback(CVDisplayLinkRef displayLink, const CVTimeStamp* now, const CVTimeStamp* outputTime, CVOptionFlags flagsIn, CVOptionFlags* flagsOut, void* displayLinkContext)
    33. {
    34. return [(__bridge OpenGLView *)displayLinkContext getFrameForTime:outputTime];
    35. }
    36. - (CVReturn)getFrameForTime:(const CVTimeStamp*)outputTime
    37. {
    38. @autoreleasepool
    39. {
    40. [self renderScene];
    41. }
    42. return kCVReturnSuccess;
    43. }
    Alles anzeigen
  • Jetzt funktioniert es!
    Also das glFlush(); muss bleiben.
    Und ich habe die prepareOpenGL Methode um folgendes erweitert:

    Quellcode

    1. - (void)prepareOpenGL
    2. {
    3. GLint swapInt = 1;
    4. [[self openGLContext] setValues:&swapInt forParameter:NSOpenGLCPSwapInterval];
    5. // Erweiterung
    6. NSRect rect = [self bounds];
    7. glViewport( 0, 0, (GLsizei)rect.size.width, (GLsizei)rect.size.height); glMatrixMode( GL_PROJECTION );
    8. glLoadIdentity();
    9. gluPerspective( 45.0, rect.size.width / rect.size.height, 1.0, 5.0 ); glMatrixMode( GL_MODELVIEW );
    10. glLoadIdentity();
    11. // Erweiterung
    12. CVDisplayLinkCreateWithActiveCGDisplays(&displayLink);
    13. CVDisplayLinkSetOutputCallback(displayLink, &MyDisplayLinkCallback, (__bridge void *) self);
    14. CGLContextObj cglContext = [[self openGLContext] CGLContextObj];
    15. CGLPixelFormatObj cglPixelFormat = [[self pixelFormat] CGLPixelFormatObj];
    16. CVDisplayLinkSetCurrentCGDisplayFromOpenGLContext(displayLink, cglContext, cglPixelFormat);
    17. CVDisplayLinkStart(displayLink);
    18. }
    Alles anzeigen