Attaching a openGL shader to bge mesh.

I have a simple shader program that I want to attach to the mesh of a game object, I also wan’t to avoid using:

#Blender assign Shadermesh = own.meshes[0]
for mat in mesh.materials:
    shader = mat.getShader()
    if shader != None:
        if not shader.isValid():
            shader.setSource(VertexShader, FragmentShader, 1)

Here is the simple shader:

import bgl


def CompileShader():
    VertexShader = """
    #version 430 core
    //Gets the view
    layout(location = 0) in vec4 vPosition;


    void main(void)
    {
      gl_Position = vPosition;
    //  gl_Position = vec4(0.0, 0.0, 0.5, 1.0);
    }
    """
    FragmentShader = """
    #version 430 core


    out vec4 Color;


    void main(void)
    {
  Color = vec4(0.0, 0.0, 1.0, 1.0);
    }
    """
    
    #Create and compile vertex shader.
    Vertex_Shader = bgl.glCreateShader(bgl.GL_VERTEX_SHADER)
    bgl.glShaderSource(Vertex_Shader,VertexShader)
    bgl.glCompileShader(Vertex_Shader)
    
    #Create and compile fragment shader.
    Fragment_Shader = bgl.glCreateShader(bgl.GL_FRAGMENT_SHADER)
    bgl.glShaderSource(Fragment_Shader,FragmentShader)
    bgl.glCompileShader(Fragment_Shader)
    
    #Create Program to link shaders
    Program = bgl.glCreateProgram()
    bgl.glAttachShader(Program,Vertex_Shader)
    bgl.glAttachShader(Program,Fragment_Shader)
    bgl.glLinkProgram(Program)
    
    #Cleanup
    bgl.glDeleteShader(Vertex_Shader)
    bgl.glDeleteShader(Fragment_Shader)
    
    return Program

This works with meshes I create with bgl, but wont work with the bge meshes?

Blender supports only GLSL 1.2.

Here a simple shader example.


from bge import logic 

vertex = """

void main(void)  
{     
   gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;  
}
          
"""

fragment = """

void main (void)  
{  
   gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);     
}
                   
"""

cont = logic.getCurrentController()
own = cont.owner
mesh = own.meshes[0]
for mat in mesh.materials:
    shader = mat.getShader()
    if shader != None:
        if not shader.isValid():
            print("Setting shader source code.")
            shader.setSource(vertex, fragment, 1)

The problem is not with the openGL support, the shader work fine when I use shader.setSource().
I want a work around for the setSource() function.

Maybe I sound stupid but why?

Hm.

Here you write that it is not working with BGE meshes.

Does now the Shader work with setSource() on a Blender material or not?

I want a work around for the setSource() function.

Using the setSource() function is the only way to replace the Blender material shader with a custom one.
What is the problem with the setSource() function?

The only way that may be is possible to use shader.getFragmentProg and shader.getVertexProg to get the Name or address of the Blender shader. Or may be you can use glGetProgramm and glGetAttachedSahders, or a other gl function, to get the program and shader name from the Blender material. But I never played myself with such things.

If I use the bgl.glRunProgram() it works on meshes made using code and bgl, but doesn’t work with the bge meshes, the shader works on bge meshes if I use setSource().

I am attempting to write a tessallation shader into blender, using c++ I expanded to opengl 4.3.
Now I want a way to use python to use the shader, with out adding more modules or changing existing ones using c++, and setSource() only takes vertex and fragment shaders.

edit: Tessallation shader, not Fragment shader.