Skip to main content
gamedev.net
🔒 Locked

GLSL uniform variable error

Started by Sheree Mar 20, 2009 at 7:47 AM 10 replies 1.2k views
Original Post
Sheree
Sheree
Hello everyone, I have a question about uniform variable in GLSL: As I understood, it will be somewhere error if I have the returned value from glGetUniformLocation(linkedprog,"name_variable"); is -1 could you please help me to understand what are the possibilities that cause such value and what should the value be, what I have done as follows: //------------in opengl main--------------- //of course here // I create shader program //link it //use it GLint loc = glGetUniformLocation(linkedprog,"name_variable"); cout<<" loc = "<<loc<<"\n"<<endl; //--------------------------------------------- the only thing that cause -1 to be returned is getting unifom variable befor linking and using the shader program..... that is already achieved ... but I still have -1 value... So,what else should I consider to get right value? and if I have -1 value that means that I couldn't use this value in shader! Other question please: if I want to get interactive by keyboard through a "int variable" that is passed into a vertex shader in order to switch between two textures, does unifrom variable do this task like: //-----opengl main------ int tex_coohse_variable; GLint loc = glGetUniformLocation(linkedprog,"tex_coohse_variable"); glUniform1i(loc,tex_coohse_variable); //----------------------------------- //----------shader main------------- uniform int tex_coohse_variable; if(tex_coohse_variable == 0) //choose text 0 if(tex_coohse_variable == 1) //choose text 1 //---------------------------------- //-----opengl keyboard--------- case'0': tex_coohse_variable = 0; brea; case'1': tex_coohse_variable = 1; brea; //------------------------------ thanks for any help, suggestions, comments in advance Sheree
Sheree
Sheree
Okay guys,
for the first question I had " returned -1 value" I think I figure out what is my problem :

if I don't use the unifrom value in my shadre I get -1 value, so it returns a value only if I used it in the shader.....done it..

but I still don't get interactive usage by keyboard if I pass the uniform value..

if you have any suggestions ...I will really appreciate yours..
during that I will try to get a clue about this problem.

Sheree
HuntsMan
HuntsMan
Variables aren't passed to the shader, it's value is passed, so when the value of the variable in the CPU side changes, that change isn't reflected on the GPU side, you need to reupload the value with glUniform() calls.
Sheree
Sheree
Okay, thanks HuntsMan for the explaining, but you said re-upload glUniform() does that mean the I have to repeat:

glGetUniformLocation();
glUniform();

in keyboard function after setting variable for update, and should I re-link and re-use the same shader at this time? ;-| .... thanks for more explanation

some thing else:
Since I would like to use this variable to switch between multi-textures, so what I have done is setting all textures and then I tried to get one of them with respect to the int values given by keyboard variable, but whatever the texture I choose the data are taken from the last one i.e :

bind text0
load data to it

bind text1
load data to it

and so on
....
bind textn
load data to it

as result I have always data from textn whatever uniform text_name I am using...
can you please give a clue about how can I deal with such task??
comments are really appreciated

Sheree

V-man
V-man
You don't have to call glGetUniformLocation(); again since you called it once, you know what value it will return.
Sheree
Sheree
alright,but could any one tell me any idea about the other problem of textures, where I still able to use texture in the vertex shader that is loaded alone in main opengl function but if I loaded multi-textures the shader is able to get the last loaded one and it doesn't see the previous loaded textures....
some useful documents, you know, can be also a way for helping me for understanding as well...

Sheree
Sheree
Okay, I think the problem was that I haven't used :

glActiveTexture() function after loading my textures therefore I had only the data that comes from the last loaded texture.

Now, my question turned to how many "uniform samplesr2D texturei" in the vertex shader can I use ?... this question regarding to the new problem that I have:
if I use more than 4 uniform for textures and the switching variable the shader doesn't get any data from any texture and the loactions for uniforms become all "-1"...
Is that a real problem or I have just to add some missing commands I have to add ;-). Could this related to the storage size of the shader I am using ..

Thanks in advance for any kind of help!
V-man
V-man
Quote:
Original post by Sheree
how many "uniform sampler2D texture" in the vertex shader can I use ?


http://www.opengl.org/wiki/Vertex_Texture_Fetch
Sheree
Sheree
Fine, I have tested how many texture can I use? and I had "4" that explains my problem. :-).
However, if I would like to use multiple textures where their number exceeds the permitted number on my vertex shader(VS), I have to use multiple VS and switch between them w.r.t the group of textures that I need, but if it is so, then I have to move this decision to the CPU side(if my vertexes are on vertex arrays) and then I can only update my vertexes on GPU.

Do you think, if I use VBO and IBO for my vertexes and their indexes, there will be an opportunity to make this decision on GPU, but I still don't know how can I achieve this since the variable for this decision is held in the vertex type as follows :

struct Vertex
{
float X,Y,Z// vertex coordinate
float D; // is used for decision
}

I'll appreciate any suggestions,comments.
Sheree
Sheree
Okay, I think I can use the vertex attribute themselves to have the value that I want to use in vertex shader and then I update the vertex w.r.t. the result of the decision.


troxtril
troxtril
Quote:
Original post by HuntsMan
Variables aren't passed to the shader, it's value is passed, so when the value of the variable in the CPU side changes, that change isn't reflected on the GPU side, you need to reupload the value with glUniform() calls.


does it preserve the last value or does it reset the value every frame you render?
HuntsMan
HuntsMan
Quote:
Original post by troxtril
Quote:
Original post by HuntsMan
Variables aren't passed to the shader, it's value is passed, so when the value of the variable in the CPU side changes, that change isn't reflected on the GPU side, you need to reupload the value with glUniform() calls.


does it preserve the last value or does it reset the value every frame you render?


Neither, they're constants to the shader, you set the value, and doesn't change until you set them again.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.