1

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;

}
  • If you change the size of the window with the mouse, does it draw anything, even just the background color? – Ripi2 Sep 13 '17 at 18:23
  • @Rabbid76 In Ubuntu many weird things can happen :( like the OGL firing on only after some action. I've seen it before with an Intel HD4000. – Ripi2 Sep 13 '17 at 19:29
  • some drivers enable depth_testing by default (violating opengl standard). Try disable it explicitly. Edit: Oh I see you are running Mesa. How brave of you, quite the gambler ;-) – Andreas Sep 13 '17 at 20:07
  • @Ripi2 - background color change is reflecting but no triangle, if tried to change window size using mouse it create corrupted image. – dilip shirke Sep 16 '17 at 02:10
  • @Andreas - added GL_DEPTH_BUFFER_BIT in "glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);" but no impact on output. – dilip shirke Sep 16 '17 at 02:21
  • Depth buffer is still on. Do glDisable(GL_DEPTH_TEST) – Andreas Sep 16 '17 at 09:31
  • Btw have you tried running the program in a debugger? CodeXL or gDEBugger – Andreas Sep 16 '17 at 09:32

1 Answers1

0

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

Ripi2
  • 7,031
  • 1
  • 17
  • 33
  • Thank you @Ripi2 for valuable information. 1. Same issue observed with GLUT lib as well. 2. Yes sure I'll try your suggested changed to bring render code inside loop, glViewport and call for glfw. 3. Meanwhile I tried with Shader code with version 320 es (ogles 3.2) it worked without any error. – dilip shirke Sep 18 '17 at 06:22