tags:

views:

38

answers:

1

I am trying to draw the normal vectors to a mesh file so I can see how the normal vectors bounce from their respected face. In the draw function, it takes in each face and draws a line from the center point of the face to the (center + normal vector). When I run it, however, I do not see any red lines bouncing off each face. What am I doing wrong here?

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;
}
A: 

the normals could also be pointing in the wrong direction -- for instance if the orientation of the triangles is reversed than the normal could be going into your drawn object. Also, since your normal here isn't normalized (made unit length) it might be difficult to see esp. if your triangles are small.

Scot Shinderman