0
$\begingroup$

I'm having troubles with my "clouds" texture while trying to sample it into my shader, the error is "invalid uniform value: clouds"

def main():
    import bge
    cont = bge.logic.getCurrentController()
    object = cont.owner

    for mesh in object.meshes:
        for material in mesh.materials:
            shader = material.getShader()
            if shader != None:
                shader.setSource(vertex_shader, fragment_shader, True)
                shader.setSampler("clouds", 0)

vertex_shader = """
uniform sampler2D clouds;
void main()
{ 
    gl_Position = ftransform();
}
"""

fragment_shader ="""

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

I'd really appreciate some help here.

$\endgroup$
1
$\begingroup$

Check the Color Option under the Influence section of the Texture panel.

enter image description here

| improve this answer | |
$\endgroup$
  • $\begingroup$ I edited the blendfile, can you have another look at my vertex shader and tell me why it's not displacing the vertices ? $\endgroup$ – Yvain Mar 21 '16 at 20:28
  • $\begingroup$ Check Normal and play with that value a little $\endgroup$ – Rick Riggs Mar 21 '16 at 20:39
  • $\begingroup$ If that is not quite what you are looking for (eg. you want to actually displace the geometry), then you should be looking into the Displace Modifier - Like this . $\endgroup$ – Rick Riggs Mar 21 '16 at 20:44
  • $\begingroup$ I am looking to actually change the geometry of the object but I need to be able to do it in-game that is why I am resorting to GLSL, i'm not sure any of those influence checkboxes should modulate the result of my shader. $\endgroup$ – Yvain Mar 21 '16 at 20:51
  • $\begingroup$ I tried both on your file, and hit "P" to activate the game. It worked even with the Displace Modifier. $\endgroup$ – Rick Riggs Mar 21 '16 at 22:46
1
$\begingroup$

Okay so it turned out there was no problem but since the texture was not in use in the script it returned an error. Since the original question of this post was "How can I get a displace from a texture in glsl ?" I'll just answer it. You need to write a program in python that will get the shader program from an object with getShader() and lets you write in the vertex and fragment shaders with setSource(fragShaderID, vertShaderID, 1). you need to call the setSampler("texture", 0) to be able to access the texture in glsl. "texture" is the name of the texture and 0 is its index in my texture stack.

def main():
    import bge
    object = bge.logic.getCurrentController().owner
    for mesh in object.meshes:
        for material in mesh.materials:
            shader = material.getShader()
            if shader != None:
                if not shader.isValid():
                    shader.setSource(vertex_shader, fragment_shader, True)
                shader.setSampler("clouds", 0)

Then you create the vertex and fragment shaders. you retrieve the position of a vertex with gl_Vertex which is a vec4 and its texture coordinates with gl_MultiTexCoord0. Once you have filled your vec4 Position object with datas from gl_Vertex you can acces its attributes .xyzw. The same for the texture coordinate object that receives the .st attributes

vertex_shader = """

varying vec4 Position;
varying vec2 TexCoord;
uniform sampler2D clouds;

void main()
{
    Position = gl_Vertex;
    TexCoord = gl_MultiTexCoord0;
    Position.z += texture2D(clouds, TexCoord.st).a; 
    gl_Position =  gl_ModelViewProjectionMatrix * Position;
}
"""

fragment_shader ="""

varying vec2 TexCoord;
uniform sampler2D clouds;

void main()
{
    float alpha = texture2D(clouds, TexCoord.st).a;
    gl_FragColor = vec4(alpha, alpha, alpha, 1.0);
}
"""
main()

In this way there is a real displace like in the "LODShader" written by benj on the blenderartists thread, There's also a wikibook that is dedicated to glsl programming for blender

enter image description here

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.