user1782677
user1782677

Reputation: 2007

Location of imported 3D models

So whenever I load a 3D model (.3ds, .obj, etc) into my openGL program, they either get placed in a seemingly random location or they're oriented in a not-ideal position. Is there an easy way to make it so that when I import a model it goes to a location I specify? I'm thinking this could be handled using a vertex shader and translating the input location by some vector addition but this would require knowing where it was going to appear in the first place in order to know how much to translate it by.

Upvotes: 1

Views: 208

Answers (2)

Rahul Banerjee
Rahul Banerjee

Reputation: 2363

It's hard to figure out what the "correct orientation" is. When you see a teapot, should its handle be on the left or the right?

However, the correct location can be slightly easier to adjust.

Let's assume you would like your loaded object to show up at (Lx, Ly, Lz)

First, compute the centroid of the vertices (Cx, Cy, Cz) by adding each coordinate and averaging it. Eg: For the 3 points (1,0,1), (0,1,1) and (0,0,1), the centroid is (1/3, 1/3, 1).

Now offset every vertex by (Lx-Cx, Ly-Cy, Lz-Cz), and your geometry should be "centered" around the location (Lx, Ly, Lz).

Note: This works well for fairly symmetrical models (eg: people, creatures, cars, etc.).

Upvotes: 1

Bart
Bart

Reputation: 20028

That entirely depends on the transformations applied to your 3D model, or the coordinate system used in your 3D modelling package. I'd say you're approaching this from the wrong end. If you want your data to be consistent, make sure it's exported in a manner you expect from your 3D modelling package.

Upvotes: 1

Related Questions