5

I know in 2.0- openGL we can draw a line simply like this.

glBegin(GL_LINES);  
glVertex3f(20.0f,150.0f,0.0f);  
glVertex3f(220.0f,150.0f,0.0f);  
glVertex3f(200.0f,160.0f,0.0f);  
glVertex3f(200.0f,160.0f,0.0f);  
glEnd();

but how to do similar thing in modern openGL(3.0+)

I have read Drawing round points using modern OpenGL but the answer is not about certain point,since I want to draw polygon with points have certain coordinates,it's not quite helpful.

I use this code,but it shows nothing except a blue background.what do I missed?

GLuint VertexArrayID;
    glGenVertexArrays(1, &VertexArrayID);
    glBindVertexArray(VertexArrayID);

static const GLfloat g_vertex_buffer_data[] = {
        20.0f, 150.0f, 0.0f, 1.0f,
            220.0f, 150.0f, 0.0f, 1.0f,
            200.0f, 160.0f, 0.0f, 1.0f
    };

GLuint vertexbuffer;
glGenBuffers(1, &vertexbuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);

do{

    // Clear the screen
    glClear( GL_COLOR_BUFFER_BIT );

    // 1rst attribute buffer : vertices
    glEnableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
    glVertexAttribPointer(
        0,                  // attribute 0. No particular reason for 0, but must match the layout in the shader.
        4,                  // size
        GL_FLOAT,           // type
        GL_FALSE,           // normalized?
        0,                  // stride
        (void*)0            // array buffer offset
    );

    // Draw the triangle !
    glDrawArrays(GL_LINES, 0, 2); // 3 indices starting at 0 -> 1 triangle

    glDisableVertexAttribArray(0);

    // Swap buffers
    glfwSwapBuffers(window);


} // Check if the ESC key was pressed or the window was closed
while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS &&
       glfwWindowShouldClose(window) == 0 );
Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
Shihe Zhang
  • 2,641
  • 5
  • 36
  • 57
  • 2
    Is this the complete code? You'll also need shaders. – Reto Koradi Mar 05 '15 at 05:03
  • what should be in the shader? @RetoKoradi – Shihe Zhang Mar 05 '15 at 07:55
  • Shaders are programs written in GLSL. Providing your own shaders is required if you want to use the OpenGL Core Profile. And if you don't, the code from the answer you accepted below is really wrong for your purpose. – Reto Koradi Mar 05 '15 at 08:08
  • For "how-to-write-and-use-shaders" please read the linked tutorial: http://www.opengl-tutorial.org/beginners-tutorials/tutorial-2-the-first-triangle/#Shaders There is all you need. – Flupp Mar 05 '15 at 17:50

1 Answers1

14

1) You have to define an array of vertices, that contain the points of your polygon lines. Like in your example:

GLfloat vertices[] =
{
    20.0f, 150.0f, 0.0f, 1.0f,
    220.0f, 150.0f, 0.0f, 1.0f,
    200.0f, 160.0f, 0.0f, 1.0f
};

2) You have to define and bind a Vertex Buffer Object (VBO) to be able to pass your vertices to the vertex shader. Like this:

// This is the identifier for your vertex buffer
GLuint vbo;
// This creates our identifier and puts it in vbo
glGenBuffers(1, &vbo);
// This binds our vbo
glBindBuffer(GL_ARRAY_BUFFER, vbo);
// This hands the vertices into the vbo and to the rendering pipeline    
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

3) Now we are ready to draw. Doing this:

// "Enable a port" to the shader pipeline
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
// pass information about how vertex array is composed
glVertexAttribPointer(0, // same as in glEnableVertexAttribArray(0)
                      4, // # of coordinates that build a vertex
                      GL_FLOAT, // data type
                      GL_FALSE, // normalized?
                      0,        // stride
                      (void*)0);// vbo offset

glDrawArrays(GL_LINES, 0, 2);
glDisableVertexAttribArray(0);

Step 1) and 2) can be done before rendering as initialization. Step 3) is done in your rendering loop. Also you'll need a vertex shader and a fragment shader to visualize the line with color.

If you don't know anything about these things and like to start with OpenGL 3, I'd suggest to start over with a tutorial like this: http://www.opengl-tutorial.org/beginners-tutorials/tutorial-1-opening-a-window/

Flupp
  • 856
  • 10
  • 24
  • Just wanted to add if you wish to change the vertices that make up your shape at any point, you will need to re-buffer the vertex data. I THINK the method name is something along the lines of `glBufferSubData`. – Benjamin James Drury Mar 04 '15 at 16:04
  • 1
    I use your code,but it shows nothing.Could you see what do I miss? – Shihe Zhang Mar 05 '15 at 02:21
  • Yes, what did he miss? I literally just tried this and nothing happens. – blkpingu Jun 27 '19 at 21:21
  • Quite a lot actually, this only covers a tiny amount of what you need to know to render in OpenGL 3.0. Follow the tutorials here: http://www.opengl-tutorial.org/beginners-tutorials/tutorial-1-opening-a-window/ and you should be up and running quickly enough. – Ian Young Nov 06 '19 at 12:16