早教吧作业答案频道 -->其他-->
自己写的顶点法线计算器,传入顶点和索引数组,结果存放在传入的法线数组中.基本思路是按顶点编号遍历,看索引中如果有相同的顶点,就计算所在三角形的面法向量,最后把所有共面法向量相
题目详情
自己写的顶点法线计算器,
传入顶点和索引数组,结果存放在传入的法线数组中.基本思路是按顶点编号遍历,看索引中如果有相同的顶点,就计算所在三角形的面法向量,最后把所有共面法向量相加求平均.因为看过一个资料说法向量和面积成正比所以没有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.计算每个顶点法线(简单的邻接面法线均值;邻接面法线根据面积均值;奇异面特殊处理的法线均值等)不过你的思路也没错,具体细节,我没看~ 查看原帖>>
看了 自己写的顶点法线计算器,传入...的网友还看了以下:
求一个超简单的电路设计有两个电路,电路1和电路2电路1是用来控制电路2的,要求只要电路1处于接通状态 2020-03-31 …
如何用电路测量铝箔厚度可以利用的器材有:米尺,电压表,电流表,电源,滑动变阻器,开关和导线设计一个 2020-05-15 …
一道家庭电路的题(在线等)~小华的家庭电路中有PZ220-40的电灯4盏,功率为60W的电风扇一台 2020-06-04 …
旧路面上铺设十厘米厚稳定层再浇筑混凝土路面可以吗?改造工程中,原路面是砼,现在新路面标高再它之上, 2020-06-14 …
帮忙找个成语是来自一个典故的.大概意思是:一个人坐马车,然后在路上捡到了一个壶(或者是匣子),然后 2020-06-21 …
排列只写序号①信息高数公路②电子管计算机③集成电路计算机④半导体计算机⑤人工智能计算机⑥大规模集成 2020-06-29 …
用自己的话写出下面谚语的意思1.一年之计在于春,一日之计在于晨2.书山有路勤为径,学海无涯苦作舟 2020-07-05 …
(2014•淮安模拟)某风景区在一个直径AB为100米的半圆形花园中设计一条观光线路(如图所示).在 2020-11-12 …
填词语.(其中要有一个表示数目的字)阳村(XX)冰冻(XX)用自己的话写出下面谚语的意思.1、一年之 2020-11-15 …
开关柜内需要多少电表开关柜是380V的电压,接到塑壳断路器在断路器下边接五路电源,然后经断路器接负载 2020-12-01 …