我正在使用带有着色器的“现代”OpenGL开发3D引擎。我遇到了一个我不知道如何解决的问题。
我正在尝试制作面向屏幕的基元。实际上,我在谈论箭头。我的输入是箭头的起点和方向。
我有一些可以执行此操作的软件,我想重现这种行为。
提及,箭头总是面向屏幕旋转模型。
此外,他们正在根据模型的缩放重新调整大小。
更新
Moby Disk 建议我尝试按照他的步骤操作。但是在应用下面的代码之后仍然无法获得理想的结果。请考虑我的代码:
var arrowStart = new Vector3(0, 0, 0);
var arrowLength = 1f;
var widthOfArrowHead = 0.1f;
var viewMatrix = Matrix4.LookAt(new Vector3(0, 0, 1), new Vector3(0, 0, 0), new Vector3(0, 1, 0));
var projectionMatrix = Matrix4.CreateOrthographic(ViewportSize.Width, ViewportSize.Height, NearZPlane, FarZPlane);
var modelMatrix = Matrix4.CreateRotationY(XRotation) * Matrix4.CreateRotationX(YRotation) * Matrix4.CreateRotationZ(0);
Vector3 cameraRight = new Vector3(viewMatrix[0, 0], viewMatrix[1, 0], viewMatrix[2, 0]);
Vector3 cameraUp = new Vector3(viewMatrix[0, 1], viewMatrix[1, 1], viewMatrix[2, 1]);
Vector3 arrowDirection = new Vector3(0, 1, 0);
Vector3 arrowEnd = new Vector3(arrowStart.X + arrowDirection.X * arrowLength, arrowStart.Y + arrowDirection.Y * arrowLength, arrowStart.Z + arrowDirection.Z * arrowLength);
arrowStart = Vector3.TransformPosition(arrowStart, viewMatrix * modelMatrix * projectionMatrix);
arrowEnd = Vector3.TransformPosition(arrowEnd, viewMatrix * modelMatrix * projectionMatrix);
var arrowHeadPoint = Vector3.One;
arrowHeadPoint = 0.9f * arrowLength * cameraUp + widthOfArrowHead * cameraRight;
var oneArrowHeadPoint4 = Vector4.Transform(new Vector4(arrowHeadPoint, 1.0f), viewMatrix * projectionMatrix);
有人可以帮助我完成这项工作吗?
答案 0 :(得分:0)
由于我的OpenGL知识已经过时,我没有资格回答这个问题!希望数学没有改变。 :-)从概念上讲,如果你知道世界空间中箭头的起点,你可以像这样计算一个高于它的终点:
CreateUpArrow(Point startOfArrow, Matrix viewMatrix)
{
const lengthOfArrow = 10
Vector cameraUp_worldspace = {viewMatrix[0][1], viewMatrix[1][1], viewMatrix[2][1]}
Point endOfArrow = cameraUp_worldspace * lengthOfarrow
// So endOfArrow is now 10 world units above the startOfArrow, where "above" is "toward the top of the screen"
// Now you have two points you can use to draw an arrow
// You can do something similar to create the arrowhead too
}
这类似于“广告牌”,因为您想要在3D空间中计算与相机对齐的点。使用广告牌,您知道广告牌的中心,并计算形成四边形的4个点。在这种情况下,也许你正在画线,所以你想要箭头的开头,箭头的末端,以及一些点来形成箭头。
另一种方法是绘制一个带箭头纹理的广告牌。
我从这篇关于广告牌http://www.opengl-tutorial.org/intermediate-tutorials/billboards-particles/billboards/的文章中偷了上面的公式:
在相机空间中,相机的向上矢量为(0,1,0)。在世界上获得它 空间,只需乘以从相机空间到的矩阵 世界空间,当然是视图矩阵的逆。
CameraRight_worldspace = {ViewMatrix [0] [0],ViewMatrix [1] [0],ViewMatrix [2] [0]}
CameraUp_worldspace = {ViewMatrix [0] [1],ViewMatrix [1] [1],ViewMatrix [2] [1]}
因此,您可以扩展我的伪代码以计算箭头上的其他点。箭头的最右边可能位于(0.9 * lengthOfArrow * cameraUp_worldspace + widthOfArrowHead * cameraRight_worldspace),以获得一个箭头长度为90%且稍微偏离右侧的点。左边做同样的事,得到4分做箭头。
我希望这有帮助!