我是opengl编程的新手,我一直在努力为我的3d形状获得顶点法线计算,这总是取决于我如何绘制和计算三角形,所以我想知道我是否可以通过使用法线贴图来避免法线计算?
任何帮助和/或参考资料都将不胜感激
发布于 2011-05-12 19:51:05
不,如果使用法线贴图,则无法避免法线计算。实际上,你必须为每个顶点计算两个额外的向量,切线和二法线,以使法线贴图工作。
然而,我看不出你有什么问题。计算法线大约是最容易做的事情。每面和每顶点法线计算的伪代码:
foreach face in model.faces:
face.normal = crossproduct(
model.vertices[face.vertindex[1]].pos - model.vertices[face.vertindex[0]].pos,
model.vertices[face.vertindex[2]].pos - model.vertices[face.vertindex[0]].pos )
foreach v in face.vertindex:
model.vertices[v].in_faces.append(face)
foreach vertex in model.vertices:
vertex.normal = (0,0,0)
for face in vertex.in_faces:
vertex.normal += face.normal
vertex.normal = vertex.normal / length(vertex.normal)
crossproduct(v0, v1):
return (
v0.y * v1.z - v0.z * v1.y,
v0.z * v1.x - v0.x * v1.z,
v0.x * v1.y - v0.y * v1.x,
)https://stackoverflow.com/questions/5976349
复制相似问题