Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Scaling models influences position in OpenGL

The scale of my models seems to changed their positions, but I don’t know why. I am doing it in the S-R-T order. The blue plane has (0,0,0) as it’s origin.

The model matrix is calculated like this:

        // set model matrix
        glm::mat4 model = glm::mat4( 1.0f );

                        //scale
        model = glm::scale( model, _entity.Scale );

                // rotate
        model = glm::rotate( model, glm::radians( _entity.Rotation.x ), glm::vec3( 1.0f, 0.0f, 0.0f ) );
        model = glm::rotate( model, glm::radians( _entity.Rotation.y ), glm::vec3( 0.0f, 1.0f, 0.0f ) );
        model = glm::rotate( model, glm::radians( _entity.Rotation.z ), glm::vec3( 0.0f, 0.0f, 1.0f ) );

                //translate
        model = glm::translate( model, _entity.Position );

        Shader::setMat4( _shader, "model", model );

In the shader it looks like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

vs_out.FragPos = vec3(model * vec4(aPos, 1.0));
vs_out.Normal = mat3(transpose(inverse(model))) * aNormal;  
vs_out.TexCoord = aTexCoord;

gl_Position = projection * view * vec4(vs_out.FragPos, 1.0);

The first picture is with 1.0 scale, the second with 10.0 scale:

0.5scale

1.0scale

>Solution :

I am doing it in the S-R-T order.

No. You do it in the order T-R-S. The order S-R-T is p' = translation * rotation * scale * p. You have to read it from right to left.

// set model matrix
glm::mat4 model = glm::mat4( 1.0f );

//translate
model = glm::translate( model, _entity.Position );

// rotate
model = glm::rotate(model, glm::radians(_entity.Rotation.x), glm::vec3(1.0f, 0.0f, 0.0f));
model = glm::rotate(model, glm::radians(_entity.Rotation.y), glm::vec3(0.0f, 1.0f, 0.0f));
model = glm::rotate(model, glm::radians(_entity.Rotation.z), glm::vec3(0.0f, 0.0f, 1.0f));

//scale
model = glm::scale(model, _entity.Scale);

Shader::setMat4( _shader, "model", model );
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading