Home > front end >  Can you write OpenGL shader in different file and later link it to the program?
Can you write OpenGL shader in different file and later link it to the program?

Time:08-24

Can you write OpenGL shader in a different file and later link it to the program? and if it's possible how? writing OpenGL shader in string makes my code messy.

Here is example code for shaders:

const char* vertexShaderSource =
    "#version 330 core\n"
    "layout (location = 0) in vec3 aPos;\n"
    "layout (location = 1) in vec3 aColor;\n"
    "\n"
    "out vec3 ourColor;\n"
    "uniform vec2 angleValues;\n"
    "\n"
    "void main()\n"
    "{\n"
    "gl_Position = vec4(aPos.x * angleValues.x - aPos.y * angleValues.y, aPos.y * angleValues.x   aPos.x * angleValues.y , aPos.z, 1.0);\n"
    "ourColor = aColor;\n"
    "}\n";

const char* fragmentShaderSource =
    "#version 330 core\n"
    "out vec4 FragColor;\n"
    "in vec3 ourColor;\n"
    "\n"
    "void main()\n"
    "{\n"
    "FragColor = vec4(ourColor, 1.0);\n"
    "}\n";

CodePudding user response:

Yes, you can have files like my_shader.vs or my_fragment.fs and link them like in this Shader class

Just initialize it like this:

shader = Shader("./shaders/my_shader.vs", "./shaders/my_fragment.fs");
  •  Tags:  
  • c
  • Related