0
votes

I'm following the OpenGL Programming Guide:8th edition, and I'm kind of stuck in the 1st chapter (triangles.cpp).

I'm running Ubuntu 14.04.2, and this is the code used:

// Draws 2 triangles on the screen

#include <iostream>
using namespace std;

#include "vgl.h"
#include "LoadShaders.h"

enum VAO_IDs { Triangles, NumVAOs };
enum Buffer_IDs { ArrayBuffer, NumBuffers };
enum Attrib_IDs { vPosition = 0 };

GLuint VAOs[NumVAOs];
GLuint Buffers[NumBuffers];

const GLuint NumVertices = 6;

// init

void init(void)
{
    glGenVertexArrays(NumVAOs, VAOs);
    glBindVertexArray(VAOs[Triangles]);

    GLfloat vertices[NumVertices][2] = {
        { -0.90, -0.90 }, //Triangle 1
        {  0.85, -0.90 },
        { -0.90,  0.85 },
        {  0.90, -0.85 }, //Triangle 2
        {  0.90,  0.90 },
        { -0.85,  0.90 }
    };

    glGenBuffers(NumBuffers, Buffers);
    glBindBuffer(GL_ARRAY_BUFFER, Buffers[ArrayBuffer]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices),
                 vertices, GL_STATIC_DRAW);

    ShaderInfo shaders[] = {
        { GL_VERTEX_SHADER, "trianges.vert" },
        { GL_FRAGMENT_SHADER, "triangles.frag" },
        { GL_NONE, NULL }
    };

    GLuint program = LoadShaders(shaders);
    glUseProgram(program);

    glVertexAttribPointer(vPosition, 2, GL_FLOAT,
                      GL_FALSE, 0, BUFFER_OFFSET(0));
    glEnableVertexAttribArray(vPosition);
}

// display
void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT);

    glBindVertexArray(VAOs[Triangles]);
    glDrawArrays(GL_TRIANGLES, 0, NumVertices);

    glFlush();
}

// main
int main (int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA);
    glutInitWindowSize(512, 512);
    glutInitContextVersion(4, 3);
    glutInitContextProfile(GLUT_CORE_PROFILE);
    glutCreateWindow(argv[0]);

    if (glewInit()) {
        cerr << "Unable to initialize GLEW ... exiting" << '\n';
        exit(EXIT_FAILURE);
    }

    init();

    glutDisplayFunc(display);

    glutMainLoop();
}

This code compiles successfully, but yields a Segmentation Fault on execution.

I used gdb to debug the error. A random window does pop up (only when I used gdb), but its simply displaying what's behind the window (so it's crashed, doesn't display the two triangles).

This is the output from gdb:

Traceback (most recent call last):
  File "/usr/share/gdb/auto-load/usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19-gdb.py", line 63, in <module>
from libstdcxx.v6.printers import register_libstdcxx_printers
ImportError: No module named 'libstdcxx'

Program received signal SIGSEGV, Segmentation fault.
0x0000000000000000 in ?? ()

The program should be displaying 2 triangles on a window.

I'm relatively new to C++, and I'm very new to linking libraries and debugging with gdb. I've seen many Q&A on the Segmentation Fault. A person did have the exact same problem as I did, but unfortunately, he deleted the question.

I don't really feel like this is a duplicate, since I didn't really understand most of the answers I've read on the same topic. Please make the answer as simple as possible.

I'm using the freeglut3-dev library, and I'm using NVIDIA Driver version 346.59.

EDIT: The exact error happens at init() in

glGenVertexArrays(NumVAOs, VAOs);
glBindVertexArray(VAOs[Triangles]);
1
compile with debugging symbols enabled (-g), then run in gdb to see where the segmentation fault occured. aside from that, post a Minimal, Complete, and Verifiable example. - m.s.
I'm trying to learn more about gdb ( I just started today ). All that I could get is that the program faults at init(), but I couldn't get the specific line in init(). As for Minimal, Complete and Verifiable, I don't really know what this program is doing (since I haven't read a lot in OpenGL yet), so I cant really minimize the code. - ErrorAtLine0
@Yooyoory: As already told, compile with -g. Once GDB traps the program, use the commands backtrack, up, down and list to determine where exactly things went a wrong turn. - datenwolf
Are you sure your GL context is properly created and bound as current? If you get segfault on the very first call to GL, that is almost always the reason. - Mateusz Grzejek
@Yooyoory: yes, GLEW is broken with core profiles. As a workaround, you have to set glewExperimental=GL_TRUE before calling glewInit(). However, this workaround is not a proper fix, I explained the issue in more detail in this post. - derhass

1 Answers

0
votes

The segmentation fault occurred at the very first command that used GL. So, I made sure to import freeglut, which included all the necessary libraries for opengl too.

#include freeglut

I would like to thank Mateusz Grzejek for pointing this out.