简体   繁体   中英

Draw wire cube using OpenTK

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.

I figured it out. I forgot the line

GL.EnableVertexAttribArray(vertex Location);

after calling

GL.vertexAttribPointer()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM