Create the shader
| 53 | |
| 54 | // Create the shader |
| 55 | bool Shader::create(const std::string vertexShaderFilename, |
| 56 | const std::string fragmentShaderFilename) { |
| 57 | |
| 58 | // Set the shader filenames |
| 59 | mFilenameVertexShader = vertexShaderFilename; |
| 60 | mFilenameFragmentShader = fragmentShaderFilename; |
| 61 | |
| 62 | // Check that the needed OpenGL extensions are available |
| 63 | /*bool isExtensionOK = checkOpenGLExtensions(); |
| 64 | if (!isExtensionOK) { |
| 65 | cerr << "Error : Impossible to use GLSL vertex or fragment shaders on this platform" << endl; |
| 66 | assert(false); |
| 67 | return false; |
| 68 | }*/ |
| 69 | |
| 70 | // Delete the current shader |
| 71 | destroy(); |
| 72 | |
| 73 | assert(!vertexShaderFilename.empty() && !fragmentShaderFilename.empty()); |
| 74 | |
| 75 | // ------------------- Load the vertex shader ------------------- // |
| 76 | GLuint vertexShaderID; |
| 77 | std::ifstream fileVertexShader; |
| 78 | fileVertexShader.open(vertexShaderFilename.c_str(), std::ios::binary); |
| 79 | |
| 80 | if (fileVertexShader.is_open()) { |
| 81 | |
| 82 | // Get the size of the file |
| 83 | fileVertexShader.seekg(0, std::ios::end); |
| 84 | uint fileSize = (uint) (fileVertexShader.tellg()); |
| 85 | assert(fileSize != 0); |
| 86 | |
| 87 | // Read the file |
| 88 | fileVertexShader.seekg(std::ios::beg); |
| 89 | char* bufferVertexShader = new char[fileSize + 1]; |
| 90 | fileVertexShader.read(bufferVertexShader, fileSize); |
| 91 | fileVertexShader.close(); |
| 92 | bufferVertexShader[fileSize] = '\0'; |
| 93 | |
| 94 | // Create the OpenGL vertex shader and compile it |
| 95 | vertexShaderID = glCreateShader(GL_VERTEX_SHADER); |
| 96 | assert(vertexShaderID != 0); |
| 97 | glShaderSource(vertexShaderID, 1, (const char **) (&bufferVertexShader), NULL); |
| 98 | glCompileShader(vertexShaderID); |
| 99 | delete[] bufferVertexShader; |
| 100 | |
| 101 | // Get the compilation information |
| 102 | int compiled; |
| 103 | glGetShaderiv(vertexShaderID, GL_COMPILE_STATUS, &compiled); |
| 104 | |
| 105 | // If the compilation failed |
| 106 | if (compiled == 0) { |
| 107 | |
| 108 | // Get the log of the compilation |
| 109 | int lengthLog; |
| 110 | glGetShaderiv(vertexShaderID, GL_INFO_LOG_LENGTH, &lengthLog); |
| 111 | char* str = new char[lengthLog]; |
| 112 | glGetShaderInfoLog(vertexShaderID, lengthLog, NULL, str); |
no outgoing calls
no test coverage detected