CSDN的烂服务器啊!!!贴图贴不上!郁闷
详细的公式见 图片-》“我收藏的美女” 中的公式图片
我采用little法的源代码如下
//这里的渲染三角网 是计算三角网各个顶点的法向量
//顶点法向量是使用该顶点所有三角形法向量平均值 或者凸组合方法中的little法
//平均值法就是计算各个三角面的法向量的平均值
//little法就是按照三角形各个点的距离来加权
//另外还要计算顶点的偏导数 同样用little法
C3DdVector cur_normal, normal, total_normal; //计算后的法向量
int index1,index2,index3; //当前三角形的邻接三角形
int i,j;
C3DdVector *p_Normal = new C3DdVector[n_ptcount]; // 点的法向量
double * p_fx = new double[n_ptcount]; // 点的偏导数
double * p_fy = new double[n_ptcount]; // 点的偏导数
double * p_count = new double[n_ptcount]; //临时存放每一点的权值
double w, l1,l2; //little法中的权值
//计算顶点法向量
for( i=0; i
p_Normal[i].SetVector(0,0,0);
p_count[i] = 0;
p_fx[i] = 0;
p_fy[i] = 0;
}
for( j=0; j
m_trinet[j].CalcNormal();
index1 = m_trinet[j].x;
index2 = m_trinet[j].y;
index3 = m_trinet[j].z;
l1 = m_trinet[j].m_pVertex[0].Distance(m_trinet[j].m_pVertex[1]);
l2 = m_trinet[j].m_pVertex[0].Distance(m_trinet[j].m_pVertex[2]);
w = l1*l1*l2*l2;
if(w>1.0e-8)
{
p_Normal[index1].vx += m_trinet[j].normal.vx/w;
p_Normal[index1].vy += m_trinet[j].normal.vy/w;
p_Normal[index1].vz += m_trinet[j].normal.vz/w;
p_count[index1] += 1.0/w ;
p_fx[index1] += -m_trinet[j].normal.vx/(m_trinet[j].normal.vz*w);
p_fy[index1] += -m_trinet[j].normal.vy/(m_trinet[j].normal.vz*w);
}
l1 = m_trinet[j].m_pVertex[1].Distance(m_trinet[j].m_pVertex[0]);
l2 = m_trinet[j].m_pVertex[1].Distance(m_trinet[j].m_pVertex[2]);
w = l1*l1*l2*l2;
if(w>1.0e-8)
{
p_Normal[index2].vx += m_trinet[j].normal.vx/w;
p_Normal[index2].vy += m_trinet[j].normal.vy/w;
p_Normal[index2].vz += m_trinet[j].normal.vz/w;
p_count[index2] += 1.0/w;
p_fx[index2] += -m_trinet[j].normal.vx/(m_trinet[j].normal.vz*w);
p_fy[index2] += -m_trinet[j].normal.vy/(m_trinet[j].normal.vz*w);
}
l1 = m_trinet[j].m_pVertex[2].Distance(m_trinet[j].m_pVertex[0]);
l2 = m_trinet[j].m_pVertex[2].Distance(m_trinet[j].m_pVertex[1]);
w = l1*l1 + l2*l2;
if(w>1.0e-8)
{
p_Normal[index3].vx += m_trinet[j].normal.vx/w;
p_Normal[index3].vy += m_trinet[j].normal.vy/w;
p_Normal[index3].vz += m_trinet[j].normal.vz/w;
p_count[index3] += 1.0/w;
p_fx[index3] += -m_trinet[j].normal.vx/(m_trinet[j].normal.vz*w);
p_fy[index3] += -m_trinet[j].normal.vy/(m_trinet[j].normal.vz*w);
}
}
// CStdioFile fp;
// CFileException e;
// fp.Open("computer_normal.txt", CFile::modeCreate|CFile::modeWrite,&e);
// CString aline;
p_Normal[0].SetVector(0,0,-p_count[0]);
p_fx[0] = 0;
p_fy[0] = 0;
for(i=0; i
if(p_count[i])
{
p_Normal[i].vx /= p_count[i];
p_Normal[i].vy /= p_count[i];
p_Normal[i].vz /= p_count[i];
p_fx[i] /= p_count[i];
p_fy[i] /= p_count[i];
p_Normal[i].Normalize(1.0);
m_sPoint[i].normal = p_Normal[i];
m_sPoint[i].fx = p_fx[i];
m_sPoint[i].fy = p_fy[i];
// C3DdPoint tttt = m_sPoint[i].GetPoint();
TRACE("/nPoint:%f %f %f ",m_sPoint[i].GetPoint().x, (m_sPoint[i].GetPoint()).y, (m_sPoint[i].GetPoint()).z );
TRACE("/nfxy: %f %f", m_sPoint[i].fx,m_sPoint[i].fy);
}
// aline.Format("point %d %f %f %f Normal %f %f %f/n",i,m_sPoint[i].x, m_sPoint[i].y, m_sPoint[i].z,
// p_Normal[i].vx, p_Normal[i].vy, p_Normal[i].vz );
// fp.WriteString(aline);
/*TRACE("/npoint %d %f %f %f /nNormal %f %f %f",i,
m_sPoint[i].x, m_sPoint[i].y, m_sPoint[i].z,
p_Normal[i].vx, p_Normal[i].vy, p_Normal[i].vz );*/
}
// fp.Close();
//把顶点法向量赋予三角形中的顶点
for(j=0; j
index1 = m_trinet[j].x;
m_trinet[j].m_pNormal[0] = p_Normal[index1];
m_trinet[j].m_fXYZ[0].x = p_fx[index1];
m_trinet[j].m_fXYZ[0].y = p_fy[index1];
index2 = m_trinet[j].y;
m_trinet[j].m_pNormal[1] = p_Normal[index2];
m_trinet[j].m_fXYZ[1].x = p_fx[index2];
m_trinet[j].m_fXYZ[1].y = p_fy[index2];
index3 = m_trinet[j].z;
m_trinet[j].m_pNormal[2] = p_Normal[index3];
m_trinet[j].m_fXYZ[2].x = p_fx[index3];
m_trinet[j].m_fXYZ[2].y = p_fy[index3];
}
delete p_Normal;