Leeres Fenster bei OpenGL

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

  • Leeres Fenster bei OpenGL

    Hallo Entwickler,

    wieso bekomme ich bei folgendem Code nur ein weißes Fenster?

    Quellcode

    1. #import "OpenGLView.h"
    2. @interface OpenGLView()
    3. - (CVReturn)getFrameForTime:(const CVTimeStamp*)outputTime;
    4. @end
    5. static const GLfloat vertexBufferData[] =
    6. {
    7. -1.0, -1.0, 0.0,
    8. 1.0, -1.0, 0.0,
    9. 0.0, 1.0, 0.0
    10. };
    11. @implementation OpenGLView
    12. - (void)prepareOpenGL
    13. {
    14. GLint swapInt = 1;
    15. [[self openGLContext] setValues:&swapInt forParameter:NSOpenGLCPSwapInterval];
    16. CVDisplayLinkCreateWithActiveCGDisplays(&displayLink);
    17. CVDisplayLinkSetOutputCallback(displayLink, &MyDisplayLinkCallback, (__bridge void *) self);
    18. CGLContextObj cglContext = [[self openGLContext] CGLContextObj];
    19. CGLPixelFormatObj cglPixelFormat = [[self pixelFormat] CGLPixelFormatObj];
    20. CVDisplayLinkSetCurrentCGDisplayFromOpenGLContext(displayLink, cglContext, cglPixelFormat);
    21. CVDisplayLinkStart(displayLink);
    22. // Init GL
    23. NSRect rect = self.bounds;
    24. glViewport(0, 0, (GLsizei)rect.size.width, (GLsizei)rect.size.height);
    25. glMatrixMode(GL_PROJECTION);
    26. glLoadIdentity();
    27. gluPerspective(45.0, rect.size.width / rect.size.height, 1.0, 5.0);
    28. glMatrixMode(GL_MODELVIEW);
    29. glLoadIdentity();
    30. glEnable(GL_DEPTH_TEST);
    31. glCullFace(GL_BACK);
    32. glEnable(GL_CULL_FACE);
    33. // Init Buffers
    34. glGenBuffers(1, &vertexBuffer);
    35. glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
    36. glBufferData(GL_ARRAY_BUFFER, sizeof(vertexBufferData), vertexBufferData, GL_STATIC_DRAW);
    37. }
    38. - (void)reshape
    39. {
    40. NSRect rect = self.bounds;
    41. glViewport(0, 0, (GLsizei)rect.size.width, (GLsizei)rect.size.height); glMatrixMode(GL_PROJECTION);
    42. glLoadIdentity();
    43. gluPerspective(45.0, rect.size.width / rect.size.height, 1.0, 5.0 ); glMatrixMode(GL_MODELVIEW);
    44. glLoadIdentity();
    45. }
    46. - (void)renderScene
    47. {
    48. [[self openGLContext] makeCurrentContext];
    49. CGLLockContext((CGLContextObj)[[self openGLContext] CGLContextObj]);
    50. // Render - Start
    51. glEnableVertexAttribArray(0);
    52. glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
    53. glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*) 0);
    54. glDrawArrays(GL_TRIANGLES, 0, 3);
    55. glDisableVertexAttribArray(0);
    56. // Render - Ende
    57. CGLFlushDrawable((CGLContextObj)[[self openGLContext] CGLContextObj]);
    58. CGLUnlockContext((CGLContextObj)[[self openGLContext] CGLContextObj]);
    59. }
    60. static CVReturn MyDisplayLinkCallback(CVDisplayLinkRef displayLink, const CVTimeStamp* now, const CVTimeStamp* outputTime, CVOptionFlags flagsIn, CVOptionFlags* flagsOut, void* displayLinkContext)
    61. {
    62. return [(__bridge OpenGLView *)displayLinkContext getFrameForTime:outputTime];
    63. }
    64. - (CVReturn)getFrameForTime:(const CVTimeStamp*)outputTime
    65. {
    66. @autoreleasepool
    67. {
    68. [self renderScene];
    69. }
    70. return kCVReturnSuccess;
    71. }
    72. @end
    Alles anzeigen


    Im Header ist der vertexBuffer und der Display Link.
  • Langsam bin ich auch am Ende :(

    @zerm
    So besser?

    Quellcode

    1. #import "OpenGLView.h"
    2. #import "ShaderManager.h"
    3. @interface OpenGLView()
    4. - (CVReturn)getFrameForTime:(const CVTimeStamp*)outputTime;
    5. @end
    6. static const GLfloat vertexBufferData[] =
    7. {
    8. -1.0, -1.0, 0.0,
    9. 1.0, -1.0, 0.0,
    10. 0.0, 1.0, 0.0
    11. };
    12. @implementation OpenGLView
    13. - (void)prepareOpenGL
    14. {
    15. GLint swapInt = 1;
    16. [[self openGLContext] setValues:&swapInt forParameter:NSOpenGLCPSwapInterval];
    17. CVDisplayLinkCreateWithActiveCGDisplays(&displayLink);
    18. CVDisplayLinkSetOutputCallback(displayLink, &MyDisplayLinkCallback, (__bridge void *) self);
    19. CGLContextObj cglContext = [[self openGLContext] CGLContextObj];
    20. CGLPixelFormatObj cglPixelFormat = [[self pixelFormat] CGLPixelFormatObj];
    21. CVDisplayLinkSetCurrentCGDisplayFromOpenGLContext(displayLink, cglContext, cglPixelFormat);
    22. CVDisplayLinkStart(displayLink);
    23. // Init GL
    24. NSRect rect = self.bounds;
    25. glClearColor(0.0, 0.0, 0.0, 1.0);
    26. glViewport(0, 0, (GLsizei)rect.size.width, (GLsizei)rect.size.height);
    27. glEnable(GL_DEPTH_TEST);
    28. glCullFace(GL_BACK);
    29. glEnable(GL_CULL_FACE);
    30. // Init Buffers
    31. glGenBuffers(1, &vertexBuffer);
    32. glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
    33. glBufferData(GL_ARRAY_BUFFER, sizeof(vertexBufferData), vertexBufferData, GL_STATIC_DRAW);
    34. // Init Shader
    35. manager = [[ShaderManager alloc] init];
    36. if (![manager createShader:@"MyShader" vertexShader:[[NSBundle mainBundle] pathForResource:@"SimpleVertexShader" ofType:@"glsl"] fragmentShader:[[NSBundle mainBundle]pathForResource:@"SimpleFragmentShader" ofType:@"glsl"]])
    37. {
    38. NSLog(@"Fehler beim Shader laden.");
    39. }
    40. }
    41. - (void)reshape
    42. {
    43. NSRect rect = self.bounds;
    44. glViewport(0, 0, (GLsizei)rect.size.width, (GLsizei)rect.size.height);
    45. gluPerspective(45.0, rect.size.width / rect.size.height, 1.0, 5.0);
    46. }
    47. - (void)renderScene
    48. {
    49. [[self openGLContext] makeCurrentContext];
    50. CGLLockContext((CGLContextObj)[[self openGLContext] CGLContextObj]);
    51. // Render - Start
    52. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    53. [manager useShader:@"MyShader"];
    54. glEnableVertexAttribArray(0);
    55. glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
    56. glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*) 0);
    57. glDrawArrays(GL_TRIANGLES, 0, 3);
    58. glDisableVertexAttribArray(0);
    59. // Render - Ende
    60. CGLFlushDrawable((CGLContextObj)[[self openGLContext] CGLContextObj]);
    61. CGLUnlockContext((CGLContextObj)[[self openGLContext] CGLContextObj]);
    62. }
    63. static CVReturn MyDisplayLinkCallback(CVDisplayLinkRef displayLink, const CVTimeStamp* now, const CVTimeStamp* outputTime, CVOptionFlags flagsIn, CVOptionFlags* flagsOut, void* displayLinkContext)
    64. {
    65. return [(__bridge OpenGLView *)displayLinkContext getFrameForTime:outputTime];
    66. }
    67. - (CVReturn)getFrameForTime:(const CVTimeStamp*)outputTime
    68. {
    69. @autoreleasepool
    70. {
    71. [self renderScene];
    72. }
    73. return kCVReturnSuccess;
    74. }
    75. @end
    Alles anzeigen

    Und ja die Shader werden richtig geladen.

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von Vyax ()

  • Lies doch bitte die posts auch in Deinen anderen threads und schau Dir die Doku an. Da steht, dass Du glFlush nicht benutzen sollst (stattdessen CGLFlushDrawable.
    If you set the swap interval attribute (kCGLCPSwapInterval) appropriately, the copy takes place during the vertical retrace of the display, rather than immediately after CGLFlushDrawable is called. An implicit glFlush operation is performed by CGLFlushDrawable before it returns. For optimal performance, an application should not call glFlush immediately before calling CGLFlushDrawable. Subsequent OpenGL commands can be issued immediately after calling CGLFlushDrawable, but are not executed until the buffer copy is completed.