How do I convert this OpenGL pointer math to Swift?

早过忘川 提交于 2019-12-05 00:35:33

问题


I’m following this tutorial about OpenGL / GLKit for iOS but trying to implement it in Swift. It’s going fine until I get to this part:

- (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

I have a property self.quad that’s a Swift struct like this:

struct TexturedVertex {
let geometryVertex: CGPoint
let textureVertex: CGPoint
}

struct TexturedQuad {

let bottomLeft: TexturedVertex
let bottomRight: TexturedVertex
let topLeft: TexturedVertex
let topRight: TexturedVertex
}

It’s a fairly straightforward structure, but I don’t know how to pass it to the last argument of glVertexAttribPointer. Any help would be stellar!


回答1:


In Swift, you can use the generic struct UnsafePointer to perform pointer arithmetic and casting. Swift doesn't have offsetof, but you can work around that cleanly by taking UnsafePointers of the geometryVertex and textureVertex elements directly.

The following code compiles in an iOS playground. I haven't tested it beyond that but I think it'll work:

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)
}

By the way, using CGPoint the way you did in your question is dangerous, because CGFloat changes size depending on your target (32-bit or 64-bit), but GL_FLOAT always means 32-bit. The tutorial you're following was written before 64-bit iOS came out.



来源:https://stackoverflow.com/questions/27995833/how-do-i-convert-this-opengl-pointer-math-to-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!