c++ - Scaling and moving 3d object with openGL -


i'm trying load 3d object .obj file, scale size to move coordinate (i prefer changing matrix scaling , moving each vector). .obj parsing function, know width, height, depth , center of object's bounding box.

to make clear, have map of tiles (-1.0, 0, 1.0) (1.0, 0, -1.0) , modeling thing use obj file of cube (with length of 40.0). clearly, had scale cube before printing it.

my problem keep failing on moving cube correctly on z axis. scaling done way:

model.scale(xn * x_unit / model.getwidth(), yn * y_unit / model.getheight(), zn * z_unit / model.getdepth()); 

where xn, yn , zn amount of tiles cube stretch on in each axis , x_unit, y_unit , z_unit length of each tile in axis (this function works perfectly)

in order move cube desired place calculated translation amount in each axis (assuming cube's center @ 0, 0, 0):

x_trans = (((xn - (2.0f / x_unit)) / 2.0f) * model.getoriginalwidth()) / xn; y_trans = (0.5f * model.getoriginalheight()); z_trans = ((zn - (2.0f / z_unit)) / 2.0f) * model.getoriginaldepth() / zn; 

and transalting model:

model.move(x_trans - model.getx(), y_trans - model.gety(), z_trans - model.getz()); 

the move function , scale function moves , scales model matrix, not real array of coordinates:

void graphic3dmodelpart::move(float dx, float dy, float dz) {     model_ = glm::translate(model_, glm::vec3(dx, dy, dz));     mvp_ = projection_ * view_ * model_; }  void graphic3dmodelpart::scale(float sx, float sy, float sz) {     model_ *= glm::scale(glm::mat4(), glm::vec3(sx, sy, sz));     mvp_ = projection_ * view_ * model_; } 

yet problem in z axis goes wrong (when cube's depth more 1 tile's length (on x axis , y axis works perfectly).

if pictures understanding situation tell me , upload.

so solved issue. since scaling method changed matrix, moving object must relative how scaled object.

float x_trans, y_trans, z_trans;  model.scale(xn * x_unit / model.getwidth(), yn * y_unit / model.getheight(), zn * z_unit / model.getdepth()); model.move(0.0f - model.getx() / (xn * x_unit / model.getoriginalwidth()), 0.0f - model.gety() / (yn * y_unit / model.getoriginalheight()), 0.0f - model.getz() / (zn * z_unit / model.getoriginaldepth()));  x_trans = (((xn - (2.0f / x_unit)) / 2.0f + x_offset) * model.getoriginalwidth()) / xn; y_trans = ((yn / 2.0f + y_offset) * model.getoriginalheight()) / yn; z_trans = (((zn - (2.0f / z_unit)) / 2.0f + z_offset) * model.getoriginaldepth()) / zn;  model.move(x_trans, y_trans, z_trans); 

this code works me.


Comments

Popular posts from this blog

serialization - Convert Any type in scala to Array[Byte] and back -

matplotlib support failed in PyCharm on OSX -

python - Matplotlib: TypeError: 'AxesSubplot' object is not callable -