Jump to content
  • Advertisement

Recommended Posts

While writing a simple renderer using OpenGL, I faced an issue with the glGetUniformLocation function. For some reason, the location is coming to be -1.

Anyone has any idea .. what should I do?

Share this post


Link to post
Share on other sites
Advertisement

https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glGetUniformLocation.xhtml

Quote

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

FRAGMENT SHADER:

#version 330 core

layout(location = 0) out vec4 color;

uniform vec4 u_color;

void main()
{
color = u_color;
}

AND THIS IS THE FUNCTION WHICH USES IT:

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.

 

#include<string>

class Shader {

private:

unsigned int m_RendererID;

public:

Shader();

~Shader();

 

void Bind() const;

void Unbind() const;

 

//set uniforms

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

 

private:

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 GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!