function wrapping glMultMatrixf in ofMatrix4x4 #605

Closed
elliotwoods opened this Issue Jun 16, 2011 · 5 comments

Projects

None yet

2 participants

@elliotwoods
Collaborator

Need some way of applying current ofMatrix4x4 to the scene without going down to gl calls

@kylemcdonald

i second this request. here's a first pass:

void ofMultMatrix(const ofMatrix4x4& matrix) {
    glMultMatrixf((GLfloat*) matrix.getPtr());
}

void ofSetMatrix(const ofMatrix4x4& matrix) {
    glLoadMatrixf((GLfloat*) matrix.getPtr());
}

ofMatrix4x4 ofGetMatrix() {
    ofMatrix4x4 matrix;
    glGetFloatv(GL_MODELVIEW_MATRIX, (GLfloat*) matrix.getPtr());
    return matrix;
}
@elliotwoods
Collaborator

this leans towards an OF_MATRIX_MODELVIEW and OF_MATRIX_PROJECTION enum / constant set

the first 2 functions are lovely

but i'm a little cautious about
glGetFloatv(GL_MODELVIEW_MATRIX, (GLfloat*) matrix.getPtr());
as this suggests that all the functions should be thought of as being tied to modelview

Generally (as per convention) best to have access to projection seperately for perspective transforms, and put only tra/rot in modelview.

however,
The only main difference (instead than multiplying out to 1 matrix) afaik
is calculation of the view vector in shaders, (e.g. when performing specular lighting)

so perhaps
ofMatrix4x4 ofGetMatrix(ofMatrixMode m=OF_MATRIX_MODELVIEW) {
ofMatrix4x4 matrix;
switch(m) {
....
}
return matrix;
}

and similar for ofSetMatrix

@kylemcdonald

any projective matrix transforms are handled by ofSetupScreenPerspective() and ofSetupScreenOrthographic() right now. i think exposing the projective matrix is overkill.

but i agree that the third function doesn't follow the expectation established by the first two. to fix this, i propose:

ofMatrix4x4 ofGetMatrix() {
    ofMatrix4x4 matrix;
    GLint glMatrixMode;
    glGetIntegerv(GL_MATRIX_MODE, &glMatrixMode);
    int targetMatrix;
    if(glMatrixMode == GL_PROJECTION) {
        targetMatrix = GL_PROJECTION_MATRIX;
    } else {
        targetMatrix = GL_MODELVIEW_MATRIX;
    }
    glGetFloatv(targetMatrix, (GLfloat*) matrix.getPtr());
    return matrix;
}
@elliotwoods
Collaborator

yep that's the decent alternative!

ofMatrix4x4 is currently agnostic about this issue, offering up functions like:

_
inline static ofMatrix4x4 newPerspectiveMatrix(double fovy, double aspectRatio,
double zNear, double zFar);

I don't think we're far off having lots of users playing with projection matrices ;)

I'm also a little disappointed that ofMatrix4x4 still doesn't have a homography creation function.
anyway. i'm deviating. you know what you doing.

@kylemcdonald

a lot has changed with the way view and projection matrices are handled in OF since this issue was posted, i think it's no longer relevant.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment