Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/107.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在Swift 3中使用GLVertexAttributePointer函数?_Ios_Opengl Es_Swift3 - Fatal编程技术网

Ios 如何在Swift 3中使用GLVertexAttributePointer函数?

Ios 如何在Swift 3中使用GLVertexAttributePointer函数?,ios,opengl-es,swift3,Ios,Opengl Es,Swift3,在OpenGL ES(3.0)和Objective-C中使用缓冲区和顶点属性时,我使用 glBufferData(GL_ARRAY_BUFFER, sizeof(attributes), attributes, GL_DYNAMIC_DRAW); 将一些顶点数据(属性)加载到缓冲区,然后 glEnableVertexAttribArray(0); glVertexAttribPointer(0, sizeof(VertexCoordinatesStruct) / sizeof(GLfloat)

在OpenGL ES(3.0)和Objective-C中使用缓冲区和顶点属性时,我使用

glBufferData(GL_ARRAY_BUFFER, sizeof(attributes), attributes, GL_DYNAMIC_DRAW);
将一些顶点数据(属性)加载到缓冲区,然后

glEnableVertexAttribArray(0);
glVertexAttribPointer(0, sizeof(VertexCoordinatesStruct) / sizeof(GLfloat), GL_FLOAT, GL_FALSE, sizeof(VertexAttributesStruct), 0);

glEnableVertexAttribArray(1);
glVertexAttribPointer(1, sizeof(TextureCoordinatesStruct) / sizeof(GLfloat), GL_FLOAT, GL_FALSE, sizeof(VertexAttributesStruct), (void *)sizeof(VertexCoordinatesStruct));
为顶点着色器指定缓冲区中的顶点数据内存布局。注意我在第二个glvertexattributepointer调用中如何将sizeof(OGLVertexCoordinates)强制转换为Void*。如果纹理坐标结构数据所在的GL_数组_缓冲区中存在偏移量,则会出现这种情况

然后我尝试在Swift 3中实现相同类型的代码。但是glvertexattributepointer函数将UnsafeRawPointer作为最后一个参数,而不是void*

这是我无法解决的问题:我无法像在Objc中那样创建具有特定值(内存偏移量)的UnsafeRawPointer。


我已经研究了我能找到的所有Q/A,但它们都不适合我的情况,或者根本不适用于Swift 3。

你检查过Xcode的OpenGL ES游戏模板吗

(在Xcode 8.1转基因种子中测试。)

在GameViewController.swift中,您可以找到:

    glEnableVertexAttribArray(GLuint(GLKVertexAttrib.position.rawValue))
    glVertexAttribPointer(GLuint(GLKVertexAttrib.position.rawValue), 3, GLenum(GL_FLOAT), GLboolean(GL_FALSE), 24, BUFFER_OFFSET(0))
    glEnableVertexAttribArray(GLuint(GLKVertexAttrib.normal.rawValue))
    glVertexAttribPointer(GLuint(GLKVertexAttrib.normal.rawValue), 3, GLenum(GL_FLOAT), GLboolean(GL_FALSE), 24, BUFFER_OFFSET(12))
缓冲区偏移量(:)
(在Objective-C模板中,它是一个宏)定义为:

func BUFFER_OFFSET(_ i: Int) -> UnsafeRawPointer {
    return UnsafeRawPointer(bitPattern: i)!
}
不幸的是,这是错误的,并导致运行时崩溃。您需要将其修复为:

func BUFFER_OFFSET(_ i: Int) -> UnsafeRawPointer? {
    return UnsafeRawPointer(bitPattern: i)
}

使用此
缓冲区偏移量(:)
您可以创建一个具有特定值的非缓冲指针

尽管感觉不太正确,但无论如何,我是这样做的

let ptr = UnsafePointer<UInt8>(bitPattern: UInt(offsetInBytes))
glVertexAttribPointer(indx, size, type, GLboolean(GL_FALSE), stride, ptr)
let ptr=UnsafePointer(位模式:UInt(offsetInBytes))
GLVertexAttribute指针(indx、大小、类型、GLboolean(GL_FALSE)、步长、ptr)

ptr
如果
offsetInBytes==0
则变为
nil
,因为
nil
是值为
0
的指针的表示形式(=null指针)。。。依我看。

就是这样!UnsafeRawPointer(位模式:i)正是我所需要的。非常感谢。我确实看到了那个初始值设定项,但没有理解为什么参数被称为bitPattern,所以我忽略了它:(稍后将检查这些运行时崩溃。如果我发现/修复了任何问题,我将在这里报告。我不会注意到任何崩溃或其他问题。工作正常。