I am trying to draw a single line as a part of learning OpenGL 4.x and above. Here are the shaders that I am using.
Vertex Shader
"#version 430 core \n"
" \n"
"void main(void) \n"
"{ \n"
" const vec4 vertices[2] = vec4[2](vec4(0.25,-0.25,0.5,1.0),vec4(-0.25,-0.25,0.5,1.0)); \n"
" gl_Position = vertices[gl_VertexID]; n"
"} \n"
and Fragment Shader
"#version 430 core \n"
" \n"
"out vec4 color; \n"
" \n"
"void main(void) \n"
"{ \n"
" color = vec4(0.0, 0.8, 0.0, 1.0); \n"
"} \n"
And here is what I do in my display()
function.
void display()
{
static const GLfloat red[] = { 1.0f, 0.0f, 0.0f, 1.0f };
glClearBufferfv(GL_COLOR, 0, red);
glUseProgram(program);
glLineWidth(8.0f);
glDrawArrays(GL_LINES, 0, 2);
glFlush();
}
However, nothing gets drawn in the window except red color. I have read this,this and this questions. Two of them refer me to use Vertex Buffers and the third one tells me to draw a triangle with two vertices in the same position.
My question is, why does glDrawArrays(GL_LINES, 0, 2)
in my code fail to draw a single line? Or is it essential that I use Vertex Array Buffers when I need to draw a single line?
UPDATE:
Here is the code from the main()
function.
startShaders();
while (!glfwWindowShouldClose(window))
{
RenderFunction();
glfwSwapBuffers(window);
glfwPollEvents();
}
shutdown();
cout << "INFO: OpenGL Version: " << glGetString(GL_VERSION) << endl;
glfwTerminate();
exit(EXIT_SUCCESS);
And here is my RenderFunction()
that calls Display()
function.
void RenderFunction()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
display();
}
UPDATE 2:
Here is the pic of console window displaying stderr
output.