Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Nerothos

Shaders

This topic is 5305 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Yes, I''ve been living in a cave. What exactly is a shader, and how do you implement and use them in OpenGL? Thanks!

Share this post


Link to post
Share on other sites
Advertisement
A shader is just code that modifies the rendering pipeline, usualy hardware accelerated. A vertex shader is run on every vertex passed in. This would be a shader:


sub shade(vertex vert)
{
vert.x = vert.y * 2;
}


The other type of shader is a fragment shader, which is run on every pixel.

sub shade(fragment frag)
{
frag.color = texture(frag.u, frag.v);
}


Yea, I made up my own shader language.

Share this post


Link to post
Share on other sites
a shader does whatever you want it to do. it performs a specific effect upon vertex/pixel, whatever you want.

some shaders are done with assembly like language. Others are different. As with above.

Share this post


Link to post
Share on other sites

As Cipher said they are usualy coded in a lower level language. They run on the GPU. They free up CPU cycles allowing you to perform more costly special effects. I''m not sure if shadowing could make use of shaders but people are very creative with effects. Skeletal animation is one use of a vertex shader I know of. From what I understand, shaders are programs. They are basicaly static variables with a single function. When you tell your graphics api to render code they are run against these shaders. First every vertex you send is run through the vertex program. Some global variables in the vertex program would be your projection matrix and model view matrix. Also there are variables you can set like bone matricies. So you call a function to set your matrix array to the bones matrix. Next you add a variable to your vertex format say a second set of texture coordinates, u is bone1''s weight, v is bone2''s weight, then you add another texture coordinate or something to to tell it which bones for the vertex. Send these all into the shader and your shader would look something like this.



matrix16 bones[15];


vertex vertex_shader(vertex vert, matrix16 modelviewprojectionmatrix)
{
vertex out;
out.xyz *= bones[out.tex3.u]*out.tex2.u + bones[out.tex3.v]*out.tex2.v;
out.xyz *= modelviewprojectionmatrix;

return out;
}



I''ve only looked at shaders but thats what I can remember.

The fragment shader is the same thing but is called for every pixel the rasterizer changes, you can do bump mapping by using 1 texture channel as a normal map and the other texture as the detail. (I may just be making all this up but its like this I think) For example:



vector3 light;

fragment fragment_shader(fragment frag, texture tex1, texture tex2)
{
fragment out;

out.color = dotproduct(makevector(tex1.r, tex1.g, tex1.b),light)*out.diffuse*tex2.fragment(frag.u, frag.v);

return out;

}



Actualy, will someone who really knows they understand these things correct me if im mistaken? I think I know what I''m talking about but may not. Op I hope that helps, I just made up my own language but that would be like the higher level shader languages.

Share this post


Link to post
Share on other sites
Shader is a terrible name for them imo but as it stuck thats what we have to work with

They come in two flavours currently, vertex and fragment/pixel.
Basicaly they are small programs which are run at every vertex which you send down the pipeline or at every fragment/pixel which the hardware draws (well, they can be run in software on the CPU of the target machine as well, which works quite well in the case of vertex shaders but pretty much flatlines your frames per second for fragment shaders).

As for doing them in OpenGL, you have a number of choices depending on your or your targets hardware.

First up there is the assembler interface, ARB_vertex_program and ARB_fragment_program are the extension names to look up and are very common, the vertex shader interface i belive exists for all of Nvidia's cards from the TNT up to the GFFX and where it cant run in hardware it will run in software mode (anything below a GF3 iirc), ATI probably have about the same coverage on their respective brands of cards probably hitting hardware coverage in at least the 8500 card. ( http://www.delphi3d.net/hardware/extsupport.php?extension=GL_ARB_vertex_program )
Fragment shader wise you'll find you'll need a GFFX or a Radeon 9500 to use it ( http://www.delphi3d.net/hardware/extsupport.php?extension=GL_ARB_fragment_program )

Next up is Nvidia's Cg system, which is a C-like high level programming language, this pretty much covers everything and can produce shaders for pretty much every type of combining out there for Nvidia cards, however because ATI havent developed a profile for it it cant produce code for the 8500's fragment shader system (PS1.4 in D3D speak) and you'll have to get someone with 9500 or greater card to comment on its code production for the fragment_program and vertex_program extensions it makes with regards to performance.

Finally, there is the OpenGL Shading Language (OGSL), which is 3DLabs/ARB's baby and is only usefull on the GFFX and Radeon9500 and up range of cards due to its use of the ARB_fragment_shader and ARB_vertex_shader extensions which arent present on any cards below them (OGLS is pretty much the OpenGL Vertex and Pixel Shader 2.0 equivilent). Again, this is a C-like language which makes it as clear to write Cg and its the ARB standard, so if you are aiming primarily for high end hardware i'd point you in that direction, although, atm there are still bugs in the implimentions, but Nvidia and ATI both now have drivers out with it in (4.4 being the latest ATI and 6x.xx being Nvidia's latest) so expect them to become more stable and bug free over the next year.
If you are intrested in learning about the OGSL then i'll point you in the direction of the OpenGL Shading Language book (Orange book) which i have and have found useful and a good learning source. If you just want to look and dont want to get a book (yet) then http://www.clockworkcoders.com/oglsl/tutorials.html have some decent tutorials on what the code is like

I think that has covered all the bases, if anyone has any corrections they want to make about Cg then jump in, i'll admit to not knowning a huge amount about it and i am a bit of a OGSL fan


[edited by - _the_phantom_ on April 11, 2004 11:44:50 PM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!