I implemented object outlining in OpenGL by following this tutorial, but the problem is that when I scale up the object, it doesn’t scale from it’s center and because of that the outline looks off. The outline is in red and it seems that it moves towards the positive Z axis.
That model is made in Blender. I set it’s origin in the center in Blender (Right Click on model → Set Origin > Origin to Geometry), but it still doesn’t scale correctly and the outline is wrong like in the image.
This is the drawing routine of the model:
glStencilFunc(GL_ALWAYS, 1, 0xFF);
glStencilMask(0xFF);
{
// Bind normal shader
glm::mat4 matrix = glm::mat4(1.0f);
matrix = glm::translate(matrix, position);
matrix = glm::rotate(matrix, rotation.x, glm::vec3(1.0f, 0.0f, 0.0f));
matrix = glm::rotate(matrix, rotation.y, glm::vec3(0.0f, 1.0f, 0.0f));
matrix = glm::rotate(matrix, rotation.z, glm::vec3(0.0f, 0.0f, 1.0f));
matrix = glm::scale(matrix, glm::vec3(scale, scale, scale));
shader->set_uniform_matrix("u_model_matrix", matrix);
// Bind stuff
// glDrawElements
}
glStencilFunc(GL_NOTEQUAL, 1, 0xFF);
glStencilMask(0x00);
glDisable(GL_DEPTH_TEST);
{
// Bind outline shader
constexpr float size = 0.7f;
glm::mat4 matrix = glm::mat4(1.0f);
matrix = glm::translate(matrix, position);
matrix = glm::rotate(matrix, rotation.x, glm::vec3(1.0f, 0.0f, 0.0f));
matrix = glm::rotate(matrix, rotation.y, glm::vec3(0.0f, 1.0f, 0.0f));
matrix = glm::rotate(matrix, rotation.z, glm::vec3(0.0f, 0.0f, 1.0f));
matrix = glm::scale(matrix, glm::vec3(scale + size, scale + size, scale + size));
outline_shader->set_uniform_matrix("u_model_matrix", matrix);
// glDrawElements
}
glStencilMask(0xFF);
glStencilFunc(GL_ALWAYS, 1, 0xFF);
glEnable(GL_DEPTH_TEST);
Can someone help me? Am I doing something wrong?
Thank you!