2016-04-17 2 views
0

J'ai des problèmes de rotation.Rotation lors du basculement du vecteur de Y à Z

J'ai la caméra configurée de sorte que le vecteur haut est Z, -X est avant et Y est à droite. J'ai des modèles exportés dans un autre système de coordonnées où Y est en haut, -Z est en avant, X est droit.

Mon défi/problème est avec la rotation. Comment faire une rotation correcte dans ce cas sans changer le système de coordonnées des objets que j'importe. L'échelle et la transformation fonctionnent comme elles le devraient.

Jusqu'à présent, je l'ai essayé permutant axe Y et Z dans le manuel de la matrice de rotation, en faisant la matrice de rotation séparément, en faisant la rotation que nous avons lu le fichier de contrôle, renversant axe de rotation Z et Y, en changeant la matrice à

| 1 0 0 |

| 0 0 1 |

| 0 1 0 |

etc.

PS. Je ne veux pas changer le système de coordonnées des fichiers objet que j'importe maintenant et quand j'écris dans le fichier de contrôle rotateY, il fera pivoter l'objet dans mon axe Y et non dans l'axe Y de l'objet.

Ceci est la partie du code que je suis en train les transformations

// control file is where i store transform, rotate and scale in a text file and objects that i want to read. 
for(int i = 0; i< controls.size(); i++) { 
     meshStruct tempMesh; 

     int isMeshLoaded = loadObjFile((char*)controls[i].path, &tempMesh.objectInfo_, &tempMesh.numObjects_); 
     if(isMeshLoaded) 
     { 
      cout<< "Mesh " << controls[i].path << " loaded sucesfully." << endl; 
     } else { 
      cout<< "Mesh " << controls[i].path << " loaded failed." << endl; 
     } 

     tempModelMatrix = mat4(1.0f, 0.0f, 0.0f, 0.0f, 
           0.0f, 1.0f, 0.0f, 0.0f, 
           0.0f, 0.0f, 1.0f, 0.0f, 
           0.0f, 0.0f, 0.0f, 1.0f); 

     //mat4 tempModelMatrixFlip = mat4(1.0f, 0.0f, 0.0f, 0.0f, 
     //        0.0f, 0.0f, 1.0f, 0.0f, 
     //        0.0f, 1.0f, 0.0f, 0.0f, 
     //        0.0f, 0.0f, 0.0f, 1.0f); 
     //tempModelMatrix *= tempModelMatrixFlip; 

     tempModelMatrix = glm::translate(tempModelMatrix, controls[i].translate); 

     tempModelMatrix = glm::rotate(tempModelMatrix, radians(controls[i].rotation.x), vec3(1.0, 0.0, 0.0)); 
     // I dont want to flip here Y and Z because some objects are in the correct coordinate system 
     tempModelMatrix = glm::rotate(tempModelMatrix, radians(controls[i].rotation.y), vec3(0.0, 1.0, 0.0)); 
     tempModelMatrix = glm::rotate(tempModelMatrix, radians(controls[i].rotation.z), vec3(0.0, 0.0, 1.0)); 
     /* 
     mat4 r = mat4(1.0f); 
     mat4 rx = mat4(1.0f); 
     mat4 ry = mat4(1.0f); 
     mat4 rz = mat4(1.0f); 
     rx = rotate(radians(controls[i].rotation.x), vec3(1.0, 0.0, 0.0)); 
     ry = rotate(radians(controls[i].rotation.y), vec3(0.0, 1.0, 0.0)); 
     rz = rotate(radians(controls[i].rotation.z), vec3(0.0, 0.0, 1.0)); 
     r = rx * ry * rz;  

     vec4 temp; 

     temp = column(r, 1); 
     cout << to_string(temp) << endl; 
     r = column(r,1, column(r, 2)); 
     r = column(r,2, temp); 

     temp = row(r,1); 
     r = row(r, 1,row(r,2)); 
     r = row(r, 2, temp); 
     //cout << to_string(column(tempModelMatrix, 1)) << endl; 

     //tempModelMatrix *= r; 
     float tempModelMatrix2[16];// = mat4(1.0f); 

     tempModelMatrix2[0] = controls[i].scale.x * column(r, 0).x;//controls[i].scale.x * controls[i].rotation.x; 
     tempModelMatrix2[1] = controls[i].scale.x * column(r, 0).y;//controls[i].scale.x * controls[i].rotation.x; 
     tempModelMatrix2[2] = controls[i].scale.x * column(r, 0).z;//controls[i].scale.x * controls[i].rotation.x; 
     tempModelMatrix2[3] = 0.0f; 
     tempModelMatrix2[4] = controls[i].scale.y * column(r, 2).x;//controls[i].scale.y * controls[i].rotation.y; 
     tempModelMatrix2[5] = controls[i].scale.y * column(r, 2).y;//controls[i].scale.y * controls[i].rotation.y; 
     tempModelMatrix2[6] = controls[i].scale.y * column(r, 2).z;//controls[i].scale.y * controls[i].rotation.y; 
     tempModelMatrix2[7] = 0.0f; 
     tempModelMatrix2[8] = controls[i].scale.z * column(r, 1).x;//controls[i].scale.z * controls[i].rotation.z; 
     tempModelMatrix2[9] = controls[i].scale.z * column(r, 1).y;//controls[i].scale.z * controls[i].rotation.z; 
     tempModelMatrix2[10] = controls[i].scale.z * column(r, 1).z;//controls[i].scale.z * controls[i].rotation.z; 
     tempModelMatrix2[11] = 0.0f; 
     tempModelMatrix2[12] = controls[i].translate.x; 
     tempModelMatrix2[13] = controls[i].translate.y; 
     tempModelMatrix2[14] = controls[i].translate.z; 
     tempModelMatrix2[15] = 1.0f;*/ 

     tempModelMatrix = glm::scale(tempModelMatrix, controls[i].scale); 
     //cout << controls[i].path << " controls[i].translate " << to_string(controls[i].translate) << endl; 
     //cout << controls[i].path << " controls[i].rotation.X " << controls[i].rotation.x << endl; 
     //cout << controls[i].path << " controls[i].rotation.y " << controls[i].rotation.y << endl; 
     //cout << controls[i].path << " controls[i].rotation.Z " << controls[i].rotation.z << endl; 
     //cout << controls[i].path << " controls[i].scale " << to_string(controls[i].scale) << endl; 

     string basedir = dirname(controls[i].path); 
     for(int j = 0; j < tempMesh.numObjects_; j++){ 
      //tempMesh.objectInfo_[j].modelMatrix = controls[i].modelMatrix; 
      tempMesh.objectInfo_[j].modelMatrix = tempModelMatrix; 
      //tempMesh.objectInfo_[j].modelMatrix = make_mat4(tempModelMatrix2); 

Répondre

0

Puisque je ne peux pas déterminer comment vous stockez les données de sommet de l'objet que je définirai quelque chose de simple:

struct Vertex { 
    glm::vec3 position; 
}; 

struct Mesh { 
    std::vector<Vertex> vertices; 
}; 

Parcourez maintenant toutes les positions de vertex et convertissez-les en coordonnées de votre monde:

Mesh originalMesh = loadObjectFromFile("object.file"); 
Mesh convertedMesh = Mesh(); 

for (Vertex v : originalMesh.vertices) { 
    convertedMesh.push_back(glm::vec3(v.y, v.z, v.x)); 
} 

L'essentiel à noter ici est que nous faisons le mappage suivant:

x = v.y 
y = v.z 
z = v.x 

Si vous avez d'autres données de sommet (par exemple normals, texcoords) assurez-vous que ceux-ci sont également convertis.

+0

Je me donne la fonction « loadObjFile » et je ne peux pas le changer maintenant. C'est la raison pour laquelle j'ai besoin d'une solution pour cela. Aussi, je ne peux pas transformer des objets avec Blender ou quelque chose de similaire. Le problème est que je discute avec mon professeur que ce n'est pas une bonne solution car certains objets tournent correctement et d'autres ne sont pas dus à un système de coordonnées local différent. J'ai passé toute la journée aujourd'hui 10+ heures à essayer de trouver une solution pour cela. Merci pour l'aide si: D –

+0

Ma réponse ne nécessite pas de modifier '' '' loadObjFile''' ou le fichier d'origine. Vous le convertissez en coordonnées de votre monde ** après ** il a été chargé. – Exide

+0

Il y a quelques objets qui tournent correctement parce qu'ils sont dans le monde coordonnéesont et le chargeur de obj envoie des données de sommet au GPU. :( –

0

Correction du problème après avoir passé quelques courriels avec le professeur et eu une petite erreur lors du calcul de la matrice de rotation. Je faisais de la manière inverse

tempModelMatrix = glm::rotate(tempModelMatrix, radians(controls[i].rotation.x), vec3(1.0, 0.0, 0.0)); 
tempModelMatrix = glm::rotate(tempModelMatrix, radians(controls[i].rotation.y), vec3(0.0, 1.0, 0.0)); 
tempModelMatrix = glm::rotate(tempModelMatrix, radians(controls[i].rotation.z), vec3(0.0, 0.0, 1.0)); 

au lieu de

tempModelMatrix = glm::rotate(tempModelMatrix, radians(controls[i].rotation.z), vec3(0.0, 0.0, 1.0)); 
tempModelMatrix = glm::rotate(tempModelMatrix, radians(controls[i].rotation.y), vec3(0.0, 1.0, 0.0)); 
tempModelMatrix = glm::rotate(tempModelMatrix, radians(controls[i].rotation.x), vec3(1.0, 0.0, 0.0)); 

Merci pour les suggestions @Exide