2

我在 Mac OS 上的目标 c 中使用 AudioVisualizer。我有 2 个 NSWindows,里面有 NSOpenGLView。

在此处输入图像描述

如果仅使用 NSOpenGLView 打开 1 个 NSWindow,它会显示没有任何问题的形状。

但是如果打开2个上面有NSOpenGLView的NSWindows,只有一个GLView绘制一个形状,但是在其他NSWindow中播放其他音频时,该形状与它的音频不匹配。

在 NSOpenGLViews 中,只要 CADisplayLink 需要显示,它就会调用 redraw 方法。

这是与OpenGLContext相关的部分。

self.openGLContext = [[NSOpenGLContext alloc] initWithFormat:self.pixelFormat
                                                shareContext:nil];

这是redraw方法

[self.openGLContext makeCurrentContext];
[self.openGLContext lock];

[self redrawWithPoints:self.info->points
            pointCount:self.info->pointCount
            baseEffect:self.baseEffect
    vertexBufferObject:self.info->vbo
     vertexArrayBuffer:self.info->vab
          interpolated:self.info->interpolated
              mirrored:self.shouldMirror
                  gain:self.gain];

[self.openGLContext flushBuffer];
[self.openGLContext unlock];
[NSOpenGLContext clearCurrentContext];

这里是 redrawWithPoints 方法

glClear(GL_COLOR_BUFFER_BIT);
GLenum mode = interpolated ? GL_TRIANGLE_STRIP : GL_LINE_STRIP;
float interpolatedFactor = interpolated ? 2.0f : 1.0f;
float xscale = 2.0f / ((float)pointCount / interpolatedFactor);
float yscale = 1.0f * gain;
GLKMatrix4 transform = GLKMatrix4MakeTranslation(-1.0f, 0.0f, 0.0f);
transform = GLKMatrix4Scale(transform, xscale, yscale, 1.0f);
baseEffect.transform.modelviewMatrix = transform;
glBindBuffer(GL_ARRAY_BUFFER, vbo);
[baseEffect prepareToDraw];
glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition,
                      2,
                      GL_FLOAT,
                      GL_FALSE,
                      sizeof(GOAudioPlotGLPoint),
                      NULL);
glDrawArrays(mode, 0, pointCount);
if (mirrored)
{
    baseEffect.transform.modelviewMatrix = GLKMatrix4Rotate(transform, M_PI, 1.0f, 0.0f, 0.0f);
    [baseEffect prepareToDraw];
    glDrawArrays(mode, 0, pointCount);
}


glFlush();

我想同时在不同的 NSOpenGLViews 中显示不同的音频可视化器。

有可能与OpenGL有关吗?

4

2 回答 2

1

您正在使用顶点缓冲区对象和顶点数组缓冲区进行绘制,但您似乎没有在两个上下文之间共享。这意味着当您在第二个上下文中使用顶点缓冲区对象时,它的 ID 在该上下文中不存在。但是,如果您使用第一个上下文作为shareContext:第二个视图创建的参数NSOpenGLContext,那么您可以在上下文之间共享对象。

它有助于glGetError()在代码中定期添加调用以提醒您注意此类问题。

于 2018-04-24T04:34:11.470 回答
1

Based on reply of @user1118321, I made NSOpenGLContextManager for managing OpenglContext.

@interface GOOpenGLContextManager : NSObject

+ (GOOpenGLContextManager*) shared;

@property (nonatomic, strong) NSOpenGLContext* context;

@end

By using it, in All NSOpenGLViews, used only one shared NSOpenGLContext.

Here is the code what I have used.

    self.openGLContext = [[NSOpenGLContext alloc] initWithFormat:self.pixelFormat
                                                    shareContext:[OpenGLContextManager shared].context];

    if ([OpenGLContextManager shared].context ==  nil) {
        [OpenGLContextManager shared].context = self.openGLContext;
    }

It works like a charm.

于 2018-04-24T09:55:57.153 回答