【发布时间】:2019-02-18 18:53:03
【问题描述】:
我正在尝试使用Swift 在GLKitView 上画一条黑色单线,但它不起作用。
我可以清除屏幕,我可以用任何颜色填充它,但我不能在上面画任何东西。
我知道,这个问题被问了很多次,但我在 Swift 上找不到任何答案。
此外,没有任何关于在 Swift 中使用 OpenGL ES 的信息,所以我来到了这里。
这是我的代码:
import GLKit
class MyGLKit: GLKView {
override func drawRect(rect: CGRect) {
glClearColor(0.8, 0.8, 0.8, 1.0)
glClear(GLbitfield(GL_COLOR_BUFFER_BIT))
glColor4f(0, 0, 0, 1)
var line: [GLfloat] = [-1, 0, 1, 0]
var len = line.count * sizeof(GLfloat)
// Create an handle for a buffer object array
var bufferObjectNameArray: GLuint = 0
// Have OpenGL generate a buffer name and store it in the buffer object array
glGenBuffers(1, &bufferObjectNameArray);
// Bind the buffer object array to the GL_ARRAY_BUFFER target buffer
glBindBuffer(GLbitfield(GL_ARRAY_BUFFER), bufferObjectNameArray);
// Send the line data over to the target buffer in GPU RAM
glBufferData(
GLbitfield(GL_ARRAY_BUFFER), // the target buffer
len, // the number of bytes to put into the buffer
line, // a pointer to the data being copied
GLbitfield(GL_STATIC_DRAW)); // the usage pattern of the data
glVertexPointer(2, GLbitfield(GL_FLOAT), 0, line);
var programHandle: GLuint = glCreateProgram()
glLinkProgram(programHandle)
glDrawArrays(GLbitfield(GL_LINES), 0, 2);
}
}
我不确定这段代码在做什么,因为在通常的 OpenGL 之后,OpenGL ES 看起来很疯狂。我的意思是,你需要写很多代码来画一条线。为什么会这样?
【问题讨论】:
-
您需要注释掉的
glVertexPointer()调用。但是使用0作为最后一个参数,因为如果绑定了缓冲区,这是缓冲区的偏移量。 -
@RetoKoradi 我不能使用
0作为最后一个参数,因为XCode 说Cannot invoke 'init' with an argument list of type '(IntegerLiteralConvertible, $T3, IntegerLiteralConvertible, IntegerLiteralConvertible)'。最后一个参数是指定指向数组中第一个顶点的第一个坐标的指针。所以它必须是指向数组第一个元素的指针。但是如何制作呢? -
我同意 OP,带有 swift 的 OpenGL-ES 是
insane。我只想从A点到B点画一条该死的线。为什么这需要20多行gl代码?为什么这不是原始的? +1insane