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.

There are not a lot of questions on StackOverflow or anywhere else that address this “system” question. “The_Fiddler” likely knows the answer, but is too senior for a noob. I registered to see an answer, but did not find it. So after 12 years, here is the answer.

In your shader file, define;

uniform float screenHeight;
uniform float screenWidth;

Then you can access those values like;

float lerpValue = gl_FragCoord.y / screenHeight;

to keep it simple, I used screenHeight only. So that it is global, I initialized at the top of my .cpp file.

GLuint screenHeightUnf;

Then, in an InitializeProgram() function, I compiled the shaders and then “getted” (lol) the location of the fragment shader’s new uniform variable.

screenHeightUnf = glGetUniformLocation(theProgram, "screenHeight");

Then, in a new function that is called by GLSL upon reshape, I bound the program, passed the variable of screen height, and closed the binding.

void reshape (int w, int h)
{
	glUseProgram(theProgram);	
	//glUniform1f(screenHeightUnf, 500.0f);		// works
	//glUniform1f(screenHeightUnf, 500);		// works
	//glUniform1ui(screenHeightUnf, h);     	// not works
	glUniform1f(screenHeightUnf, h);     		// have to cast h to float in the front function part
	glUseProgram(0);
    ...
}

After 12 years… I hope that you are no longer struggling with this. Though, I hope that some of the new noobs reading McKesson’s online book stumble upon this.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.