Sign in to follow this  
Followers 0
KaiserJohan

OpenGL
Why does glm::scale move my mesh?

6 posts in this topic

I have a mesh to which I apply Scale.

Here's before I apply scale to the cube behind:

http://s7.postimage.org/r25iv2s0r/1png.png

And here's after:

http://s2.postimage.org/6qfk75ul5/image.png

The cube scaled correctly, but why did the cube move?

Here's the code for rendering it:
 

 void OpenGLRenderer::RenderMesh(MeshPtr mesh, const Mat4& modelMatrix, const Mat4& viewMatrix, const Mat4& projectionMatrix)
    {
        if (mesh)
        {
            // upload MVP matrix
            Mat4 MVP = projectionMatrix * viewMatrix * modelMatrix;    // modelMatrix is a Mat4(1.0f) that has been applied glm::scale(Mat4(1.0f), Vec3(2.0f, 1.0f, 1.0f))
            ShaderData shaderData;
            shaderData.Color.x = 1.0f; shaderData.Color.y = 0.5f; shaderData.Color.z = 0.5f; shaderData.Color.w = 1.0f;
            shaderData.MVPMatrix = MVP;
            mUniBuffer.SetData(shaderData);

            // render
            mesh->GetVertexBuffer()->Render();
        }
    }

 

 



What could be wrong?

EDIT: I can note that if I do translate instead of scale, the cube would move as expected

Edited by KaiserJohan
0

Share this post


Link to post
Share on other sites

If there are no explicit translations anywhere, then: what are the coordinates of the vertices of the cube?

 

Consider, for example, that the cube spans the axis-aligned coordinates from (1, 1, 1) to (2, 2, 2). That is, a unit-sized cube centered at (1.5, 1.5, 1.5). Now scale the X-axis by 2, and the cube now spans the coordinates (2, 1, 1) to (4, 2, 2). As you can see, the size of the cube has been scaled by 2, but so has its center which is now at (3, 1.5, 1.5). This is not an additional translation, but an inherent effect of the scaling: scaling happens around the origin, and if your cube's center-of-scaling is not at the origin, the offset from the origin will also be scaled accordingly.

3

Share this post


Link to post
Share on other sites

Here's the vertex data and vertex indices:

 

const float vertexPositions1[] =
        {
            // front vertices
            -4.0f, -5.5f, -20.0f,
            -4.5f, -5.5f, -20.0f,
            -4.5f, -5.0f, -20.0f,
            -4.0f, -5.0f, -20.0f,

            // back vertices
            -4.0f, -5.5f, -20.5f,
            -4.5f, -5.5f, -20.5f,
            -4.5f, -5.0f, -20.5f,
            -4.0f, -5.0f, -20.5f
        };
 

        const uint16_t indexData1[] =
        {
            // back
            5, 4, 7,
            5, 7, 6,

            // right
            1, 5, 6,
            1, 6, 2,

            // left
            0, 4, 7,
            0, 7, 3,

            // top
            5, 4, 0,
            5, 0, 1,

            // bottom
            6, 7, 3,
            6, 3, 2,

            // front
            1, 0, 3,
            1, 3, 2
        };
0

Share this post


Link to post
Share on other sites

That just proves what Brother Bob said.

 

You could translate it to the origin, scale, translate it back to where it should be.

Or even better just have all your model coordinates around the origin, then scale and translate to where it should be.

1

Share this post


Link to post
Share on other sites

Centered around the origin; like this?

 

// front vertices
1.0f, -1.0f, 1.0f,           // bottom right
-1.0f, -1.0f, 1.0f,          // bottom left
-1.0f, 1.0f, 1.0f,          // top left
1.0f, 1.0f, 1.0f,            // top right
 
// back vertices
1.0f, -1.0f, -1.0f,           // bottom right
-1.0f, -1.0f, -1.0f,          // bottom left
-1.0f, 1.0f, -1.0f,          // top left
1.0f, 1.0f, -1.0f,            // top right
1

Share this post


Link to post
Share on other sites
For a flying cube it would be good to have the center of the cube at origin. For things on ground it may be easier if the center of the bottom surface is on origin or one of the bottom corners depending on how it is easier for you to place it somewhere. Edited by wintertime
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0