• Advertisement

Recommended Posts

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

  • Advertisement
  • Popular Now

  • Similar Content

    • By owenjr
      Hi there.
      I'm pretty new to this and I don't know if it has been asked before, but here I go.
      I'm developing a game using SFML and C++.
      I would like to use the "Tiled" tool to load maps into my game but I don't actually find any tutorial or guide on how to exaclty use it (I know that I have to read an XML file and stuff). I just step into diverse projects that make all a mess. 
      Anyone knows where can I find good information to make my map loader by myself?
      Thanks in advantage!!
    • By MHG OstryTM
      Hello guys,
      I've released my game for the first time. I'm very excited about it and I hope you'll enjoy the game - Beer Ranger. It's a retro-like puzzle-platfromer which makes you think a lot or die trying. You have a squad of skilled dwarfs with special powers and your goal is tasty beer. There is a lot of traps as well as many solutions how to endure them - it is up to your choice how to complete the level! 
      Link to the project: Project site
      Link to the Steam site with video: Beer Ranger
      Have fun and please write feedback if you feel up to.
      Some screens: 




    • By too_many_stars
      Hello Everyone,
      I have been going over a number of books and examples that deal with GLSL. It's common after viewing the source code to have something like this...
      class Model{ public: Model(); void render(); private: GLSL glsl_program; }; ////// .cpp Model::Model(){ glsl_program.compileAndLinkShaders() } void Model::render(){ glsl_program.use() //render something glsl_program.unUse(); } Is this how a shader program should be used in real time applications? For example, if I have a particle class, for every particle that's created, do I want to compiling and linking a vertex, frag shader? It seems to a noob such as myself this might not be the best approach to real time applications.
      If I am correct, what is the best work around?
      Thanks so much for all the help,
       
      Mike
       
    • By katastrophic88
       

      Hello Everyone!
      I’m now working with Zugalu -- developer of an upcoming SHMUP named Technolites – as Community Manager.
      In Technolites, you’ll take command of a fully customizable ship and dash across the universe to defeat an ancient alien threat. Deploy more than 500 weapons and utility upgrades – fighting by yourself or with a friend. The fate of humanity rests in your hands. We’re currently live on Kickstarter, and we’d love it if you could check out our page! Any feedback is greatly appreciated
      https://www.kickstarter.com/projects/643340596/technolites-a-story-driven-shoot-em-up-by-zugalu
      Looking forward to seeing all of the great game projects being developed and launching this year!
  • Advertisement