Main Page | Report this Page
 
   
Computers Forum Index  »  Computer - Graphics - API (Opengl)  »  GLSL Vertex Sh. uniform aren't set (OGL 2.0)
Page 1 of 1    
Author Message
Alessandro [AkiRoss] Re
Posted: Sat Jun 10, 2006 5:57 pm
Guest
Hi there
after getting a new videocard i started to learn GLSL (i hope this is
the right place where ask).

Well: i did a really simple demo: a matrix (a plane) of quads is drawn
by OpenGL, then a vertex shader should animate it like weaving and a
fragment shader should give it a color.

Well, the shader is like this

uniform float waveTime;
uniform float waveWidth;

void main()
{
vec4 v = gl_Vertex;
v.z = sin(v.x * waveWidth + waveTime);
gl_Position = gl_ModelViewProjectionMatrix * v;
}

And i pass the uniforms like this:
sp = glCreateProgram();
/* initializing shaders and linking program & error checking */
GLfloat waveTime = 0.0,
waveWidth = 0.0;
GLint waveTimeLoc = glGetUniformLocation(sp, "waveTime");
GLint waveWidthLoc = glGetUniformLocation(sp, "waveWidth");
/* locations are ok, no errors. Then starts main loop */
glUniform1f(waveTimeLoc, waveTime);
glUniform1f(waveWidthLoc, waveWidth);

The problem is that it seems that these values aren't passed to the
shader.
To be more precise, i did the followin test:
1. With SDL i associated a keypress with an increment of waveTime or
waveWidth. This is done before calling, in the main loop, the
glUniform1f
2. I modified the shader, to set the v.z value equal to the variable
if (v.x < 0) // Half board is set like waveWidth
v.z = waveWidth;
else // The other half is waveTime
v.z = waveTime;

When the program starts, the plane is flat.
If i press the key to increase waveWidth (+1: 1.0, 2.0, 3.0 and so on)
nothing changes.
If i press the key to increase waveTime (+0.001 each step), the plane
have strange behavior, like it's translated on the Z axyis of absurde
values, like -10000 +12345 and randomness like this.

I'm sorry if this is a silly question, but i still don't have the
Orange Book and tutorials i read didn't help.

Thanks
Wolfgang Draxinger
Posted: Sat Jun 10, 2006 9:10 pm
Guest
Alessandro [AkiRoss] Re wrote:

Quote:
When the program starts, the plane is flat.
If i press the key to increase waveWidth (+1: 1.0, 2.0, 3.0 and
so on) nothing changes.
If i press the key to increase waveTime (+0.001 each step), the
plane have strange behavior, like it's translated on the Z
axyis of absurde values, like -10000 +12345 and randomness like
this.

sin expects it's parameter to be in radians, i.e in the range
0...2pi. Your values look like if you're thinking in degrees.
OpenGL is a bit incostintent there, that glRotate is in degrees
but the rest want's radians. I "solved" this by using my very
own matrix operation functions; I keep a local copy of the
matrix stack anyway so that I can look up the current matrix
without having to call glGetFloatv. The responsible code is also
maintining the appropriate inverse matrix, but not by inverting
each new calculated matrix, but by applying the inverse
transformation (transposed rotation matrix, negated translation
matrix, inverted scaling matrix) with each matrix operation with
an L multiplication (inverse order of operation).

Quote:
I'm sorry if this is a silly question, but i still don't have
the Orange Book and tutorials i read didn't help.

You don't really need the Orange Book. Personally I only own the
Red Book as a hardcopy, and it's been over a year that I lastly
touched it (not counting yesterday when I needed to look up the
numeric value of a token).

Wolfgang Draxinger
--
vizowl
Posted: Sat Jun 10, 2006 9:42 pm
Guest
Alessandro [AkiRoss] Re wrote:
Quote:
uniform float waveTime;
uniform float waveWidth;

void main()
{
vec4 v = gl_Vertex;
v.z = sin(v.x * waveWidth + waveTime);
gl_Position = gl_ModelViewProjectionMatrix * v;
}

And i pass the uniforms like this:
sp = glCreateProgram();
/* initializing shaders and linking program & error checking */
GLfloat waveTime = 0.0,
waveWidth = 0.0;
GLint waveTimeLoc = glGetUniformLocation(sp, "waveTime");
GLint waveWidthLoc = glGetUniformLocation(sp, "waveWidth");
/* locations are ok, no errors. Then starts main loop */
glUniform1f(waveTimeLoc, waveTime);
glUniform1f(waveWidthLoc, waveWidth);

The problem is that it seems that these values aren't passed to the
shader.

One thing that may be helpful - it is not clear from your code where
you are calling glUseProgram - but my understanding is that you need to
make you glUniformXXX calls after the glUseProgram call. This is to
ensure that you bind the shader uniform variable to the current state
of the program.

Hope that helps,
Chris
Alessandro [AkiRoss] Re
Posted: Sat Jun 10, 2006 11:40 pm
Guest
vizowl ha scritto:
Quote:
One thing that may be helpful - it is not clear from your code where
you are calling glUseProgram - but my understanding is that you need to
make you glUniformXXX calls after the glUseProgram call. This is to
ensure that you bind the shader uniform variable to the current state
of the program.

yes, sorry, glUseProgram is called just before the main loop, and the
glUniform are called *in* the main loop, so they are called after it.

Wolfgang:
yes, i know it need radians, but the problem isn't that. The sin()
application you see there *works*.
What doesn't work is the uniform calling:

uniform float waveTime;
uniform float waveWidth;

void main()
{
vec4 v = gl_Vertex;

if (v.x < -10.0)
v.z = waveWidth;
else
v.z = waveTime;

gl_Position = gl_ModelViewProjectionMatrix * v;
}

As you see, in this shader i don't execute any trigonometrical
function: i just set the Z component of the vertex to the waveWidth or
waveTime value.
Ergo, incrementing these values i should see the plane that grow, but
it doesn't. It have undefined behavior.

This is how i execute the program

- SDL & OpenGL Init
- Loading shader sources
- Compiling and linking (no errors in glsl code)
- Using program
- Main loop: while(!quit)
-- Process keyboard event: if button==1 then
--- increase waveWidth
--- else increase waveTime
-- glUniform1f(waveTimeLocation, waveTime)
-- glUniform1f(waveWidthLocation, waveWidth)
-- Draw the mesh
-- Swap buffers
-- Continue with loop

I hope this help to clarify what problem i've :(

Thanks guys
Alessandro [AkiRoss] Re
Posted: Mon Jun 12, 2006 5:26 pm
Guest
Well, i found a way to make program work, but i still have a question
for you.
I took an example code of glsh, and it uses the gl extension wrangler
(glew).
After working a bit i found out that _my code_ works perfectly if i use
glewInit().

Now i've to look at it's code, and understand what it does...
It's strange it doesn't work without it :\ i'm using OpenGL 2.0, no
extensions.

Any help is appreciated
Bye
Alessandro [AkiRoss] Re
Posted: Tue Jun 20, 2006 6:45 pm
Guest
Well, i'm asking again since looking around didn't help.

I tried to create an SDL (1.2.10) application which uses OpenGL 2.0
with shaders, but i noticed that GLSL ubsystem didn't work correctly
(i.e. uniform variables are random valued). I experienced the same
problem using glut, but using GLEW fixed this.

The question is: why do i need glew to use OpenGL 2 features? This
isn't normal, since GLSL is included in the language, imho.

Any idea?

Thanks
 
Page 1 of 1       All times are GMT
The time now is Tue Jan 06, 2009 4:36 am