I am using Blender Game Engines Filter 2D to make a post effect with GLSL. In an fragment shader applied on the frame buffer, I want to access the size in pixels of that buffer in GLSL.

uniform vec2 screensize;

This is possible for objects of the type BL_Shader

shader.setUniform2f("screensize", w, h)

The viewport size can be accessed in this way

import bge

w = bge.render.getWindowWidth()
h = bge.render.getWindowHeight()

print (w,h)

But I don't know how to get the BL_Shader object. The SCA_2DFilterActuator does not have any reference to such object.

co = bge.logic.getCurrentController()
distortion = co.actuators["riftDistortion"]

All the game properties stored in the object that calls the Filter 2D are passed to the shader as uniforms. So if you have a width and a height float game properties you can set their values at run-time to match the window size.

However, be aware that the window size (which you get from render.getWindowWidth() or render.getWindowHeight()) is not the buffer size. The buffersize is the closest power of two for that dimension (e.g., a window that is 1024 x 768 will have as buffers 1024 x 1024).

| improve this answer | |
  • $\begingroup$ That said, I can't see why do you want to access the size of the buffer in pixels. You are much better of by using the normalized coordinates of the visible part of the buffer. You get this with gl_TexCoord[3] link $\endgroup$ – dfelinto Jun 23 '13 at 18:16
  • $\begingroup$ Thank you, gl_TextCoord[3] helped. I am also able to set Uniforms with Python. I didn't know that the buffer size differs to the screen size, interesting. I am trying to achieve a optical distortion inversion for the Oculus Rift, the screen size is needed to achieve that. I actually have another question related to that. $\endgroup$ – lubosz Jun 24 '13 at 15:32

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.