Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 3 of 3

Thread: Drawing lines with glDrawElements

  1. #1
    Newbie Newbie
    Join Date
    May 2018
    Posts
    1

    Question Drawing lines with glDrawElements

    Hi everyone,

    I've just started learning OpenGL over the last couple of days and am currently trying to draw a grid. For my testing purposes however I'm attempting to draw a single line. I should also mention I'm trying to do it with a VBO.

    So creating my buffers & shader:

    Code :
    std::vector<glm::vec3> vertices;
    std::vector<GLuint> indices;
    GLuint vbo, indexBuffer;
    // Add Vertices
    vertices.push_back(glm::vec3(0.0f, 0.0f, 0.0f));
    vertices.push_back(glm::vec3(5.0f, 0.0f, 0.0f));
    // Add indicies
    indices.push_back(0);
    indices.push_back(1);
     
    // Create buffers
    glGenBuffers(1, &vbo);
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec3) * vertices.size(), &vertices[0], GL_STATIC_DRAW);
     
    glGenBuffers(1, &indexBuffer);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint) * indices.size(), &indices[0], GL_STATIC_DRAW);
     
    // Create shader
    Shader gridShader = Shader();
    gridShader.loadShaders("shaders/shdr_grid.vert", "shaders/shdr_grid.frag");

    Then in my render loop my code for drawing is:
    Code :
    gridShader.use();
    gridShader.setUniform("view", view);
    gridShader.setUniform("projection", projection);
     
    glEnableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
     
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
    glDrawElements(GL_LINES, indices.size(), GL_UNSIGNED_INT, 0);
     
    glDisableVertexAttribArray(0);

    The view and projection matrix are confirmed correct when displaying models.

    Now shader time

    Vertex:
    Code :
    #version 330
     
    uniform mat4 projection;
    uniform mat4 view;
     
    layout (location = 0) in vec3 pos;
     
    void main(void) 
    {
    	gl_Position = projection * view * vec4(pos, 1.0);
    }

    Fragment:
    Code :
    #version 330
     
    out vec4 outColor;
     
    void main(void) {
    	outColor = vec4(1.0, 1.0, 1.0, 1.0);
    }

    I figured I didn't need a model matrix if the grid was never going to transform (which it isn't), as I'd just be passing in an identity matrix.

    Thanks in advance!
    Kaine

  2. #2
    Junior Member Newbie
    Join Date
    May 2018
    Posts
    9
    Hi
    You could use a geometry shader, which is a lot more flexible and versatile. Since in openGL, we are already using shaders, learning of the geometry shader is a bonus. https://learnopengl.com/Advanced-OpenGL/Geometry-Shader
    By the way, that's a good source for learning openGL.
    Cheers

  3. #3
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    2,928
    Quote Originally Posted by AiJai1 View Post
    You could use a geometry shader, which is a lot more flexible and versatile.
    A geometry shader isn't much use here. Also, they're slow (mostly because they de-duplicate all of the vertices, which drastically increases memory pressure).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •