123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #include "shader.hpp"
  2. slop::Shader::Shader( std::string vert, std::string frag, bool file ) {
  3. std::string vert_contents;
  4. std::string frag_contents;
  5. if ( file ) {
  6. vert = resource->getRealPath(vert);
  7. frag = resource->getRealPath(frag);
  8. std::ifstream v( vert.c_str() );
  9. if (!v.is_open()) {
  10. throw new std::runtime_error( "Failed to open " + vert );
  11. }
  12. vert_contents = std::string((std::istreambuf_iterator<char>(v)),
  13. std::istreambuf_iterator<char>());
  14. std::ifstream f( frag.c_str() );
  15. if (!f.is_open()) {
  16. throw new std::runtime_error( "Failed to open " + frag );
  17. }
  18. frag_contents = std::string((std::istreambuf_iterator<char>(f)),
  19. std::istreambuf_iterator<char>());
  20. } else {
  21. vert_contents = vert;
  22. frag_contents = frag;
  23. }
  24. const char* vertsrc = vert_contents.c_str();
  25. const char* fragsrc = frag_contents.c_str();
  26. // Create the program to link to.
  27. program = glCreateProgram();
  28. if ( vert_contents.length() <= 0 ) {
  29. std::string errstring = "Failed to open file (or is empty) `" + vert + "`.\n";
  30. throw new std::runtime_error(errstring);
  31. }
  32. if ( frag_contents.length() <= 0 ) {
  33. std::string errstring = "Failed to open file (or is empty) `" + frag + "`.\n";
  34. throw new std::runtime_error(errstring);
  35. }
  36. // Compile both shaders.
  37. unsigned int vertShader = glCreateShader( GL_VERTEX_SHADER );
  38. glShaderSource( vertShader, 1, &vertsrc , NULL );
  39. std::string errortxt;
  40. int err = compile( vertShader, errortxt );
  41. if ( err ) {
  42. std::string errstring = "Failed to compile shader `" + vert + "`:\n" + errortxt;
  43. throw new std::runtime_error(errstring);
  44. glDeleteShader( vertShader );
  45. return;
  46. }
  47. unsigned int fragShader = glCreateShader( GL_FRAGMENT_SHADER );
  48. glShaderSource( fragShader, 1, &fragsrc, NULL );
  49. err = compile( fragShader, errortxt );
  50. if ( err ) {
  51. std::string errstring = "Failed to compile shader `" + frag + "`:\n" + errortxt;
  52. throw new std::runtime_error(errstring);
  53. glDeleteShader( vertShader );
  54. glDeleteShader( fragShader );
  55. return;
  56. }
  57. // Then attempt to link them to this shader.
  58. err = link( vertShader, fragShader, errortxt );
  59. if ( err ) {
  60. std::string errstring = "Failed to link shader `" + vert + "` and `" + frag + "`:\n" + errortxt;
  61. throw new std::runtime_error(errstring);
  62. glDeleteShader( vertShader );
  63. glDeleteShader( fragShader );
  64. return;
  65. }
  66. // Clean up :)
  67. glDeleteShader( vertShader );
  68. glDeleteShader( fragShader );
  69. glUseProgram( 0 );
  70. }
  71. slop::Shader::~Shader() {
  72. glDeleteProgram( program );
  73. }
  74. unsigned int slop::Shader::getProgram() {
  75. return program;
  76. }
  77. void slop::Shader::bind() {
  78. glUseProgram( program );
  79. }
  80. int slop::Shader::compile( unsigned int shader, std::string& error ) {
  81. glCompileShader( shader );
  82. // Compiling the shader is the easy part, all this junk down here is for printing the error it might generate.
  83. int result = GL_FALSE;
  84. int logLength;
  85. glGetShaderiv( shader, GL_COMPILE_STATUS, &result );
  86. glGetShaderiv( shader, GL_INFO_LOG_LENGTH, &logLength );
  87. if ( result == GL_FALSE ) {
  88. char* errormsg = new char[ logLength ];
  89. glGetShaderInfoLog( shader, logLength, NULL, errormsg );
  90. error = errormsg;
  91. delete[] errormsg;
  92. return 1;
  93. }
  94. return 0;
  95. }
  96. int slop::Shader::link( unsigned int vertshader, unsigned int fragshader, std::string& error ) {
  97. glAttachShader( program, vertshader );
  98. glAttachShader( program, fragshader );
  99. glLinkProgram( program );
  100. // Linking the shader is the easy part, all this junk down here is for printing the error it might generate.
  101. int result = GL_FALSE;
  102. int logLength;
  103. glGetProgramiv( program, GL_LINK_STATUS, &result);
  104. glGetProgramiv( program, GL_INFO_LOG_LENGTH, &logLength);
  105. if ( result == GL_FALSE ) {
  106. char* errormsg = new char[ logLength ];
  107. glGetProgramInfoLog( program, logLength, NULL, errormsg );
  108. error = errormsg;
  109. delete[] errormsg;
  110. return 1;
  111. }
  112. return 0;
  113. }
  114. unsigned int slop::Shader::getUniformLocation( std::string name ) {
  115. glUseProgram( program );
  116. return glGetUniformLocation( program, name.c_str() );
  117. }
  118. bool slop::Shader::hasParameter( std::string name ) {
  119. glUseProgram( program );
  120. return glGetUniformLocation( program, name.c_str() ) != -1;
  121. }
  122. void slop::Shader::setParameter( std::string name, int foo ) {
  123. glUniform1i( getUniformLocation( name ), foo );
  124. }
  125. void slop::Shader::setParameter( std::string name, float foo ) {
  126. glUniform1f( getUniformLocation( name ), foo );
  127. }
  128. void slop::Shader::setParameter( std::string name, glm::mat4& foo ) {
  129. glUniformMatrix4fv( getUniformLocation( name ), 1, GL_FALSE, glm::value_ptr( foo ) );
  130. }
  131. void slop::Shader::setParameter( std::string name, glm::vec4 foo ) {
  132. glUniform4f( getUniformLocation( name ), foo.x, foo.y, foo.z, foo.w );
  133. }
  134. void slop::Shader::setParameter( std::string name, glm::vec2 foo ) {
  135. glUniform2f( getUniformLocation( name ), foo.x, foo.y );
  136. }
  137. void slop::Shader::setAttribute( std::string name, unsigned int buffer, unsigned int stepsize ) {
  138. unsigned int a = glGetAttribLocation( program, name.c_str() );
  139. glEnableVertexAttribArray( a );
  140. glBindBuffer( GL_ARRAY_BUFFER, buffer );
  141. glVertexAttribPointer( a, stepsize, GL_FLOAT, GL_FALSE, 0, NULL );
  142. activeAttributes.push_back( a );
  143. }
  144. void slop::Shader::unbind() {
  145. for ( unsigned int i=0; i<activeAttributes.size(); i++ ) {
  146. glDisableVertexAttribArray( activeAttributes[i] );
  147. }
  148. activeAttributes.clear();
  149. glUseProgram( 0 );
  150. }