Home > Back-end >  Scaling models influences position in OpenGL
Scaling models influences position in OpenGL

Time:08-21

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:

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

CodePudding user response:

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 );
  • Related