Question

I am compiling this project on a linux machine. After fixing all the include and lib errors, I get a core dumped error. but if I comment out the initGLSL line, it runs well. The initGLSL function is given below.

void Viewer :: initGLSL( void )
{
   shader.loadVertex( "shaders/vertex.glsl" );
   shader.loadFragment( "shaders/fragment.glsl" );
}

vertex.glsl:

varying vec3 position;
varying vec3 normal;

void main()
{   
   gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
   gl_FrontColor = gl_Color;

   position = gl_Vertex.xyz;
   normal = gl_Normal.xyz; 
}

I don't know how to debug this program. How can I check if this error is because of the GLSL, or the loader function?

---------------Thanks for your comment------------

The load function:

void Shader::loadVertex( const char* filename )
{
   load( GL_VERTEX_SHADER, filename, vertexShader );
}

void Shader::loadFragment( const char* filename )
{
   load( GL_FRAGMENT_SHADER, filename, fragmentShader );
}


void Shader::load( GLenum shaderType, const char* filename, GLuint& shader )
// read vertex shader from GLSL source file, compile, and attach to program
{
   string source;

   if( !readSource( filename, source ))
   {
      return;
   }

   if( program == 0 )
   {
      program = glCreateProgram();
   }

   if( shader != 0 )
   {
      glDetachShader( program, shader );
   }

   shader = glCreateShader( shaderType );
   const char* source_c_str = source.c_str();
   glShaderSource( shader, 1, &(source_c_str), NULL );

   glCompileShader( shader );
   GLint compileStatus;
   glGetShaderiv( shader, GL_COMPILE_STATUS, &compileStatus );

   if( compileStatus == GL_TRUE )
   {
      glAttachShader( program, shader );
      linked = false;
   }
   else
   {
      GLsizei maxLength = 0;
      glGetShaderiv( shader, GL_INFO_LOG_LENGTH, &maxLength );

      if( maxLength > 0 )
      {
         GLchar* infoLog = new char[ maxLength ];
         GLsizei length;

         glGetShaderInfoLog( shader, maxLength, &length, infoLog );

         cerr << "GLSL Error: " << infoLog << endl;

         delete[] infoLog;
      }
   }
}

and when I tried debugging with gdb, I get the msg:

(gdb) p (filename)
$1 = 0x482e41 "shaders/vertex.glsl"
(gdb) n
77        if( program == 0 )
(gdb) n
79           program = glCreateProgram();
(gdb) n

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

Solution

Have you definitely created an OpenGL context before calling shader.load*?

E.g. one of these, if you're using the corresponding library:

glutInit
glutCreateWindow

glfwInit
glfwCreateWindow

SDL_init 
SDL_CreateWindow
SDL_GL_CreateContext

I don't have much experience with this, but some GL functions aren't linked at runtime by default - glCreateProgram may still be NULL after creating a context. I use GLEW to do this for me (glewInit()), but there are other ways.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top