objective-c - NSOpengl View 不起作用

标签 objective-c macos cocoa opengl

嗨,我一直在尝试使用 NSOpenGLView 在 cocoa 中制作我的第一个 opengl“应用程序”。我想用蓝色清除背景并绘制红点,但 View 是白色的。而且它不会画红点。也许我应该将其与核心视频一起使用以循环刷新它。这是“OpenGL Superbible”书中的代码,所以我认为这是 cocoa 的错。

#import <Cocoa/Cocoa.h>

@interface View : NSOpenGLView

@end

//---------------------------------

#import "View.h"
#include <OpenGL/gl3.h>

@implementation View

GLuint rendering_program;
GLuint VAO;


-(void)awakeFromNib{

    NSOpenGLPixelFormatAttribute pixelFormatAttributes[] =
    {
        NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core,
        NSOpenGLPFAColorSize    , 24                           ,
        NSOpenGLPFAAlphaSize    , 8                            ,
        NSOpenGLPFADoubleBuffer ,
        NSOpenGLPFAAccelerated  ,
        NSOpenGLPFANoRecovery   ,
        0
    };
    NSOpenGLPixelFormat *pixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:pixelFormatAttributes] ;
    NSOpenGLContext* glc = [[NSOpenGLContext  alloc]initWithFormat:pixelFormat shareContext:nil];
    [self setOpenGLContext:glc];


}


-(void)prepareOpenGL{
    GLuint vs;
    GLuint fs;
    const GLchar*vss[] = {
      "#version 330 core \n"
    "void main (void)\n"
    "{\n"
    "gl_Position = vec4(0.0,0.0,0.5,1.0);\n"
    "}\n"

    };

    vs = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vs, 1, vss, 0);
    glCompileShader(vs);
    int s;
    glGetShaderiv(vs, GL_COMPILE_STATUS, &s);
    if(!s)printf("ok");

    const GLchar*fss[] = {
        "#version 330 core \n"
        "out vec4 color;"
        "void main (void)\n"
        "{\n"
        "color = vec4(1.0,0.0,0.0,1.0);\n"
        "}\n"

    };

    fs = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fs, 1,fss, 0);
    glCompileShader(fs);
    int k;
    glGetShaderiv(fs, GL_COMPILE_STATUS, &k);
    if(!k)printf("okkk");

    rendering_program = glCreateProgram();
    glAttachShader(rendering_program,vs );
    glAttachShader(rendering_program,fs );
    glLinkProgram(rendering_program);
    int n;
    glGetProgramiv(rendering_program, GL_LINK_STATUS, &n);
    glDeleteShader(vs);
    glDeleteShader(fs);
    glGenVertexArrays(1, &VAO);
    glBindVertexArray(VAO);













}

-(void)drawRect:(NSRect)dirtyRect{
    glPointSize(40);
    glClearColor(0, 0, 1, 1);
    glClear(GL_COLOR_BUFFER_BIT);
    glUseProgram(rendering_program);
    glDrawArrays(GL_POINTS, 0, 1);
    glFlush();


}

@end

这是屏幕截图: enter image description here

最佳答案

由于您使用双缓冲(NSOpenGLPFADoubleBuffer),因此必须在渲染后交换缓冲区:

[[self openGLContext] flushBuffer]

如果没有双缓冲(单缓冲区),glFlush() 就足够了。
另请参阅glFlush() vs [[self openGLContext] flushBuffer]

关于objective-c - NSOpengl View 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50900027/

相关文章:

objective-c - 激活 WebView 键盘事件

c++ - CUDA、Qt creator 和 Mac

objective-c - 如何更改 NSOutlineView 中节点的样式?

objective-c - Cocoa Binding 仅用于检索

objective-c - NSMutableString 究竟是如何工作的?

ios - 用数据填充核心数据模型

ios - 应用程序在后台时深层链接不起作用?

java - 在桌面上从 Objective-C 启动一个进程

c++ - 在 Mac 上为 Interactive Broker API 使用 C++? - 例子?

python - 我如何在 mac 上重新安装 python,但版本 3.2(最新)并让所有命令行工作正常