views:

87

answers:

1

Im new to shaders and I'm trying to learn the basics. But everytime I change some code in my vertex shader it results in an error.

"Validation Failed: Program is not successfully linked."

Im using the standard openGL project for the iphone as a starter in Xcode ( with a 2d cube moving in y-position).

From the beginning the shader looks like this:

attribute vec4 position;  
attribute vec4 color;  

varying vec4 colorVarying;  

void main()  
{  
     gl_Position = position;  
     gl_Position.y += sin(translate) / 2.0;  
     colorVarying = color;  
}  

and I want to change it to :

uniform mat4 gl_ProjectionMatrix;  
uniform mat4 gl_ModelViewMatrix;  

attribute vec4 gl_Vertex;  

void main()  
{  
     gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * gl_Vertex;  
}  

I all get is a white screen and the error mentioned above.

What's the problem?

/Niclas

+1  A: 

Notice that you are using a "varying" parameter. This needs to be defined for both the vertex and the fragment shader. Since you appear to have only changed the vertex shader I'm guessing that this is causing your link problem as the colorVarying is probably still in the fragment shader. Try putting the logic of "colorVarying" back into your vertex shader. Something like:

uniform mat4 gl_ProjectionMatrix;   
uniform mat4 gl_ModelViewMatrix;   

attribute vec4 gl_Vertex;   

attribute vec4 color;
varying vec4 colorVarying;

void main()   
{   
    gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * gl_Vertex;  
    colorVarying = color; 
}
No one in particular
I have tried that aswell =( Still the same problem..
Adl
What happens if you put the original shader code back?
No one in particular
Then it works. And for example if I change gl_Position.y += sin(translate) / 2.0;to gl_Position.y += sin(translate) / 4.0; my behavior changes.
Adl
Cool. Now slowly change one line at a time until we hit the problem. Start by adding the uniforms then attribute. Then change the gl_Position calculation. Then remove the lines that you don't need. You'll hit the problem somewhere.
No one in particular
If I just add uniform mat4 gl_ProjectionMatrix; it will break.. =)
Adl
Try changing gl_ProjectionMatrix to myProjectionMatrix.
No one in particular
then it works.. but shoudn't I use the global gl_ProjectionMatrix?
Adl
Okay. I'm stupid. OpenGL declares a whole mass of parameters for you that begin with gl_*. There's no need to declare them, just use and set them. Try your final shader but with only the main() code. Try it without the declarations above the main().
No one in particular
If I do only this:attribute vec4 color;varying vec4 colorVarying;void main(){ gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * gl_Vertex; colorVarying = color; } It still doesn't work..
Adl
Weird. Right now I created a basic project. And I edited the vertex shader to: void main() { gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * gl_Vertex; } and I see a bouncing box. This shouldn't work as I'm not using sin() to change the position. It's like it remembers the last build.
No one in particular
Ok. The above was defaulting to the OpenGL ES 1.x code - which is why I saw it working. ... Now I'm seeing the same problem as you. Which is odd as this is considered the minimum vertex shader code. See: http://www.lighthouse3d.com/opengl/glsl/index.php?minimal
No one in particular
Yes, that was the tutorial I was following.. I really dont understand whats wrong =)
Adl
Mystery solved. We were both researching shaders for OpenGL the full version. The OpenGL ES 2.0 is a much more striped down version. Take a look at the language summary http://www.khronos.org/opengles/sdk/2.0/docs/reference_cards/OpenGL-ES-2_0-Reference-card.pdf (shaders are down the bottom). We're seeing issues because in the mobile version of OpenGL shader language many gl_* terms are not defined that the desktop version of OpenGL contains.
No one in particular
Is that so =) Well thank you. Then I will look into that.
Adl