shader.cpp 5.4KB

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