Allen
Allen

Reputation: 6882

How to cast GLKMatrix4 to GLfloat* array?

I have a GLKMatrix4 variable, and want to use it's GLfloat *array values for glUniformMatrix4fv function. I googled but not found any useful information. There's a function called CC3Matrix4x4PopulateFromGLKMatrix4 in cocos3d SDK, but it replys too many files. I really don't want to use it.

Is there any easy way to cast GLKMatrix4 to GLfloat* array?

Upvotes: 0

Views: 1582

Answers (2)

BurtK
BurtK

Reputation: 1036

You need to write extension like below.

import GLKit

extension GLKMatrix2 {
    var array: [Float] {
        return (0..<4).map { i in
            self[i]
        }
    }
}


extension GLKMatrix3 {
    var array: [Float] {
        return (0..<9).map { i in
            self[i]
        }
    }
}

extension GLKMatrix4 {
    var array: [Float] {
        return (0..<16).map { i in
            self[i]
        }
    }
}

and then you can use glUniformMatrix4fv or glUniformMatrix3fv like below

...
var modelViewMatrix : GLKMatrix4 = GLKMatrix4Identity
...

glUniformMatrix4fv(self.modelViewMatrixUniform, 1, GLboolean(GL_FALSE), self.modelViewMatrix.array)

Upvotes: 4

BDL
BDL

Reputation: 22165

The GLKMatrix4 class has a member m, which is exactly the array you need.

GLKMatrix4 myMatrix = GLKMatrix4Identity;
glUniformMatrix4fv(uniform, 1, 0, myMatrix.m);

Upvotes: 8

Related Questions