# OpenGL GLSL tc_offset variable

## Recommended Posts

I'm trying to implement the blur fragment shader from the OpenGL Superbible, but I'm confused as to where the tc_offset variable is being set. Here's the code:
uniform sampler2D sampler0;
uniform vec2 tc_offset[9];

void main(void)
{
vec4 sample[9];
for( int i = 0; i < 9; i++ )
{
sample[i] = texture2D( sampler0, gl_TexCoord[0].st + tc_offset[i] );
}

//  1 2 1
//  2 1 2  /  13
//  1 2 1

gl_FragColor = sample[1];/*(sample[0] + (2.0*sample[1]) + sample[2] +
(2.0*sample[3]) + sample[4] + (2.0*sample[5]) +
sample[6] + (2.0*sample[7]) + sample[8]) / 13.0;
}


Is tc_offset automatically set to the texture coordinates of the neighboring texels, like sampler0 is automatically set to the first texture unit?

##### Share on other sites
My best guess is that its set in the application via glUniform.

##### Share on other sites
Quote:
 Original post by doctorsixstringIs tc_offset automatically set to the texture coordinates of the neighboring texels,

You have to set them yourself application side.

Quote:
 Original post by doctorsixstringlike sampler0 is automatically set to the first texture unit?

It's not. This is just a coincidence. All uniform variables are initialized to zero before first use, regardless of their name, type or usage in the shader. You could call the variable "sampler23" or "greenSamplerWithRedDots" and both would still point to unit 0 by default.

##### Share on other sites
That makes sense. I did some reading on glGetUniform, and it looks like I first need to query the location of a uniform variable with glGetUniformLocation() before setting it with glUniform().

If that's the case, then I'm not sure what I'm doing wrong. Calling glGetUniformLocation for the tc_offset variable returns -1. Calling it for sampler0 returns 0, so that seems to indicate a problem with tc_offset. I also tried creating other variables (e.g. "uniform int a;"), but -1 was returned for those, too. I also noticed that calling glGetProgramiv with GL_ACTIVE_UNIFORMS returned 1, regardless of how many uniform variables I declared.

Here's a code snippet. I'm using Python, but the GL calls should be clear regardless:

	self.program = glCreateProgram()	glAttachShader(self.program, fragmentShader)	glLinkProgram(self.program)	glValidateProgram(self.program)	print glGetUniformLocation( self.program, "sampler0" )   # returns 0	print glGetUniformLocation( self.program, "tc_offset" )  # returns -1

That code is executed after creating and compiling the shader (stored in the fragmentShader variable).

##### Share on other sites
Quote:
 Original post by doctorsixstringThat makes sense. I did some reading on glGetUniform, and it looks like I first need to query the location of a uniform variable with glGetUniformLocation() before setting it with glUniform().

Correct.

Quote:
 Original post by doctorsixstringIf that's the case, then I'm not sure what I'm doing wrong. Calling glGetUniformLocation for the tc_offset variable returns -1. Calling it for sampler0 returns 0, so that seems to indicate a problem with tc_offset. I also tried creating other variables (e.g. "uniform int a;"), but -1 was returned for those, too. I also noticed that calling glGetProgramiv with GL_ACTIVE_UNIFORMS returned 1, regardless of how many uniform variables I declared.

Keep in mind that uniforms declared without being actively used by the shader will be optimized away by the GLSL compiler. So simply declaring a new variable without actually accessing it (or without using the results of operations done with it) will result in glGetUniformLocation returning -1. The optimizer is usually very smart with this, and will remove uniforms whenever it sees the slightest possibility of doing so.

##### Share on other sites
Quote:
 Original post by Yann LKeep in mind that uniforms declared without being actively used by the shader will be optimized away by the GLSL compiler.

That makes perfect sense, and I immediately noticed the problem after reading this. If you look closely at the code in my original post, you'll notice that I was simply setting gl_FragColor = sample[1]. A multi-line comment was started (but not ended) after that, so the tc_offset usage was optimized away.

I just changed it to the following code, and glGetUniformLocation returns 1 for the variable, and glGetProgramiv with GL_ACTIVE_UNIFORMS returns 2.

    gl_FragColor = (sample[0] + (2.0*sample[1]) + sample[2] +         (2.0*sample[3]) + sample[4] + (2.0*sample[5]) +         sample[6] + (2.0*sample[7]) + sample[8]) / 13.0;

Thanks!

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627662
• Total Posts
2978516
• ### Similar Content

• Both functions are available since 3.0, and I'm currently using glMapBuffer(), which works fine.
But, I was wondering if anyone has experienced advantage in using glMapBufferRange(), which allows to specify the range of the mapped buffer. Could this be only a safety measure or does it improve performance?
Note: I'm not asking about glBufferSubData()/glBufferData. Those two are irrelevant in this case.
• By xhcao
Before using void glBindImageTexture(    GLuint unit, GLuint texture, GLint level, GLboolean layered, GLint layer, GLenum access, GLenum format), does need to make sure that texture is completeness.
• By cebugdev
hi guys,
are there any books, link online or any other resources that discusses on how to build special effects such as magic, lightning, etc. in OpenGL? i mean, yeah most of them are using particles but im looking for resources specifically on how to manipulate the particles to look like an effect that can be use for games,. i did fire particle before, and I want to learn how to do the other 'magic' as well.
Like are there one book or link(cant find in google) that atleast featured how to make different particle effects in OpenGL (or DirectX)? If there is no one stop shop for it, maybe ill just look for some tips on how to make a particle engine that is flexible enough to enable me to design different effects/magic
let me know if you guys have recommendations.
• By dud3
How do we rotate the camera around x axis 360 degrees, without having the strange effect as in my video below?
Mine behaves exactly the same way spherical coordinates would, I'm using euler angles.
Tried googling, but couldn't find a proper answer, guessing I don't know what exactly to google for, googled 'rotate 360 around x axis', got no proper answers.

References:
Code: https://pastebin.com/Hcshj3FQ
The video shows the difference between blender and my rotation:

• By Defend
I've had a Google around for this but haven't yet found some solid advice. There is a lot of "it depends", but I'm not sure on what.
My question is what's a good rule of thumb to follow when it comes to creating/using VBOs & VAOs? As in, when should I use multiple or when should I not? My understanding so far is that if I need a new VBO, then I need a new VAO. So when it comes to rendering multiple objects I can either:
* make lots of VAO/VBO pairs and flip through them to render different objects, or
* make one big VBO and jump around its memory to render different objects.
I also understand that if I need to render objects with different vertex attributes, then a new VAO is necessary in this case.
If that "it depends" really is quite variable, what's best for a beginner with OpenGL, assuming that better approaches can be learnt later with better understanding?

• 10
• 10
• 12
• 22
• 13