2
0

PolyGLSLProgram.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. Copyright (C) 2011 by Ivan Safrin
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #include "PolyGLSLProgram.h"
  20. #include "PolyVector3.h"
  21. #include "PolyVector2.h"
  22. #include "PolyColor.h"
  23. #include "PolyLogger.h"
  24. #include "PolyCoreServices.h"
  25. #include "PolyLogger.h"
  26. #include "PolyGLHeaders.h"
  27. #ifdef _WINDOWS
  28. #include <windows.h>
  29. // Some shader functions that aren't defined in glext/wglext
  30. extern PFNGLGETSHADERIVPROC glGetShaderiv;
  31. extern PFNGLGETSHADERINFOLOGPROC glGetShaderInfoLog;
  32. #endif
  33. #include "PolyGLHeaders.h"
  34. using std::vector;
  35. #ifdef _WINDOWS
  36. extern PFNGLUSEPROGRAMPROC glUseProgram;
  37. extern PFNGLUNIFORM1IPROC glUniform1i;
  38. extern PFNGLACTIVETEXTUREPROC glActiveTexture;
  39. extern PFNGLCREATESHADERPROC glCreateShader;
  40. extern PFNGLSHADERSOURCEPROC glShaderSource;
  41. extern PFNGLCOMPILESHADERPROC glCompileShader;
  42. extern PFNGLCREATEPROGRAMPROC glCreateProgram;
  43. extern PFNGLATTACHSHADERPROC glAttachShader;
  44. extern PFNGLLINKPROGRAMPROC glLinkProgram;
  45. extern PFNGLDETACHSHADERPROC glDetachShader;
  46. extern PFNGLDELETESHADERPROC glDeleteShader;
  47. extern PFNGLDELETEPROGRAMPROC glDeleteProgram;
  48. #ifndef _MINGW
  49. extern PFNGLGETUNIFORMLOCATIONARBPROC glGetUniformLocation;
  50. #endif
  51. #endif
  52. using namespace Polycode;
  53. GLSLProgram::GLSLProgram(String fileName, int type) : ShaderProgram(type) {
  54. program = -1;
  55. this->fileName = fileName;
  56. reloadProgram();
  57. }
  58. GLSLProgram::~GLSLProgram() {
  59. glDeleteShader(program);
  60. }
  61. void GLSLProgram::reloadProgram() {
  62. if(program != -1)
  63. glDeleteShader(program);
  64. OSFILE *file = OSBasics::open(fileName, "rb");
  65. if (!file) {
  66. Logger::log("Error: shader file %s not found\n", fileName.c_str());
  67. program = -1;
  68. return;
  69. }
  70. OSBasics::seek(file, 0, SEEK_END);
  71. long progsize = OSBasics::tell(file);
  72. OSBasics::seek(file, 0, SEEK_SET);
  73. char *buffer = (char*)malloc(progsize+1);
  74. memset(buffer, 0, progsize+1);
  75. OSBasics::read(buffer, progsize, 1, file);
  76. OSBasics::close(file);
  77. if(type == GLSLProgram::TYPE_VERT) {
  78. program = glCreateShader(GL_VERTEX_SHADER);
  79. } else {
  80. program = glCreateShader(GL_FRAGMENT_SHADER);
  81. }
  82. glShaderSource(program, 1, (const GLchar**)&buffer, 0);
  83. glCompileShader(program);
  84. GLint compiled = true;
  85. glGetShaderiv(program, GL_COMPILE_STATUS, &compiled);
  86. if(!compiled) {
  87. GLint length;
  88. GLchar* log;
  89. glGetShaderiv(program, GL_INFO_LOG_LENGTH, &length);
  90. log = (GLchar*)malloc(length);
  91. glGetShaderInfoLog(program, length, &length, log);
  92. printf("GLSL ERROR: %s\n", log);
  93. CoreServices::getInstance()->getLogger()->logBroadcast("GLSL ERROR:" + String(log));
  94. free(log);
  95. }
  96. free(buffer);
  97. }