Window Size in GLSL

Greetings,

This is very much a beginner question, but I’m writing a program in GLSL and need to be able to access the width and height of my window. I’ve seen some people use GLUT, but it looks very complicated, and I’d rather not fuss with that. What I’m looking for is something that accesses the window width and height as easily as glFragCoord accesses the coordinates of a pixel. Any suggestions?

Thanks.

Pass a vec2 uniform that contains the width and height of the window:


// In your shader declare
uniform vec2 WindowSize;

// In your code load your shaders, link your program and call:
int uniform_WindowSize = glGetUniformLocation(program_id,"WindowSize");
[...]
glUseProgram(program_id);
glUniform2f(uniform_WindowSize, Width, Height);

Just FYI built-in uniforms like glFragColor are considered deprecated in OpenGL 3.0+.

Ugh. As strange as it may sound, I’d rather not fuss with 3.0+ at the moment. I’m using GLSL to write a 2D filter in Blender, and I’m not sure Blender supports 3.0+ yet. This also means that I don’t have another OpenGL program from which I’m linking my shader. Is there a way to do this strictly from within GLSL using 2.0?

P.S. I apologize if these questions demonstrate a lack of understanding of OpenGL and GLSL. As I’ve said before, I’m still a beginner.

I think, there’s no built-in uniforms in GLSL for this. You need a different approach if you can’t send in the viewport/window values into the shader via Uniforms as Stephen suggests.

Are you sure there’s no mechanism in Blender to send these values to the shader?
Are you sure it’s necessary for the shader to work?

I don’t know whether there’s a way to pass in the screen width and height from Blender. I’ve gotten the impression that the 2D filters in Blender are standalone GLSL code widgets that don’t talk to anything else, but I could be mistaken. I suppose I could look into it.

The width and height are necessary for the filter to work. I’m writing a filter that gets darker as you move closer to the center of the screen, so I need to define the darkest point as (width/2, height/2).


// In your shader declare
uniform vec2 WindowSize;

// In your code load your shaders, link your program and call:
int uniform_WindowSize = glGetUniformLocation(program_id,"WindowSize");
[...]
glUseProgram(program_id);
glUniform2f(uniform_WindowSize, Width, Height);

So in the code that loads the shader, where and how do I define the values for Width and Height that I’m passing in via glUniform2f?

The application (Blender in this case) is responsible for this. Check its documentation.