OpenGL:查找顶点的法向单一矢量 [英] OpenGL: finding the normal, unitary vector of a vertex

查看:116
本文介绍了OpenGL:查找顶点的法向单一矢量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要在其上应用照明的对象.在具有顶点坐标的文本文件中对其进行了描述.

I have an object on which I want to apply lighting. It is described in a text file with its vertex coordinates.

我在OpenGL Superbible中阅读了以下内容:

I read the following in the the OpenGL Superbible:

void m3dFindNormal(M3DVector3f vNormal, const M3DVector3f vP1, const M3DVector3f vP2, const M3DVector3f vP3);

要使用此功能,请向其传递一个向量 存储法线和三个向量 (每个只是三个浮点数的数组) 从多边形或三角形 (指定为逆时针缠绕 命令).请注意,这返回正常 向量不一定是单位长度 (归一化).

To use this function, pass it a vector to store the normal, and three vectors (each just an array of three floats) from your polygon or triangle (specified in counterclockwise winding order). Note that this returned normal vector is not necessarily unit length (normalized).

我知道我通过顶点的顺序(顺时针或逆时针)确定法向矢量的方向,所以我想知道如何使该函数与

I understand that the order in which I pass the vertices (clockwise or counterclockwise) determines the orientation of the normal vector, so I wonder how can I make the function work with a file of this format.

推荐答案

嗯,这种格式看起来有点像OBJ,但是带有颜色,并且没有法线和纹理坐标.似乎这不是一个很好的选择!但是,如果您真的想要坚持下去...

Hmm, this format looks kind of OBJ-ish, but with colors and without normals and texture coords. Seems like it's not really a good choice! But if you really want to stick with it...

我的建议不是. 0是:

My piece of advice no. 0 is:

请认真使用其他格式. :)原始的原始二进制转储顶点(位置+法线+纹理坐标+颜色+其他),然后使用索引通常是最简单的解决方案.如果您真的 *真的*想要使用.off ...

Use another format, seriously. :) Raw binary dump of vertices (positions+normals+texture coords+colors+whatever) and then indices is the simplest solution usually. And if you really *really* want to use .off...

我的建议不是. 1是:

My piece of advice no. 1 is:

  • 将其导入Blender(用Python编写导入脚本或使用其他工具更改格式)
  • 让Blender为您计算法线(它将做得很好!)
  • 将其导出为具有法线的任何格式.

如果您出于任何原因不想(或不能)这样做,那么,关于如何自己生成法线的建议之二.

If you don't want to (or can't) do that for any reason, there's piece of advice no 2. on how to generate normals yourself:

  • 为每个脸部计算其临时脸部法线(对于脸部A,B,C,其为(B-A)x(C-B)).不要将其缩放为length = 1-脸部面积越大,法线向量将越长,这就是我们想要的(目前).
  • 对于每个顶点,将来自与该矢量相接触的面的所有面法线相加(这是按面法线的非规格化非常方便的地方-您将获得某种加权总和"的面面积)是权重),然后 then 归一化所得向量.这是可以使用的每个顶点法线的近似值.
  • for each face, calculate its temporary face-normal (for a face A,B,C, it's (B-A)x(C-B) ). Don't rescale it to length=1 - the bigger the face's area, the longer the resulting normal vector will be and that's what we want (for now).
  • for each vertex, sum all the face-normals from the faces touching that vector (this is where non-normalization of per-face normals comes handy - you'd get some kind of a "weighted sum" with the face area being the weight) and then normalize the resulting vector. This is a nice approximation of a per-vertex normal which you can use.

这篇关于OpenGL:查找顶点的法向单一矢量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆