将法线绘制到曲面 [英] Drawing Normals to Surfaces

查看:130
本文介绍了将法线绘制到曲面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将法线向量绘制到网格文件中,以便可以看到法线向量如何从其受人尊敬的脸部反弹.在绘制功能中,它接受每个面并从面的中心点到(中心+法向矢量)画一条线.但是,当我运行它时,我看不到任何红线从每个面上弹起.我在这里做什么错了?

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

推荐答案

法线也可能指向错误的方向-例如,如果三角形的方向与法线相反,则法线可能会进入绘制的对象.另外,由于此处的法线未标准化(单位长度),因此可能很难看到esp.如果您的三角形很小.

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.

这篇关于将法线绘制到曲面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆