【发布时间】:2015-12-23 03:57:48
【问题描述】:
我正在阅读 Ray Wenderlich 的 OpenGL for iOS 教程,试图将他的代码从 Objective-C 转换为 Swift。
我对 OpenGL 和 Swift 非常陌生,我相信我的问题与我如何翻译 Objective-C 有关。原因如下:
在用于设置包含 OpenGL 内容的视图的 swift 文件中,在最后的逻辑步骤(调用 glDrawElements)中,应用程序将崩溃并出现 EXC_BAD_ACCESS 警报。 但是,如果我将这部分代码移动到一个 Objective-C 文件中,该应用程序将按预期工作。
此代码的 Swift 版本:
var positionDataOffset: Int = 0
glVertexAttribPointer(self.positionSlot, 3 as GLint, GL_FLOAT.asUnsigned(),
GLboolean.convertFromIntegerLiteral(UInt8(GL_FALSE)),
VertexDataSource.sizeOfVertex(), &positionDataOffset)
var colorDataOffset = (sizeof(Float) * 3) as AnyObject
glVertexAttribPointer(self.positionSlot, 4 as GLint, GL_FLOAT.asUnsigned(),
GLboolean.convertFromIntegerLiteral(UInt8(GL_FALSE)),
VertexDataSource.sizeOfVertex(), VertexDataSource.vertexBufferOffset())
var vertexOffset: Int = 0
glDrawElements(GL_TRIANGLES.asUnsigned(), VertexDataSource.vertexCount(),
GL_UNSIGNED_BYTE.asUnsigned(), &vertexOffset)
这里是 Objective-C 版本:
glVertexAttribPointer(position, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
glVertexAttribPointer(color, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex),
(GLvoid*) (sizeof(float) * 3));
glDrawElements(GL_TRIANGLES, sizeof(Indices)/sizeof(Indices[0]), GL_UNSIGNED_BYTE, 0);
如您所见,Swift 更加冗长...我和其他人一样是新手。 :)
另一个注意事项:在 Swift 版本中,您会看到对类 VertexDataSource 的类方法的多次调用。本质上,我无法确定如何将 Objective-C 的某些部分转换为 swift,因此决定(现在)在 Objective-C 中创建一个可以为 Swift 代码提供这些属性的小类。以下是 Objective-C 中的那些方法:
+ (GLint)sizeOfVertex {
return sizeof(Vertex);
}
+ (GLint)sizeOfIndices {
return sizeof(Indices);
}
+ (GLint)sizeOfIndicesAtPositionZero {
return sizeof(Indices[0]);
}
+ (GLint)sizeOfVertices {
return sizeof(Vertices);
}
+ (GLvoid *)vertexBufferOffset {
return (GLvoid *)(sizeof(float) * 3);
}
+ (GLint)vertexCount {
return self.sizeOfIndices / sizeof(GLubyte);
}
任何将这些行翻译成 Swift 的帮助都会很棒。
编辑#1
正如 Reto Koradi 所指出的,上面的 Swift 代码引用了 self.positionSlot 两次,而不是使用 colorSlot。这是我在这里发布代码时犯的一个错误,实际上并不是我的代码中的错误。
所以问题依然存在。
更新的斯威夫特:
var positionDataOffset: Int = 0
glVertexAttribPointer(self.positionSlot, 3 as GLint, GL_FLOAT.asUnsigned(),
GLboolean.convertFromIntegerLiteral(UInt8(GL_FALSE)),
VertexDataSource.sizeOfVertex(), &positionDataOffset)
var colorDataOffset = (sizeof(Float) * 3) as AnyObject
glVertexAttribPointer(self.colorSlot, 4 as GLint, GL_FLOAT.asUnsigned(),
GLboolean.convertFromIntegerLiteral(UInt8(GL_FALSE)),
VertexDataSource.sizeOfVertex(), VertexDataSource.vertexBufferOffset())
var vertexOffset: Int = 0
glDrawElements(GL_TRIANGLES.asUnsigned(), VertexDataSource.vertexCount(),
GL_UNSIGNED_BYTE.asUnsigned(), &vertexOffset)
编辑 #2:已解决
我最终解决了这个问题。我的问题是我将 Objective-C 转换为 Swift 在某些情况下是不正确的。为简洁起见,我将发布我最初关心的部分的 Swift 代码的最终版本,但您可以在 this example GitHub repo 中查看工作结果的完整源代码。
最终的 Swift 代码:
let positionSlotFirstComponent: CConstVoidPointer = COpaquePointer(UnsafePointer<Int>(0))
glVertexAttribPointer(self.positionSlot, 3 as GLint, GL_FLOAT.asUnsigned(),
GLboolean.convertFromIntegerLiteral(UInt8(GL_FALSE)), Int32(sizeof(Vertex)),
positionSlotFirstComponent)
let colorSlotFirstComponent: CConstVoidPointer = COpaquePointer(UnsafePointer<Int>(sizeof(Float) * 3))
glVertexAttribPointer(self.colorSlot, 4 as GLint, GL_FLOAT.asUnsigned(),
GLboolean.convertFromIntegerLiteral(UInt8(GL_FALSE)), Int32(sizeof(Vertex)),
colorSlotFirstComponent)
let vertextBufferOffset: CConstVoidPointer = COpaquePointer(UnsafePointer<Int>(0))
glDrawElements(GL_TRIANGLES.asUnsigned(), Int32(GLfloat(sizeofValue(Indices)) /
GLfloat(sizeofValue(Indices.0))), GL_UNSIGNED_BYTE.asUnsigned(), vertextBufferOffset)
我将继续接受 Reto Koradi 的回答,因为它确实让我走上了正确的道路。
【问题讨论】:
-
嗨,
let vertextBufferOffset: CConstVoidPointer = COpaquePointer(UnsafePointer<Int>(0)) 似乎不再起作用了。你会碰巧知道其他选择吗?提前致谢。 -
不要使用那些冗长的
convertFrom...函数,试试GLboolean(GL_FALSE) -
还有
GLenum(GL_TRIANGLES) -
我正在用同样的教程学习OpenGL,我在swift上也有问题。
标签: ios objective-c opengl-es swift