Need a little help in coverting glTranlate / glRotate to glm::...


#1

Hi there,

i just want to remove depretched function from my source So i’m just replacing the old glRotate, glTranslate, etc. by glm-calculations. I’ve to admit, that i’m quiet stupid in this point. I downloaded glm yesterday and also, have to relearn matrix-math after yeras without practice.

I wrote a class “Matrix” (still at work), that does almost the same, then the old OpenGL-matrix-functions. It provides (currently) three stacked glm::mat4x4-matrices. So, it works with Push, Pop, and MatrixMode. After this, i replaced the old-OpenGL calls by the new ons and found that several things work as expected. So i can rotate and translate my model and also light-sources, but unfortunately the mesh itself is completly distorted, which seems to be some problem with the perspevtive. It looks a bit as if the object is proected inside of a fractal sphere.
Next to a problem with the perspective, it migth also be that the the my vbo-data get destorted while transferring to the shader. In the old version of my shader i used gl_Vertex etc… Now i use in-varyings, where i have no expreience with.

I’ve in beween tried around a bit with different Values, but without success. Also - i don’t really know what i do there, so i hope to find some help here…

Additionally, i’ve the proplem, that it’s almost impossilbe to post my code here, because it is too complex. What i basically do is this (this is no C-code - its extracted from a script executed by my program):


        MatrixMode     ( _PROJ_MATRIX); 
        SetPerspective ( vcAngel.GetValue(), 1, 0.001, 1000);
        
        MatrixMode     ( _VIEW_MATRIX );
        RotateView     ( "Z", vcRotZ.Get()   );
        RotateView     ( "Y", vcRotX.Get()   );
        RotateView     ( "X", vcRotY.Get()   );

        H              = vcHeight.GetInt();
        Ratio          = vcWidth .GetInt() / H;     
        ScaleView       ( 1, Ratio, 1);   
[...]
        F0 = vcOfsF0.Get(); TranslateMatrix (vcOfsX0.Get()*F0, vcOfsY0.Get()*F0, vcOfsZ0.Get()*F0);
        RotateMatrix ("Z", vcRotX0.Get());
        RotateMatrix ("Y", vcRotY0.Get());
        RotateMatrix ("X", vcRotZ0.Get());

        F1 = vcOfsF1.Get(); TranslateMatrix (vcOfsX1.Get()*F1, vcOfsY1.Get()*F1, vcOfsZ1.Get()*F1);
        RotateMatrix ("Z", vcRotX1.Get());
        RotateMatrix ("Y", vcRotY1.Get());
        RotateMatrix ("X", vcRotZ1.Get());
[...]
        MatrixMode      ( _MODEL_MATRIX );
        Radius          = vcScale.Get() * (0.1 * pow(4, selLevel.GetInt() + 1) + 1) * 0.25;
        ScaleMatrix     (Radius, Radius, Radius);

        F0 = vcOfsF0.Get(); TranslateMatrix (vcOfsX0.Get()*F0, vcOfsY0.Get()*F0, vcOfsZ0.Get()*F0);
        RotateMatrix ("Z", vcRotX0.Get());
        RotateMatrix ("Y", vcRotY0.Get());
        RotateMatrix ("X", vcRotZ0.Get());

        F1 = vcOfsF1.Get(); TranslateMatrix (vcOfsX1.Get()*F1, vcOfsY1.Get()*F1, vcOfsZ1.Get()*F1);
        RotateMatrix ("Z", vcRotX1.Get());
        RotateMatrix ("Y", vcRotY1.Get());
        RotateMatrix ("X", vcRotZ1.Get());
[...]
        ApplyState();


The following is the C-code, that is controlled by the upper script (“list” is a GLfloat-container-class-array):


/*n19*/ else if (!strcmp (nam, "PushState"))               Matrix.push ();                      
/*n19*/ else if (!strcmp (nam, "PopState"))                Matrix.pop  ();                      
/*n19*/ else if (!strcmp (nam, "SetPerspective"))          Matrix.Proj () = glm::perspective( glm::radians(list[0][0]), list[1][0], list[2][0], list[3][0]  );   
/*n19*/ else if (!strcmp (nam, "MatrixMode"))              Matrix.SetMMode( list[0][0]  );     
/*n19*/ else if (!strcmp (nam, "TranslateMatrix"))         *Matrix = glm::translate  (*Matrix, glm::vec3   (list[0][0] , list[1][0], list[2][0])  );              
/*n19*/ else if (!strcmp (nam, "RotateMatrix"))            *Matrix = glm::rotate     (*Matrix, glm::radians(list[1][0]), Matrix.RotVec(*(char*)list[0])  );       
/*n19*/ else if (!strcmp (nam, "ScaleMatrix"))             *Matrix = glm::scale      (*Matrix, glm::vec3   (list[0][0] , list[1][0], list[2][0])  );         
/*n19*/ else if (!strcmp (nam, "ApplyState"))       
            {  
                glUniformMatrix4fv(glGetUniformLocation(curPRG, "projMatrix"),  1, GL_FALSE, &Matrix.Proj ()[0][0]);
                glUniformMatrix4fv(glGetUniformLocation(curPRG, "viewMatrix"),  1, GL_FALSE, &Matrix.View ()[0][0]);
                glUniformMatrix4fv(glGetUniformLocation(curPRG, "modelMatrix"), 1, GL_FALSE, &Matrix.Model()[0][0]); 
            }

In “Marix” are these lines:


        inline virtual void  _init ()           {   
            _Proj .push(new glm::mat4x4(glm::vec4(0,0,0,0),glm::vec4(0,0,0,0),glm::vec4(0,0,0,0),glm::vec4(0,0,0,0)));
            _View .push(new glm::mat4x4(glm::vec4(1,0,0,0),glm::vec4(0,1,0,0),glm::vec4(0,0,1,0),glm::vec4(0,0,0,1)));
            _Model.push(new glm::mat4x4(glm::vec4(1,0,0,0),glm::vec4(0,1,0,0),glm::vec4(0,0,1,0),glm::vec4(0,0,0,1)));
            _size++;
        };

        inline virtual const glm::vec3 RotVec(unsigned char axis) {         
            return axis==2||axis=='z'||axis=='Z' ? glm::vec3( 0.0f, 0.0f, 1.0f) 
                   : axis==1||axis=='y'||axis=='Y' ? glm::vec3(-1.0f, 0.0f, 0.0f) 
                   :                                 glm::vec3( 0.0f, 1.0f, 0.0f);
        }; 

        inline virtual void          SetMMode(unsigned char i) {            _mode = i;      };

        inline virtual glm::mat4x4 &operator*() {                           return *_MAT4X4[_mode].top();   };
        
        inline virtual glm::mat4x4 &Proj () {       return *_Proj .top();   };

The vertex-shader does nothing more then this:


uniform mat4      projMatrix;
uniform mat4      viewMatrix; 
uniform mat4      modelMatrix;   

//T2F_N3F_V3F
in    vec2 TexCoord;
in    vec3 Normal;
in    vec3 Vertex;


out Data {
    vec3 Vertex;
    vec3 Normal;
    vec2 TexCoord;
} Out;


void main () {

    Out.Normal      = Normal;//normalize (mat3(projMatrix)* Normal);
    Out.Vertex      = mat3x3(modelMatrix * viewMatrix) * Vertex;
    Out.TexCoord    = TexCoord;
    gl_Position     = projMatrix * modelMatrix * viewMatrix * vec4(Vertex,1);
}

For me this looks correct so far. I hope someone else finds the point.

Thank you for reading,
Frank