0

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. enter image description here

Community
  • 1
  • 1
Recker
  • 1,915
  • 25
  • 55
  • where do you define the contents of the enabled arrays? you might also test the return value on the `glDrawArrays` call. – woolstar Dec 04 '13 at 04:11
  • Inside the vertex shader code.I have included it in the question. – Recker Dec 04 '13 at 04:12
  • You technically do not need any arrays enabled to draw vertices. If you have none then the only two things you will get unique to each vertex in the vertex shader are the pre-declared variables: `gl_VertexID` and `gl_InstanceID`. My concern is actually that you are using single-buffered rendering in an OpenGL 4.3 app. It is unheard of to use single-buffered drawing these days, in modern window systems you need double-buffering because they only update the contents of a window when you swap a back buffer to the front. Some hybrid GPU solutions also require double-buffering to work correctly. – Andon M. Coleman Dec 04 '13 at 04:28
  • @Andon M. Coleman I have `glfwSwapBuffers(window)` in my main rendering loop inside my `main()` function.Doesn't that mean that I am using Double buffering? – Recker Dec 04 '13 at 04:37
  • @Recker: Yes, usually. I saw the `glFlush (...)` at the end of a function called `display (...)` and you can see why I jumped to the conclusion that you were using single-buffered rendering. `glFlush (...)` is how you end a frame in a single-buffered solution. I am not sure why you are flushing the command buffer otherwise, to be honest. Please update your question to show the main rendering loop, it is possible you are doing something like clearing the color buffer twice or any manner of other things now that it is apparent that `display (...)` is not the entire scope of your drawing. – Andon M. Coleman Dec 04 '13 at 04:44
  • Updated. Indeed I am clearing the color buffer twice. Aren't I? – Recker Dec 04 '13 at 04:55
  • Yes, you are. But the order you are clearing the buffers in is perfectly safe. You clear the color / depth buffer, and then the first thing your call to `display (...)` on the next line does is clear the color buffer again. It is redundant, but this in itself should not explain the behavior you are experiencing :-\ You can remove the call to `glFlush (...)` completely from your software, by the way, because `glfwSwapBuffers (...)` implicitly flushes the command buffer. At this point, I suspect that your shaders are not compiled correctly - since you are not checking `glGetError (...)`. – Andon M. Coleman Dec 04 '13 at 04:58
  • Information with `glGetError(...)` updated. – Recker Dec 04 '13 at 05:15
  • `glFlush` never hurts. However, with that spam of GL errors in stdout - well, it shouldn't work, so it does not ^^. If you don't use some sort of GL debugger (like glintercept or whatever available on windows.. i don't know) - you could use KHR_debug extension combined with backtrace to pin down where exactly your problem started. – keltar Dec 04 '13 at 09:22
  • 1
    As I suspected, `glUseProgram (...)` will generate `GL_INVALID_OPERATION` if you try to use a program which has shaders that were not successfully compiled and/or linked attached. There are many other causes of `GL_INVALID_OPERATION`, but in your context this is the most likely. You should check the values of `glGetProgramInfoLog (...)` for your program object after linking and `glGetShaderInfoLog (...)` after compiling the vertex shader and after compiling the fragment shader. – Andon M. Coleman Dec 04 '13 at 21:01
  • @AndonM.Coleman To be honest, I am using source code in modified form from OpenGL SuperBible (6th Edition). I copied the code once again and changed the vertex shader code for Triangle by commenting out one point (line containing that point) and I was able to render the line by making changes in `glDrawArrays(...)` function. Why is underlined shader compiler so nit picky and meticulous about tabs and all? – Recker Dec 05 '13 at 02:13

0 Answers0