objective-c - 在不同的NSWindow中的NSOpenGLView上绘制不同的形状

标签 objective-c macos opengl audio nsopenglview

我在 Mac OS 上的 Objective C 中使用 AudioVisualizer。 我有 2 个 NSWindows,里面有 NSOpenGLView。

enter image description here

如果仅使用 NSOpenGLView 打开 1 个 NSWindow,则显示形状不会出现任何问题。

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

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

这里是与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();

我想同时在不同的 NSOpenGLView 中显示不同的音频可视化工具。

可以用OpenGL来做吗?

最佳答案

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

它可以帮助您在代码中定期添加对 glGetError() 的调用,以提醒您此类问题。

关于objective-c - 在不同的NSWindow中的NSOpenGLView上绘制不同的形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49986990/

相关文章:

ios - 推送 segues 只能由 UINavigationController 错误使用

swift - PromsieKit + Alamofire 用于加载分页 HTTP 数据

ios - 在没有 Init() 的情况下创建 UIView 子类 - Swift?

c++ - std::vector 和 VBO 仅渲染最后一个形状

c++ - opengl——在 Ubuntu 上设置 glad

objective-c - 模态视图 Controller 调用其他的 "viewDidLoad"

iphone - 当观察者变为零时观察者会自动移除吗?

objective-c - 如何在 Mac OS X 中获取带有显示 ID 的显示名称?

css - 如何使用 Visual Studio Code(在 Mac 上)禁用 HTML 文件的 CSS 错误

optimization - GLSL-是否优化了纹理查找?