-1

I am trying to draw a wire cube (edges only) using ElementBufferObject. I have set the coordinates of the eight vertices of the cube and an array of indices. Next, I initialize VAO, VBO and EBO for the future cube. Then I try to draw it, but I don't see any result.

Cube vertices and indices:

    private readonly float[] _cubeVertices =
    {
    //  Bottom side
        -0.5f, -0.5f,  0.0f, // [0] Front-left
         0.5f, -0.5f,  0.0f, // [1] Front-right
        -0.5f, -0.5f, -1.0f, // [2] Rear-left
         0.5f, -0.5f, -1.0f, // [3] Rear-right

    //  Upper side
        -0.5f,  0.5f,  0.0f, // [4] Front-left
         0.5f,  0.5f,  0.0f, // [5] Front-right
        -0.5f,  0.5f, -1.0f, // [6] Rear-left
         0.5f,  0.5f, -1.0f  // [7] Rear-right
    };

    //indices
    private readonly uint[] _cubeEdges =
    {
        0, 1,
        0, 2,
        0, 4,

        3, 2,
        3, 1,
        3, 7,

        5, 6,
        5, 4,
        5, 1,

        6, 7,
        6, 4,
        6, 2
    };

onLoad():

    _cubeShader = new Shader("../../../Shaders/cubeShader.vert", "../../../Shaders/shader.frag");
    _cubeShader.Use();

    _vboCube = GL.GenBuffer();
    GL.BindBuffer(BufferTarget.ArrayBuffer, _vboCube);
    GL.BufferData(BufferTarget.ArrayBuffer, _cubeVertices.Length * sizeof(float), _cubeVertices, BufferUsageHint.StaticDraw);

    _vaoCube = GL.GenVertexArray();
    GL.BindVertexArray(_vaoCube);

    var vertexLocation = _cubeShader.GetAttribLocation("aPosition");
    GL.VertexAttribPointer(vertexLocation, 3, VertexAttribPointerType.Float, false, 3 * sizeof(float), 0);

    _eboCube = GL.GenBuffer();
    GL.BindBuffer(BufferTarget.ElementArrayBuffer, _eboCube);
    GL.BufferData(BufferTarget.ElementArrayBuffer, _cubeEdges.Length * sizeof(uint), _cubeEdges, BufferUsageHint.StaticDraw);

Actual drawing:

    _cubeShader.Use();
    GL.BindVertexArray(_vaoCube);
    //GL.LineWidth(2.0f);
    GL.DrawElements(PrimitiveType.Lines, _cubeEdges.Length, DrawElementsType.UnsignedInt, 0);

cubeShader.vert:

    #version 330 core

    in vec3 aPosition;

    void main(void)
    {
        gl_Position = vec4(aPosition, 1.0);
    }

shader.frag:

    #version 330 core

    out vec4 FragColor;

    void main()
    {
        FragColor = vec4(1.0);
    }

Shader.cs code you can find here GitHub. Actually, all my code based on LearnOpenTK repo

This is my first introduction to OpenGL, so most likely I'm missing something important in the process. Is it even possible to draw lines using EBO? I have already tried the same thing with triangles and everything worked out.

Dezrhog
  • 28
  • 6
  • So your cube has only the top and bottom parts, and you're not doing any perspective projection, and are expecting to see anything? You're also not setting up your VAO, as far as the code shown here ..well, shows. Not checking any errors. Essentially not doing anything to help fix your problem, either yourself or by throwing it to other people. – Blindy Jun 11 '22 at 23:39
  • @Blindy I took all the code from here [GitHub](https://github.com/opentk/LearnOpenTK/tree/master/Chapter1/3-ElementBufferObjects). In the content of the post, I gave only my own changes. Using almost the same code, but for four vertices and triangles, I have successfully drawn a square. – Dezrhog Jun 11 '22 at 23:46
  • "I took all the code from here GitHub" -- well good, cause then your code should work identically to that code, and that code presumably works. Problem solved. I'm voting to close this, feel free to post actual compilable code that shows your problem if you later find out that your code isn't identical to the magical git repo you keep linking. – Blindy Jun 12 '22 at 00:07
  • @Blindy Here is my repository with all the current code [GitHub](https://github.com/Dezrhog/OpenGL-lab). At the moment, only the pyramid is displayed, but I need another cube. I don't have much experience in C#, because I work in Java. And there is no experience in OpenGL at all. I just don't know what specific information is needed to help me. – Dezrhog Jun 12 '22 at 00:14
  • @Rabbid76 Yes, it was also a problem, besides the fact that nothing was drawn. But I managed to do this by adding the corresponding matrices to the vertex shader of the cube. Thanks! – Dezrhog Jun 12 '22 at 10:17

1 Answers1

0

I figured it out. I forgot the line

GL.EnableVertexAttribArray(vertex Location);

after calling

GL.vertexAttribPointer()
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
Dezrhog
  • 28
  • 6
  • Just in case you're still confused why the negativity, there was no way to figure this out from what you posted, because you didn't post your VAO setup, you just claimed, and I quote, "I took all the code from here GitHub". And I still managed to guess it was your VAO setup, so yay go me lol. I figured if someone doesn't know enough to post their VAO setup calls, they don't know how to set it up in the first place and don't realize it's the most important part. – Blindy Jun 12 '22 at 14:11
  • @Blindy I'm not exactly surprised by the negativity on the Internet. But I showed in the question all the code that I used for the cube model. Therefore, I was rather surprised that you wrote that I do not show everything and I do not help solve my problem. If the GL.EnableVertexAttribArray function is what you were talking about, then I didn't understand you. Vertex Attrib Array and Vertex Attrib Object are not connected in my head. Perhaps it was worth adding definitions of the functions in which this code is executed so that there is more context. – Dezrhog Jun 13 '22 at 07:17
  • @Blindy By the way, in case someone doesn't have enough information, I left at the end of the text a question about the actual possibility of drawing models with lines while using EBO. In general, a positive answer to this question would be enough for me to understand that I am doing everything right in general, and I just need to find something missing. – Dezrhog Jun 13 '22 at 07:22