Simple vertex shader

Started by
16 comments, last by amtri 11 years, 8 months ago
Hello,

I'm a newbie to vertex shaders. I'm just trying to get my feet wet. I coded the following simple vertex shader:

[source lang="java"]
const GLchar *shadersrc[] = {
"void main()\n",
"{\n",
"gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n",
"}"
};[/source]

I understand that gl_ModelViewProjectionMatrix may be deprecated, but I assume that if it's not working then the above code won't compile - which is not the case. So I assume that all's well.

I'm compiling/linking/using the shader with the following simple code:

[source lang="java"]
nos = sizeof(shadersrc)/sizeof(char*);
shad = glCreateShader (GL_VERTEX_SHADER);
glShaderSource (shad,nos,shadersrc,NULL);
glCompileShader (shad);
glGetShaderiv (shad,GL_COMPILE_STATUS,&iflg);
if(iflg == GL_TRUE) {
printf("Shader successfully compiled\n");
} else {
printf("Shader compilation failed\n");
return;
}
prog = glCreateProgram();
glAttachShader (prog,shad);
glLinkProgram (prog);
glUseProgram (prog);[/source]
I call glGetError() at this point and I get no error.

My understanding is that the above vertex shader should not change anything. However, if I use the above code nothing gets displayed.

Does anybody have a clue to what may be going on?

Thanks.
Advertisement
Do you have a pixel shader to go with that vertex shader?

Edit: Well, I guess you don't have to have one. From the docs of glLinkProgram - "If program contains shader objects of type GL_VERTEX_SHADER but does not contain shader objects of type GL_FRAGMENT_SHADER, the vertex shader will be linked against the implicit interface for fixed functionality fragment processing."
Well, I added the following code:

[source lang="java"] glGetShaderiv (shad,GL_LINK_STATUS,&iflg);
if(iflg == GL_TRUE) {
printf("link true\n");
} else {
printf("link false\n");
}
}[/source]

and I'm getting "link false". So it compiles, but doesn't link... I'm puzzled.
web383: I do NOT have a pixel shader. I saw the same notice about the default pixel shader so I decided to do things one at a time...
You should use glGetProgramiv for checking the link status.

Derp

Yeah... I know... just realized that now.

But I have more information:

1) I added a simple fragment shader, with gl_FragColor = gl_Color.

This fails. Nothing gets displayed.

2) Then I changed to gl_FragColor = vec4(1.,0.,0.,1.);

Then I do get a red dot, as expected.

My questions are:

1) I had called glColor before. Shouldn't this stay in effect so that gl_Color is set to that?
2) Why do I see nothing when I set gl_FragColor to gl_Color? I know I have a gl_Color set because before using the program I display the image with the default (white) color. But when I enable the program nothing shows, unless I set gl_FragColor to, say, red explicitly.
Why is you shadersrc an array of character pointers? If I remember right, this grantees that the POINTERS are in concurrent memory space, but the strings themselves can technically live anywhere in memory (okay, okay, not anywhere but you get the point). They aren't guaranteed to evaluate to <string1><string2>..<string4> in memory and can have garbage in between depending on how the OS optimizes laying out the memory.

I assume your nos calculation evaluates to 4?

Try modifying it such that

[source lang="cpp"]const GLchar shadersrc[] =
"void main()\n"
"{\n"
"gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
"}";

nos = sizeof(shadersrc);
[/source]

<edit> I could of course be wrong in the use of glShaderSource since I don't have any references in front of me.
Aumnayan,

glShaderSource takes a GLchar** as an argument. And yes... I AM getting nos = 4, so all's well on that end.

I guess my problem really is trying to create a simple fragment shader that does exactly what a program with no fragment shader would do. I thought setting gl_FragColor to gl_Color was sufficient, but apparently that's not the case. But I don't know why...
Okay, I've never spent to long with playing with the initialization code and tend to copy/paste from previous applications so I don't have how to use it burned into my head. Do you know what version of GL and GLSL you're using? If it's a latter one (after gl_ModelViewProjectionMatrix has been depreciated) it could be possible that you're GLSL compiler has left the prototype of the variable but the declaration of it was removed. This would allow the compiler to complete but cause the linker to fall on it's backside. Try taking it out, and see if it compiles/links. Even if it dosn't do anything that information will help you get to the bottom of this.
Here's the info:

GL version= 2.1.2
GLSL version= 1.20 NVIDIA via Cg compiler

My vertex shader seems to work just fine, so I assume that gl_ModelViewProjectionMatrix is enabled.

My problem is the fragment shader. Apparently, the "default" fragment shader sets the color to (0,0,0), and nothing gets displayed. I would have imagined that the default fragment shader would behave just as if there were no shaders at all, but that's not the case.

So the real questions are: (1) why do I need something more than just gl_FragColor = gl_Color? (2) And, if I do need more, and if my vertex shader is so trivial, then how do I write a fragment shader so that I get the same images I was getting without any shaders?

This topic is closed to new replies.

Advertisement