私はC#でOpenTKを使用していますが、ここ数時間壁にぶつかっています。
Glu.UnProject()を使用して、ユーザーがクリックした宇宙を通る光線を作成しようとしています。トレースする光線は、投影面に垂直な線であるため、ユーザーには見えないはずです。
問題は、基本的なカメラ位置を使用している場合にのみ光線が見えないことです。カメラ(cameraXとcameraY)を動かすと、DrawLineからの線がUIに表示されます。近点と遠点はカメラの「目」と同一直線上にありません。
ここで問題のコードを分離しました:
glSurface_Paint(Object sender, PaintEventArgs e) {
double time = stopwatch.ElapsedMilliseconds * TimeScale + stopwatchOffset;
GL.MatrixMode(MatrixMode.Projection);
GL.LoadIdentity();
Glu.Perspective(10, (double)Width / Height, nearClip, farClip);
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
GL.MatrixMode(MatrixMode.Modelview);
GL.LoadIdentity();
Glu.LookAt(cameraX, cameraY, cameraZ * scaleFactor, 0, 0, 0, 0, 1, 0);
GL.ClearColor(Color.DarkGray);
if (ShowGrid) {
DrawGrid();
}
DrawLine();
glSurface.SwapBuffers();
}
これがDrawLineです。ハードコードされた値を使用して、ニア(z = 0)およびファー(z = 1)平面の(100、200)のポイントを投影解除します。次に、これら2つのポイントの間に線を引くだけです。
public void DrawLine() {
int[] viewport = new int[4];
double[] modelMatrix = new double[16];
double[] projMatrix = new double[16];
GL.GetInteger(GetPName.Viewport, viewport);
GL.GetDouble(GetPName.ModelviewMatrix, modelMatrix);
GL.GetDouble(GetPName.ProjectionMatrix, projMatrix);
Vector3 clickNearPoint;
Glu.UnProject(new Vector3(100, 200, 0), modelMatrix, projMatrix, viewport, out clickNearPoint);
Vector3 clickFarPoint;
Glu.UnProject(new Vector3(100, 200, 1), modelMatrix, projMatrix, viewport, out clickFarPoint);
nearPoint = new Point3D(clickNearPoint.X, -clickNearPoint.Y, clickNearPoint.Z);
farPoint = new Point3D(clickFarPoint.X, -clickFarPoint.Y, clickFarPoint.Z);
if (nearPoint.HasValue && farPoint.HasValue) {
GL.Color3(Color.White);
GL.Begin(BeginMode.Lines);
GL.Vertex3(nearPoint.Value.X, nearPoint.Value.Y, nearPoint.Value.Z);
GL.Vertex3(farPoint.Value.X, farPoint.Value.Y, farPoint.Value.Z);
GL.End();
}
}
これが問題のスクリーンショットです。左側の線は表示されないはずです!
私はこれを何時間も調査してきましたが、試すことができません。どんな入力でも大いに役立ちます。
PS。光源と問題を示すために必要のないものをすべて削除したため、白い線は黒く表示されます。