Advertisement Jump to content

Recommended Posts



This function returns -1 if name does not correspond to an active uniform variable in program, if name starts with the reserved prefix "gl_", or if name is associated with an atomic counter or a named uniform block.

Typically if you're certain you didn't make a typo, it's the part I've bolded about "active" that catches people off guard. If the shader as compiled doesn't use the uniform variable, it's considered not active and won't be assigned a location even if it's listed in the source code.

Share this post

Link to post
Share on other sites
1 hour ago, Devashish Khandelwal said:

So, how do i make the Uniform Active?

Use it in your shader.

Specifically, use it in an expression that contributes to one of the shader outputs (i.e. gl_Position for the vertex shader, gl_FragColor for the fragment shader). Otherwise the compiler is likely to optimise it away.

Note that unless you want to spend a lot of time worrying about this, you probably want to make your code emit a warning on inactive uniform, but keep on running regardless. As you debug shaders you are going to be making changes to them, and inevitably you'll find that some uniforms become temporarily inactive as you change or comment out code.

Share this post

Link to post
Share on other sites


#version 330 core

layout(location = 0) out vec4 color;

uniform vec4 u_color;

void main()
color = u_color;


unsigned int Shader::GetUniformLocation(const std::string& name)
int location = glGetUniformLocation(m_RendererID, name.c_str());
if (location == -1)
cout << "Warning :uniform" << name << " does not exist";
return location;


I've handled the situation when location becomes -1 and therefore in the output, it's just showing Warning:uniform u_color does not exist.

Edited by swiftcoder
added code tags

Share this post

Link to post
Share on other sites

Yeah, it is the shader's id.



class Shader {


unsigned int m_RendererID;





void Bind() const;

void Unbind() const;


//set uniforms

void SetUniforms4f(const std::string& name, float v1, float v2, float v3, float v4);



std::string ParseShader(std::string filename);

unsigned int CreateShader(const std::string& vertexShader, const std::string& fragmentShader);


unsigned int CompileShader(unsigned int type, const std::string& source);

unsigned int GetUniformLocation(const std::string& name);


Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!