• Advertisement
Sign in to follow this  

glGetUniformLocation error 1282

This topic is 1928 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi!
I try to get the uniform location from shader, but it give me an error 1282(GL_INVALID_OPERATION)
This is the code:

Shader.hpp

class Shader : public object
{
public:
Shader();
virtual ~Shader();
GLuint ID;
void Load(const char* filename);
protected:
private:
GLuint vertexShaderID,
fragmentShaderID;
void Initialize(void);
GLuint CreateShader(std::string shader, GLenum type_shader);
}
;

Shader.cpp

void Shader::Load(const char* filename)
{
ifstream in(filename, ios::binary);
if(!in)
{
cerr << "Could open file" << filename << endl; // Lanzamos un mensaje de error
exit(1); // exit(1) usado para abortar el programa
}

std::string glsl_source((std::istreambuf_iterator<char>(in)),
std::istreambuf_iterator<char>());

in.close();

if(glsl_source.length() == 0)
{
exit(1);
}
std::string fs_shader = "#define COMPILING_FS\n" + glsl_source;
std::string vs_shader = "#define COMPILING_VS\n" + glsl_source;
std::cout<<vs_shader<<std::endl;
ID = glCreateProgram();
CreateShader(fragmentShaderID,fs_shader,GL_FRAGMENT_SHADER);
CreateShader(vertexShaderID,vs_shader,GL_VERTEX_SHADER);
Initialize();
}
GLuint Shader::CreateShader(std::string shader, GLenum type_shader)
{
GLuint shader_id = 0;
shader_id = glCreateShader(type_shader);
const char* glsl_cstr;
glShaderSource(shader_id, 1, &(glsl_cstr=shader.c_str()),NULL);
glCompileShader(shader_id);
ExitOnGLError("Could compile shader");

return shader_id;
}
void Shader::Initialize(void)
{
glAttachShader(ID, fragmentShaderID);
ExitOnGLError("Could attach fragmentshader");
glAttachShader(ID, vertexShaderID);
ExitOnGLError("Could attach vertexshader");
glLinkProgram(ID);
ExitOnGLError("Could link program");
}


main


ModelMatrixUniformLocation = glGetUniformLocation(shader->ID, "ModelMatrix"); //Error in here
cout<<glGetError()<<endl; // output '1282'
cout<<shader->ID<<endl; // output '1'


Can anyone see the error? Edited by ShotoReaper

Share this post


Link to post
Share on other sites
Advertisement
One condition for glGetUniformLocation to fail with that error code is that your program didn't link correctly. You should add checks to your code to see if there are compile and/or link errors.

Add this after compiling each shader file.

GLint compiled = 0;
GLint length = 0;
glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &length);

if(length > 1) {
std::string log(length, '\0');
glGetShaderInfoLog(shader, length, &length, &log[0]);
}

Likewise after attempting to link the shader, but replace GL_COMPILE_STATUS with GL_LINK_STATUS and the shader ID with the program ID.

Share this post


Link to post
Share on other sites
I apologize, I quickly glanced over my own code to verify the code I posted but didn't notice the obvious that the functions are different; you need to use glGetProgramiv and glGetProgramInfoLog when querying the program object and its link status. The code otherwise should be the same.

edit: Good, you apparently found and corrected my mistake already. Edited by Brother Bob

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement