OpenGL - 加载顶点,索引和用于照明的正常矢量

时间:2018-05-12 18:15:56

标签: c# opengl opentk light

目前我正在尝试在我的OpenTK应用程序中使用照明。我正在使用教程https://learnopengl.com/Lighting/Basic-Lighting。在其中,它们将所有顶点与用于漫反射照明的面的方向的标准化矢量一起加载。问题是我正在使用ElementArrayBuffer(索引)和我的ArrayBuffer(顶点),我无法弄清楚如何加载规范化的向量。我无法将它们与顶点组合,因为它们对应于面,而不是点。这些是我的观点:

         Vector3[] vertices = {     //s is the size of the cube
            new Vector3(s, s, s),   //0
            new Vector3(s, s, -s),  //1
            new Vector3(-s, s, -s), //2
            new Vector3(-s, s, s),  //3

            new Vector3(s, -s, s),  //4
            new Vector3(s, -s, -s), //5
            new Vector3(-s, -s, -s),//6
            new Vector3(-s, -s, s), //7
        };
        Vector3[] normals =
        {
            Vector3.UnitY,
            Vector3.UnitX,
            Vector3.UnitZ,
            -Vector3.UnitZ,
            -Vector3.UnitX,
            -Vector3.UnitY,
        };
        int[] indices =
        {
            0, 1, 3, 1, 2, 3, //normals[0]
            1, 0, 4, 1, 5, 4, //normals[1]
            3, 0, 4, 3, 7, 4, //normals[2]
            1, 2, 6, 1, 5, 6, //normals[3]
            3, 2, 6, 3, 7, 6, //normals[4]
            4, 5, 6, 4, 7, 6, //normals[5]
        };

如何将标准化数据与顶点以有效的方式组合以传递到GL.bufferData中的着色器?

UPDATE *

我不需要帮助组合数据,我需要帮助渲染它。

我试过的是将法线放入顶点数组中,如下所示:

        Vector3[] vertices = {
            new Vector3(s, s, s),   //0
            new Vector3(s, s, -s),  //1
            new Vector3(-s, s, -s), //2
            new Vector3(-s, s, s),  //3

            new Vector3(s, -s, s),  //4
            new Vector3(s, -s, -s), //5
            new Vector3(-s, -s, -s),//6
            new Vector3(-s, -s, s), //7

            Vector3.UnitY,  //8
            Vector3.UnitX,  //9
            Vector3.UnitZ,  //10
            -Vector3.UnitZ, //11
            -Vector3.UnitX, //12
            -Vector3.UnitY, //13
        };

        int[] indices =
        {
            0,  8, 1,  8, 3,  8, 1,  8, 2,  8, 3,  8,
            1,  9, 0,  9, 4,  9, 1,  9, 5,  9, 4,  9,
            3, 10, 0, 10, 4, 10, 3, 10, 7, 10, 4, 10,
            1, 11, 2, 11, 6, 11, 1, 11, 5, 11, 6, 11,
            3, 12, 2, 12, 6, 12, 3, 12, 7, 12, 6, 12,
            4, 13, 5, 13, 6, 13, 4, 13, 7, 13, 6, 13,
        };

我试着告诉第一个看每一个其他索引,第二个看其余的索引,但它没有正确呈现......指数不能那样工作吗?

我的设置和渲染代码:

        GL.BufferData(BufferTarget.ArrayBuffer, sizeof(float) * Vertices.Length * 3, Vertices, BufferUsageHint.DynamicDraw);
        GL.BufferData(BufferTarget.ElementArrayBuffer, sizeof(int) * Indices.Length, Indices, BufferUsageHint.DynamicDraw);

        //format data and enable attributes 4 shaders
        GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 6 * sizeof(float), 0);
        GL.EnableVertexAttribArray(0);

GL.DrawElements(BeginMode.Triangles, Indices.Length, DrawElementsType.UnsignedInt, 0);

我想知道如果Draw功能因为它是如何查看索引而无法正常工作,是吗?

0 个答案:

没有答案