1

Currently I am trying to get lighting working in my OpenTK application. Im using the tutorial https://learnopengl.com/Lighting/Basic-Lighting. In it, they load all their vertices along with normalized vectors for the direction of the faces for use in diffuse lighting. The problem is I'm using an ElementArrayBuffer (indices) along with my ArrayBuffer (Vertices) and I can't figure out how to load in the normalized vectors. I can't combine them with the vertices, because they correspond to faces, not the points. These are my points:

         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]
        };

How can I combine the normalized data with the vertices in an efficient way to pass to the shaders in GL.bufferData ?

UPDATE*

I do not need help combining the data, I need help rendering it.

Something I tried was to put the normals into the vertex array like this:

        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,
        };

I tried telling the 1st to look at every other index, and the 2nd to look at the rest, but it doesn't render properly... Do indices not work like that?

My setup and render code:

        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);

And:

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

I was wondering If the Draw function wasnt working due to how it was looking at the indices, Is that right?

  • Possible duplicate of [OpenGL - Index buffers difficulties](https://stackoverflow.com/questions/23349080/opengl-index-buffers-difficulties) – Nico Schertler May 13 '18 at 07:13

0 Answers0