gpt4 book ai didi

c# - GL 项目无法正常工作

转载 作者:太空宇宙 更新时间:2023-11-03 14:25:06 25 4
gpt4 key购买 nike

我正在使用尝试像 Glu.Project() 一样工作的代码,因为 OpenTK 不支持 Glu。

        Vector4 pos = new Vector4(s.Position.X, 0.0f, s.Position.Y, 1.0f);
Matrix4 mov = new Matrix4();
Matrix4 prj = new Matrix4();
Matrix4 mpj = new Matrix4();
float[] vp = new float[4];

GL.GetFloat(GetPName.ModelviewMatrix, out mov);
GL.GetFloat(GetPName.ProjectionMatrix, out prj);
GL.GetFloat(GetPName.Viewport, vp);

Matrix4.Mult(ref prj, ref mov, out mpj);
Vector4.Transform(ref pos, ref mpj, out pos);

// Final mathematics as described in OpenGL 2.1 Glu specs
s.set2DPos(new Vector2f( (vp[0] + (vp[2] * (pos.X + 1) / 2.0f)),
(vp[1] + (vp[3] * (pos.Y + 1) / 2.0f)) ));

// Final mathematics as described in OpenGL 3 Vector specs
s.set2DPos(new Vector2f( (view[2] / 2 * pos.X + view[0]),
(view[3] / 2 * pos.X + view[1]) ));

// Neither of them work, but in relation OpenGL 3 vector specs work better.

s 是一个主要作为 3D 空间模型存在于 s.Position 的类。但是我从中得到的值在天文数字上远远超出了窗口边界。

断点处的 ModelView 矩阵:

{(1, 0, 0, 0)
(0, 0.7071068, 0.7071068, 0)
(0, -0.7071068, 0.7071068, 0)
(0, -141.4214, -141.4214, 1)}

断点的投影矩阵:

{(1.931371, 0, 0, 0)
(0, 2.414213, 0, 0)
(0, 0, -1.0002, -1)
(0, 0, -2.0002, 0)}

是我做错了什么还是我做错了什么?我错过了什么吗?

最佳答案

    Vector2 GetScreenCoordinates(Vector3 ObjectCoordinate)
{
// ref: http://www.songho.ca/opengl/gl_transform.html
Vector4 obj = new Vector4(ObjectCoordinate.X, ObjectCoordinate.Y, ObjectCoordinate.Z, 1.0f);
Matrix4 projection = new Matrix4();
Matrix4 modelView = new Matrix4();
Vector4 viewPort = new Vector4();

GL.GetFloat(GetPName.ModelviewMatrix, out modelView);
GL.GetFloat(GetPName.ProjectionMatrix, out projection);
GL.GetFloat(GetPName.Viewport, out viewPort);

Vector4
eye = Vector4.Transform(obj, modelView),
clip = Vector4.Transform(eye, projection);
Vector3
ndc = new Vector3(clip.X / clip.W, clip.Y / clip.W, clip.Z / clip.W);
Vector2
w = new Vector2(viewPort.Z / 2 * ndc.X + viewPort.X + viewPort.Z / 2,
viewPort.W / 2 * ndc.Y + viewPort.Y + viewPort.W / 2);

return w;
}

关于c# - GL 项目无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4316330/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com