This code draws the line, but if I want to draw a line from coordinate (10,10) to coordinate (20,20) what should I do? What do (.25, .25) and (.75, .75) mean?
3,342 6 6 gold badges 11 11 silver badges 29 29 bronze badges asked Jan 23, 2013 at 18:00 Abu Yousuf Abu Yousuf 6,057 4 4 gold badges 34 34 silver badges 56 56 bronze badgesWith OpenGL2 :
Each point value in glVertex2f is between -1 and 1, bottom left is (-1, -1), top right is (1,1) and center is (0, 0).
To map an absolute point to normalized space:
double x1 = 10; double y1 = 10; double x2 = 20; double y2 = 20; x1 = 2*x1 / w - 1; y1 = 2*y1 / h - 1; x2 = 2*x2 / w - 1; y2 = 2*y2 / h - 1; glBegin(GL_LINES); glVertex2f(x1, y1); glVertex2f(x2, y2); glEnd();
With OpenGL3+ : Using the programmable pipeline to draw a line is slightly more involved. You can create a Line class that will take two points and send them to the GPU, and draw them with a simple shader program. All the setup can be done in the constructor, and can be modified with a few access functions:
class Line < int shaderProgram; unsigned int VBO, VAO; vectorvertices; vec3 startPoint; vec3 endPoint; mat4 MVP; vec3 lineColor; public: Line(vec3 start, vec3 end) < startPoint = start; endPoint = end; lineColor = vec3(1,1,1); MVP = mat4(1.0f); const char *vertexShaderSource = "#version 330 core\n" "layout (location = 0) in vec3 aPos;\n" "uniform mat4 MVP;\n" "void main()\n" "\0"; const char *fragmentShaderSource = "#version 330 core\n" "out vec4 FragColor;\n" "uniform vec3 color;\n" "void main()\n" "\n\0"; // vertex shader int vertexShader = glCreateShader(GL_VERTEX_SHADER); glShaderSource(vertexShader, 1, &vertexShaderSource, NULL); glCompileShader(vertexShader); // check for shader compile errors // fragment shader int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL); glCompileShader(fragmentShader); // check for shader compile errors // link shaders shaderProgram = glCreateProgram(); glAttachShader(shaderProgram, vertexShader); glAttachShader(shaderProgram, fragmentShader); glLinkProgram(shaderProgram); // check for linking errors glDeleteShader(vertexShader); glDeleteShader(fragmentShader); vertices = < start.x, start.y, start.z, end.x, end.y, end.z, >; glGenVertexArrays(1, &VAO); glGenBuffers(1, &VBO); glBindVertexArray(VAO); glBindBuffer(GL_ARRAY_BUFFER, VBO); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices.data(), GL_STATIC_DRAW); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0); glEnableVertexAttribArray(0); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); > int setMVP(mat4 mvp) < MVP = mvp; return 1; >int setColor(vec3 color) < lineColor = color; return 1; >int draw() < glUseProgram(shaderProgram); glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "MVP"), 1, GL_FALSE, &MVP[0][0]); glUniform3fv(glGetUniformLocation(shaderProgram, "color"), 1, &lineColor[0]); glBindVertexArray(VAO); glDrawArrays(GL_LINES, 0, 2); return 1; >~Line() < glDeleteVertexArrays(1, &VAO); glDeleteBuffers(1, &VBO); glDeleteProgram(shaderProgram); >>;
Initializing some 3D lines with Line line(vec3 . vec3 . ) , setting the model-view-projection matrix line.setMVP(projection * view * model) and line.draw() and rotating the camera will produce something like this:
Note: If all you need is 2D lines, you will just need to specify the vec3 end point coordinates with the z value set to 0, and remove the projection matrix from the setMVP call, and set camera position to (0,0,0). The same applies to drawing 2D lines as explained above for OpenGL2, so coordinates will need to be sent to OpenGL in NDC space.