I have been reading a tutorial on modern OpenGL and GLSL, but when I run the examples for the project, it sometimes breaks. The code throws:
terminate called after throwing an instance of '__gnu_cxx::recursive_init_error'
what(): std::exception
Aborted (core dumped)
I then decided to remake it to see if it was a problem with the dependencies in it's source, but the same error occurs. If I run it enough times, it will eventually produce the desired output.
I have looked for other answers but they are either:
- About threads (which I don't believe I have in my code);
- In a foreign language (which comes to the same conclusion, after being translated); or
- Point to an obscure location in the
std
implementation talking about static objects (therefore no help at all).
main.cpp:
#include <GL/glew.h>
#include <GL/freeglut.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>
GLfloat vertices[] =
{
0.5, 0.5, 0.0, 1.0,
0.5, -0.5, 0.0, 1.0,
-0.5, -0.5, 0.0, 1.0,
-0.5, 0.5, 0.0, 1.0,
};
GLuint program;
GLuint vbo;
GLuint vao;
GLuint loadShader(GLenum shaderType, const char *filename)
{
GLuint shader = glCreateShader(shaderType);
std::ifstream file(filename);
std::stringstream data;
data << file.rdbuf();
file.close();
const char *shaderData = data.str().c_str();
glShaderSource(shader, 1, &shaderData, NULL);
glCompileShader(shader);
return shader;
}
GLuint createProgram(std::vector<GLuint> *list)
{
GLuint prog = glCreateProgram();
for(int i = 0; i < list->size(); i++)
{
glAttachShader(prog, list->at(i));
}
glLinkProgram(prog);
for(int i = 0; i < list->size(); i++)
{
glDetachShader(prog, list->at(i));
}
return prog;
}
void display(void)
{
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(program);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);
glDrawArrays(GL_QUADS, 0, 4);
glDisableVertexAttribArray(0);
glUseProgram(0);
glutSwapBuffers();
}
void init(void)
{
std::vector<GLuint> shaderList;
shaderList.push_back(loadShader(GL_VERTEX_SHADER, "vertexshader.vert"));
shaderList.push_back(loadShader(GL_FRAGMENT_SHADER, "fragmentshader.frag"));
program = createProgram(&shaderList);
std::for_each(shaderList.begin(), shaderList.end(), glDeleteShader);
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glGenBuffers(1, &vao);
glBindVertexArray(vao);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE);
glutInitWindowSize(250, 250);
glutCreateWindow("OpenGL");
glewInit();
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
vertexshader.vert:
#version 330
layout(location = 0) in vec4 pos;
void main()
{
gl_Position = pos;
}
fragmentshader.frag:
#version 330
out vec4 frag;
void main()
{
frag = vec4(0.0f, 0.0f, 1.0f, 1.0f);
}