Inside a Python script defining a GLSL shader used on the game engine, how do I access the width/height of the game window?


It depends if the shader is set as an actuator "Filter 2D" or not. Then you can use the predefined uniform floats bgl_RenderedTextureWidth and bgl_RenderedTextureHeight. If it is just an arbitrary shader you would need to pass the values yourself as uniforms e.g.:

// glsl
uniform float ww;
uniform float wh;

And pass the values via the python api like:

# python
shader.setUniform1f("ww", float(bge.render.getWindowWidth()))
shader.setUniform1f("wh", float(bge.render.getWindowHeight()))

To set up the shaders you usually use code like this:

cont = bge.logic.getCurrentController()
own = cont.owner

vs = """ 
// vertex shader code here

fs = """ 
// fragment shader code here

mesh = own.meshes[0]
for mat in mesh.materials:
  shader = mat.getShader()
  if shader != None:
    if not shader.isValid():
        shader.setSource(vs, fs, True)

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.