January 17, 2012

Creating OpenGL shaders

include : glut.h, glew.h
library : glut32.lib, glew32.lib
bin : glut32.dll, glew32.dll

// initialize glew in the begging of application
glewInit();


// compile and link shader program from string
GLhandleARB InstallShaders ( const char *_vsSource, const char *_fsSource )
{
 GLuint program;
 GLuint vs, fs;
 GLint vertCompiled;
 GLint fragCompiled;
 GLint linked;

 vs = glCreateShader ( GL_VERTEX_SHADER );
 if ( vs )
 {
  glShaderSource ( vs, 1, &_vsSource, NULL );
  glCompileShader ( vs );
  glGetShaderiv ( vs, GL_COMPILE_STATUS, &vertCompiled );
  if ( !vertCompiled )
  {
   GLint infologLength = 0;
   glGetShaderiv ( vs, GL_INFO_LOG_LENGTH, &infologLength );

   if ( infologLength > 0 )
   {
    char *infoLog = (char*)malloc ( infologLength );
    glGetShaderInfoLog ( vs, infologLength, NULL, infoLog );
    int dsize = MultiByteToWideChar ( CP_ACP, 0, (char*)infoLog, -1, NULL, NULL );
    WCHAR *des = new WCHAR[dsize];
    MultiByteToWideChar ( CP_ACP, 0, (char*)infoLog, dsize, des, dsize );
    MessageBox ( g_hMain, des, L"Vertex Shader Compile Error", MB_ICONERROR );
    delete[] des;
    free ( infoLog );
   }
   glDeleteShader ( vs );
   vs = 0;
   return 0;
  }
 }

 fs = glCreateShader ( GL_FRAGMENT_SHADER );
 if ( fs )
 {
  glShaderSource ( fs, 1, &_fsSource, NULL );
  glCompileShader ( fs );
  glGetShaderiv ( fs, GL_COMPILE_STATUS, &fragCompiled );
  if ( !fragCompiled )
  {
   int infologLength = 0;
   glGetShaderiv ( fs, GL_INFO_LOG_LENGTH, &infologLength );

   if ( infologLength > 0 )
   {
    char *infoLog = (char*)malloc ( infologLength );
    glGetShaderInfoLog ( fs, infologLength, NULL, infoLog );
    int dsize = MultiByteToWideChar ( CP_ACP, 0, (char*)infoLog, -1, NULL, NULL );
    WCHAR *des = new WCHAR[dsize];
    MultiByteToWideChar ( CP_ACP, 0, (char*)infoLog, dsize, des, dsize );
    MessageBox ( g_hMain, des, L"Fragment Shader Compile Error", MB_ICONERROR );
    delete[] des;
    free ( infoLog );
   }
   glDeleteShader ( fs );
   fs = 0;
   return 0;
  }
 }

 if ( vs > 0 && fs > 0 )
 {
  program = glCreateProgram();
  if ( program )
  {
   glAttachShader ( program, vs );
   glAttachShader ( program, fs );

   glDeleteShader ( vs );
   glDeleteShader ( fs );

   glLinkProgram ( program );
   glGetProgramiv ( program, GL_LINK_STATUS, &linked );
   if ( !linked )
   {
    int infologLength = 0;
    glGetProgramiv ( program, GL_INFO_LOG_LENGTH, &infologLength);

    if ( infologLength > 0 )
    {
     char *infoLog = (char*)malloc ( infologLength );
     glGetProgramInfoLog ( program, infologLength, NULL, infoLog );
     int dsize = MultiByteToWideChar ( CP_ACP, 0, (char*)infoLog, -1, NULL, NULL );
     WCHAR *des = new WCHAR[dsize];
     MultiByteToWideChar ( CP_ACP, 0, (char*)infoLog, dsize, des, dsize );
     MessageBox ( g_hMain, des, L"Shader Link Error", MB_ICONERROR );
     delete[] des;
     free ( infoLog );
    }
    glDeleteProgram ( program );
    program = 0;
    return 0;
   }

   return program;
  }
  return 0;
 }
 return 0;
}


// clean up shader
GLint CleanUpShaders ( GLuint *_program )
{
 glDeleteProgram ( *_program );
 _program = 0;
 return 1;
}

No comments:

Post a Comment