0

私は、いくつかの NeHe チュートリアル (OpenTK に変換) に従うことと、オンラインで見つけた三角形を描画するための OpenTK サンプル (簡単な初期設定用) との間のクロスを行っています。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Diagnostics;
using System.IO;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using System.Windows.Forms;

namespace GravSimBasic
{
    class GLMain : GameWindow
    {
        int vbo;
        Vector3[,] vertices;

        float time = 0.01f;

        void CreateVertexBuffer()
        {
            vertices = new Vector3[2,3];
            vertices[0,0] = new Vector3(-1f, -1f, (float)Math.Sin(time));
            vertices[0, 1] = new Vector3(0.5f, -1f, (float)Math.Sin(time));
            vertices[0, 2] = new Vector3(-0.25f, 1f, -(float)Math.Sin(time));
            vertices[1, 0] = new Vector3(-0.5f, -1f, (float)Math.Cos(time));
            vertices[1, 1] = new Vector3(1f, -1f, (float)Math.Cos(time));
            vertices[1, 2] = new Vector3(0.25f, 1f, -(float)Math.Cos(time));

            //MessageBox.Show("Length: " + vertices.Length.ToString());

            GL.GenBuffers(1, out vbo);
            GL.BindBuffer(BufferTarget.ArrayBuffer, vbo);
            GL.BufferData<Vector3>(BufferTarget.ArrayBuffer,
                                   new IntPtr(vertices.Length * Vector3.SizeInBytes),
                                   vertices, BufferUsageHint.StaticDraw);
        }

        protected override void OnLoad(EventArgs e)
        {
            //set the window area
            GL.Viewport(0, 0, 400, 400);
            //background color
            GL.ClearColor(Color.Black);
            //set the view area
            GL.MatrixMode(MatrixMode.Projection);
            GL.LoadIdentity();
            GL.Ortho(-2, 2, -2, 2, 2, -2);
            //now back to 'scene editing' mode
            GL.MatrixMode(MatrixMode.Modelview);
            GL.LoadIdentity();
            //make things look nice
            GL.ShadeModel(ShadingModel.Smooth);

            //set up our z-rendering logic
            GL.ClearDepth(2.0000f);
            GL.Enable(EnableCap.DepthTest);
            GL.DepthFunc(DepthFunction.Lequal);

            //other improvements to quality
            GL.Hint(HintTarget.PerspectiveCorrectionHint, HintMode.Nicest);
            GL.Hint(HintTarget.LineSmoothHint, HintMode.Nicest);
            //initialize our scene data
            CreateVertexBuffer();

        }

        protected override void OnRenderFrame(FrameEventArgs e)
        {
            time += 0.01f;
            vertices[0, 0].Z = (float)Math.Sin(time);
            vertices[0, 1].Z = (float)Math.Sin(time);
            vertices[0, 2].Z = -(float)Math.Sin(time);
            vertices[1, 0].Z = (float)Math.Cos(time);
            vertices[1, 1].Z = (float)Math.Cos(time);
            vertices[1, 2].Z = -(float)Math.Cos(time);
            GL.BufferData<Vector3>(BufferTarget.ArrayBuffer,
                                   new IntPtr(vertices.Length * Vector3.SizeInBytes),
                                   vertices, BufferUsageHint.StaticDraw);
            System.Threading.Thread.Sleep(10);



            GL.Clear(ClearBufferMask.ColorBufferBit);

            GL.EnableVertexAttribArray(0);
            GL.BindBuffer(BufferTarget.ArrayBuffer, vbo);
            GL.Color4(0.75f,0.0f,0.0f,0.25f);
            GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 0, 0);

            GL.DrawArrays(BeginMode.Triangles, 0, 3);

            GL.Color4(0.0f, 0.75f, 0.0f, 0.55f);
            GL.VertexAttribPointer(3, 3, VertexAttribPointerType.Float, false, 0, 0);
            GL.DrawArrays(BeginMode.Triangles, 3, 3);

            GL.DisableVertexAttribArray(0);

            SwapBuffers();
        }
    }
}

問題は行 58 ~ 60 にあると思われますが、行 58 の値を -2.0、0.00001、および 2.0 の間で変更しましたが、結果は変わりませんでした。ただし、数行前のパースペクティブ設定である可能性があります。60行目のパラメーターとして利用可能なほぼすべての関数を試しました.Lequalは、私が期待する最良のオプションのようであり、私が望むものに最も近い結果を生成しますが、完全には正しくありません.

セットアップ: 緑と赤の三角形があります。それらは、xy 軸上で部分的に重なっています。上の z 軸は -sin(time) 関数によってマップされ、下の z 軸は sin(time) 関数によってマップされます。もう 1 つは sin の代わりに cos() を使用しますが、それ以外は同じです。「時間」の値はレンダリングごとに変わります。

私が望む/期待するもの: 2 つの重なり合う三角形 - 1 つは赤、もう 1 つは緑。前後に回転すると、それぞれの重複しない部分が常に表示され、重複する部分は最前面の三角形のみが表示されます。

私が得たもの:(a)何も(b)両方の三角形を重ねて表示。(c) 三角形の 1 つまたは両方のビットの変化する画像、または三角形のいずれも表示されていない場合でも、いずれかの三角形のビットが表示されているはずですが、欠落しています (背景)。

時間を削除すると、正しいスナップショットが表示されます。下半分の前にある赤い三角形と、上の前にある緑の三角形です。

誰かがこれを診断するのを助けることができますか?

4

1 に答える 1

2

86 行目では、深度バッファーではなくカラー バッファーのみをクリアしているため、58 を変更しても効果はありません。

 GL.Clear(ClearBufferMask.ColorBufferBit);

私は OpenTK を使用したことがありませんが、次のようにする必要があると思います。

 GL.Clear(ClearBufferMask.ColorBufferBit|ClearBufferMask.DepthBufferBit);
于 2012-10-06T16:07:07.283 に答える