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
  • Advertisement
  • Popular Tags

  • Popular Now

  • Advertisement
  • Similar Content

    • By Aryndon
      Project Redemption is an semi-fantasy RPG with a linear story and an elaborate combat system.
      We are building in Unity and are currently looking animators and artists.
      What we are looking for
      -Someone who is okay with split revenue/profits when finished
      -Collaborate with others in the team. Do you have an idea/thought on what should be included? Tell us!
      -Someone who wants to work with people that are passionate about this project
      If you are interested. Please message me and I will get back to you as soon as possible! Or add me on Discord AJ#6664
    • By BearishSun
      bs::framework is a newly released, free and open-source C++ game development framework. It aims to provide a modern C++14 API & codebase, focus on high-end technologies comparable to commercial engine offerings and a highly optimized core capable of running demanding projects. Additionally it aims to offer a clean, simple architecture with lightweight implementations that allow the framework to be easily enhanced with new features and therefore be ready for future growth.
      Some of the currently available features include a physically based renderer based on Vulkan, DirectX and OpenGL, unified shading language, systems for animation, audio, GUI, physics, scripting, heavily multi-threaded core, full API documentation + user manuals, support for Windows, Linux and macOS and more.
      The next few updates are focusing on adding support for scripting languages like C#, Python and Lua, further enhancing the rendering fidelity and adding sub-systems for particle and terrain rendering.
      A complete editor based on the framework is also in development, currently available in pre-alpha stage.
      You can find out more information on www.bsframework.io.

      View full story
    • By BearishSun
      bs::framework is a newly released, free and open-source C++ game development framework. It aims to provide a modern C++14 API & codebase, focus on high-end technologies comparable to commercial engine offerings and a highly optimized core capable of running demanding projects. Additionally it aims to offer a clean, simple architecture with lightweight implementations that allow the framework to be easily enhanced with new features and therefore be ready for future growth.
      Some of the currently available features include a physically based renderer based on Vulkan, DirectX and OpenGL, unified shading language, systems for animation, audio, GUI, physics, scripting, heavily multi-threaded core, full API documentation + user manuals, support for Windows, Linux and macOS and more.
      The next few updates are focusing on adding support for scripting languages like C#, Python and Lua, further enhancing the rendering fidelity and adding sub-systems for particle and terrain rendering.
      A complete editor based on the framework is also in development, currently available in pre-alpha stage.
      You can find out more information on www.bsframework.io.
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!