我对 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()
函数均被弃用的原因是它们是固定函数管道的一部分。现在一切都是基于着色器的,现在你应该使用 VAOs与 VBOs (和 IBO)。
VAO 附带的功能是。
创建
- glEnableVertexAttribArray()
- glVertexAttribPointer()
渲染
- glBindVertexArray()
- glDrawArrays()
- glDrawElements()
是的,仍然使用 glDrawArrays()
和 glDrawElements()
,当您需要为 VAO 创建和绑定(bind) VBO 时,您仍然可以在相同的方法中执行此操作和以前一样。
关于macos - 在 Mac OSX 上绘制 OpenGL 顶点数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19763702/