c语言sscanf函数的用法是什么
396
2022-09-23
openGL中关于顶点的法线、切线、副切线
openGL系列文章目录
前言
我们在openGL中绘制球体,或者圆环体的时候,经常可以到法线、切线、副切线这几个词,一脸疑惑,特别是副切线,这时什么鬼?
一、法线(normal)
物体模型由成百上千个平坦的三角形围成.三角形上附加纹理的方式来增加额外细节. 提升真实感. 但是近看时,就有问题了.变成平面.缺乏细节.原因: 物体表面并非平坦的. 而是表示出无数(凹凸不平的)细节.如果以光的视角来看这个问题:是什么使表面被视为完全平坦的表面来照亮? 这时就需要法线
法线定义
二、切线(tangent)
切线定义
副切线(bitangent)
在着色器中的描述和计算
vec3 tangent;vec3 binormal;vec3 c1 = cross(a_normal, vec3(0.0, 0.0, 1.0));vec3 c2 = cross(a_normal, vec3(0.0, 1.0, 0.0));if (length(c1)>length(c2)){ tangent = c1;}else{ tangent = c2;}tangent = normalize(tangent);binormal = cross(v_nglNormal, tangent);binormal = normalize(binormal);
法线,切线,副切线函数实现
Vector3 Math::calculateTangentSpaceVector( const Vector3& position1, const Vector3& position2, const Vector3& position3, Real u1, Real v1, Real u2, Real v2, Real u3, Real v3) { //side0 is the vector along one side of the triangle of vertices passed in, //and side1 is the vector along another side. Taking the cross product of these returns the normal. Vector3 side0 = position1 - position2; Vector3 side1 = position3 - position1; //Calculate face normal Vector3 normal = side1.crossProduct(side0); normal.normalise(); //Now we use a formula to calculate the tangent. Real deltaV0 = v1 - v2; Real deltaV1 = v3 - v1; Vector3 tangent = deltaV1 * side0 - deltaV0 * side1; tangent.normalise(); //Calculate binormal Real deltaU0 = u1 - u2; Real deltaU1 = u3 - u1; Vector3 binormal = deltaU1 * side0 - deltaU0 * side1; binormal.normalise(); //Now, we take the cross product of the tangents to get a vector which //should point in the same direction as our normal calculated above. //If it points in the opposite direction (the dot product between the normals is less than zero), //then we need to reverse the s and t tangents. //This is because the triangle has been mirrored when going from tangent space to object space. //reverse tangents if necessary Vector3 tangentCross = tangent.crossProduct(binormal); if (tangentCross.dotProduct(normal) < 0.0f) { tangent = -tangent; binormal = -binormal; } return tangent; }
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~