openglrenderingubuntu-16.04intelmesa

OpenGL-Intel 630 GPU -Ubuntu 16.04 failed to render simple triangle


I have written a simple openGL code to render a triangle, it is compiled successfully but failed to render triangle. Only create a window as such there is no error return by glGetError() api call. Same code works fine on AMD R9 GPU. Driver installation is also correct as I'm able to run glxdemos like glxgears or glxhead without any error. Please help me to catch root cause of this issue.

here is my system configuration. CPU - intel i5 7400 (Kaby Lake 630 HD GPU) OS - Ubuntu 16.04 64-bit MESA - 3.0 v17.03

here is my code to render a triangle.

#include <stdio.h>
#include <stdlib.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>

int main(int agrc, char **argv)
{
    //do windowing related stuff here

    if ( !glfwInit())
    {
            printf("Error: Failed to initialize GLFW\n");
            return -1;
    }

    GLFWwindow* window = glfwCreateWindow(800, 600, "Triangle", NULL, NULL);
    if (window == NULL)
    {
            printf("Failed to create GLFW window\n");
            glfwTerminate();
            return -1;
    }
    glfwMakeContextCurrent(window);

    glewExperimental = GL_TRUE;
    if (glewInit() != GLEW_OK)
    {
            printf("Error: Failed to initialize GLEW\n");
            return -1;
    }

    //declare vertices
    GLfloat verts[] =
    {
            +0.0f, +0.5f, +0.0f,
            -0.5f, -0.5f, +0.0f,
            +0.5f, -0.5f, +0.0f,
    };

    //VBO related activity
    //declare VAO, VBO 
    GLuint VAO, VBO, EBO;

    //get unique name/ID
    glGenVertexArrays(1, &VAO);
    glGenBuffers(1, &VBO);
    //glGenBuffers(1, &EBO);

    // Bind VAO first, then bind and set VBOs and then configure vertex attributes
    //bind VAO
    glBindVertexArray(VAO);

    //bind VBO
    glBindBuffer(GL_ARRAY_BUFFER, VBO);

    //copy data to GPU
    glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);

    glEnableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindVertexArray(0);

    glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);

    glBindVertexArray(VAO);
    glDrawArrays(GL_TRIANGLES, 0, 3);

    glfwSwapBuffers(window);

    do{
            glfwPollEvents();
    }while(glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS && glfwWindowShouldClose(window) == 0);
    return 0;

}

Solution

  • I don't know if this is a solution for your issue, at least here are some advices:

    You render the triangle (by calling glDrawArrays) only once, right after glew & other gl setup. This is not good, because as soon as something changes your picture gets lost. The main of "something changes" is the window size and position.

    The window manager in Ubuntu (Xorg or Wayland) is asynchronous. This means that you sould wait until the window is available ("realized" in Xorg parlance). A way of dealing with this is to poll events, because normally the window is realized and then given a size and position, and these actions trigger events.

    So the first thing for your code is that you should move your render code inside the loop where you call glfwPollEvents.

    Other matter is that you don't set a size for the viewport (rectangle to be draw inside the window, likely the whole window). OpenGL uses a default viewport fitted to the window. But it you change the size of the window you need to call glViewport again. Conclusion: make a "callback" for glfw size-event where you change the viewport.

    Also you don't use shaders. Well, perhaps for your first test app you can avoid them (OpenGL has default shaders). But I strongly advise to start right now using them, even being them a bit hard for the first time. They are a must.

    Finally, follow a good tutorial that uses OpenGL >= 3.2 The are plenty of out there. To name two: https://learnopengl.com/ and Learning Modern 3D Graphics Programming