GLSL加载核心转储

时间:2014-03-04 11:57:08

标签: opengl glsl

我正在linux机器上编译this project。修复所有include和lib错误后,我得到一个核心转储错误。但如果我注释掉initGLSL行,它运行良好。 initGLSL函数如下所示。

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; 
}

我不知道如何调试这个程序。如何检查此错误是由于GLSL还是加载器函数?

---------------感谢您的评论------------

加载功能:

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;
      }
   }
}

当我尝试使用gdb进行调试时,我得到了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 ?? () 

1 个答案:

答案 0 :(得分:0)

在调用shader.load*之前,你确实创建了一个OpenGL上下文吗?

E.g。其中之一,如果你使用相应的库:

glutInit
glutCreateWindow

glfwInit
glfwCreateWindow

SDL_init 
SDL_CreateWindow
SDL_GL_CreateContext

我对此没有多少经验,但默认情况下某些GL函数在运行时没有链接 - 创建上下文后glCreateProgram可能仍然是NULL。我使用GLEW为我(glewInit())执行此操作,但there are other ways