My goal is to display multiple straight lines using OpenGL 3.0 or newer in C. I want the window to look something like the following.
Deprecated way
This is how I would do it using OpenGL 2.0, but I want to achieve this with newer versions of OpenGL.
glBegin(GL_LINES);
glVertex2f(10, 10);
glVertex2f(20, 20);
glEnd();
Create window
This code creates a simple window using OpenGL. Now I want to draw multiple lines inside that window, which are defined with absolute coordinates.
#include <stdio.h>
#include <stdlib.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
int main(int argc, char *argv[])
{
if (glfwInit() != GL_TRUE) {
fputs("Failed to initialize GLFW\n", stderr);
return EXIT_FAILURE;
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
GLFWwindow* window = glfwCreateWindow(640, 480, "Example", NULL, NULL);
glfwMakeContextCurrent(window);
glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK) {
fputs("Failed to initialize GLEW\n", stderr);
return EXIT_FAILURE;
}
while (!glfwWindowShouldClose(window)) {
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return EXIT_SUCCESS;
}