1

I would like to draw a triangle with a C++ program. I compiled the program with g++, with the '-Wall' flags and it compiles with no warnings. However, I get a segmentation fault coming from the function glGenBuffers(1, &m_VertexBuffer);. I have tried replacing the type declaration from uint32_t to GLuint, but the issue persists.

#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>

int main()
{
    if (!glfwInit())
    {
        std::cout << "Failed glfwInit()" << std::endl;
        return 1;
    }

    GLFWwindow* window = glfwCreateWindow(600,600, "triangle", NULL, NULL);
    if (!window)
    {
        glfwTerminate();
        std::cout << "Failed to create window" << std::endl;
        return 1;
    }
    glfwMakeContextCurrent(window);

    if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
    {
        glfwTerminate();
        std::cout << "Failed gladLoadGLLoader(...)" << std::endl;
        return 1;
    }
    
    float vertices[] = 
    {
        -.5f, -.5f, 0.f,
        .5f, .5f, 0.f,
        0.f, -.5f, 0.f
    }; //triangle

    uint32_t m_VertexBuffer;

    std::cout << "Generating buffer object from vertex array..." << std::endl;

    glGenBuffers(1, &m_VertexBuffer);

    std::cout << "Done." << std::endl;

    glBindBuffer(GL_ARRAY_BUFFER, m_VertexBuffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), &vertices[0], GL_STATIC_DRAW);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(float)*3, (void *) 0);
    glEnableVertexAttribArray(0);
    glViewport(0, 0, 600, 600);

    while(!glfwWindowShouldClose(window))
    {
        glClear(GL_COLOR_BUFFER_BIT);
        glDrawArrays(GL_TRIANGLES, 0, 3);
        glfwSwapBuffers(window);
        glfwPollEvents();
    }
    glfwTerminate();
}

When I execute the binary, here is the output of the terminal:

Generating buffer object from vertex array...
Segmentation fault

When I use gdb, here is the output (line 40 corresponds to the glGenBuffers(1, &m_VertexBuffer);) line.

Starting program: /home/jiageng/tri/build/tri 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".

Program received signal SIGSEGV, Segmentation fault.
0x0000000000000000 in ?? ()
(gdb) backtrace
#0  0x0000000000000000 in ?? ()
#1  0x0000555555557d8b in main () at /home/jiageng/tri/main.cpp:40

I see a black window called "triangle", but nothing is shown on this window.

Additional details: The code I am showing is adapted from a tutorial on youtube I was following. The working code of this tutorial is here (https://github.com/codetechandtutorials/tri/blob/main/main.cpp). The only things I have changed are replacing one-letter variable names with more descriptive terms, and added breakpoints and verbose statements.

I am on Windows Subsystem for Linux 2, and have verified that X11-port forwarding works properly so I can run GUI applications through VcXSrv.

Jia Geng
  • 11
  • 3
  • In the tutorial I am following, this is how the person calculated the number of bytes. Taking that it is wrong, it somehow still worked for that person. The segffault comes before this though. – Jia Geng Jan 06 '22 at 14:43
  • Why has my question been closed as a duplicate? It is not an issue with the size of array, and I am following original code of the tutorial, which works. – Jia Geng Jan 06 '22 at 14:46
  • @JiaGeng You could prove that the issue is not with the size of the array by removing that code from your [mre]. Try replacing everything after `std::cout << "Done." << std::endl;` with a comment like `// Program continues`, *then run your code again and update the stack trace if needed.* Additional code after a crash should not affect the crash, right? – JaMiT Jan 06 '22 at 14:49
  • What is you OS? – Rabbid76 Jan 06 '22 at 14:57
  • Thanks for explaining the reason. My OS is Ubuntu 20.04 LTS. Linux kernel is 5.10.16.3-microsoft-standard-WSL2. – Jia Geng Jan 06 '22 at 15:00
  • What is the OpenGL version ([`glGetString(GL_VERSION)`](https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glGetString.xhtml) after `gladLoadGLLoader`)? Have you tried updating the graphics driver? – Rabbid76 Jan 06 '22 at 15:06
  • It says its 1.4 (4.6.0 - Build 27.20.100.9664). I see, I think that is the problem because I downloaded glad version 3.3. I will try updating the graphics driver. – Jia Geng Jan 06 '22 at 15:10
  • @JiaGeng That's your problem. [`glGenBuffers`](https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glGenBuffers.xhtmlv) needs at least 2.0. – Rabbid76 Jan 06 '22 at 15:14
  • @Rabbid76 Thanks a lot for the spot! I will update OpenGL. – Jia Geng Jan 06 '22 at 15:19
  • I don't think you'll get this working on WSL with X11 forwarding. That type of environment (X11 forwarding) only supports OpenGL 1.4 (legacy MESA drivers). But my knowledge on this is at least 3 years old. So I may be wrong. – Atekihcan Jan 07 '22 at 10:38
  • @Atekihcan you are right, x11 forwarding does not allow any version of OpenGL higher than 1.4. OpenGL does not work on WSL. – Jia Geng Jan 20 '22 at 15:56
  • Microsoft had some plans to support GUI based apps on WSL. Not sure what happened to that. – Atekihcan Jan 21 '22 at 18:00

0 Answers0