ios - How to use glVertexAttribPointer function in Swift 3? -
when working buffers , vertex attributes in opengl es (3.0) , objective-c use
glbufferdata(gl_array_buffer, sizeof(attributes), attributes, gl_dynamic_draw);
to load vertex data (attributes) buffer, , then
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));
to specify vertex data memory layout in buffer vertex shader. notice how cast sizeof(oglvertexcoordinates) void* in second glvertexattribpointer call. if offset in gl_array_buffer @ texturecoordinatesstruct data lying.
and i'm trying implement same exact type of code in swift 3. glvertexattribpointer function takes unsaferawpointer last parameter, not void*.
and problem can't solve: i'm unable create unsaferawpointer specific value (memory offset) can in objc.
i've studied q/a find, of them aren't suitable case or don't work in swift 3.
have checked opengl es game template of xcode?
(tested in xcode 8.1 gm seed.)
in gameviewcontroller.swift, can find this:
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))
and buffer_offset(_:)
(in objective-c template, it's macro) defined as:
func buffer_offset(_ i: int) -> unsaferawpointer { return unsaferawpointer(bitpattern: i)! }
unfortunately, buggy , causes runtime crash. need fix to:
func buffer_offset(_ i: int) -> unsaferawpointer? { return unsaferawpointer(bitpattern: i) }
with buffer_offset(_:)
can create unsaferawpointer specific value.
Comments
Post a Comment