如何将此 OpenGL 指针数学转换为 Swift?
How do I convert this OpenGL pointer math to Swift?
我正在关注 this tutorial 关于 iOS 的 OpenGL / GLKit,但试图在 Swift 中实现它。在我到达这一部分之前一切顺利:
- (void)render {
// 1
self.effect.texture2d0.name = self.textureInfo.name;
self.effect.texture2d0.enabled = YES;
// 2
[self.effect prepareToDraw];
// 3
glEnableVertexAttribArray(GLKVertexAttribPosition);
glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
//---------------- This is where things break down...
long offset = (long)&_quad;
glVertexAttribPointer(GLKVertexAttribPosition, 2, GL_FLOAT, GL_FALSE, sizeof(TexturedVertex), (void *) (offset + offsetof(TexturedVertex, geometryVertex)));
glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, sizeof(TexturedVertex), (void *) (offset + offsetof(TexturedVertex, textureVertex)));
// 5
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
@end
我有一个 属性 self.quad
是一个 Swift 结构,如下所示:
struct TexturedVertex {
let geometryVertex: CGPoint
let textureVertex: CGPoint
}
struct TexturedQuad {
let bottomLeft: TexturedVertex
let bottomRight: TexturedVertex
let topLeft: TexturedVertex
let topRight: TexturedVertex
}
这是一个相当简单的结构,但是我不知道如何将它传递给glVertexAttribPointer
的最后一个参数。任何帮助都会很棒!
在Swift中,您可以使用通用结构UnsafePointer
来执行指针运算和转换。 Swift 没有 offsetof
,但您可以通过直接获取 geometryVertex
和 textureVertex
元素的 UnsafePointer
来干净地解决这个问题。
以下代码在 iOS 游乐场中编译。我还没有测试过它,但我认为它会起作用:
import OpenGLES
import GLKit
struct TexturedVertex {
var geometryVertex = GLKVector2()
var textureVertex = GLKVector2()
}
struct TexturedQuad {
var bl = TexturedVertex()
var br = TexturedVertex()
var tl = TexturedVertex()
var tr = TexturedVertex()
init() { }
}
var _quad = TexturedQuad()
withUnsafePointer(&_quad.bl.geometryVertex) { (pointer) -> Void in
glVertexAttribPointer(GLuint(GLKVertexAttrib.Position.rawValue),
2, GLenum(GL_FLOAT), GLboolean(GL_FALSE),
GLsizei(sizeof(TexturedVertex)), pointer)
}
withUnsafePointer(&_quad.bl.textureVertex) { (pointer) -> Void in
glVertexAttribPointer(GLuint(GLKVertexAttrib.TexCoord0.rawValue),
2, GLenum(GL_FLOAT), GLboolean(GL_FALSE),
GLsizei(sizeof(TexturedVertex)), pointer)
}
顺便说一句,使用 CGPoint
你在问题中所做的方式是危险的,因为 CGFloat
会根据你的目标(32 位或 64 位)改变大小,但是 GL_FLOAT
总是表示 32 位。您正在学习的教程是在 64 位 iOS 出现之前编写的。
我正在关注 this tutorial 关于 iOS 的 OpenGL / GLKit,但试图在 Swift 中实现它。在我到达这一部分之前一切顺利:
- (void)render {
// 1
self.effect.texture2d0.name = self.textureInfo.name;
self.effect.texture2d0.enabled = YES;
// 2
[self.effect prepareToDraw];
// 3
glEnableVertexAttribArray(GLKVertexAttribPosition);
glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
//---------------- This is where things break down...
long offset = (long)&_quad;
glVertexAttribPointer(GLKVertexAttribPosition, 2, GL_FLOAT, GL_FALSE, sizeof(TexturedVertex), (void *) (offset + offsetof(TexturedVertex, geometryVertex)));
glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, sizeof(TexturedVertex), (void *) (offset + offsetof(TexturedVertex, textureVertex)));
// 5
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
@end
我有一个 属性 self.quad
是一个 Swift 结构,如下所示:
struct TexturedVertex {
let geometryVertex: CGPoint
let textureVertex: CGPoint
}
struct TexturedQuad {
let bottomLeft: TexturedVertex
let bottomRight: TexturedVertex
let topLeft: TexturedVertex
let topRight: TexturedVertex
}
这是一个相当简单的结构,但是我不知道如何将它传递给glVertexAttribPointer
的最后一个参数。任何帮助都会很棒!
在Swift中,您可以使用通用结构UnsafePointer
来执行指针运算和转换。 Swift 没有 offsetof
,但您可以通过直接获取 geometryVertex
和 textureVertex
元素的 UnsafePointer
来干净地解决这个问题。
以下代码在 iOS 游乐场中编译。我还没有测试过它,但我认为它会起作用:
import OpenGLES
import GLKit
struct TexturedVertex {
var geometryVertex = GLKVector2()
var textureVertex = GLKVector2()
}
struct TexturedQuad {
var bl = TexturedVertex()
var br = TexturedVertex()
var tl = TexturedVertex()
var tr = TexturedVertex()
init() { }
}
var _quad = TexturedQuad()
withUnsafePointer(&_quad.bl.geometryVertex) { (pointer) -> Void in
glVertexAttribPointer(GLuint(GLKVertexAttrib.Position.rawValue),
2, GLenum(GL_FLOAT), GLboolean(GL_FALSE),
GLsizei(sizeof(TexturedVertex)), pointer)
}
withUnsafePointer(&_quad.bl.textureVertex) { (pointer) -> Void in
glVertexAttribPointer(GLuint(GLKVertexAttrib.TexCoord0.rawValue),
2, GLenum(GL_FLOAT), GLboolean(GL_FALSE),
GLsizei(sizeof(TexturedVertex)), pointer)
}
顺便说一句,使用 CGPoint
你在问题中所做的方式是危险的,因为 CGFloat
会根据你的目标(32 位或 64 位)改变大小,但是 GL_FLOAT
总是表示 32 位。您正在学习的教程是在 64 位 iOS 出现之前编写的。