PDA

View Full Version : My first shader



devdept
07-27-2008, 08:24 AM
Hi All,

I managed to put at work my first - very easy - fragment shader:


void main()
{
gl_FragColor = vec4(0.9,0.4,0.8,1.0);
}

Suppose I draw 10 triangles, I want to use the shader only on the third and fourth. How can I do?

I use the following code:


myProgram = glCreateProgram();

glAttachShader(myProgram, myShader);

glLinkProgram(myProgram);

glUseProgram(myProgram);



Thanks,

Alberto

Seth Hoffert
07-27-2008, 08:29 AM
You could bind program 0 (glUseProgram(0)) to draw triangles 1-2, 5-10, then bind program myProgram to draw triangles 3-4. :)

dletozeun
07-27-2008, 12:44 PM
this post should be in the shader section, but anyway, here (http://www.lighthouse3d.com/opengl/glsl/) you can find a great tutorial to learn the basics of glsl. :)

devdept
07-27-2008, 02:39 PM
Hi All,


I didn't know there was a dedicated forum.

HexCat, thanks I didn't know I could do that.

Now probably I see the problem.

I can't have a fragment only shader, can I?

I noticed that all the examples I saw provide both vertex and fragment shader.


Thanks,

Alberto

dletozeun
07-27-2008, 03:12 PM
I can't have a fragment only shader, can I?

No you can't, a vertex shader, as its name says, processes on vertices, fragment shader works on fragments and always use interpolated data computed in the vertex shader (at least fragment position). So you can say, "so a vertex shader could work alone!" And no it is not possible, since fragment shader have to do all the work about "pixels color on screen" since using a shader disable the fixed pipeline that does for exemple "gouraud shading" "texturing"...

And please read carefully the tutorial i gave you in my last post, you will find inside answers of all the very basic questions you ask (without wanting to be offensive :) ). Then don't hesitate to come back ask more questions.

Xmas
07-28-2008, 03:10 AM
I can't have a fragment only shader, can I?

No you can't
Yes, you can. OpenGL supports mixing fixed function vertex processing and fragment shaders, and vice versa.

dletozeun
07-28-2008, 03:45 AM
Oh yes you are right Xmas! I apologize, I was convinced of the opposite until I found the paragraph in the glsl spec that talks about that.

But in my opinion, this seems useless and dangerous, source of many bugs since you have to take care of computing all the necessary data used by the fixed functionality in the vertex shader when you don't use a fragment shader. On the other hand it looks like easier to not use a vertex shader but it limits almost always what you can do in the fragment shader

Jan
07-28-2008, 04:43 AM
Using only one shader and the ffp for the rest is usually not very useful. The only case, where it is sometimes convenient, is when you only want to change the vertex-processing, such that you don't need to write a dedicated fragment-shader.

Using no vertex-shader, but a fragment-shader is more a pain, than anything else.

Jan.