Jump to content

  • Log In with Google      Sign In   
  • Create Account

Why does glm::scale move my mesh?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 KaiserJohan   Members   -  Reputation: 1243

Like
0Likes
Like

Posted 06 January 2013 - 06:10 AM

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, 06 January 2013 - 06:13 AM.


Sponsor:

#2 Brother Bob   Moderators   -  Reputation: 8631

Like
3Likes
Like

Posted 06 January 2013 - 06:54 AM

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 KaiserJohan   Members   -  Reputation: 1243

Like
0Likes
Like

Posted 06 January 2013 - 06:59 AM

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


#4 wintertime   Members   -  Reputation: 1882

Like
1Likes
Like

Posted 06 January 2013 - 07:04 AM

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.



#5 KaiserJohan   Members   -  Reputation: 1243

Like
1Likes
Like

Posted 06 January 2013 - 07:19 AM

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


#6 wintertime   Members   -  Reputation: 1882

Like
0Likes
Like

Posted 06 January 2013 - 07:42 AM

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, 06 January 2013 - 07:43 AM.


#7 KaiserJohan   Members   -  Reputation: 1243

Like
0Likes
Like

Posted 06 January 2013 - 08:04 AM

Thanks, I got it working properly now. 






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS