Loading and compiling shaders in OpenGL
In Chapter 2, Using Essential Libraries, our tiny OpenGL examples loaded all the GLSL shaders directly from the const char*
variables defined inside our source code. While this approach is acceptable in the territory of 100-line demos, it does not scale well beyond that. In this recipe, we will learn how to load, compile, and link shaders and shader programs. This approach will be used throughout the rest of the examples in this book.
Getting ready
Before we can proceed with the actual shader loading, we need two graphics API-agnostic functions. The first one loads a text file as std::string
:
std::string readShaderFile(const char* fileName) { FILE* file = fopen(fileName, "r"); if (!file) { printf("I/O error. Cannot open '%s'\n", fileName); return std::string(); } fseek(file, 0L, SEEK_END); &...