DJSchaffner
DJSchaffner

Reputation: 582

Modern OpenGL(4.6) - Compiling shader into library

I want to create a very basic 2D game engine to play around and learn. To accomplish this i came up with the idea to write an engine in c++ and compile it into a library together with a header file so i can just include these into my project, create an instance of the engine class and start from there. Basically i dont want to work with OpenGL code directly in the project itself.

Im using GLAD together with GLFW if that has any relevance

To draw the content to the screen i want to draw a full-screen quad and apply my pixels to it as texture but now comes the problem:

In modern OpenGL you have to use the programmable pipeline which involves shaders and im not sure on how to add the drawing to the screen into my library without having to add the shader itself to the project that uses the engine. I'd like to keep the includes etc to a minimum.

Basically the question comes down to: Can you compile shaders into a library somehow?

If not, or if anyone has a better suggestion for how to go about programming this drawing to the screen. Hints are welcome

Upvotes: 0

Views: 986

Answers (1)

Lukas-T
Lukas-T

Reputation: 11340

There is no requirement that a shader has to be a file. You can store the shader code in a variable and thus have it compiled into the library. How you do this depends on you, you could, for example, use constexpr const char*:

constexpr const char *DefaultVertexShader = 
"#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n"  
"out vec4 vertexColor;\n"
"void main() {\n"
"gl_Position = vec4(aPos, 1.0);\n"
"vertexColor = vec4(0.5, 0.0, 0.0, 1.0);\n"
"}\n";

Upvotes: 2

Related Questions