• Announcements

• Wondering what's new and changed at GameDev.net?06/20/17

Check out the latest Staff Blog update that talks about what's changed, what's new, and what's up with these "Pixels".
Followers 0

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

// 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 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 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 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 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 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 on other sites

Thanks, I got it working properly now.

0

Create an account

Register a new account

Followers 0

• Game Jobs

• Similar Content

• Hi guys,
I would like to contribute to a game project as a developer (open source possibly). I have some experiences in C/C++ in game development (perso projects). I don't know either unreal or unity but I have some knowledges in opengl, glsl and shading theory as I had some courses at university regarding to that. I have some knowledges in maths and basic in physics. I know a little how to use blender to do modelling, texturing and simple game assets (no characters, no animation no skinning/rigging). I have no game preferences but I like aventure game, dungeon crawler, platformers, randomly generated things. I know these kind of projects involve a lot of time and I'd be really to work on it but if there are no cleary defined specific design goals/stories/gameplay mechanics I would like to not be part of it x) and I would rather prefer a smaller but well defined project to work on that a huge and not 'finishable' one.
CircleOfLight97

• Hi, I finally released KILL COMMANDO on gamejolt for free. It is a retro-funsplatter-shooter with C64 style. Give it a try.

• void TimerFunction(int value) {  glutPostRedisplay();  glutTimerFunc(1000, TimerFunction, 1); } void drawScene() {  glClear(GL_COLOR_BUFFER_BIT);      drawScene_bug();  TimerFunction(1);  eraseScene_bug(); // drawScene_bug_two(); // eraseScene_bug_two(); drawScene_ship(); drawScene_bullet();  glutSwapBuffers(); }