我刚开始学习C++和OpenGL。我试图在OpenGL中计算顶点法线。
我知道有一个函数glNormal3f。但是,我不允许使用这个功能。相反,我必须用代码和obj文件计算顶点法线。所以我要做的是,首先计算曲面法线,然后计算顶点法线。
我声明了操作符,如+,-,*和其他函数(如innerproduct, crossproduct )。
void calcul_color(){
VECTOR kd;
VECTOR ks;
kd=VECTOR(0.8, 0.8, 0.8);
ks=VECTOR(1.0, 0.0, 0.0);
double inner = kd.InnerProduct(ks);
int i, j;
for(i=0;i<cube.vertex.size();i++)
{
VECTOR n = cube.vertex_normal[i];
VECTOR l = VECTOR(100,100,0) - cube.vertex[i];
VECTOR v = VECTOR(0,0,1) - cube.vertex[i];
float xl = n.InnerProduct(l)/n.Magnitude();
VECTOR x = (n * (1.0/ n.Magnitude())) * xl;
VECTOR r = x - (l-x);
VECTOR color = kd * (n.InnerProduct(l)) + ks * pow((v.InnerProduct(r)),10);
cube.vertex_color[i] = color;
}
for(i=0;i<cube.face.size();i++)
{
FACE cur_face = cube.face[i];
glColor3f(cube.vertex_color[cur_face.id1].x,cube.vertex_color[cur_face.id1].y,cube.vertex_color[cur_face.id1].z);
glVertex3f(cube.vertex[cur_face.id1].x,cube.vertex[cur_face.id1].y,cube.vertex[cur_face.id1].z);
glColor3f(cube.vertex_color[cur_face.id2].x,cube.vertex_color[cur_face.id2].y,cube.vertex_color[cur_face.id2].z);
glVertex3f(cube.vertex[cur_face.id2].x,cube.vertex[cur_face.id2].y,cube.vertex[cur_face.id2].z);
glColor3f(cube.vertex_color[cur_face.id3].x,cube.vertex_color[cur_face.id3].y,cube.vertex_color[cur_face.id3].z);
glVertex3f(cube.vertex[cur_face.id3].x,cube.vertex[cur_face.id3].y,cube.vertex[cur_face.id3].z);
}发布于 2015-05-08 05:03:15
计算顶点法线的方法如下:
这个循环是一个很好的O(n)。这里需要注意的一点是,如果顶点是共享的,法线就会像球体一样平滑。如果顶点不被共享,你就会在立方体上得到你想要的硬面。重复这样的顶点应该在之前进行。
如果你的问题是如何从正常到颜色,这取决于你的照明方程!最简单的方法是:color = dot(normal,globallightdir)*globallightcolor,另一种方式是color = texturecubemap(normal)。但也有无限的可能性!
https://stackoverflow.com/questions/30115683
复制相似问题