Finally looking to learning some GLSL

Hello All!
After a few years it’s probably a good idea that I delve into the depths of GLSL :yes:
However, as a first project, i’ve decided to tackle my interpretation of a solution for leonnn in his game (whether he wants it or not).
Now, i know what i need to do, or at least what would make sense…
He wants to invert the color of any object behind a (invisible i presume) plane.
So, my thoughts:

  • Get the plane’s vertices
  • Multiply their xyz() coordinates with the object’s worldTransform()
  • Get the screen space (camera.getScreenPosition())
  • Use something similar to a point in polygon as a condition
  • If true, invert pixel colours at coordinate

So, that’s the theory, now for some questions;

  • Does the screenposition returned by camera.getScreenPosition([x, y, z]) actually relate to GLSL screen positions
  • How does one convert the above to a GLSL position
  • Any basics on how the vertex and fragment shaders interact
  • Any final tips?

Thank you in advance BlenderArtists :slight_smile:

Ok, now this is just annoying. With an object set to transparent, this doesn’t work. but should it not?


import bge
 
cont = bge.logic.getCurrentController()
 
DEFAULT_VERTEX_SHADER = '''
void main() { 
    gl_TexCoord[0] = gl_MultiTexCoord0; 
    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}'''
DEFAULT_FRAGMENT_SHADER = '''
uniform sampler2D bgl_RenderedTexture;
void main() {
    vec4 color = texture2D(bgl_RenderedTexture, gl_TexCoord[0].st);
    gl_FragColor = vec4( 1.0 - color[0], 1.0 - color[0], 1.0 - color.b, 1.0); 
    }''' 

mesh = cont.owner.meshes[0]
for mat in mesh.materials:
   shader = mat.getShader()
   if shader != None:
      if not shader.isValid():
         shader.setSource(DEFAULT_VERTEX_SHADER, DEFAULT_FRAGMENT_SHADER, 1)

It looks like you might be mixing material and screen space shaders. Material shaders are shaders applied on an object, and you’ll need both a vertex and a fragment shader. A screen space shader is one used through the BGE’s 2D filter system, and they only need a fragment shader because the 2D filter system will handle the vertex shader. bgl_RenderedTexture is something from 2D filters.