Shaders: simplified

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
dart_theg
Posts: 77
Joined: Sun Dec 29, 2024 3:13 am

Shaders: simplified

Post by dart_theg »

Hey everyone. I’ve been looking over the tutorial for shaders and I’ve been wondering if anyone can simplify it for a noob? I don’t really ever touch shaders but I want to add the functionality. Like, how does the event callback stuff work for shader materials? Setting variables in shaders that then show change in the scene?
CuteAlien
Admin
Posts: 9988
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Shaders: simplified

Post by CuteAlien »

I don't think it can be made much simpler. Note it's shader callbacks for shaders, not event callbacks. And what they are there for is to allow you to pass updated variables to your shader code.

The important one is usually OnSetConstants which is called every time you draw a mesh. OnSetMaterial is called when the material changes to the shader material (so you can read out SMaterial values there and remember them and pass them laster in OnSetConstants for example). And in trunk you have also OnCreate which allows you to create the variables a bit simpler (you could also just do that in OnSetMaterial and make sure yourself you create them only in the first call).

If you check the shader code (the .frag and .vert files for OpenGL or the .hlsl file for D3D) you see that they have some global variables (in OpenGL they are labeled as "uniform" in D3D they are just the globals in your shader). And those have the same names as used in the getVertexShaderConstantID/getPixelShaderConstantID functions. So this is the connection between c++ and shader variables where you pass your own variables from cpu to gpu.

The trick is usually - start with a working shader. Just copy-paste the stuff from the example and from the shader code. And then start modifying simply things - the fragment/pixel shader are the easiest to modify. So to start - I'd use the Irrlicht example and just experiment a bit with opengl.frag - all it needs to do is return some values between 0 and 1 for gl_FragColor. And you can directly access the rgba with gl_FragColor.r, gl_FragColor.g, gl_FragColor.b, gl_FragColor.a and write any values in there. For example write gl_FragColor.r = 1.0; at the end of opengl.frag and see the result.

The main difference to the fixed function pipeline is that you do nearly everything yourself. All you get more or less automatic are vertex data. So it gets a bit more complicated with the vertex shader (but again - just start by copying one that works). So the way Irrlicht implements the 3d transformations in opengl.vert is just one option to do things.

I can help if you have concrete questions, but ... I can't make things simpler. Shaders have a bit of a startup bump you have to get over.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
dart_theg
Posts: 77
Joined: Sun Dec 29, 2024 3:13 am

Re: Shaders: simplified

Post by dart_theg »

I think I got it now, not so much about writing them but implementing them at least. Thanks!
Post Reply