This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Jan 25th, 2013  |  syntax: C++  |  size: 2.22 KB  |  views: 41  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <GL/glew.h>
  2. #include <GL/glut.h>
  3. #include <iostream>
  4. #include "Shader.hpp"
  5.  
  6.  
  7. namespace
  8. {
  9.   std::string getCode(std::string path)
  10.   {
  11.     std::string contents;
  12.     std::ifstream fin(path,std::ios::in | std::ios::binary);
  13.     if(!fin.good())
  14.     {
  15.       throw std::runtime_error("Could not open shader file: " + path);
  16.     }
  17.     fin.seekg(0,std::ios::end);
  18.     contents.resize(fin.tellg());
  19.     fin.seekg(0,std::ios::beg);
  20.     fin.read(&contents[0],contents.size());
  21.     fin.close();
  22.   #ifdef GL_ES_VERSION_2_0
  23.     std::string header = "#version 100\n"
  24.                         "#define GLES2\n";
  25.   #else
  26.     std::string header = "#version 120\n";
  27.   #endif  
  28.     return header + contents;
  29.   }
  30.   void print_log(GLuint object)
  31.   {
  32.           GLint log_length = 0;
  33.   if (glIsShader(object))
  34.     glGetShaderiv(object, GL_INFO_LOG_LENGTH, &log_length);
  35.   else if (glIsProgram(object))
  36.     glGetProgramiv(object, GL_INFO_LOG_LENGTH, &log_length);
  37.   else {
  38.     fprintf(stderr, "printlog: Not a shader or a program\n");
  39.     return;
  40.   }
  41.  
  42.   char* log = (char*)malloc(log_length);
  43.  
  44.   if (glIsShader(object))
  45.     glGetShaderInfoLog(object, log_length, NULL, log);
  46.   else if (glIsProgram(object))
  47.     glGetProgramInfoLog(object, log_length, NULL, log);
  48.  
  49.   fprintf(stderr, "%s", log);
  50.   free(log);
  51.   }
  52.  
  53.   void setCode(GLuint handle,const std::string & code)
  54.   {
  55.     const GLchar* source[1] = {code.c_str()};
  56.     glShaderSource(handle,1,source,NULL);
  57.     glCompileShader(handle);
  58.     GLint compile_ok = GL_FALSE;
  59.     glGetShaderiv(handle,GL_COMPILE_STATUS,&compile_ok);
  60.     if(compile_ok == GL_FALSE)
  61.         {
  62.           print_log(handle);
  63.       throw std::runtime_error("Shader failed to compile");
  64.         }
  65.  
  66.   }
  67.  
  68.  
  69. }
  70.  
  71.  
  72. template<typename T>
  73. std::shared_ptr<cs5400::Shader<T>> make_shader(std::string path)
  74. {
  75.   auto code = getCode(path);
  76.   auto shader = std::make_shared<cs5400::Shader<T>>();
  77.   setCode(shader->getHandle(),code);
  78.   return shader;
  79. }
  80.  
  81. std::shared_ptr<cs5400::VertexShader> cs5400::make_vertexShader(std::string path)
  82. {
  83.   return make_shader<detail::VertexShaderTag>(path);
  84. }
  85.  
  86. std::shared_ptr<cs5400::FragmentShader> cs5400::make_fragmentShader(std::string path)
  87. {
  88.   return make_shader<detail::FragmentShaderTag>(path);
  89. }
clone this paste RAW Paste Data