缩放模型会影响OpenGL中的位置

i7uq4tfw  于 2022-09-26  发布在  其他
关注(0)|答案(1)|浏览(181)

我的模型的比例似乎改变了他们的立场,但我不知道为什么。我是按照S-R-T顺序来做的。蓝色平面的原点为(0,0,0)。

模型矩阵的计算方法如下:

// 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 );

在着色器中,它看起来如下所示:

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

第一张图片是1.0比例尺,第二张是10.0比例尺:

relj7zay

relj7zay1#

我是按照S-R-T顺序来做的。

不是的。你要按T-R-S的顺序来做。S-R-T的顺序是p' = translation * rotation * scale * p。你必须从右到左阅读。

// 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 );

相关问题