Camera matrix modelview and shader

Hello,

I’m working on OpenGL and Shader, and regarding the getModelViewMatrix() of the camera class, I don’t really understand the meaning of that method : does it return only the view matrix ? or does it return the combined with the model matrix ?

When I pass the matrix returned by this function to a vert shader and I try to get the gl_position with this modelview matrix it doesn’t give the same result as if i use the modelview matrix passed automatically by OF. So it seems that the getModelViewMatrix() return only the view matrix … but how to get the “real” modelview matrix ?

I’m a bit lost between all this matrix, so if someone can explain me a bit that would be awesome :slight_smile:

yes that name is not very correct, it’s only thee view matrix not the modelview. the camera doesn’t have access to the model, anything that you modify using ofTranslate, rotate… or for example an ofNode so it can only give you the view

1 Like

And it seems that the ofGetCurrentMatrix(ofMatrixMode::OF_MATRIX_MODELVIEW) function does the same, isn’t it ?
So is there a way to get de real modelview ?

no, ofGetCurrentMatrix(ofMatrixMode::OF_MATRIX_MODELVIEW) returns the modelView matrix

Arturo,

Thank you for your time !

That’s weird because I didn’t get a good result with ofGetCurrentMatrix(ofMatrixMode::OF_MATRIX_MODELVIEW), it didn’t take care of the model modification on an ofNode or via transformation between ofPushMatrix() / ofPopMatrix().

If you have some time for helping me, here is my code.

#include "ofApp.h"

//--------------------------------------------------------------
void ofApp::setup(){
        
    ofSetVerticalSync(true);
    ofSetFrameRate(60);
    
    /* --- shader loading --- */
    customShader.load("shaders/customShader");
    
    /* --- camera setup --- */
    cam.setAutoDistance(false);
    cam.setGlobalPosition(-1000, 410, 0);
    cam.setTarget(ofVec3f(0,0,0));
    
    plane = ofPlanePrimitive(500, 500, 10, 10);
    cube = ofBoxPrimitive(100, 500, 100);
    cube.rotate(90, ofVec3f(1,0,0));

    
}

//--------------------------------------------------------------
void ofApp::update(){
    ofSetWindowTitle(ofToString(ofGetFrameRate()));
}

//--------------------------------------------------------------
void ofApp::draw(){

    
    cam.begin();
    customShader.begin();
    customShader.setUniformMatrix4f("modView", ofGetCurrentMatrix(ofMatrixMode::OF_MATRIX_MODELVIEW));
    ofEnableDepthTest();
    renderScene();
    ofDrawAxis(2000);
    ofDisableDepthTest();
    customShader.end();
    cam.end();
    
}

//--------------------------------------------------------------
void ofApp::renderScene(){
    

    ofSetColor(0,255,0);
    cube.draw();
    
    ofSetColor(255,0,0);
    plane.draw();
    
}

Vert

#version 330
//Default from OF::programmableRenderer
uniform mat4 modelViewMatrix;
uniform mat4 projectionMatrix;
uniform mat4 modelViewProjectionMatrix;
uniform vec4 globalColor;

//Default from OF::shader
in vec4  position;

//Out for frag
out vec4 colorVarying;


//Uniform setup in ofApp
uniform mat4 modView;


void main() {
    colorVarying = globalColor;
    
    //gl_Position = projectionMatrix * modelViewMatrix * position;
    gl_Position = projectionMatrix * modView * position;
    
}

frag
#version 330

//From the vert
in vec4 colorVarying;


out vec4 fragColor;


void main (void) {
    fragColor = colorVarying;

}

the problem is that each 3d primitive has it’s own model matrix which it uploads when drawing itself so the modelmatrix you upload is the correct one when you upload it but then it changes. there’s really no way right now to access that model matrix unless you draw the 3d primitive’s mesh manually and get it’s transformation matrix separately like:

ofMatrix4x4 view = camera.getModelViewMatrix();
ofMatrix4x4 model = cube.getGlobalTransformation();
shader.setUniformMatrix4f("modelView", model * view);
cube.getMesh().draw();
1 Like

Thanks you very much, it totally make sens that a model matrix is bound to a 3D primitive.
I’m gonna dig into the OF sources to understand how it works internally !