早教吧作业答案频道 -->其他-->
自己写的顶点法线计算器,传入顶点和索引数组,结果存放在传入的法线数组中.基本思路是按顶点编号遍历,看索引中如果有相同的顶点,就计算所在三角形的面法向量,最后把所有共面法向量相
题目详情
自己写的顶点法线计算器,
传入顶点和索引数组,结果存放在传入的法线数组中.基本思路是按顶点编号遍历,看索引中如果有相同的顶点,就计算所在三角形的面法向量,最后把所有共面法向量相加求平均.因为看过一个资料说法向量和面积成正比所以没有normalize.但是结果却很不理想,float[] vertices = {...};short[] indices = {...};float[] normals = new float[vertices.length];// 存放normals的数组,大小与顶点数组一致private void prepareNormals(float[] vertices,short[] indices,float[] normals) { int numofIndices = indices.length;// 索引数 int numofVertices = vertices.length / 3;// 顶点数量 int numofTriangles = numofIndices / 3;// mesh的三角形数量 int temp0,temp1,temp2,numofPlane; // 3个临时变量等下做数组下标减少计算量 float px,py,pz,qx,qy,qz;// 用于计算单位三角形面法线的两个向量 for (int i = 0; i < numofVertices; i++) { numofPlane = 0;//重置共面数量 for (int j = 0; j < numofTriangles; j++) { temp0 = j * 3; temp1 = j * 3 + 1; temp2 = j * 3 + 2; if (indices[temp0] == i || indices[temp1] == i || indices[temp2] == i) { numofPlane++;//如果找到,共面+1 px = vertices[indices[temp1] * 3] - vertices[indices[temp0] * 3]; py = vertices[indices[temp1] * 3 + 1] - vertices[indices[temp0] * 3 + 1]; pz = vertices[indices[temp1] * 3 + 2] - vertices[indices[temp0] * 3 + 2]; qx = vertices[indices[temp2] * 3] - vertices[indices[temp0] * 3]; qy = vertices[indices[temp2] * 3 + 1] - vertices[indices[temp0] * 3 + 1]; qz = vertices[indices[temp2] * 3 + 2] - vertices[indices[temp0] * 3 + 2]; normals[i] += py * qz - pz * qy; normals[i + 1] += pz * qy - px * pz; normals[i + 2] += px * qy - py * qx; } } normals[i] /= numofPlane; normals[i + 1] /= numofPlane; normals[i + 2] /= numofPlane; } }
传入顶点和索引数组,结果存放在传入的法线数组中.基本思路是按顶点编号遍历,看索引中如果有相同的顶点,就计算所在三角形的面法向量,最后把所有共面法向量相加求平均.因为看过一个资料说法向量和面积成正比所以没有normalize.但是结果却很不理想,float[] vertices = {...};short[] indices = {...};float[] normals = new float[vertices.length];// 存放normals的数组,大小与顶点数组一致private void prepareNormals(float[] vertices,short[] indices,float[] normals) { int numofIndices = indices.length;// 索引数 int numofVertices = vertices.length / 3;// 顶点数量 int numofTriangles = numofIndices / 3;// mesh的三角形数量 int temp0,temp1,temp2,numofPlane; // 3个临时变量等下做数组下标减少计算量 float px,py,pz,qx,qy,qz;// 用于计算单位三角形面法线的两个向量 for (int i = 0; i < numofVertices; i++) { numofPlane = 0;//重置共面数量 for (int j = 0; j < numofTriangles; j++) { temp0 = j * 3; temp1 = j * 3 + 1; temp2 = j * 3 + 2; if (indices[temp0] == i || indices[temp1] == i || indices[temp2] == i) { numofPlane++;//如果找到,共面+1 px = vertices[indices[temp1] * 3] - vertices[indices[temp0] * 3]; py = vertices[indices[temp1] * 3 + 1] - vertices[indices[temp0] * 3 + 1]; pz = vertices[indices[temp1] * 3 + 2] - vertices[indices[temp0] * 3 + 2]; qx = vertices[indices[temp2] * 3] - vertices[indices[temp0] * 3]; qy = vertices[indices[temp2] * 3 + 1] - vertices[indices[temp0] * 3 + 1]; qz = vertices[indices[temp2] * 3 + 2] - vertices[indices[temp0] * 3 + 2]; normals[i] += py * qz - pz * qy; normals[i + 1] += pz * qy - px * pz; normals[i + 2] += px * qy - py * qx; } } normals[i] /= numofPlane; normals[i + 1] /= numofPlane; normals[i + 2] /= numofPlane; } }
▼优质解答
答案和解析
建议:1.设计一个合理的数据结构,方便计算,比如设计顶点的结构Point{顶点索引、邻接面等}2.计算每个顶点法线(简单的邻接面法线均值;邻接面法线根据面积均值;奇异面特殊处理的法线均值等)不过你的思路也没错,具体细节,我没看~ 查看原帖>>
看了 自己写的顶点法线计算器,传入...的网友还看了以下:
语文基本语法知识我喜欢张根硕(标出主谓宾)坚持的我狂热地喜欢帅气的张根硕(把什么宾语状语定语的都标 2020-05-13 …
如图所示,一质点沿螺旋线自外向内运动,已知其走过的弧长s与时间t成正比,关于该质点的运动,下列说法 2020-05-16 …
怎么学英语比较快,有什么方法?有点点基础.但就是学得慢 2020-05-17 …
利润计算方法做点小生意:过去卖货,与客户讨价还价,比如我100元成本价,一般我都会加价30个点,1 2020-07-23 …
下列成语或谚语中,符合马克思主义唯物辩证法观点或原理的是()1.守株待兔下列成语或谚语中,符合马克 2020-07-24 …
向量法求点到面的距离棱长为2的正方体ABCD-A1B1C1D1中,P,M,B分别为棱DD1,AB,B 2020-10-31 …
问,老师按照这样的方法,要多久这包烟就没了?老师给我布置一道题『我刚才买了一包烟20支装,假如我点着 2020-11-05 …
小明在运用尺规作已知∠O的平分线时,他的作法是①以点O为圆心,以任意长为半径画弧,两弧交角AOB两边 2020-11-06 …
颜公变法出新意,细筋入骨如秋鹰.——苏轼(1)材料中的“颜公”指的是谁?(2)他的书法的特点有什么? 2020-11-14 …
直线l:x-y=0与椭圆x方/2+y方=1相交于A,B两点,点C是椭圆上的动点,求三角形ABC面积的 2020-11-27 …