2010-10-27 6 views
1

J'essaie de dessiner les vecteurs normaux dans un fichier de maillage afin que je puisse voir comment les vecteurs normaux rebondissent à partir de leur visage respecté. Dans la fonction de tirage, il prend dans chaque face et trace une ligne du centre du visage vers le (centre + vecteur normal). Quand je le lance, cependant, je ne vois pas de lignes rouges rebondir sur chaque visage. Qu'est-ce que je fais mal ici?Dessiner des normales à des surfaces

void drawTria(myFace face) { 

    glNormal3f((face.getNormal().x), (face.getNormal().y), (face.getNormal().z)); 
    wired ? glBegin(GL_LINE_LOOP) : glBegin(GL_POLYGON); 
    glColor3f(0.0, 0.0, 0.5); 
    glVertex3f(vertexList.at(face.v1-1).x, vertexList.at(face.v1-1).y, vertexList.at(face.v1-1).z); 
    glVertex3f(vertexList.at(face.v2-1).x, vertexList.at(face.v2-1).y, vertexList.at(face.v2-1).z); 
    glVertex3f(vertexList.at(face.v3-1).x, vertexList.at(face.v3-1).y, vertexList.at(face.v3-1).z); 
    glEnd(); 


    // Drawing normals 
    glBegin(GL_LINES); 
    glColor3f(1.0, 0.0, 0.0); 
    glVertex3f(face.getCenter().x, face.getCenter().y, face.getCenter().z); 
    glVertex3f((face.getCenter().x+face.getNormal().x), (face.getCenter().y+face.getNormal().y), (face.getCenter().z+face.getNormal().z)); 
    glEnd(); 

} 

myVertex myFace::getCenter() { 
    myVertex center; 

    center.x = (vertexList.at(v1-1).x + vertexList.at(v2-1).x + vertexList.at(v3-1).x)/3; 
    center.y = (vertexList.at(v1-1).y + vertexList.at(v2-1).y + vertexList.at(v3-1).y)/3; 
    center.z = (vertexList.at(v1-1).z + vertexList.at(v2-1).z + vertexList.at(v3-1).z)/3; 

    return center; 
} 

myVertex myFace::getNormal() { 
    myVertex normal; 

    normal.x = ((vertexList.at(v2-1).y - vertexList.at(v1-1).y) 
         * (vertexList.at(v3-1).z - vertexList.at(v1-1).z)) 
         - ((vertexList.at(v2-1).z - vertexList.at(v1-1).z) 
          * (vertexList.at(v3-1).y - vertexList.at(v1-1).y)); 

    normal.y = ((vertexList.at(v2-1).z - vertexList.at(v1-1).z) 
         * (vertexList.at(v3-1).x - vertexList.at(v1-1).x)) 
         - ((vertexList.at(v2-1).x - vertexList.at(v1-1).x) 
          * (vertexList.at(v3-1).z - vertexList.at(v1-1).z)); 

    normal.z = ((vertexList.at(v2-1).x - vertexList.at(v1-1).x) 
         * (vertexList.at(v3-1).y - vertexList.at(v1-1).y)) 
         - ((vertexList.at(v2-1).y - vertexList.at(v1-1).y) 
          * (vertexList.at(v3-1).x - vertexList.at(v1-1).x)); 

    return normal; 
} 
+4

Créer un "modèle" constitué d'un seul triangle (0,0) (1,0) (0,1) et assurez-vous que votre normale est la valeur attendue (0,0,1) et que votre centre est approprié. – genpfault

+2

Vous pourriez également envisager de simplifier votre code. Il est difficile à lire, et effectue beaucoup trop d'opérations pour votre tâche simple - comme ne pas appeler getCenter 6 fois quand un seul est nécessaire, etc – rotoglup

+1

Comme genpfault a dit, réduire à des problèmes plus simples: getNormal() produit le bonne réponse pour un cas simple? est-ce que getCenter() Vous pouvez également imprimer les sommets sous '// drawing normales 'et voir si le premier point est le même que le second point. – LarsH

Répondre

0

les Normales pourraient également pointer dans la mauvaise direction - par exemple, si l'orientation des triangles est inversée que la normale pourrait être entrer dans votre objet dessiné. En outre, puisque votre normale ici n'est pas normalisée (longueur d'unité faite) il pourrait être difficile de voir esp. si vos triangles sont petits.

Questions connexes