macos - 在 Mac OSX 上绘制 OpenGL 顶点数组

标签 macos cocoa opengl

我对 OpenGL 比较陌生,希望让它在 cocoa 框架内绘制。我尝试了苹果开发者页面上的示例代码,效果非常好。然而,现在我希望能够从顶点结构中进行绘制,以便掌握这个概念。当我对 OpenGLView 使用以下代码时,我只得到一个黑色窗口(而不是一个花哨的彩色三角形......)。

#import "MyOpenGLView.h" 
#include <OpenGL/gl.h>
#include <GLUT/GLUT.h>

@implementation MyOpenGLView

    typedef struct _vertexStruct{
        GLfloat position[2];
        GLubyte color[4];
    } vertexStruct;

- (void)drawRect:(NSRect) bounds
{
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_COLOR_ARRAY);
    drawAnObject();
    glFlush();
}

static void drawAnObject()
{
    const vertexStruct vertices[] = {
        {{0.0f, 1.0f},{1.0, 0.0,0.0,1.0}},
        {{1.0f, -1.0f},{0.0, 1.0,0.0,1.0}},
        {{-1.0f , -1.0f},{0.0, 0.0,1.0,1.0}}
    };

    const GLshort indices[] = {0,1,2};
    glVertexPointer(2, sizeof(vertexStruct),0,&vertices[0].position);
    glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(vertexStruct), &vertices[0].color);
    glDrawElements(GL_TRIANGLES, sizeof(indices)/sizeof(GLshort), GL_SHORT, indices);
}

@end

我在这里缺少什么?

最佳答案

OS X 10.9 says it's running OpenGL 4.1

好吧,这就是你的问题。

虽然我不明白为什么您没有收到“访问冲突”错误,因为您应该收到“访问冲突”错误,因为您在 OpenGL 中使用了已弃用的函数。

以下函数是您正在使用的 OpenGL 3.1 版本中已弃用的一些函数。

  • glEnableClientState()
  • glVertexPointer()
  • glColorPointer()

所有 gl*Pointer() 函数均被弃用的原因是它们是固定函数管道的一部分。现在一切都是基于着色器的,现在你应该使用 VAOsVBOs (和 IBO)。

VAO 附带的功能是。

创建

  • glEnableVertexAttribArray()
  • glVertexAttribPointer()

渲染

  • glBindVertexArray()
  • glDrawArrays()
  • glDrawElements()

是的,仍然使用 glDrawArrays()glDrawElements(),当您需要为 VAO 创建和绑定(bind) VBO 时,您仍然可以在相同的方法中执行此操作和以前一样。

关于macos - 在 Mac OSX 上绘制 OpenGL 顶点数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19763702/

相关文章:

macos - 在 OSX 上加载/执行 ELF 文件的可能性

c++ - 将 SDL_Surface Blit 到另一个 SDL_Surface 并应用颜色键

python - 将 Python 3.1 与 TextMate 一起使用

objective-c - 尺寸已定义

iphone - 在 cocoa 中使用 JSON

ios - 如何注册 cocoa 应用程序以接收远程通知以更新用户界面?

opengl - 如何为 JT 3D 文件格式创建查看器

c++ - OpenGL glfw无法使用着色器绘制点

macos - NSProgressIndicator 在 El Capitan 中不起作用

macos - Mac OS X 上的 node.js npm "Error: No such module"